Anonymous avatar Anonymous committed ad1ae64

WW-1480
- Unit test fail randomly on different boxes (Courtesy of Konstantin, Thanks Konstantin)

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@2954 573baa09-0c28-0410-bef9-dab3c582ae83

Comments (0)

Files changed (4)

src/test/com/opensymphony/webwork/dispatcher/ServletActionRedirectResultTest.java

 import com.opensymphony.webwork.ServletActionContext;
 import com.opensymphony.webwork.WebWorkTestCase;
 import com.opensymphony.webwork.dispatcher.ServletActionRedirectResult;
+import com.opensymphony.webwork.views.util.UrlHelper;
 
 import org.easymock.MockControl;
 import org.springframework.mock.web.MockHttpServletRequest;
 		actionInvocationControl.replay();
 		
 		result.execute(mockInvocation);
-		assertEquals("/myNamespace/myAction.action?param2=value+2&param1=value+1&param3=value+3", res.getRedirectedUrl());
+		
+		// ugly hack to ensure consistent ordering
+		HashMap hm = new HashMap();
+		hm.put("param1", "value 1");		
+		hm.put("param2", "value 2");		
+		hm.put("param3", "value 3");	
+		StringBuffer expected = new StringBuffer("/myNamespace/myAction.action");
+		UrlHelper.buildParametersString(hm, expected,"&");
+
+		assertEquals(expected.toString(), res.getRedirectedUrl());
 		
 		actionProxyControl.verify();
 		actionInvocationControl.verify();
 		
 		actionProxyControl.replay();
 		actionInvocationControl.replay();
+
 		
+				
 		result.execute(mockInvocation);
-		assertEquals("/myNamespace/myAction.action?param2=value+2&param1=value+1&param3=value+3", res.getRedirectedUrl());
+		
+		//ugly hack to ensure consistent ordering
+		HashMap hm = new HashMap();
+		hm.put("param1", "value 1");		
+		hm.put("param2", "value 2");		
+		hm.put("param3", "value 3");	
+		StringBuffer expected = new StringBuffer("/myNamespace/myAction.action");
+		UrlHelper.buildParametersString(hm, expected,"&");
+
+		assertEquals(expected.toString(), res.getRedirectedUrl());
 		
 		actionProxyControl.verify();
 		actionInvocationControl.verify();

src/test/com/opensymphony/webwork/views/jsp/PortletUrlTagTest.java

 package com.opensymphony.webwork.views.jsp;
 
 import java.lang.reflect.Field;
