Anonymous avatar Anonymous committed c02e9a3

XW-386
- fix "XWork don't create Result using Spring"

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/xwork_1-2@1083 e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (12)

src/java/com/opensymphony/xwork/config/entities/ResultConfig.java

  * In the xml configuration file this is defined as the <code>result</code> tag.
  * 
  * @author Mike
+ * @author tm_jee
+ * 
+ * @version $Date$ $Id$
  */
 public class ResultConfig extends Located implements Parameterizable, Serializable {
 
-    private Map params;
+	private static final long serialVersionUID = 2840978123123791897L;
+	
+	private Map params;
     private String className;
     private String name;
 
         params = new LinkedHashMap();
     }
 
-    public ResultConfig(String name, Class clazz) {
+    public ResultConfig(String name, String clazz) {
         this(name, clazz, new LinkedHashMap());
     }
 
-    public ResultConfig(String name, Class clazz, Map params) {
-        this.name = name;
-        this.className = clazz.getName();
-        this.params = params;
-    }
-
-    public ResultConfig(String name, String className, Map params) {
+    public ResultConfig(String name, String clazz, Map params) {
         this.name = name;
-        this.className = className;
+        this.className = clazz;
         this.params = params;
     }
 

src/java/com/opensymphony/xwork/config/entities/ResultTypeConfig.java

  */
 public class ResultTypeConfig extends Located implements Serializable {
 
-    private Class clazz;
+	private static final long serialVersionUID = -4536350324880589642L;
+	
+	private String clazz;
     private String name;
+    private String defaultResultParam;
 
     private Map params;
 
     public ResultTypeConfig() {
     }
 
-    public ResultTypeConfig(String name, Class clazz) {
+    public ResultTypeConfig(String name, String clazz, String defaultResultParam) {
         this.name = name;
         this.clazz = clazz;
+        this.defaultResultParam = defaultResultParam;
     }
 
+    public String getDefaultResultParam() {
+    	return this.defaultResultParam;
+    }
+    
+    public void setDefaultResultParam(String defaultResultParam) {
+    	this.defaultResultParam  = defaultResultParam;
+    }
 
-    public void setClazz(Class clazz) {
+    public void setClazz(String clazz) {
         this.clazz = clazz;
     }
 
-    public Class getClazz() {
+    public String getClazz() {
         return clazz;
     }
 

src/java/com/opensymphony/xwork/config/providers/XmlConfigurationProvider.java

             String name = resultTypeElement.getAttribute("name");
             String className = resultTypeElement.getAttribute("class");
             String def = resultTypeElement.getAttribute("default");
+            
             Location loc = DomHelper.getLocationObject(resultTypeElement);
 
             Class clazz = verifyResultType(className, loc);
             if (clazz != null) {
-                ResultTypeConfig resultType = new ResultTypeConfig(name, clazz);
+            	String paramName = null;
+            	try {
+            		paramName = (String) clazz.getField("DEFAULT_PARAM").get(null);
+            	}
+            	catch(Throwable t) {
+            		// if we get here, the result type doesn't have a default param defined.
+            	}
+                ResultTypeConfig resultType = new ResultTypeConfig(name, className, paramName);
                 resultType.setLocation(DomHelper.getLocationObject(resultTypeElement));
                 
                 Map params = XmlHelper.getParams(resultTypeElement);
                     throw new ConfigurationException("There is no result type defined for type '" + resultType + "' mapped with name '" + resultName + "'", resultElement);
                 }
 
-                Class resultClass = config.getClazz();
+                String resultClass = config.getClazz();
 
                 // invalid result type specified in result definition
                 if (resultClass == null) {
                     {
                         resultParams = new LinkedHashMap();
 
-                        try {
-                            String paramName = (String) resultClass.getField("DEFAULT_PARAM").get(null);
+                        String paramName = config.getDefaultResultParam();
+                        if (paramName != null) {
                             String paramValue = resultElement.getChildNodes().item(0).getNodeValue();
                             if (paramValue != null) {
                                 paramValue = paramValue.trim();
                             }
                             resultParams.put(paramName, paramValue);
-                        } catch (Throwable t) {
+                        }
+                        else {
+                        	LOG.warn("no default parameter defined for result of type "+config.getName());
                         }
                     }
                 }

src/test/com/opensymphony/xwork/config/providers/MockConfigurationProvider.java

         HashMap results = new HashMap();
         HashMap successParams = new HashMap();
         successParams.put("actionName", "bar");
-        results.put("success", new ResultConfig("success", ActionChainResult.class, successParams));
+        results.put("success", new ResultConfig("success", ActionChainResult.class.getName(), successParams));
 
         ActionConfig fooActionConfig = new ActionConfig(null, SimpleAction.class, params, results, null);
         fooActionConfig.setPackageName("defaultPackage");
         results = new HashMap();
         successParams = new HashMap();
         successParams.put("actionName", "bar");
-        results.put("success", new ResultConfig("success", ActionChainResult.class, successParams));
+        results.put("success", new ResultConfig("success", ActionChainResult.class.getName(), successParams));
 
         List interceptors = new ArrayList();
         interceptors.add(new InterceptorMapping("params", new ParametersInterceptor()));
         results = new HashMap();
         successParams = new HashMap();
         successParams.put("actionName", "bar");
-        results.put("success", new ResultConfig("success", ActionChainResult.class, successParams));
+        results.put("success", new ResultConfig("success", ActionChainResult.class.getName(), successParams));
 
         interceptors = new ArrayList();
         interceptors.add(new InterceptorMapping("static-params", new StaticParametersInterceptor()));

src/test/com/opensymphony/xwork/config/providers/XmlConfigurationProviderActionsTest.java

         params = new HashMap();
         params.put("foo", "18");
         params.put("bar", "24");
-        results.put("success", new ResultConfig("success", MockResult.class, new HashMap()));
+        results.put("success", new ResultConfig("success", MockResult.class.getName(), new HashMap()));
 
         InterceptorConfig timerInterceptorConfig = new InterceptorConfig("timer", TimerInterceptor.class, new HashMap());
         interceptors.add(new InterceptorMapping("timer", objectFactory.buildInterceptor(timerInterceptorConfig, new HashMap())));
 
         // wildcard action is simple wildcard example
         results = new HashMap();
-        results.put("*", new ResultConfig("*", MockResult.class, new HashMap()));
+        results.put("*", new ResultConfig("*", MockResult.class.getName(), new HashMap()));
 
         ActionConfig wildcardAction = new ActionConfig(null, SimpleAction.class, new HashMap(), results, interceptors);
 
         params.put("foo", "18");
         params.put("bar", "24");
         results = new HashMap();
-        results.put("success", new ResultConfig("success", MockResult.class, new HashMap()));
+        results.put("success", new ResultConfig("success", MockResult.class.getName(), new HashMap()));
 
         ExceptionMappingConfig exceptionConfig = new ExceptionMappingConfig("runtime", "java.lang.RuntimeException", "exception");
         exceptionMappings.add(exceptionConfig);

src/test/com/opensymphony/xwork/config/providers/XmlConfigurationProviderExceptionMappingsTest.java

 
         exceptionMappings.add(new ExceptionMappingConfig("spooky-result", "com.opensymphony.xwork.SpookyException", "spooky-result"));
 
-        results.put("spooky-result", new ResultConfig("spooky-result", MockResult.class, new HashMap()));
+        results.put("spooky-result", new ResultConfig("spooky-result", MockResult.class.getName(), new HashMap()));
 
         Map resultParams = new HashMap();
         resultParams.put("actionName", "bar.vm");
-        results.put("specificLocationResult", new ResultConfig("specificLocationResult", ActionChainResult.class, resultParams));
+        results.put("specificLocationResult", new ResultConfig("specificLocationResult", ActionChainResult.class.getName(), resultParams));
 
         ActionConfig expectedAction = new ActionConfig(null, SimpleAction.class, parameters, results, new ArrayList(), exceptionMappings);
 

src/test/com/opensymphony/xwork/config/providers/XmlConfigurationProviderResultsTest.java

         HashMap parameters = new HashMap();
         HashMap results = new HashMap();
 
-        results.put("chainDefaultTypedResult", new ResultConfig("chainDefaultTypedResult", ActionChainResult.class, new HashMap()));
+        results.put("chainDefaultTypedResult", new ResultConfig("chainDefaultTypedResult", ActionChainResult.class.getName(), new HashMap()));
 
-        results.put("mockTypedResult", new ResultConfig("mockTypedResult", MockResult.class, new HashMap()));
+        results.put("mockTypedResult", new ResultConfig("mockTypedResult", MockResult.class.getName(), new HashMap()));
 
         Map resultParams = new HashMap();
         resultParams.put("actionName", "bar.vm");
-        results.put("specificLocationResult", new ResultConfig("specificLocationResult", ActionChainResult.class, resultParams));
+        results.put("specificLocationResult", new ResultConfig("specificLocationResult", ActionChainResult.class.getName(), resultParams));
 
         resultParams = new HashMap();
         resultParams.put("actionName", "foo.vm");
-        results.put("defaultLocationResult", new ResultConfig("defaultLocationResult", ActionChainResult.class, resultParams));
+        results.put("defaultLocationResult", new ResultConfig("defaultLocationResult", ActionChainResult.class.getName(), resultParams));
 
         resultParams = new HashMap();
         resultParams.put("foo", "bar");
-        results.put("noDefaultLocationResult", new ResultConfig("noDefaultLocationResult", ActionChainResult.class, resultParams));
+        results.put("noDefaultLocationResult", new ResultConfig("noDefaultLocationResult", ActionChainResult.class.getName(), resultParams));
 
         ActionConfig expectedAction = new ActionConfig(null, SimpleAction.class, parameters, results, new ArrayList());
 
         ConfigurationProvider provider = buildConfigurationProvider(filename);
 
         // setup expectations
-        ResultTypeConfig chainResult = new ResultTypeConfig("chain", ActionChainResult.class);
-        ResultTypeConfig mockResult = new ResultTypeConfig("mock", MockResult.class);
+        ResultTypeConfig chainResult = new ResultTypeConfig("chain", ActionChainResult.class.getName(), null);
+        ResultTypeConfig mockResult = new ResultTypeConfig("mock", MockResult.class.getName(), null);
 
         // execute the configuration
         provider.init(configuration);

src/test/com/opensymphony/xwork/spring/ActionsFromSpringTest.java

 import com.opensymphony.xwork.*;
 import com.opensymphony.xwork.config.ConfigurationManager;
 import com.opensymphony.xwork.config.providers.XmlConfigurationProvider;
+import com.opensymphony.xwork.ActionProxy;
+import com.opensymphony.xwork.ActionProxyFactory;
+import com.opensymphony.xwork.spring.SpringResult;
+
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
         String result = action.execute();
         assertEquals(Action.INPUT, result);
     }
+    
+    public void testActionWithSpringResult() throws Exception {
+        ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(
+        		null, "simpleActionSpringResult", null);
+                
+        proxy.execute();
+        
+        SpringResult springResult = (SpringResult) proxy.getInvocation().getResult();
+        assertTrue(springResult.isInitialize());
+        assertNotNull(springResult.getStringParameter());
+    }
 }

src/test/com/opensymphony/xwork/spring/SpringResult.java

+package com.opensymphony.xwork.spring;
+
+import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.Result;
+
+public class SpringResult implements Result {
+	
+	private static final long serialVersionUID = 9178210103553917940L;
+
+	private boolean initialize = false;
+    
+    //  this String should be populated by spring
+    private String stringParameter;    
+
+    public void initialize() {
+        // this method should be called by spring
+        this.initialize = true;
+    }
+    
+    public void execute(ActionInvocation invocation) throws Exception {
+        // intetionally empty
+    }
+
+    public void setStringParameter(String stringParameter) {
+        this.stringParameter = stringParameter;
+    }
+    
+    public String getStringParameter() {
+        return this.stringParameter;
+    }
+    
+    public boolean isInitialize() {
+        return this.initialize;
+    }
+}

src/test/com/opensymphony/xwork/spring/actionContext-spring.xml

 		</property>
         <property name="beanNames"><value>auto-proxied-action</value></property>
     </bean>
+    
+    <bean id="springResult" class="com.opensymphony.xwork.spring.SpringResult"
+		init-method="initialize">
+		<property name="stringParameter" value="my string"/>
+	</bean>
 </beans>

src/test/com/opensymphony/xwork/spring/actionContext-xwork.xml

     <package name="default">
         <result-types>
             <result-type name="null" class="com.opensymphony.xwork.result.NullResult" default="true"/>
+            <result-type name="springResult" class="springResult" />
         </result-types>
 
         <interceptors>
         <action name="proxiedAction" class="proxied-action"/>
 
         <action name="autoProxiedAction" class="auto-proxied-action"/>
+        
+        <action name="simpleActionSpringResult" class="simple-action">
+			<result name="error" type="springResult"/>
+		</action>
     </package>
 </xwork>

src/test/com/opensymphony/xwork/util/LocalizedTextUtilTest.java

  */
 public class LocalizedTextUtilTest extends TestCase {
 
+	public void testNpeWhenClassIsPrimitive() throws Exception {
+		OgnlValueStack stack = new OgnlValueStack();
+		stack.push(new MyObject());
+		String result = LocalizedTextUtil.findText(MyObject.class, "someObj.someI18nKey", Locale.ENGLISH, "default message", null, stack);
+		// we should not get any exception;
+		assertEquals(result, "default message");
+	}
+	
+	public static class MyObject extends ActionSupport {
+		
+		private static final long serialVersionUID = -3835214664442662228L;
+
+		public boolean getSomeObj() {
+			return true;
+		}
+	}
+	
+	
     public void testActionGetText() throws Exception {
         ModelDrivenAction2 action = new ModelDrivenAction2();
         TestBean2 bean = (TestBean2) action.getModel();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.