KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 import javax.servlet.jsp.JspException JavaDoc;
30
31 import org.apache.taglibs.standard.lang.jstl.Evaluator;
32
33 /**
34  *
35  * <p>This runs a series of tests specifically for the parser. It
36  * parses various expressions and prints out the canonical
37  * representation of those parsed expressions.
38  *
39  * <p>The expressions are stored in an input text file, with one line
40  * per expression. Blank lines and lines that start with # are
41  * ignored. The results are written to an output file (blank lines
42  * and # lines are included in the output file). The output file may
43  * be compared against an existing output file to do regression
44  * testing.
45  *
46  * @author Nathan Abramson - Art Technology Group
47  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: pierred $
48  **/

49
50 public class ParserTest
51 {
52   //-------------------------------------
53
// Properties
54
//-------------------------------------
55

56   //-------------------------------------
57
// Member variables
58
//-------------------------------------
59

60   //-------------------------------------
61
/**
62    *
63    * Constructor
64    **/

65   public ParserTest ()
66   {
67   }
68
69   //-------------------------------------
70
/**
71    *
72    * Runs the tests, reading expressions from pIn and writing the
73    * results to pOut.
74    **/

75   public static void runTests (DataInput JavaDoc pIn,
76                    PrintStream JavaDoc pOut)
77     throws IOException JavaDoc
78   {
79     while (true) {
80       String JavaDoc str = pIn.readLine ();
81       if (str == null) break;
82       if (str.startsWith ("#") ||
83       "".equals (str.trim ())) {
84     pOut.println (str);
85       }
86       else {
87     // For testing non-ASCII values, the string @@non-ascii gets
88
// converted internally to '\u1111'
89
if ("@@non-ascii".equals (str)) {
90       str = "\u1111";
91     }
92
93     pOut.println ("Attribute value: " + str);
94     try {
95       String JavaDoc result = Evaluator.parseAndRender (str);
96       pOut.println ("Parses to: " + result);
97     }
98     catch (JspException JavaDoc exc) {
99       pOut.println ("Causes an error: " + exc.getMessage ());
100     }
101       }
102     }
103
104   }
105
106   //-------------------------------------
107
/**
108    *
109    * Runs the tests, reading from the given input file and writing to
110    * the given output file.
111    **/

112   public static void runTests (File JavaDoc pInputFile,
113                    File JavaDoc pOutputFile)
114     throws IOException JavaDoc
115   {
116     FileInputStream JavaDoc fin = null;
117     FileOutputStream JavaDoc fout = null;
118     try {
119       fin = new FileInputStream JavaDoc (pInputFile);
120       BufferedInputStream JavaDoc bin = new BufferedInputStream JavaDoc (fin);
121       DataInputStream JavaDoc din = new DataInputStream JavaDoc (bin);
122
123       try {
124     fout = new FileOutputStream JavaDoc (pOutputFile);
125     BufferedOutputStream JavaDoc bout = new BufferedOutputStream JavaDoc (fout);
126     PrintStream JavaDoc pout = new PrintStream JavaDoc (bout);
127
128     runTests (din, pout);
129
130     pout.flush ();
131       }
132       finally {
133     if (fout != null) {
134       fout.close ();
135     }
136       }
137     }
138     finally {
139       if (fin != null) {
140     fin.close ();
141       }
142     }
143   }
144
145   //-------------------------------------
146
/**
147    *
148    * Performs a line-by-line comparison of the two files, returning
149    * true if the files are different, false if not.
150    **/

151   public static boolean isDifferentFiles (DataInput JavaDoc pIn1,
152                       DataInput JavaDoc pIn2)
153     throws IOException JavaDoc
154   {
155     while (true) {
156       String JavaDoc str1 = pIn1.readLine ();
157       String JavaDoc str2 = pIn2.readLine ();
158       if (str1 == null &&
159       str2 == null) {
160     return false;
161       }
162       else if (str1 == null ||
163            str2 == null) {
164     return true;
165       }
166       else {
167     if (!str1.equals (str2)) {
168       return true;
169     }
170       }
171     }
172   }
173
174   //-------------------------------------
175
/**
176    *
177    * Performs a line-by-line comparison of the two files, returning
178    * true if the files are different, false if not.
179    **/

180   public static boolean isDifferentFiles (File JavaDoc pFile1,
181                       File JavaDoc pFile2)
182     throws IOException JavaDoc
183   {
184     FileInputStream JavaDoc fin1 = null;
185     try {
186       fin1 = new FileInputStream JavaDoc (pFile1);
187       BufferedInputStream JavaDoc bin1 = new BufferedInputStream JavaDoc (fin1);
188       DataInputStream JavaDoc din1 = new DataInputStream JavaDoc (bin1);
189
190       FileInputStream JavaDoc fin2 = null;
191       try {
192     fin2 = new FileInputStream JavaDoc (pFile2);
193     BufferedInputStream JavaDoc bin2 = new BufferedInputStream JavaDoc (fin2);
194     DataInputStream JavaDoc din2 = new DataInputStream JavaDoc (bin2);
195
196     return isDifferentFiles (din1, din2);
197       }
198       finally {
199     if (fin2 != null) {
200       fin2.close ();
201     }
202       }
203     }
204     finally {
205       if (fin1 != null) {
206     fin1.close ();
207       }
208     }
209   }
210
211   //-------------------------------------
212
// Main method
213
//-------------------------------------
214
/**
215    *
216    * Runs the parser test
217    **/

218   public static void main (String JavaDoc [] pArgs)
219     throws IOException JavaDoc
220   {
221     if (pArgs.length != 2 &&
222     pArgs.length != 3) {
223       usage ();
224       System.exit (1);
225     }
226
227     File JavaDoc in = new File JavaDoc (pArgs [0]);
228     File JavaDoc out = new File JavaDoc (pArgs [1]);
229
230     runTests (in, out);
231
232     if (pArgs.length > 2) {
233       File JavaDoc compare = new File JavaDoc (pArgs [2]);
234       if (isDifferentFiles (out, compare)) {
235     System.out.println ("Test failure - output file " +
236                 out +
237                 " differs from expected output file " +
238                 compare);
239       }
240       else {
241     System.out.println ("tests passed");
242       }
243     }
244   }
245
246   //-------------------------------------
247
static void usage ()
248   {
249     System.err.println ("usage: java org.apache.taglibs.standard.lang.jstl.test.ParserTest {input file} {output file} [{compare file}]");
250   }
251
252   //-------------------------------------
253

254 }
255
Popular Tags