Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/tripi/asp/test/AspTest.java


1   /**
2    * ArrowHead ASP Server 
3    * This is a source file for the ArrowHead ASP Server - an 100% Java
4    * VBScript interpreter and ASP server.
5    *
6    * For more information, see http://www.tripi.com/arrowhead
7    *
8    * Copyright (C) 2002  Terence Haddock
9    *
10   * This program is free software; you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as published by
12   * the Free Software Foundation; either version 2 of the License, or
13   * (at your option) any later version.
14   *
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   *
20   * You should have received a copy of the GNU General Public License
21   * along with this program; if not, write to the Free Software
22   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23   *
24   */
25  package com.tripi.asp.test;
26  
27  import java.io.BufferedReader;
28  import java.io.FileInputStream;
29  import java.io.FileNotFoundException;
30  import java.io.FileOutputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.InputStreamReader;
34  import java.io.OutputStream;
35  import java.net.MalformedURLException;
36  import java.net.URL;
37  import java.net.URLConnection;
38  
39  import junit.framework.Test;
40  import junit.framework.TestSuite;
41  
42  import org.apache.log4j.Category;
43  import org.apache.log4j.PropertyConfigurator;
44  
45  import com.tripi.asp.AspContext;
46  import com.tripi.asp.AspException;
47  import com.tripi.asp.IdentNode;
48  import com.tripi.asp.JavaObjectNode;
49  import com.tripi.asp.Response;
50  
51  /**
52   * AspTest executes an ASP script directly like a command script.
53   *
54   * Usage: java com.tripi.asp.AspTest &lt;filename&gt;<br>
55   * Where <i>filename</i> is the name of the file.
56   *
57   * @author Terence Haddock
58   * @version 0.9
59   */
60  public class AspTest
61  {
62      /** Debugging category */
63      static final Category DBG = Category.getInstance(AspTest.class);
64  
65      /** Log4j file to use */
66      static String  param_log4j = "log4j.configure";
67  
68      /**
69       * Displays the help information file.
70       */
71      private static void displayHelp()
72      {
73          System.err.println("Usage: java com.tripi.asp.AspTest [options] <input_file>");
74          System.err.println("Options available (options can be abbreviated):");
75          System.err.println("  -log4j <file>      log4j configuration file to use");
76          System.err.println("  -help              Show this help");
77      }
78  
79      /**
80       * Parameter parser.
81       * @param args Runtime arguments
82       */
83      private static void parseParameters(String args[])
84      {
85          int i;
86          for (i = 0; i < args.length; i++)
87          {
88              if (args[i].startsWith("-"))
89              {
90                  if (args[i].startsWith("-l")) {
91                      i++;
92                      if (i >= args.length) {
93                          System.err.println("Expected argument for -log4j");
94                          displayHelp();
95                          System.exit(1);
96                      }
97                      param_log4j = args[i];
98                  } else if (args[i].startsWith("-h")) {
99                      displayHelp();
100                     System.exit(1);
101                 } else {
102                     System.err.println("Unexpected argument: " + args[i]);
103                     displayHelp();
104                     System.exit(1);
105                 }
106             } else {
107                 System.err.println("Unexpected argument: " + args[i]);
108                 displayHelp();
109                 System.exit(1);
110             }
111         }
112     }
113 
114     /**
115      * Entry point.
116      * 
117      * @param args Arguments, see above
118      */
119     public static void main(String args[])
120     {
121         parseParameters(args);
122 
123         PropertyConfigurator.configure(param_log4j);
124 
125         junit.textui.TestRunner.run (suite());
126     }
127 
128     /**
129      * Test suite
130      */
131     public static Test suite()
132     {
133         param_log4j = System.getProperty("log4j.configure", "log4j.configure");
134         PropertyConfigurator.configure(param_log4j);
135 
136         TestSuite suite = new TestSuite("All Test");
137         suite.addTest(new TestSuite(AspCollectionTest.class));
138         suite.addTest(new TestSuite(ParseQueryStringTest.class));
139         suite.addTest(FunctionsTest.suite());
140         suite.addTest(ObjectsTest.suite());
141         suite.addTest(StatementsTest.suite());
142         suite.addTest(OperatorsTest.suite());
143         suite.addTest(MiscTest.suite());
144         suite.addTest(ADODBTest.suite());
145         return suite;
146     }
147 
148     /**
149      * This function returns the ASP Response object, for testing.
150      * @param context AspContext to search
151      * @return Internal HTTP Response object
152      */
153     public static Response getResponse(AspContext context) throws AspException
154     {
155         JavaObjectNode objNode = (JavaObjectNode)getValue(context, "response");
156         return (Response)objNode.getSubObject();
157     }
158 
159     /**
160      * This function is a utility function to obtain the value of a 
161      * variable in a context.
162      * @param context AspContext to find variable in
163      * @param name Name of variable
164      */
165     public static Object getValue(AspContext context, String name) throws AspException
166     {
167         return context.getValue(new IdentNode(name));
168     }
169 
170     /**
171      * Obtains the connection for the given relative URL.
172      * @param location relative location for the URL
173      * @return new URLConnection
174      */
175     public static URLConnection getConnection(String location)
176         throws MalformedURLException, IOException
177     {
178         String testServer = System.getProperty("asptest.server", "localhost:8080");
179         URL url = new URL("http://" + testServer + "/asptest/" + location);
180         return url.openConnection();
181     }
182 
183     /**
184      * Saves the text of an output test case.
185      * @param text Text to save
186      * @param file filename to save to
187      */
188     public static void save(String content, String filename) throws IOException
189     {
190         OutputStream output = new FileOutputStream(filename);
191         output.write(content.getBytes());
192         output.close();
193     }
194 
195     /**
196      * Compares the text to the give template.
197      * @param text Text to compare
198      * @param filename Filename of template to compare to
199      */
200     public static boolean compare(String content, String filename)
201         throws IOException
202     {
203         InputStream is = new FileInputStream(filename);
204         String temp = getContent(is);
205         is.close();
206 
207         TestComparator cmp = new TestComparator(temp);
208         return cmp.matches(content);
209     }
210 
211     /**
212      * Performs a simple output test.
213      * @param directory Sub-directory where tests reside (relative to test
214      * directory)
215      * @param name Test name
216      */
217     public static boolean doSimpleTest(String directory, String name)
218         throws MalformedURLException, IOException
219     {
220         InputStream is;
221         try {
222             URLConnection con = getConnection(directory + "/" + name + ".asp");
223             is = con.getInputStream();
224         } catch (FileNotFoundException ex) {
225             return false;
226         } catch (IOException ex) {
227             return false;
228         }
229         String content = getContent(is);
230         is.close();
231         save(content, directory + "/out/" + name + ".out");
232         return compare(content, directory + "/expect/" + name + ".out");
233     }
234 
235     /**
236      * Performs a simple output test, which will be a error.
237      * @param directory Sub-directory where tests reside (relative to test
238      * directory)
239      * @param name Test name
240      * @param code Error code expected
241      */
242     public static boolean doSimpleFailureTest(String directory, String name)
243         throws MalformedURLException, IOException
244     {
245         return !doSimpleTest(directory, name);
246     }
247 
248     /**
249      * Gets the full content of the data read in from an input stream.
250      * @param is Input Stream to read.
251      * @return String as full content of input stream
252      */
253     public static String getContent(InputStream is) throws IOException
254     {
255         BufferedReader br = new BufferedReader(new InputStreamReader(is));
256         String line;
257         StringBuffer ret = new StringBuffer();
258         while ((line = br.readLine()) != null)
259         {
260             ret.append(line);
261             ret.append("\n");
262         }
263         return ret.toString();
264     }
265 
266     /**
267      * Gets the full content of this URL connection.
268      * @param con connection
269      * @return String as full content of URL connection.
270      */
271     public static String getContent(URLConnection con) throws IOException
272     {
273         InputStream is = con.getInputStream();
274         String content = getContent(is);
275         is.close();
276         return content;
277     }
278 
279     /**
280      * Writes the full content to the URL connection.
281      * @param con connection
282      * @param str String to write
283      */
284     public static void writeContent(URLConnection con, String str)
285         throws IOException
286     {
287         OutputStream os = con.getOutputStream();
288         os.write(str.getBytes());
289         os.close();
290     }
291 
292     /**
293      * Gets a cookie by name.
294      * @param con Connection
295      * @param name Cookie name
296      * @return the entire Set-Cookie header corresponding to the cookie
297      */
298     public static String getCookie(URLConnection con, String name)
299         throws IOException
300     {
301         int x = 1;
302         while (con.getHeaderFieldKey(x) != null)
303         {
304             if (con.getHeaderFieldKey(x).equals("Set-Cookie")) {
305                 String value = con.getHeaderField(x);
306                 if (value.startsWith(name + "=")) {
307                     return value;
308                 }
309             }
310             x++;
311         }
312         return null;
313     }
314 }
315