You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AntClassLoaderTest.java 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. */
  18. package org.apache.tools.ant;
  19. import java.io.File;
  20. import java.io.PrintStream;
  21. import java.net.URL;
  22. import org.apache.tools.ant.types.Path;
  23. import org.apache.tools.ant.util.FileUtils;
  24. /**
  25. * Test case for ant class loader
  26. *
  27. */
  28. public class AntClassLoaderTest extends BuildFileTest {
  29. private Project p;
  30. private AntClassLoader loader;
  31. public AntClassLoaderTest(String name) {
  32. super(name);
  33. }
  34. public void setUp() {
  35. p = new Project();
  36. p.init();
  37. configureProject("src/etc/testcases/core/antclassloader.xml");
  38. getProject().executeTarget("setup");
  39. }
  40. public void tearDown() {
  41. if (loader != null) {
  42. loader.cleanup();
  43. }
  44. getProject().executeTarget("cleanup");
  45. }
  46. //test inspired by bug report 37085
  47. public void testJarWithManifestInDirWithSpace() {
  48. String mainjarstring = getProject().getProperty("main.jar");
  49. String extjarstring = getProject().getProperty("ext.jar");
  50. Path myPath = new Path(getProject());
  51. myPath.setLocation(new File(mainjarstring));
  52. getProject().setUserProperty("build.sysclasspath","ignore");
  53. loader = getProject().createClassLoader(myPath);
  54. String path = loader.getClasspath();
  55. assertEquals(mainjarstring + File.pathSeparator + extjarstring, path);
  56. }
  57. public void testJarWithManifestInNonAsciiDir() {
  58. String mainjarstring = getProject().getProperty("main.jar.nonascii");
  59. String extjarstring = getProject().getProperty("ext.jar.nonascii");
  60. Path myPath = new Path(getProject());
  61. myPath.setLocation(new File(mainjarstring));
  62. getProject().setUserProperty("build.sysclasspath","ignore");
  63. loader = getProject().createClassLoader(myPath);
  64. String path = loader.getClasspath();
  65. assertEquals(mainjarstring + File.pathSeparator + extjarstring, path);
  66. }
  67. public void testCleanup() throws BuildException {
  68. Path path = new Path(p, ".");
  69. loader = p.createClassLoader(path);
  70. try {
  71. // we don't expect to find this
  72. loader.findClass("fubar");
  73. fail("Did not expect to find fubar class");
  74. } catch (ClassNotFoundException e) {
  75. // ignore expected
  76. }
  77. loader.cleanup();
  78. try {
  79. // we don't expect to find this
  80. loader.findClass("fubar");
  81. fail("Did not expect to find fubar class");
  82. } catch (ClassNotFoundException e) {
  83. // ignore expected
  84. } catch (NullPointerException e) {
  85. fail("loader should not fail even if cleaned up");
  86. }
  87. // tell the build it is finished
  88. p.fireBuildFinished(null);
  89. try {
  90. // we don't expect to find this
  91. loader.findClass("fubar");
  92. fail("Did not expect to find fubar class");
  93. } catch (ClassNotFoundException e) {
  94. // ignore expected
  95. } catch (NullPointerException e) {
  96. fail("loader should not fail even if project finished");
  97. }
  98. }
  99. public void testGetPackage() throws Exception {
  100. executeTarget("prepareGetPackageTest");
  101. Path myPath = new Path(getProject());
  102. myPath.setLocation(new File(getProject().getProperty("test.jar")));
  103. getProject().setUserProperty("build.sysclasspath","ignore");
  104. loader = getProject().createClassLoader(myPath);
  105. assertNotNull("should find class", loader.findClass("org.example.Foo"));
  106. assertNotNull("should find package",
  107. new GetPackageWrapper(loader).getPackage("org.example"));
  108. }
  109. public void testCodeSource() throws Exception {
  110. executeTarget("prepareGetPackageTest");
  111. Path myPath = new Path(getProject());
  112. File testJar = new File(getProject().getProperty("test.jar"));
  113. myPath.setLocation(testJar);
  114. getProject().setUserProperty("build.sysclasspath","ignore");
  115. loader = getProject().createClassLoader(myPath);
  116. Class foo = loader.findClass("org.example.Foo");
  117. URL codeSourceLocation =
  118. foo.getProtectionDomain().getCodeSource().getLocation();
  119. assertEquals(codeSourceLocation + " should point to test.jar",
  120. FileUtils.getFileUtils().getFileURL(testJar), codeSourceLocation);
  121. }
  122. public void testSignedJar() throws Exception {
  123. executeTarget("signTestJar");
  124. File jar = new File(getProject().getProperty("test.jar"));
  125. Path myPath = new Path(getProject());
  126. myPath.setLocation(jar);
  127. getProject().setUserProperty("build.sysclasspath","ignore");
  128. loader = getProject().createClassLoader(myPath);
  129. Class foo = loader.findClass("org.example.Foo");
  130. assertNotNull("should find class", foo);
  131. assertNotNull("should have certificates",
  132. foo.getProtectionDomain().getCodeSource()
  133. .getCertificates());
  134. assertNotNull("should be signed", foo.getSigners());
  135. }
  136. /**
  137. * @see https://issues.apache.org/bugzilla/show_bug.cgi?id=47593
  138. */
  139. public void testInvalidZipException() throws Exception {
  140. executeTarget("createNonJar");
  141. File jar = new File(getProject().getProperty("tmp.dir")
  142. + "/foo.jar");
  143. Path myPath = new Path(getProject());
  144. myPath.setLocation(jar);
  145. getProject().setUserProperty("build.sysclasspath","ignore");
  146. loader = getProject().createClassLoader(myPath);
  147. PrintStream sysErr = System.err;
  148. try {
  149. StringBuffer errBuffer = new StringBuffer();
  150. PrintStream err =
  151. new PrintStream(new BuildFileTest.AntOutputStream(errBuffer));
  152. System.setErr(err);
  153. loader.getResource("foo.txt");
  154. String log = getLog();
  155. int startMessage = log.indexOf("Unable to obtain resource from ");
  156. assertTrue(startMessage >= 0);
  157. assertTrue(log.indexOf("foo.jar", startMessage) > 0);
  158. log = errBuffer.toString();
  159. startMessage = log.indexOf("Unable to obtain resource from ");
  160. assertTrue(startMessage >= 0);
  161. assertTrue(log.indexOf("foo.jar", startMessage) > 0);
  162. } finally {
  163. System.setErr(sysErr);
  164. }
  165. }
  166. private static class GetPackageWrapper extends ClassLoader {
  167. GetPackageWrapper(ClassLoader parent) {
  168. super(parent);
  169. }
  170. public Package getPackage(String s) {
  171. return super.getPackage(s);
  172. }
  173. }
  174. }