1 20 package org.enhydra.barracuda.core.view; 21 22 import java.io.*; 23 import java.util.*; 24 import java.security.*; 25 import javax.servlet.*; 26 import javax.servlet.http.*; 27 28 import org.w3c.dom.*; 29 import junit.framework.*; 30 31 import org.enhydra.barracuda.plankton.*; 32 import org.enhydra.barracuda.plankton.data.*; 33 import org.enhydra.barracuda.core.util.dom.*; 34 import org.apache.log4j.*; 35 import org.enhydra.barracuda.core.view.*; 36 import org.enhydra.barracuda.examples.xmlc.*; 37 import org.enhydra.barracuda.testbed.*; 38 import org.enhydra.barracuda.testbed.servlet.*; 39 40 41 45 public class TestViewUtil extends DefaultTestCase { 46 private static String testClass = TestViewUtil.class.getName(); 48 private static Logger logger = Logger.getLogger("test."+testClass); 49 public static final String UAS_PROPS = "user-agents.properties"; 50 51 String uas = null; 53 ClientType ctTarget = null; 54 55 59 public TestViewUtil(String name) { 60 super(name); 61 } 62 63 66 public TestViewUtil(String name, String iuas, ClientType ictTarget) { 67 super(name); 68 uas = iuas; 69 ctTarget = ictTarget; 70 } 71 72 79 public static void main(String args[]) { 80 TestUtil.parseParams(args); 82 83 if (TestUtil.BATCH_MODE) junit.textui.TestRunner.main(new String[] {testClass}); 85 else junit.swingui.TestRunner.main(new String[] {testClass}); 86 } 87 88 92 public static Test suite() { 93 TestSuite suite = new TestSuite(); 95 96 suite.addTest(new TestViewUtil("testMockup")); 99 100 InputStream is = TestViewUtil.class.getResourceAsStream(UAS_PROPS); 102 BufferedReader in = new BufferedReader(new InputStreamReader(is)); 103 boolean inComments = false; 104 try { 105 while (true) { 106 String s = in.readLine(); 108 if (s==null) break; 109 s = s.trim(); 110 if (s.startsWith("//")) continue; 111 if (!inComments) { 112 int cpos = s.indexOf("/*"); 113 if (cpos>-1) { 114 s = s.substring(0,cpos); 115 inComments = true; 116 } 117 } else { 118 int cpos = s.indexOf("*/"); 119 if (cpos>-1) { 120 s = s.substring(cpos+2); 121 inComments = false; 122 } else { 123 continue; 124 } 125 } 126 if (s.equals("")) continue; 127 128 String uas = null; 130 String cts = null; 131 int spos = s.indexOf("=="); 132 if (spos==-1) throw new RuntimeException("Fatal error parsing "+UAS_PROPS+" --> "+s); 133 uas = s.substring(0,spos).trim(); 134 if (uas.equals("")) throw new RuntimeException("Invalid user-agent string in "+UAS_PROPS+" --> "+s); 135 cts = s.substring(spos+2).trim(); 136 if (cts==null) throw new RuntimeException("Fatal error parsing client type "+UAS_PROPS+" --> "+s); 137 ClientType ct = ClientType.getInstance(cts); 138 if (ct==null) throw new RuntimeException("Invalid client type in "+UAS_PROPS+" --> "+cts); 139 140 suite.addTest(new TestViewUtil("testClientType", uas, ct)); 142 } 143 } catch (IOException e) {} 144 145 return suite; 147 } 148 149 155 156 160 public void testClientType() { 161 if (logger.isInfoEnabled()) logger.info("testing ViewUtil.getClientType: uas="+uas); 162 MockHttpServletRequest req = new MockHttpServletRequest(null); 163 req.setHeader("User-Agent", uas); 164 ClientType ct = ViewUtil.getClientType(req); 165 assertTrue("null value returned for user-agent='"+uas+"'", ct!=null); 166 assertTrue("returned '"+ct+"' instead of '"+ctTarget+"' for user-agent='"+uas, ct==ctTarget); 167 } 168 169 174 public void testMockup() { 175 if (logger.isInfoEnabled()) logger.info("testing servlet request mockup"); 176 177 MockHttpServletRequest req = null; 179 Enumeration enum = null; 180 String k1 = "foo1"; 181 String k2 = "foo2"; 182 String s1 = "blah1"; 183 String s2a = "blah2.a"; 184 String s2b = "blah2.b"; 185 186 req = new MockHttpServletRequest(null); 189 assertTrue("mockup check 1a failed - hdr map not converted correctly", req.hdrMap.size()==0); 190 assertTrue("mockup check 1b.1 failed - returned bad value", req.getHeader(k1)==null); 191 enum = req.getHeaderNames(); 192 assertTrue("mockup check 1c.1 failed - returned null or non-empty enum", enum!=null && !enum.hasMoreElements()); 193 enum = req.getHeaders("foo"); 194 assertTrue("mockup check 1d.1 failed - returned null or non-empty enum", enum!=null && !enum.hasMoreElements()); 195 req = new MockHttpServletRequest(null); 197 req.setHeader(k1,s1); 198 req.addHeader(k2,s2a); 199 req.addHeader(k2,s2b); 200 assertTrue("mockup check 2a failed - hdr map not converted correctly", req.hdrMap.size()==2); 201 assertTrue("mockup check 2b.1 failed - returned bad value", "blah1".equals(req.getHeader(k1))); 202 enum = req.getHeaderNames(); 203 for (int i=0; i<2; i++) { 204 Object o = enum.nextElement(); 205 assertTrue("mockup check 2c.1 failed - wrong element, cntr="+i, o==k1 || o==k2); 206 } 207 assertTrue("mockup check 2c.2 failed - enum claims more elements", !enum.hasMoreElements()); 208 assertTrue("mockup check 2d failed - wrong value", s1.equals(req.getHeader(k1))); 209 enum = req.getHeaders(k2); 210 for (int i=0; i<2; i++) { 211 Object o = enum.nextElement(); 212 assertTrue("mockup check 2e.2 failed - wrong element, cntr="+i, o==s2a || o==s2b); 213 } 214 assertTrue("mockup check 2e.2 failed - enum claims more elements", !enum.hasMoreElements()); 215 } 216 217 } 218 | Popular Tags |