1 package org.displaytag.test; 2 3 import java.io.File; 4 import java.net.URL; 5 import java.net.URLDecoder; 6 7 import junit.framework.TestCase; 8 9 import org.apache.commons.lang.ArrayUtils; 10 import org.apache.commons.lang.StringUtils; 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 14 import com.meterware.servletunit.ServletRunner; 15 16 17 /** 18 * Base TestCase class for tests. 19 * @author Fabrizio Giustina 20 * @version $Revision: 1023 $ ($Author: fgiust $) 21 */ 22 public abstract class DisplaytagCase extends TestCase 23 { 24 25 /** 26 * Context mapped to the test application. 27 */ 28 public static final String CONTEXT = "/context"; 29 30 /** 31 * logger. 32 */ 33 protected final Log log = LogFactory.getLog(getClass()); 34 35 /** 36 * HttpUnit ServletRunner. 37 */ 38 protected ServletRunner runner; 39 40 /** 41 * Returns the tested jsp name. 42 * @return jsp name 43 */ 44 public abstract String getJspName(); 45 46 /** 47 * Runs the test. 48 * @param jspName jsp name, with full path 49 * @throws Exception any axception thrown during test. 50 */ 51 public abstract void doTest(String jspName) throws Exception; 52 53 /** 54 * run the test with the non-el tld. 55 * @throws Exception any axception thrown during test. 56 */ 57 public void test11() throws Exception 58 { 59 doTest("http://localhost" + CONTEXT + "/standard/" + getJspName()); 60 } 61 62 /** 63 * run the test with the el tld. 64 * @throws Exception any axception thrown during test. 65 */ 66 public void testEL() throws Exception 67 { 68 doTest("http://localhost" + CONTEXT + "/el/" + getJspName()); 69 } 70 71 /** 72 * @see junit.framework.TestCase#setUp() 73 */ 74 protected void setUp() throws Exception 75 { 76 // need to pass a web.xml file to setup servletunit working directory 77 ClassLoader classLoader = getClass().getClassLoader(); 78 URL webXmlUrl = classLoader.getResource("WEB-INF/web.xml"); 79 String path = URLDecoder.decode(webXmlUrl.getFile(), "UTF-8"); 80 81 // start servletRunner 82 runner = new ServletRunner(new File(path), CONTEXT); 83 log.debug("ServletRunner setup OK"); 84 85 super.setUp(); 86 } 87 88 /** 89 * @see junit.framework.TestCase#tearDown() 90 */ 91 protected void tearDown() throws Exception 92 { 93 // shutdown servlet engine 94 runner.shutDown(); 95 96 super.tearDown(); 97 } 98 99 /** 100 * @see junit.framework.TestCase#getName() 101 */ 102 public String getName() 103 { 104 return getClass().getName() + "." + super.getName() + " (" + getJspName() + ")"; 105 } 106 107 /** 108 * Compare 2 arrays of string ignoring order. 109 * @param message message to output in case of failure 110 * @param expected expected array 111 * @param actual actual array 112 */ 113 public void assertEqualsIgnoreOrder(String message, String[] expected, String[] actual) 114 { 115 if (expected.length != actual.length) 116 { 117 fail(message 118 + " Wrong number of values, expected " 119 + expected.length 120 + " (" 121 + ArrayUtils.toString(expected) 122 + "), actual " 123 + actual.length 124 + " (" 125 + ArrayUtils.toString(actual) 126 + ")"); 127 } 128 129 outer : for (int j = 0; j < expected.length; j++) 130 { 131 String exp = expected[j]; 132 for (int q = 0; q < actual.length; q++) 133 { 134 if (StringUtils.equals(exp, actual[q])) 135 { 136 continue outer; 137 } 138 } 139 fail(message + " Expected value \"" + exp + "\" not found in actual array: " + ArrayUtils.toString(actual)); 140 } 141 } 142 }