Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RunTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.tools.anttasks;
  19. import java.io.File;
  20. import java.lang.reflect.InvocationTargetException;
  21. import java.lang.reflect.Method;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.net.URLClassLoader;
  25. import java.security.AccessController;
  26. import java.security.PrivilegedAction;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import org.apache.tools.ant.BuildException;
  32. import org.apache.tools.ant.Task;
  33. /**
  34. * Testing ant task.
  35. * This task is used to test FOP as a build target.
  36. * This uses the TestConverter (with weak code dependency)
  37. * to run the tests and check the results.
  38. */
  39. public class RunTest extends Task {
  40. private String basedir;
  41. private String testsuite = "";
  42. private String referenceJar = "";
  43. private String refVersion = "";
  44. /**
  45. * Sets the test suite name.
  46. * @param str name of the test suite
  47. */
  48. public void setTestSuite(String str) {
  49. testsuite = str;
  50. }
  51. /**
  52. * Sets the base directory.
  53. * @param str base directory
  54. */
  55. public void setBasedir(String str) {
  56. basedir = str;
  57. }
  58. /**
  59. * Sets the reference directory.
  60. * @param str reference directory
  61. */
  62. public void setReference(String str) {
  63. referenceJar = str;
  64. }
  65. /**
  66. * Sets the reference version.
  67. * @param str reference version
  68. */
  69. public void setRefVersion(String str) {
  70. refVersion = str;
  71. }
  72. /**
  73. * This creates the reference output, if required, then tests
  74. * the current build.
  75. * {@inheritDoc}
  76. */
  77. public void execute() throws BuildException {
  78. runReference();
  79. testNewBuild();
  80. }
  81. /**
  82. * Test the current build.
  83. * This uses the current jar file (in build/fop.jar) to run the
  84. * tests with.
  85. * The output is then compared with the reference output.
  86. */
  87. protected void testNewBuild() {
  88. try {
  89. ClassLoader loader = new URLClassLoader(
  90. createUrls("build/fop.jar"));
  91. Map diff = runConverter(loader, "areatree",
  92. "reference/output/");
  93. if (diff != null && !diff.isEmpty()) {
  94. System.out.println("====================================");
  95. System.out.println("The following files differ:");
  96. boolean broke = false;
  97. for (Map.Entry<Object, Object> e : (Set<Map.Entry<Object, Object>>) diff.entrySet()) {
  98. Object fname = e.getKey();
  99. Boolean pass = (Boolean)e.getValue();
  100. System.out.println("file: " + fname
  101. + " - reference success: " + pass);
  102. if (pass) {
  103. broke = true;
  104. }
  105. }
  106. if (broke) {
  107. throw new BuildException("Working tests have been changed.");
  108. }
  109. }
  110. } catch (MalformedURLException mue) {
  111. mue.printStackTrace();
  112. }
  113. }
  114. /**
  115. * Run the tests for the reference jar file.
  116. * This checks that the reference output has not already been
  117. * run and then checks the version of the reference jar against
  118. * the version required.
  119. * The reference output is then created.
  120. * @throws BuildException if an error occurs
  121. */
  122. protected void runReference() throws BuildException {
  123. // check not already done
  124. File f = new File(basedir + "/reference/output/");
  125. // if(f.exists()) {
  126. // need to check that files have actually been created.
  127. // return;
  128. // } else {
  129. try {
  130. final URL[] urls = createUrls(referenceJar);
  131. ClassLoader loader = (ClassLoader)
  132. AccessController.doPrivileged(new PrivilegedAction() {
  133. public Object run() {
  134. return new URLClassLoader(urls);
  135. }
  136. });
  137. boolean failed = false;
  138. try {
  139. Class cla = Class.forName("org.apache.fop.apps.Fop", true,
  140. loader);
  141. Method get = cla.getMethod("getVersion", new Class[]{});
  142. if (!get.invoke(null, new Object[]{}).equals(refVersion)) {
  143. throw new BuildException("Reference jar is not correct version it must be: "
  144. + refVersion);
  145. }
  146. } catch (IllegalAccessException iae) {
  147. failed = true;
  148. } catch (IllegalArgumentException are) {
  149. failed = true;
  150. } catch (InvocationTargetException are) {
  151. failed = true;
  152. } catch (ClassNotFoundException are) {
  153. failed = true;
  154. } catch (NoSuchMethodException are) {
  155. failed = true;
  156. }
  157. if (failed) {
  158. throw new BuildException("Reference jar could not be found in: "
  159. + basedir + "/reference/");
  160. }
  161. f.mkdirs();
  162. runConverter(loader, "reference/output/", null);
  163. } catch (MalformedURLException mue) {
  164. mue.printStackTrace();
  165. }
  166. // }
  167. }
  168. /**
  169. * Run the Converter.
  170. * Runs the test converter using the specified class loader.
  171. * This loads the TestConverter using the class loader and
  172. * then runs the test suite for the current test suite
  173. * file in the base directory.
  174. * (Note class loader option provided to allow for different
  175. * fop.jar and other libraries to be activated.)
  176. * @param loader the class loader to use to run the tests with
  177. * @param dest destination directory
  178. * @param compDir comparison directory
  179. * @return A Map with differences
  180. */
  181. protected Map runConverter(ClassLoader loader, String dest,
  182. String compDir) {
  183. String converter = "org.apache.fop.tools.TestConverter";
  184. Map diff = null;
  185. try {
  186. Class cla = Class.forName(converter, true, loader);
  187. Object tc = cla.newInstance();
  188. Method meth;
  189. meth = cla.getMethod("setBaseDir", new Class[] {
  190. String.class
  191. });
  192. meth.invoke(tc, new Object[] {
  193. basedir
  194. });
  195. meth = cla.getMethod("runTests", new Class[] {
  196. String.class, String.class, String.class
  197. });
  198. diff = (Map)meth.invoke(tc, new Object[] {
  199. testsuite, dest, compDir
  200. });
  201. } catch (Exception e) {
  202. e.printStackTrace();
  203. }
  204. return diff;
  205. }
  206. /**
  207. * Return a list of URL's with the specified URL first and followed
  208. * by all the jar files from lib/.
  209. * @return a list of urls to the runtime jar files.
  210. */
  211. private URL[] createUrls(String mainJar) throws MalformedURLException {
  212. List<URL> urls = new ArrayList<URL>();
  213. urls.add(new File(mainJar).toURI().toURL());
  214. File[] libFiles = new File("lib").listFiles();
  215. if (libFiles != null) {
  216. for (File libFile : libFiles) {
  217. if (libFile.getPath().endsWith(".jar")) {
  218. urls.add(libFile.toURI().toURL());
  219. }
  220. }
  221. }
  222. return urls.toArray(new URL[urls.size()]);
  223. }
  224. }