Commits

Anonymous committed 8c2770c

Minor tweaks to better handle different states of ActionContext

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1849e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 0a49a4e

Comments (0)

Files changed (5)

File src/java/com/opensymphony/xwork2/config/impl/DefaultConfiguration.java

                 return DefaultConfiguration.this;
             }
         });
-        
+
+        ActionContext oldContext = ActionContext.getContext();
         try {
             // Set the bootstrap container for the purposes of factory creation
             Container bootstrap = createBootstrapContainer();
     
             rebuildRuntimeConfiguration();
         } finally {
-            ActionContext.setContext(null);
+            if (oldContext == null) {
+                ActionContext.setContext(null);
+            }
         }
         return packageProviders;
     }
     
     protected ActionContext setContext(Container cont) {
-        ValueStack vs = cont.getInstance(ValueStackFactory.class).createValueStack();
-        ActionContext context = new ActionContext(vs.getContext());
-        ActionContext.setContext(context);
+        ActionContext context = ActionContext.getContext();
+        if (context == null) {
+            ValueStack vs = cont.getInstance(ValueStackFactory.class).createValueStack();
+            context = new ActionContext(vs.getContext());
+            ActionContext.setContext(context);
+        }
         return context;
     }
 

File src/java/com/opensymphony/xwork2/conversion/impl/XWorkConverter.java

 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ObjectFactory;
 import com.opensymphony.xwork2.XWorkMessages;
+import com.opensymphony.xwork2.XWorkException;
 import com.opensymphony.xwork2.conversion.TypeConverter;
 import com.opensymphony.xwork2.conversion.annotations.Conversion;
 import com.opensymphony.xwork2.conversion.annotations.ConversionRule;
     @Inject
     public void setObjectFactory(ObjectFactory factory) {
         this.objectFactory = factory;
-        try {
-            // note: this file is deprecated
-            loadConversionProperties("xwork-default-conversion.properties");
-        } catch (Exception e) {
-            //swallow
-        }
+        // note: this file is deprecated
+        loadConversionProperties("xwork-default-conversion.properties");
 
-        try {
-            loadConversionProperties("xwork-conversion.properties");
-        } catch (Exception e) {
-            //swallow
-        }
+        loadConversionProperties("xwork-conversion.properties");
     }
 
     @Inject
     }
 
     protected void handleConversionException(Map<String, Object> context, String property, Object value, Object object) {
-        if ((Boolean.TRUE.equals(context.get(REPORT_CONVERSION_ERRORS)))) {
+        if (context != null && (Boolean.TRUE.equals(context.get(REPORT_CONVERSION_ERRORS)))) {
             String realProperty = property;
             String fullName = (String) context.get(CONVERSION_PROPERTY_FULLNAME);
 
         }
     }
 
-    public void loadConversionProperties(String propsName) throws IOException {
-        Iterator<URL> resources = ClassLoaderUtil.getResources(propsName, getClass(), true);
-        while (resources.hasNext()) {
-            URL url = resources.next();
-            Properties props = new Properties();
-            props.load(url.openStream());
+    public void loadConversionProperties(String propsName) {
+        loadConversionProperties(propsName, false);
+    }
 
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("processing conversion file [" + propsName + "]");
-            }
+    public void loadConversionProperties(String propsName, boolean require) {
+        try {
+            Iterator<URL> resources = ClassLoaderUtil.getResources(propsName, getClass(), true);
+            while (resources.hasNext()) {
+                URL url = resources.next();
+                Properties props = new Properties();
+                props.load(url.openStream());
+
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("processing conversion file [" + propsName + "]");
+                }
 
-            for (Object o : props.entrySet()) {
-                Map.Entry entry = (Map.Entry) o;
-                String key = (String) entry.getKey();
+                for (Object o : props.entrySet()) {
+                    Map.Entry entry = (Map.Entry) o;
+                    String key = (String) entry.getKey();
 
-                try {
-                    TypeConverter _typeConverter = createTypeConverter((String) entry.getValue());
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("\t" + key + ":" + entry.getValue() + " [treated as TypeConverter " + _typeConverter + "]");
+                    try {
+                        TypeConverter _typeConverter = createTypeConverter((String) entry.getValue());
+                        if (LOG.isDebugEnabled()) {
+                            LOG.debug("\t" + key + ":" + entry.getValue() + " [treated as TypeConverter " + _typeConverter + "]");
+                        }
+                        defaultMappings.put(key, _typeConverter);
+                    } catch (Exception e) {
+                        LOG.error("Conversion registration error", e);
                     }
-                    defaultMappings.put(key, _typeConverter);
-                } catch (Exception e) {
-                    LOG.error("Conversion registration error", e);
                 }
             }
+        } catch (IOException ex) {
+            if (require) {
+                throw new XWorkException("Cannot load conversion properties file: "+propsName, ex);
+            } else {
+                LOG.debug("Cannot load conversion properties file: "+propsName, ex);
+            }
         }
     }
 

File src/test/com/opensymphony/xwork2/WildCardResultTest.java

     }
 
     public void testWildCardEvaluation() throws Exception {
+        ActionContext.setContext(null);
         ActionProxy proxy = actionProxyFactory.createActionProxy(null, "WildCard", null);
         assertEquals("success", proxy.execute());
         assertEquals(VoidResult.class, proxy.getInvocation().getResult().getClass());
 
+        ActionContext.setContext(null);
         proxy = actionProxyFactory.createActionProxy(null, "WildCardInput", null);
         assertEquals("input", proxy.execute());
         assertEquals(MockResult.class, proxy.getInvocation().getResult().getClass());
 
+        ActionContext.setContext(null);
         proxy = actionProxyFactory.createActionProxy(null, "WildCardError", null);
         assertEquals("error", proxy.execute());
         assertEquals(MockResult.class, proxy.getInvocation().getResult().getClass());

File src/test/com/opensymphony/xwork2/conversion/impl/XWorkConverterTest.java

                                         "too many times");
                                 }
 
-                        try {
-                                    done = true;
-                                    return new File(
-                                        "src/test/com/opensymphony/xwork2/conversion/impl/test-xwork-conversion.properties").
-                                        toURI().toURL();
-                                } catch (MalformedURLException e) {
-                                    // Eeck
-                                    throw new RuntimeException(e);
-                        }
+                                done = true;
+                                return getClass().getResource("/com/opensymphony/xwork2/conversion/impl/test-xwork-conversion.properties");
                             }
                         };
                     } else {

File src/test/com/opensymphony/xwork2/validator/SimpleActionValidationTest.java

             assertFalse(validationAware.hasFieldErrors());
 
             // put in an out-of-range value to see if the old validators still work
+            ActionContext.setContext(new ActionContext(new HashMap<String, Object>()));
             params.put("bar", "42");
             proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ALIAS_NAME, extraContext);
             proxy.execute();
             validationAware = (ValidationAware) proxy.getAction();
             assertTrue(validationAware.hasFieldErrors());
 
