KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > tools > anttasks > RunTest


1 /*
2  * $Id: RunTest.java,v 1.5.2.3 2003/02/25 15:19:58 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.tools.anttasks;
52
53 // Ant
54
import org.apache.tools.ant.*;
55
56 import java.io.*;
57 import java.lang.reflect.*;
58 import java.net.URLClassLoader JavaDoc;
59 import java.net.URL JavaDoc;
60 import java.net.MalformedURLException JavaDoc;
61 import java.util.*;
62
63 /**
64  * Testing ant task.
65  * This task is used to test FOP as a build target.
66  * This uses the TestConverter (with weak code dependancy) to run the tests
67  * and check the results.
68  */

69 public class RunTest extends Task {
70     String JavaDoc basedir;
71     String JavaDoc testsuite = "";
72     String JavaDoc referenceJar = "";
73     String JavaDoc refVersion = "";
74
75     public RunTest() {}
76
77     public void setTestSuite(String JavaDoc str) {
78         testsuite = str;
79     }
80
81     public void setBasedir(String JavaDoc str) {
82         basedir = str;
83     }
84
85     public void setReference(String JavaDoc str) {
86         referenceJar = str;
87     }
88
89     public void setRefVersion(String JavaDoc str) {
90         refVersion = str;
91     }
92
93     /**
94      * Execute this ant task.
95      * This creates the reference output, if required, then tests
96      * the current build.
97      */

98     public void execute() throws BuildException {
99         runReference();
100         testNewBuild();
101     }
102
103     /**
104      * Test the current build.
105      * This uses the current jar file (in build/fop.jar) to run the
106      * tests with.
107      * The output is then compared with the reference output.
108      */

109     protected void testNewBuild() {
110         try {
111             ClassLoader JavaDoc loader = new URLClassLoader JavaDoc(new URL JavaDoc[] {
112                 new URL JavaDoc("file:build/fop.jar")
113             });
114             HashMap diff = runConverter(loader, "areatree",
115                                           "reference/output/");
116             if (diff != null &&!diff.isEmpty()) {
117                 System.out.println("====================================");
118                 System.out.println("The following files differ:");
119                 boolean broke = false;
120                 for (Iterator keys = diff.keySet().iterator(); keys.hasNext(); ) {
121                     Object JavaDoc fname = keys.next();
122                     Boolean JavaDoc pass = (Boolean JavaDoc)diff.get(fname);
123                     System.out.println("file: " + fname
124                                        + " - reference success: " + pass);
125                     if (pass.booleanValue()) {
126                         broke = true;
127                     }
128                 }
129                 if (broke) {
130                     throw new BuildException("Working tests have been changed.");
131                 }
132             }
133         } catch (MalformedURLException JavaDoc mue) {
134             mue.printStackTrace();
135         }
136     }
137
138     /**
139      * Run the tests for the reference jar file.
140      * This checks that the reference output has not already been
141      * run and then checks the version of the reference jar against
142      * the version required.
143      * The reference output is then created.
144      */

145     protected void runReference() throws BuildException {
146         // check not already done
147
File f = new File(basedir + "/reference/output/");
148         // if(f.exists()) {
149
// need to check that files have actually been created.
150
// return;
151
// } else {
152
try {
153             ClassLoader JavaDoc loader = new URLClassLoader JavaDoc(new URL JavaDoc[] {
154                 new URL JavaDoc("file:" + referenceJar)
155             });
156             boolean failed = false;
157
158             try {
159                 Class JavaDoc cla = Class.forName("org.apache.fop.apps.Options",
160                                           true, loader);
161                 Object JavaDoc opts = cla.newInstance();
162                 cla = Class.forName("org.apache.fop.apps.Version", true,
163                                     loader);
164                 Method get = cla.getMethod("getVersion", new Class JavaDoc[]{});
165                 if (!get.invoke(null, new Object JavaDoc[]{}).equals(refVersion)) {
166                     throw new BuildException("Reference jar is not correct version it must be: "
167                                              + refVersion);
168                 }
169             } catch (IllegalAccessException JavaDoc iae) {
170                 failed = true;
171             } catch (IllegalArgumentException JavaDoc are) {
172                 failed = true;
173             } catch (InvocationTargetException are) {
174                 failed = true;
175             } catch (ClassNotFoundException JavaDoc are) {
176                 failed = true;
177             } catch (InstantiationException JavaDoc are) {
178                 failed = true;
179             } catch (NoSuchMethodException JavaDoc are) {
180                 failed = true;
181             }
182             if (failed) {
183                 throw new BuildException("Reference jar could not be found in: "
184                                          + basedir + "/reference/");
185             }
186             f.mkdirs();
187             runConverter(loader, "reference/output/", null);
188         } catch (MalformedURLException JavaDoc mue) {
189             mue.printStackTrace();
190         }
191         // }
192
}
193
194     /**
195      * Run the Converter.
196      * Runs the test converter using the specified class loader.
197      * This loads the TestConverter using the class loader and
198      * then runs the test suite for the current test suite
199      * file in the base directory.
200      * @param loader the class loader to use to run the tests with
201      */

202     protected HashMap runConverter(ClassLoader JavaDoc loader, String JavaDoc dest,
203                                      String JavaDoc compDir) {
204         String JavaDoc converter = "org.apache.fop.tools.TestConverter";
205
206         HashMap diff = null;
207         try {
208             Class JavaDoc cla = Class.forName(converter, true, loader);
209             Object JavaDoc tc = cla.newInstance();
210             Method meth;
211
212             meth = cla.getMethod("setBaseDir", new Class JavaDoc[] {
213                 String JavaDoc.class
214             });
215             meth.invoke(tc, new Object JavaDoc[] {
216                 basedir
217             });
218
219             meth = cla.getMethod("runTests", new Class JavaDoc[] {
220                 String JavaDoc.class, String JavaDoc.class, String JavaDoc.class
221             });
222             diff = (HashMap)meth.invoke(tc, new Object JavaDoc[] {
223                 testsuite, dest, compDir
224             });
225         } catch (Exception JavaDoc e) {
226             e.printStackTrace();
227         }
228         return diff;
229     }
230
231 }
232
Popular Tags