+import java.net.URLEncoder;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 	}
 	
 	public void testResourceUrlWithTwoNestedParam() throws Exception {
+		// this is ugly, and replicates  what is done
+		// in ProtletUrlHelper - but prevents faulures ariding
+		// from inconsistent ordering of hashmap 
+		// unfortunately it is impossible to use PortletURLHelper, due to 
+		// mocked classes in context 
+		HashMap pp = new HashMap();
+		pp.put("testParam1", "testValue1");
+		pp.put("testParam2", "testValue2");
+		
+		StringBuffer sb = new StringBuffer("/contextPath/image.gif?");
+        Iterator it = pp.keySet().iterator();
+        while(it.hasNext()) {
+            String key = (String)it.next();
+            String val = (String)pp.get(key);
+            sb.append(URLEncoder.encode(key)).append("=");
+            sb.append(URLEncoder.encode(val));
+            if(it.hasNext()) {
+                sb.append("&");
+            }
+        }
+	
+		
+		String toCompare = sb.toString();
+		// end of ulgy hack.
+		
 		mockHttpReq.stubs().method("getQueryString").will(returnValue(""));
-		mockPortletRes.expects(once()).method("encodeURL").with(eq("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2")).will(returnValue("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2"));
-		mockJspWriter.setExpectedData("/contextPath/image.gif?testParam1=testValue1&testParam2=testValue2");
+		mockPortletRes.expects(once()).method("encodeURL").with(eq(toCompare)).will(returnValue(toCompare));
+		mockJspWriter.setExpectedData(toCompare);
 		
 		ParamTag paramTag = new ParamTag();
 		paramTag.setPageContext((PageContext)mockPageCtx.proxy());

src/test/com/opensymphony/webwork/views/jsp/URLTagTest.java

 import com.opensymphony.webwork.components.URL;
 
 import com.opensymphony.webwork.views.jsp.ParamTag;
+import com.opensymphony.webwork.views.util.UrlHelper;
+
 import org.springframework.mock.web.MockHttpServletRequest;
 import org.springframework.mock.web.MockHttpServletResponse;
                                                                         
         tag.component.addParameter("param1", "value1");
         tag.component.addParameter("param2", "value2");
         tag.doEndTag();
-        assertEquals("/TestAction.action?param2=value2&param0=value0&param1=value1", writer.toString());
+        
+		//ugly hack to ensure consistent ordering
+		HashMap hm = new HashMap();
+		hm.put("param1", "value1");		
+		hm.put("param2", "value2");		
+		hm.put("param0", "value0");	
+		StringBuffer expected = new StringBuffer("/TestAction.action");
+		UrlHelper.buildParametersString(hm, expected);
+
+        assertEquals(expected.toString(), writer.toString());
     }
 
     public void testEvaluateValue() throws Exception {
     	paramTag.doStartTag();
     	paramTag.doEndTag();
     	urlTag.doEndTag();
+       	
+		//ugly hack to ensure consistent ordering
+		HashMap hm = new HashMap();
+		hm.put("name", "John");		
+		hm.put("id", "33");		
+		StringBuffer expected = new StringBuffer("/context/someAction.action");
+		UrlHelper.buildParametersString(hm, expected);
     	
-    	assertEquals(writer.getBuffer().toString(), "/context/someAction.action?name=John&id=33");
+    	assertEquals(expected.toString(),writer.getBuffer().toString());
     }
 
     public void testParamPrecedenceWithAnchor() throws Exception {
     	paramTag.doStartTag();
     	paramTag.doEndTag();
     	urlTag.doEndTag();
-
-    	assertEquals(writer.getBuffer().toString(), "/context/someAction.action?name=John&id=33#testAnchor");
+    	
+		//ugly hack to ensure consistent ordering
+		HashMap hm = new HashMap();
+		hm.put("name", "John");		
+		hm.put("id", "33");		
+		StringBuffer expected = new StringBuffer("/context/someAction.action");
+		UrlHelper.buildParametersString(hm, expected);
+		expected.append("#testAnchor");
+		
+    	assertEquals(expected.toString(), writer.getBuffer().toString());
     }
 
     public void testPutId() throws Exception {
         tag.setIncludeParams("get");
         tag.doStartTag();
         tag.doEndTag();
-
-        assertEquals("/team.action?section=team&company=acme+inc", writer.toString());
+		//ugly hack to ensure consistent ordering
+		HashMap hm = new HashMap();
+		hm.put("section", "team");		
+		hm.put("company", "acme inc");		
+		StringBuffer expected = new StringBuffer("/team.action");
+		UrlHelper.buildParametersString(hm, expected);
+
+        assertEquals(expected.toString(), writer.toString());
     }
 
     public void testRequestURINoActionIncludeNone() throws Exception {
         tag.doStartTag();
         tag.doEndTag();
 
-        assertEquals("/public/about?section=team&company=acme+inc", writer.toString());
+		//ugly hack to ensure consistent ordering
+		HashMap hm = new HashMap();
+		hm.put("section", "team");		
+		hm.put("company", "acme inc");		
+		StringBuffer expected = new StringBuffer("/public/about");
+		UrlHelper.buildParametersString(hm, expected);
+
+        assertEquals(expected.toString(), writer.toString());
     }
 
     public void testRequestURIActionIncludeAll() throws Exception {
         paramTag.doEndTag();
 
         tag.doEndTag();
+		//ugly hack to ensure consistent ordering
+		HashMap hm = new HashMap();
+		hm.put("section", "team");		
+		hm.put("company", "acme inc");		
+		hm.put("year", "2006");	
+		StringBuffer expected = new StringBuffer("/team.action");
+		UrlHelper.buildParametersString(hm, expected);
 
-        assertEquals("/team.action?section=team&year=2006&company=acme+inc", writer.toString());
-    }
+        assertEquals(expected.toString(), writer.toString());
+   }
 
     public void testRequestURINoActionIncludeAll() throws Exception {
         request.setRequestURI("/public/about");
         paramTag.doEndTag();
 
         tag.doEndTag();
-
-        assertEquals("/public/about?section=team&year=2006&company=acme+inc", writer.toString());
+		//ugly hack to ensure consistent ordering
+		HashMap hm = new HashMap();
+		hm.put("section", "team");		
+		hm.put("company", "acme inc");		
+		hm.put("year", "2006");	
+		StringBuffer expected = new StringBuffer("/public/about");
+		UrlHelper.buildParametersString(hm, expected);
+
+        assertEquals(expected.toString(), writer.toString());
     }
 
     public void testUnknownIncludeParam() throws Exception {

src/test/com/opensymphony/webwork/views/jsp/ui/AbstractRichtexteditorTest.java

 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 
+import javax.xml.parsers.DocumentBuilder;
+
+import org.codehaus.plexus.util.StringInputStream;
+import org.custommonkey.xmlunit.Diff;
+import org.custommonkey.xmlunit.Difference;
+import org.custommonkey.xmlunit.DifferenceConstants;
+import org.custommonkey.xmlunit.DifferenceListener;
+import org.custommonkey.xmlunit.XMLTestCase;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.w3c.dom.Document;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
 import com.mockobjects.servlet.MockHttpServletResponse;
 import com.opensymphony.webwork.ServletActionContext;
 import com.opensymphony.webwork.views.jsp.AbstractUITagTest;
 import junit.framework.TestCase;
 
 /**
- * Abstract test case for all RichTextEditor based test case. (contains common methods)
+ * Abstract test case for all RichTextEditor based test case. (contains common
+ * methods)
  * 
  * @author tm_jee
- * @version $Date$ $Id$
+ * @version $Date$ $Id:
+ *          AbstractRichtexteditorTest.java 2308 2006-03-04 15:07:50Z rainerh $
  */
 public abstract class AbstractRichtexteditorTest extends TestCase {
-	
+
 	protected MockActionInvocation invocation;
+
 	protected MockHttpServletResponse response;
-	
+
+	protected DocumentBuilder expectedParser;
+
+	protected DocumentBuilder actualParser;
+
 	protected void setUp() throws Exception {
 		super.setUp();
-		//ActionProxy actionProxy = ActionProxyFactory.getFactory().createActionProxy("namespace", "actionName", new HashMap());
-		//ActionProxy actionProxy = new MockActionProxy();
-		//invocation = ActionProxyFactory.getFactory().createActionInvocation(actionProxy);
+		// ActionProxy actionProxy =
+		// ActionProxyFactory.getFactory().createActionProxy("namespace",
+		// "actionName", new HashMap());
+		// ActionProxy actionProxy = new MockActionProxy();
+		// invocation =
+		// ActionProxyFactory.getFactory().createActionInvocation(actionProxy);
 		invocation = new MockActionInvocation();
 		invocation.setStack(new OgnlValueStack());
 		response = new MockHttpServletResponse();
 		ServletActionContext.setResponse(response);
+
+		// rig up xmlunit
+		XMLUnit.setIgnoreWhitespace(true);
+		XMLUnit.getControlDocumentBuilderFactory().setIgnoringComments(true);
+		XMLUnit.getTestDocumentBuilderFactory().setIgnoringComments(true);
+		ErrorHandler errorHandler = new ErrorHandler() {
+			public void error(SAXParseException exception) throws SAXException {
+				throw exception;
+			}
+
+			public void fatalError(SAXParseException exception)
+					throws SAXException {
+				throw exception;
+			}
+
+			public void warning(SAXParseException exception) {
+			}
+		};
+
+		expectedParser = XMLUnit.getControlParser();
+		expectedParser.setErrorHandler(errorHandler);
+
+		actualParser = XMLUnit.getTestParser();
+		actualParser.setErrorHandler(errorHandler);
+
 	}
-	
+
 	protected void tearDown() throws Exception {
 		super.tearDown();
 		invocation = null;
 		response = null;
 	}
-	
-	
+
 	protected void verify(InputStream is) throws Exception {
-		String result1 = response.getOutputStreamContents();
-		
-		ByteArrayOutputStream baos = new ByteArrayOutputStream();
-		byte[] tmpBytes = new byte[1024];
-		while(is.read(tmpBytes) != -1) {
-			baos.write(tmpBytes);
-		}
-		String result2 = new String(baos.toByteArray());
-		baos.close();
-		
-		//System.out.println("*** result1 (p)="+result1);
-		//System.out.println("*** result2 (p)="+result2);
-		
-		assertEquals(AbstractUITagTest.normalize(result2.trim(), false), AbstractUITagTest.normalize(result1.trim(), false));
-	}
+		Document expectedDocument = XMLUnit.buildDocument(expectedParser,
+				new InputSource(is));
+		Document actualDocument = XMLUnit.buildDocument(actualParser,
+				new InputSource(new StringInputStream(response.getOutputStreamContents())));
+
+	       Diff diff = new Diff(expectedDocument, actualDocument) {
+	            public int differenceFound(Difference difference) {
+	                if (difference == DifferenceConstants.CHILD_NODELIST_SEQUENCE ||
+	                        difference == DifferenceConstants.ATTR_SEQUENCE) {
+	                    return DifferenceListener.RETURN_ACCEPT_DIFFERENCE;
+	                }
+	/*
+	                if (difference == DifferenceConstants.DOCTYPE_NAME ||
+	                        difference == DifferenceConstants.DOCTYPE_PUBLIC_ID ||
+	                        difference == DifferenceConstants.DOCTYPE_SYSTEM_ID) {
+	                    return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
+	                }
+	*/
+	                return super.differenceFound(difference);
+	            }
+	        };
+	        XMLTestCase xmlunit = new XMLTestCase(getName());
+
+	        xmlunit.assertXMLIdentical(diff, true);	}
 }
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.