-            Map errors = validationAware.getFieldErrors();
+            Map<String, List<String>> errors = validationAware.getFieldErrors();
             assertTrue(errors.containsKey("baz"));
 
-            List bazErrors = (List) errors.get("baz");
+            List<String> bazErrors = errors.get("baz");
             assertEquals(1, bazErrors.size());
 
-            String message = (String) bazErrors.get(0);
+            String message = bazErrors.get(0);
             assertEquals("baz out of range.", message);
             assertTrue(errors.containsKey("bar"));
 
-            List barErrors = (List) errors.get("bar");
+            List<String> barErrors = errors.get("bar");
             assertEquals(1, barErrors.size());
-            message = (String) barErrors.get(0);
+            message = barErrors.get(0);
             assertEquals("bar must be between 6 and 10, current value is 42.", message);
         } catch (Exception e) {
             e.printStackTrace();
             proxy.execute();
             assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
 
-            Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
-            List bazErrors = (List) errors.get("baz");
+            Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
+            List<String> bazErrors = errors.get("baz");
             assertEquals(1, bazErrors.size());
 
-            String errorMessage = (String) bazErrors.get(0);
+            String errorMessage = bazErrors.get(0);
             assertNotNull(errorMessage);
             assertEquals("Baz Field must be greater than 0", errorMessage);
         } catch (Exception e) {
             proxy.execute();
             assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
 
-            Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
-            List fooErrors = (List) errors.get("foo");
+            Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
+            List<String> fooErrors = errors.get("foo");
             assertEquals(1, fooErrors.size());
 
-            String errorMessage = (String) fooErrors.get(0);
+            String errorMessage = fooErrors.get(0);
             assertNotNull(errorMessage);
             assertEquals("Foo Range Message", errorMessage);
         } catch (Exception e) {
         validator.validate(this);
         assertTrue(validatorContext.hasActionErrors());
 
-        Collection errors = validatorContext.getActionErrors();
+        Collection<String> errors = validatorContext.getActionErrors();
         assertEquals(1, errors.size());
         assertEquals(messageKey, errors.toArray()[0]);
     }
             proxy.execute();
             assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
 
-            Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
-            List barErrors = (List) errors.get("bar");
+            Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
+            List<String> barErrors = errors.get("bar");
             assertEquals(1, barErrors.size());
 
-            String errorMessage = (String) barErrors.get(0);
+            String errorMessage = barErrors.get(0);
             assertNotNull(errorMessage);
             assertEquals("bar must be between 6 and 10, current value is 42.", errorMessage);
         } catch (Exception e) {
             proxy.execute();
             assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
 
-            Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
-            List beanCountErrors = (List) errors.get("bean.count");
+            Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
+            List<String> beanCountErrors = errors.get("bean.count");
             assertEquals(1, beanCountErrors.size());
 
-            String errorMessage = (String) beanCountErrors.get(0);
+            String errorMessage = beanCountErrors.get(0);
             assertNotNull(errorMessage);
             assertEquals("bean.count out of range.", errorMessage);
         } catch (Exception e) {