Steve Lancashire avatar Steve Lancashire committed 705e6ff

Update unit test for changes to ConversionErrorInterceptor

Comments (0)

Files changed (2)

src/java/com/opensymphony/xwork/interceptor/ConversionErrorInterceptor.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
 package com.opensymphony.xwork.interceptor;
- 
-import java.util.HashMap;
-import java.util.Map;
 
 import com.opensymphony.xwork.ActionContext;
 import com.opensymphony.xwork.ActionInvocation;
 import com.opensymphony.xwork.ValidationAware;
 import com.opensymphony.xwork.util.XWorkConverter;
- 
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+
 /**
  * A simple {@link ConversionErrorInterceptor} that sets the value of the field directly back in the context rather than
  * in the stack, which get evaluated as an OGNL expression. Workaround for:
  * https://www.sec-consult.com/files/20120104-0_Apache_Struts2_Multiple_Critical_Vulnerabilities.txt
  *
- * @since v3.4
  */
-@SuppressWarnings({"unchecked"})
-public class ConversionErrorInterceptor implements Interceptor
-{
-   // ------------------------------------------------------------------------------------------------------- Constants
-   // ------------------------------------------------------------------------------------------------- Type Properties
-   // ---------------------------------------------------------------------------------------------------- Dependencies
-   // ---------------------------------------------------------------------------------------------------- Constructors
-   // ----------------------------------------------------------------------------------------------- Interface Methods
-   public void destroy()
-   {
-   }
- 
-   public void init()
-   {
-   }
- 
-   public String intercept(final ActionInvocation invocation) throws Exception
-   {
-       ActionContext invocationContext = invocation.getInvocationContext();
-       final Map<String, Object> conversionErrors = invocationContext.getConversionErrors();
-       
-       ValidationAware action = null;
-       if(invocation.getAction() instanceof ValidationAware)
-       {
-    	   action = (ValidationAware)action;
-       }
-       final Map<String, String> filteredConversionErrors = new HashMap<String, String>();
- 
-       for (final Map.Entry<String, Object> stringObjectEntry : conversionErrors.entrySet())
-       {
-           Map.Entry<String, Object> entry = stringObjectEntry;
-           String propertyName = entry.getKey();
-           String value = getStringValue(entry.getValue());
- 
-           if (value != null && !value.isEmpty())
-           {
-               filteredConversionErrors.put(propertyName, value);
- 
-               if (action != null)
-               {
-                   String message = XWorkConverter.getConversionErrorMessage(propertyName, invocationContext.getValueStack());
-                   action.addFieldError(propertyName, message);
-               }
-           }
-       }
- 
-       // Add the value back to the page context, doesn't go through all the OGNL malarky. Form fields will see the value in the action first though
-       invocation.addPreResultListener(new PreResultListener()
-       {
-           public void beforeResult(ActionInvocation invocation, String resultCode)
-           {
-               if (invocation.getStack() != null && invocation.getStack().getContext() != null)
-               {
-                   Map<String, Object> map = invocation.getStack().getContext();
- 
-                   map.putAll(filteredConversionErrors);
-               }
-           }
-       });
- 
-       return invocation.invoke();
-   }
- 
-   // -------------------------------------------------------------------------------------------------- Public Methods
-   // -------------------------------------------------------------------------------------------------- Helper Methods
-   // -------------------------------------------------------------------------------------- Basic Accessors / Mutators
-   private static String getStringValue(final Object o)
-   {
-       if (o instanceof String[])
-       {
-           String[] strings = (String[]) o;
-           if (strings.length > 0)
-           {
-               return strings[0];
-           }
-           else
-           {
-               return null;
-           }
-       }
-       else
-       {
-           return (String) o;
-       }
-   }
-}
+public class ConversionErrorInterceptor implements Interceptor {
+  
+    public void destroy() {
+    }
+
+    public void init() {
+    }
+
+    public String intercept(final ActionInvocation invocation) throws Exception {
+        ActionContext invocationContext = invocation.getInvocationContext();
+        final Map conversionErrors = invocationContext.getConversionErrors();
+
+        ValidationAware action = null;
+
+        if (invocation.getAction() instanceof ValidationAware) {
+            action = (ValidationAware) invocation.getAction();
+        }
+
+        final Map filteredConversionErrors = new HashMap();
+
+        for (Iterator it = conversionErrors.entrySet().iterator();
+                it.hasNext();) {
+            Map.Entry entry = (Map.Entry) it.next();
+            String propertyName = (String) entry.getKey();
+            String value = getStringValue(entry.getValue());
+
+            if ((value != null) && !value.isEmpty()) {
+                filteredConversionErrors.put(propertyName, value);
+
+                if (action != null) {
+                    String message = XWorkConverter.getConversionErrorMessage(propertyName, invocationContext.getValueStack());
+                    action.addFieldError(propertyName, message);
+                }
+            }
+        }
+
+        // Add the value back to the page context, doesn't go through all the OGNL malarky. Form fields will see the value in the action first though
+        invocation.addPreResultListener(new PreResultListener() {
+                public void beforeResult(ActionInvocation invocation, String resultCode) {
+                    if ((invocation.getStack() != null) && (invocation.getStack().getContext() != null)) {
+                        Map map = invocation.getStack().getContext();
+
+                        map.putAll(filteredConversionErrors);
+                    }
+                }
+            });
+
+        return invocation.invoke();
+    }
+
+    private static String getStringValue(final Object o) {
+        if (o instanceof String[]) {
+            String[] strings = (String[]) o;
+
+            if (strings.length > 0) {
+                return strings[0];
+            } else {
+                return null;
+            }
+        } else {
+            return o != null ? o.toString() : "";
+        }
+    }
+}

src/test/com/opensymphony/xwork/interceptor/ConversionErrorInterceptorTest.java

         conversionErrors.put("foo", new Long(123));
 
         SimpleAction action = new SimpleAction();
+        mockInvocation.expectAndReturn("getAction", action);
+        mockInvocation.expectAndReturn("getAction", action); 
         stack.push(action);
         assertNull(action.getFieldErrors().get("foo"));
         interceptor.intercept(invocation);
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.