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 9.8 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertFalse;
  21. import static org.junit.Assert.assertNotNull;
  22. import static org.junit.Assert.assertNull;
  23. import static org.junit.Assert.assertTrue;
  24. import static org.junit.Assert.fail;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.io.PrintStream;
  28. import java.net.URL;
  29. import java.util.Enumeration;
  30. import org.apache.tools.ant.types.Path;
  31. import org.apache.tools.ant.util.FileUtils;
  32. import org.apache.tools.ant.util.CollectionUtils;
  33. import org.junit.After;
  34. import org.junit.Before;
  35. import org.junit.Rule;
  36. import org.junit.Test;
  37. /**
  38. * Test case for ant class loader
  39. *
  40. */
  41. public class AntClassLoaderTest {
  42. @Rule
  43. public BuildFileRule buildRule = new BuildFileRule();
  44. private AntClassLoader loader;
  45. @Before
  46. public void setUp() {
  47. buildRule.configureProject("src/etc/testcases/core/antclassloader.xml");
  48. buildRule.executeTarget("setUp");
  49. }
  50. @After
  51. public void tearDown() {
  52. if (loader != null) {
  53. loader.cleanup();
  54. }
  55. }
  56. //test inspired by bug report 37085
  57. @Test
  58. public void testJarWithManifestInDirWithSpace() {
  59. String mainjarstring = buildRule.getProject().getProperty("main.jar");
  60. String extjarstring = buildRule.getProject().getProperty("ext.jar");
  61. Path myPath = new Path(buildRule.getProject());
  62. myPath.setLocation(new File(mainjarstring));
  63. buildRule.getProject().setUserProperty("build.sysclasspath","ignore");
  64. loader = buildRule.getProject().createClassLoader(myPath);
  65. String path = loader.getClasspath();
  66. assertEquals(mainjarstring + File.pathSeparator + extjarstring, path);
  67. }
  68. @Test
  69. public void testJarWithManifestInNonAsciiDir() {
  70. String mainjarstring = buildRule.getProject().getProperty("main.jar.nonascii");
  71. String extjarstring = buildRule.getProject().getProperty("ext.jar.nonascii");
  72. Path myPath = new Path(buildRule.getProject());
  73. myPath.setLocation(new File(mainjarstring));
  74. buildRule.getProject().setUserProperty("build.sysclasspath","ignore");
  75. loader = buildRule.getProject().createClassLoader(myPath);
  76. String path = loader.getClasspath();
  77. assertEquals(mainjarstring + File.pathSeparator + extjarstring, path);
  78. }
  79. @Test
  80. public void testCleanup() throws BuildException {
  81. Path path = new Path(buildRule.getProject(), ".");
  82. loader = buildRule.getProject().createClassLoader(path);
  83. try {
  84. // we don't expect to find this
  85. loader.findClass("fubar");
  86. fail("Did not expect to find fubar class");
  87. } catch (ClassNotFoundException e) {
  88. // ignore expected
  89. }
  90. loader.cleanup();
  91. try {
  92. // we don't expect to find this
  93. loader.findClass("fubar");
  94. fail("Did not expect to find fubar class");
  95. } catch (ClassNotFoundException e) {
  96. // ignore expected
  97. } catch (NullPointerException e) {
  98. fail("loader should not fail even if cleaned up");
  99. }
  100. // tell the build it is finished
  101. buildRule.getProject().fireBuildFinished(null);
  102. try {
  103. // we don't expect to find this
  104. loader.findClass("fubar");
  105. fail("Did not expect to find fubar class");
  106. } catch (ClassNotFoundException e) {
  107. // ignore expected
  108. } catch (NullPointerException e) {
  109. fail("loader should not fail even if project finished");
  110. }
  111. }
  112. @Test
  113. public void testGetPackage() throws Exception {
  114. buildRule.executeTarget("prepareGetPackageTest");
  115. Path myPath = new Path(buildRule.getProject());
  116. myPath.setLocation(new File(buildRule.getProject().getProperty("test.jar")));
  117. buildRule.getProject().setUserProperty("build.sysclasspath","ignore");
  118. loader = buildRule.getProject().createClassLoader(myPath);
  119. assertNotNull("should find class", loader.findClass("org.example.Foo"));
  120. assertNotNull("should find package",
  121. new GetPackageWrapper(loader).getPackage("org.example"));
  122. }
  123. @Test
  124. public void testCodeSource() throws Exception {
  125. buildRule.executeTarget("prepareGetPackageTest");
  126. Path myPath = new Path(buildRule.getProject());
  127. File testJar = new File(buildRule.getProject().getProperty("test.jar"));
  128. myPath.setLocation(testJar);
  129. buildRule.getProject().setUserProperty("build.sysclasspath","ignore");
  130. loader = buildRule.getProject().createClassLoader(myPath);
  131. Class<?> foo = loader.findClass("org.example.Foo");
  132. URL codeSourceLocation =
  133. foo.getProtectionDomain().getCodeSource().getLocation();
  134. assertEquals(codeSourceLocation + " should point to test.jar",
  135. FileUtils.getFileUtils().getFileURL(testJar), codeSourceLocation);
  136. }
  137. @Test
  138. public void testSignedJar() throws Exception {
  139. buildRule.executeTarget("signTestJar");
  140. File jar = new File(buildRule.getProject().getProperty("test.jar"));
  141. Path myPath = new Path(buildRule.getProject());
  142. myPath.setLocation(jar);
  143. buildRule.getProject().setUserProperty("build.sysclasspath","ignore");
  144. loader = buildRule.getProject().createClassLoader(myPath);
  145. Class<?> foo = loader.findClass("org.example.Foo");
  146. assertNotNull("should find class", foo);
  147. assertNotNull("should have certificates",
  148. foo.getProtectionDomain().getCodeSource()
  149. .getCertificates());
  150. assertNotNull("should be signed", foo.getSigners());
  151. }
  152. /**
  153. * @see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=47593">
  154. * bug 47593, request to log the name of corrupt zip files from which
  155. * classes cannot be loaded</a>
  156. */
  157. @Test
  158. public void testInvalidZipException() throws Exception {
  159. buildRule.executeTarget("createNonJar");
  160. File jar = new File(buildRule.getProject().getProperty("tmp.dir")
  161. + "/foo.jar");
  162. Path myPath = new Path(buildRule.getProject());
  163. myPath.setLocation(jar);
  164. buildRule.getProject().setUserProperty("build.sysclasspath","ignore");
  165. loader = buildRule.getProject().createClassLoader(myPath);
  166. PrintStream sysErr = System.err;
  167. try {
  168. StringBuffer errBuffer = new StringBuffer();
  169. PrintStream err =
  170. new PrintStream(new BuildFileRule.AntOutputStream(errBuffer));
  171. System.setErr(err);
  172. loader.getResource("foo.txt");
  173. String log = buildRule.getLog();
  174. int startMessage = log.indexOf("CLASSPATH element ");
  175. assertTrue(startMessage >= 0);
  176. assertTrue(log.indexOf("foo.jar is not a JAR", startMessage) > 0);
  177. } finally {
  178. System.setErr(sysErr);
  179. }
  180. }
  181. /**
  182. * Asserts that getResources won't return resources that cannot be
  183. * seen by AntClassLoader but by ClassLoader.this.parent.
  184. *
  185. * @see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=46752">
  186. * https://issues.apache.org/bugzilla/show_bug.cgi?id=46752</a>
  187. */
  188. @SuppressWarnings("resource")
  189. @Test
  190. public void testGetResources() throws IOException {
  191. AntClassLoader acl = new AntClassLoader(new EmptyLoader(), null,
  192. new Path(null), true);
  193. assertNull(acl.getResource("META-INF/MANIFEST.MF"));
  194. assertFalse(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements());
  195. // double check using system classloader as parent
  196. acl = new AntClassLoader(null, null, new Path(null), true);
  197. assertNotNull(acl.getResource("META-INF/MANIFEST.MF"));
  198. assertTrue(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements());
  199. }
  200. @Test
  201. public void testGetResourcesUsingFactory() throws IOException {
  202. AntClassLoader acl =
  203. AntClassLoader.newAntClassLoader(new EmptyLoader(), null,
  204. new Path(null), true);
  205. assertNull(acl.getResource("META-INF/MANIFEST.MF"));
  206. assertFalse(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements());
  207. }
  208. private static class EmptyLoader extends ClassLoader {
  209. public URL getResource(String n) {
  210. return null;
  211. }
  212. public Enumeration getResources(String n) {
  213. return new CollectionUtils.EmptyEnumeration();
  214. }
  215. }
  216. private static class GetPackageWrapper extends ClassLoader {
  217. GetPackageWrapper(ClassLoader parent) {
  218. super(parent);
  219. }
  220. public Package getPackage(String s) {
  221. return super.getPackage(s);
  222. }
  223. }
  224. }