KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jstl > test > EvaluationTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.lang.jstl.test;
18
19 import java.io.BufferedInputStream JavaDoc;
20 import java.io.BufferedOutputStream JavaDoc;
21 import java.io.DataInput JavaDoc;
22 import java.io.DataInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import javax.servlet.jsp.JspException JavaDoc;
36 import javax.servlet.jsp.PageContext JavaDoc;
37
38 import org.apache.taglibs.standard.lang.jstl.Evaluator;
39 import org.apache.taglibs.standard.lang.jstl.test.beans.Factory;
40
41 /**
42  *
43  * <p>This runs a series of tests specifically for the evaluator. It
44  * parses and evaluates various expressions in the context of a test
45  * PageContext containing preset data, and prints out the results of
46  * the evaluations.
47  *
48  * <p>The expressions are stored in an input text file, where one line
49  * contains the expression and the next line contains the expected
50  * type. Blank lines and lines that start with # are ignored. The
51  * results are written to an output file (blank lines and # lines are
52  * included in the output file). The output file may be compared
53  * against an existing output file to do regression testing.
54  *
55  * @author Nathan Abramson - Art Technology Group
56  * @version $Change: 181181 $$DateTime: 2001/06/26 09:55:09 $$Author: pierred $
57  **/

58
59 public class EvaluationTest
60 {
61   //-------------------------------------
62
// Properties
63
//-------------------------------------
64

65   //-------------------------------------
66
// Member variables
67
//-------------------------------------
68

69   //-------------------------------------
70
/**
71    *
72    * Constructor
73    **/

74   public EvaluationTest ()
75   {
76   }
77
78   //-------------------------------------
79
/**
80    *
81    * Runs the tests, reading expressions from pIn and writing the
82    * results to pOut.
83    **/

84   public static void runTests (DataInput JavaDoc pIn,
85                    PrintStream JavaDoc pOut)
86     throws IOException JavaDoc
87   {
88     PageContext JavaDoc context = createTestContext ();
89
90     while (true) {
91       String JavaDoc str = pIn.readLine ();
92       if (str == null) break;
93       if (str.startsWith ("#") ||
94       "".equals (str.trim ())) {
95     pOut.println (str);
96       }
97       else {
98     String JavaDoc typeStr = pIn.readLine ();
99     pOut.println ("Expression: " + str);
100
101     try {
102       Class JavaDoc cl = parseClassName (typeStr);
103       pOut.println ("ExpectedType: " + cl);
104       Evaluator e = new Evaluator ();
105       Object JavaDoc val = e.evaluate ("test", str, cl, null, context);
106       pOut.println ("Evaluates to: " + val);
107       if (val != null) {
108         pOut.println ("With type: " + val.getClass ().getName ());
109       }
110       pOut.println ();
111     }
112     catch (JspException JavaDoc exc) {
113       pOut.println ("Causes an error: " + exc);
114     }
115     catch (ClassNotFoundException JavaDoc exc) {
116       pOut.println ("Causes an error: " + exc);
117     }
118       }
119     }
120
121   }
122
123   //-------------------------------------
124
/**
125    *
126    * Finds the class for a class name, including primitive names
127    **/

128   static Class JavaDoc parseClassName (String JavaDoc pClassName)
129     throws ClassNotFoundException JavaDoc
130   {
131     String JavaDoc c = pClassName.trim ();
132     if ("boolean".equals (c)) {
133       return Boolean.TYPE;
134     }
135     else if ("byte".equals (c)) {
136       return Byte.TYPE;
137     }
138     else if ("char".equals (c)) {
139       return Character.TYPE;
140     }
141     else if ("short".equals (c)) {
142       return Short.TYPE;
143     }
144     else if ("int".equals (c)) {
145       return Integer.TYPE;
146     }
147     else if ("long".equals (c)) {
148       return Long.TYPE;
149     }
150     else if ("float".equals (c)) {
151       return Float.TYPE;
152     }
153     else if ("double".equals (c)) {
154       return Double.TYPE;
155     }
156     else {
157       return Class.forName (pClassName);
158     }
159   }
160
161   //-------------------------------------
162
/**
163    *
164    * Runs the tests, reading from the given input file and writing to
165    * the given output file.
166    **/

167   public static void runTests (File JavaDoc pInputFile,
168                    File JavaDoc pOutputFile)
169     throws IOException JavaDoc
170   {
171     FileInputStream JavaDoc fin = null;
172     FileOutputStream JavaDoc fout = null;
173     try {
174       fin = new FileInputStream JavaDoc (pInputFile);
175       BufferedInputStream JavaDoc bin = new BufferedInputStream JavaDoc (fin);
176       DataInputStream JavaDoc din = new DataInputStream JavaDoc (bin);
177
178       try {
179     fout = new FileOutputStream JavaDoc (pOutputFile);
180     BufferedOutputStream JavaDoc bout = new BufferedOutputStream JavaDoc (fout);
181     PrintStream JavaDoc pout = new PrintStream JavaDoc (bout);
182
183     runTests (din, pout);
184
185     pout.flush ();
186       }
187       finally {
188     if (fout != null) {
189       fout.close ();
190     }
191       }
192     }
193     finally {
194       if (fin != null) {
195     fin.close ();
196       }
197     }
198   }
199
200   //-------------------------------------
201
/**
202    *
203    * Performs a line-by-line comparison of the two files, returning
204    * true if the files are different, false if not.
205    **/

206   public static boolean isDifferentFiles (DataInput JavaDoc pIn1,
207                       DataInput JavaDoc pIn2)
208     throws IOException JavaDoc
209   {
210     while (true) {
211       String JavaDoc str1 = pIn1.readLine ();
212       String JavaDoc str2 = pIn2.readLine ();
213       if (str1 == null &&
214       str2 == null) {
215     return false;
216       }
217       else if (str1 == null ||
218            str2 == null) {
219     return true;
220       }
221       else {
222     if (!str1.equals (str2)) {
223       return true;
224     }
225       }
226     }
227   }
228
229   //-------------------------------------
230
/**
231    *
232    * Performs a line-by-line comparison of the two files, returning
233    * true if the files are different, false if not.
234    **/

235   public static boolean isDifferentFiles (File JavaDoc pFile1,
236                       File JavaDoc pFile2)
237     throws IOException JavaDoc
238   {
239     FileInputStream JavaDoc fin1 = null;
240     try {
241       fin1 = new FileInputStream JavaDoc (pFile1);
242       BufferedInputStream JavaDoc bin1 = new BufferedInputStream JavaDoc (fin1);
243       DataInputStream JavaDoc din1 = new DataInputStream JavaDoc (bin1);
244
245       FileInputStream JavaDoc fin2 = null;
246       try {
247     fin2 = new FileInputStream JavaDoc (pFile2);
248     BufferedInputStream JavaDoc bin2 = new BufferedInputStream JavaDoc (fin2);
249     DataInputStream JavaDoc din2 = new DataInputStream JavaDoc (bin2);
250
251     return isDifferentFiles (din1, din2);
252       }
253       finally {
254     if (fin2 != null) {
255       fin2.close ();
256     }
257       }
258     }
259     finally {
260       if (fin1 != null) {
261     fin1.close ();
262       }
263     }
264   }
265
266   //-------------------------------------
267
// Test data
268
//-------------------------------------
269
/**
270    *
271    * Creates and returns the test PageContext that will be used for
272    * the tests.
273    **/

274   static PageContext JavaDoc createTestContext ()
275   {
276     PageContext JavaDoc ret = new PageContextImpl ();
277
278     // Create some basic values for lookups
279
ret.setAttribute ("val1a", "page-scoped1", PageContext.PAGE_SCOPE);
280     ret.setAttribute ("val1b", "request-scoped1", PageContext.REQUEST_SCOPE);
281     ret.setAttribute ("val1c", "session-scoped1", PageContext.SESSION_SCOPE);
282     ret.setAttribute ("val1d", "app-scoped1", PageContext.APPLICATION_SCOPE);
283
284     // Create a bean
285
{
286       Bean1 b1 = new Bean1 ();
287       b1.setBoolean1 (true);
288       b1.setByte1 ((byte) 12);
289       b1.setShort1 ((short) -124);
290       b1.setChar1 ('b');
291       b1.setInt1 (4);
292       b1.setLong1 (222423);
293       b1.setFloat1 ((float) 12.4);
294       b1.setDouble1 (89.224);
295       b1.setString1 ("hello");
296       b1.setStringArray1 (new String JavaDoc [] {
297     "string1",
298     "string2",
299     "string3",
300     "string4"
301       });
302       {
303     List JavaDoc l = new ArrayList JavaDoc ();
304     l.add (new Integer JavaDoc (14));
305     l.add ("another value");
306     l.add (b1.getStringArray1 ());
307     b1.setList1 (l);
308       }
309       {
310     Map JavaDoc m = new HashMap JavaDoc ();
311     m.put ("key1", "value1");
312     m.put (new Integer JavaDoc (14), "value2");
313     m.put (new Long JavaDoc (14), "value3");
314     m.put ("recurse", b1);
315     b1.setMap1 (m);
316       }
317       ret.setAttribute ("bean1a", b1);
318
319       Bean1 b2 = new Bean1 ();
320       b2.setInt2 (new Integer JavaDoc (-224));
321       b2.setString2 ("bean2's string");
322       b1.setBean1 (b2);
323
324       Bean1 b3 = new Bean1 ();
325       b3.setDouble1 (1422.332);
326       b3.setString2 ("bean3's string");
327       b2.setBean2 (b3);
328     }
329
330     // Create the public/private beans
331
{
332       ret.setAttribute ("pbean1", Factory.createBean1 ());
333       ret.setAttribute ("pbean2", Factory.createBean2 ());
334       ret.setAttribute ("pbean3", Factory.createBean3 ());
335       ret.setAttribute ("pbean4", Factory.createBean4 ());
336       ret.setAttribute ("pbean5", Factory.createBean5 ());
337       ret.setAttribute ("pbean6", Factory.createBean6 ());
338       ret.setAttribute ("pbean7", Factory.createBean7 ());
339     }
340
341     // Create the empty tests
342
{
343       Map JavaDoc m = new HashMap JavaDoc ();
344       m.put ("emptyArray", new Object JavaDoc [0]);
345       m.put ("nonemptyArray", new Object JavaDoc [] {"abc"});
346       m.put ("emptyList", new ArrayList JavaDoc ());
347       {
348     List JavaDoc l = new ArrayList JavaDoc ();
349     l.add ("hello");
350     m.put ("nonemptyList", l);
351       }
352       m.put ("emptyMap", new HashMap JavaDoc ());
353       {
354     Map JavaDoc m2 = new HashMap JavaDoc ();
355     m2.put ("a", "a");
356     m.put ("nonemptyMap", m2);
357       }
358       m.put ("emptySet", new HashSet JavaDoc ());
359       {
360     Set JavaDoc s = new HashSet JavaDoc ();
361     s.add ("hello");
362     m.put ("nonemptySet", s);
363       }
364       ret.setAttribute ("emptyTests", m);
365     }
366
367     return ret;
368   }
369
370   //-------------------------------------
371
// Main method
372
//-------------------------------------
373
/**
374    *
375    * Runs the evaluation test
376    **/

377   public static void main (String JavaDoc [] pArgs)
378     throws IOException JavaDoc
379   {
380     if (pArgs.length != 2 &&
381     pArgs.length != 3) {
382       usage ();
383       System.exit (1);
384     }
385
386     File JavaDoc in = new File JavaDoc (pArgs [0]);
387     File JavaDoc out = new File JavaDoc (pArgs [1]);
388
389     runTests (in, out);
390
391     if (pArgs.length > 2) {
392       File JavaDoc compare = new File JavaDoc (pArgs [2]);
393       if (isDifferentFiles (out, compare)) {
394     System.out.println ("Test failure - output file " +
395                 out +
396                 " differs from expected output file " +
397                 compare);
398       }
399       else {
400     System.out.println ("tests passed");
401       }
402     }
403   }
404
405   //-------------------------------------
406
static void usage ()
407   {
408     System.err.println ("usage: java org.apache.taglibs.standard.lang.jstl.test.EvaluationTest {input file} {output file} [{compare file}]");
409   }
410
411   //-------------------------------------
412

413 }
414
Popular Tags