KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > view > TestViewUtil


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: TestViewUtil.java,v 1.9 2004/02/01 05:16:33 christianc Exp $
19  */

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 /**
42  * This test verifies that the ViewUtil class can parse
43  * request headers to validate client capabilities
44  */

45 public class TestViewUtil extends DefaultTestCase {
46     //common vars (customize for every test class)
47
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     //variables
52
String uas = null;
53     ClientType ctTarget = null;
54
55     //-------------------- Basics --------------------------------
56
/**
57      * Public Constructor
58      */

59     public TestViewUtil(String name) {
60         super(name);
61     }
62
63     /**
64      * Public Constructor
65      */

66     public TestViewUtil(String name, String iuas, ClientType ictTarget) {
67         super(name);
68         uas = iuas;
69         ctTarget = ictTarget;
70     }
71
72     /**
73      * Every test class should have a main method so it can be run
74      * directly (when debugging tests you often will not want to run
75      * the whole suite)
76      *
77      * @param args defined in test.util.TestUtil
78      */

79     public static void main(String args[]) {
80         //check for standard runtime parameters
81
TestUtil.parseParams(args);
82
83         //launch the test
84
if (TestUtil.BATCH_MODE) junit.textui.TestRunner.main(new String[] {testClass});
85         else junit.swingui.TestRunner.main(new String[] {testClass});
86     }
87
88     /**
89      * Because we need to test so many different user-agent strings,
90      * we will return a suite of tests...
91      */

92     public static Test suite() {
93         //create the test suite
94
TestSuite suite = new TestSuite();
95         
96         //this test fundamentally depends on the servlet wrapper object,
97
//so we need to test that as well
98
suite.addTest(new TestViewUtil("testMockup"));
99
100         //start by getting a reference to the properties file
101
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                 //read a line & make sure its valid
107
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                 //now figure out the uas and ctype settings
129
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                 //add the test
141
suite.addTest(new TestViewUtil("testClientType", uas, ct));
142             }
143         } catch (IOException e) {}
144         
145         //finally return the suite
146
return suite;
147     }
148
149     //-------------------- Actual Tests --------------------------
150
//Note: all the methods herein should follow the testXXXX naming convention
151
//Also keep in mind that local vars set in one test method are NOT retained
152
//when the next method is invoked because JUnit makes a separate instance of
153
//the test class for each testXXXX method!!!
154

155
156     /**
157      * Test the client types to make sure we get what we expect for a given
158      * user agent string
159      */

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     /**
170      * Simple test to make sure the mockup servlet request to
171      * make sure it functions as expected (ie. the necessary methods
172      * do what the wrapper class will expect them to do)
173      */

174     public void testMockup() {
175         if (logger.isInfoEnabled()) logger.info("testing servlet request mockup");
176
177         //vars
178
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         //try something simple first (to make sure our mockup is working)
187
//...null param string
188
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         //...now try with some data
196
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