29import java.io.IOException;
30import java.net.JarURLConnection;
31import java.net.MalformedURLException;
32import java.net.URISyntaxException;
34import java.net.URLClassLoader;
35import java.net.URLConnection;
36import java.net.URLStreamHandler;
37import java.util.Enumeration;
38import java.util.jar.JarEntry;
39import java.util.jar.JarFile;
41import org.jau.net.Uri;
42import org.jau.pkg.JarUtil;
43import org.jau.pkg.cache.TempCacheReg;
44import org.jau.pkg.cache.TempFileCache;
45import org.jau.pkg.cache.TempJarCache;
46import org.jau.sys.AndroidVersion;
47import org.jau.sys.RuntimeProps;
48import org.junit.Assert;
49import org.junit.BeforeClass;
50import org.junit.FixMethodOrder;
52import org.junit.runners.MethodSorters;
54import jau.test.junit.util.JunitTracer;
55import jau.test.net.URIDumpUtil;
57@FixMethodOrder(MethodSorters.NAME_ASCENDING)
59 static TempFileCache fileCache;
62 public static void init() {
63 if(AndroidVersion.isAvailable) {
65 setTestSupported(
false);
70 Assert.assertTrue(TempFileCache.initSingleton());
71 Assert.assertTrue(TempCacheReg.isTempFileCacheUsed());
73 fileCache =
new TempFileCache();
74 Assert.assertTrue(fileCache.isValid(
false));
75 System.err.println(
"tmp dir: "+fileCache.getTempDir());
78 static class TestClassLoader
extends URLClassLoader {
79 public TestClassLoader(
final URL[] urls) {
82 public TestClassLoader(
final URL[] urls,
final ClassLoader parent) {
87 void validateJarFile(
final JarFile jarFile)
throws IllegalArgumentException, IOException {
88 Assert.assertNotNull(jarFile);
89 Assert.assertTrue(
"jarFile has zero entries: "+jarFile, jarFile.size()>0);
90 final Enumeration<JarEntry> entries = jarFile.entries();
91 System.err.println(
"Entries of "+jarFile.getName()+
": ");
93 while(entries.hasMoreElements()) {
94 System.err.println(i+
": "+entries.nextElement().getName());
99 void validateJarFileURL(
final Uri jarFileURI)
throws IllegalArgumentException, IOException, URISyntaxException {
100 Assert.assertNotNull(jarFileURI);
101 final URL jarFileURL = jarFileURI.toURL();
102 final URLConnection aURLc = jarFileURL.openConnection();
103 Assert.assertTrue(
"jarFileURI/URL has zero content: "+jarFileURL, aURLc.getContentLength()>0);
104 System.err.println(
"URLConnection: "+aURLc);
105 Assert.assertTrue(
"Not a JarURLConnection: "+aURLc, (aURLc instanceof JarURLConnection) );
106 final JarURLConnection jURLc = (JarURLConnection) aURLc;
107 final JarFile jarFile = jURLc.getJarFile();
108 validateJarFile(jarFile);
111 void validateJarUtil(
final String expJarName,
final String clazzBinName,
final ClassLoader cl)
throws IllegalArgumentException, IOException, URISyntaxException {
112 final Uri.Encoded expJarNameE = Uri.Encoded.cast(expJarName);
113 final Uri.Encoded jarName= JarUtil.getJarBasename(clazzBinName, cl);
114 Assert.assertNotNull(jarName);
115 Assert.assertEquals(expJarNameE, jarName);
117 final Uri jarUri = JarUtil.getJarUri(clazzBinName, cl);
118 Assert.assertNotNull(jarUri);
119 System.err.println(
"1 - jarUri:");
120 URIDumpUtil.showUri(jarUri);
122 final Uri jarSubUri = jarUri.getContainedUri();
123 Assert.assertNotNull(jarSubUri);
124 System.err.println(
"2 - jarSubUri:");
125 URIDumpUtil.showUri(jarSubUri);
127 final URL jarSubURL= jarSubUri.toURL();
128 final URLConnection urlConn = jarSubURL.openConnection();
129 Assert.assertTrue(
"jarSubURL has zero content: "+jarSubURL, urlConn.getContentLength()>0);
130 System.err.println(
"URLConnection of jarSubURL: "+urlConn);
132 final Uri jarFileURL = JarUtil.getJarFileUri(clazzBinName, cl);
133 validateJarFileURL(jarFileURL);
135 final JarFile jarFile = JarUtil.getJarFile(clazzBinName, cl);
136 validateJarFile(jarFile);
140 public void testJarUtilFlat01() throws IOException, IllegalArgumentException, URISyntaxException {
141 System.err.println(
"XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
142 validateJarUtil(
"jaulib-fat.jar",
"org.jau.base.JaulibVersion", this.getClass().getClassLoader());
143 System.err.println(
"XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
147 public void testJarUtilJarInJar01() throws IOException, ClassNotFoundException, IllegalArgumentException, URISyntaxException {
148 System.err.println(
"XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
150 Assert.assertTrue(TempJarCache.initSingleton());
151 Assert.assertTrue(TempCacheReg.isTempJarCacheUsed(
false));
152 Assert.assertTrue(TempJarCache.isInitialized(
false));
154 final ClassLoader rootCL = this.getClass().getClassLoader();
157 TempJarCache.addAll(RuntimeProps.class, JarUtil.getJarFileUri(
"ClassInJar0", rootCL));
160 final URL ClassInJar1_jarFileURL = JarUtil.getJarFileUri(TempJarCache.getResourceUri(
"ClassInJar1.jar")).toURL();
161 final ClassLoader cl =
new URLClassLoader(
new URL[] { ClassInJar1_jarFileURL }, rootCL);
162 Assert.assertNotNull(cl);
163 validateJarUtil(
"ClassInJar1.jar",
"ClassInJar1", cl);
164 System.err.println(
"XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
168 public void testJarUtilJarInJar02() throws IOException, ClassNotFoundException, IllegalArgumentException, URISyntaxException {
169 System.err.println(
"XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
171 Assert.assertTrue(TempJarCache.initSingleton());
172 Assert.assertTrue(TempCacheReg.isTempJarCacheUsed(
false));
173 Assert.assertTrue(TempJarCache.isInitialized(
false));
175 final ClassLoader rootCL = this.getClass().getClassLoader();
178 TempJarCache.addAll(RuntimeProps.class, JarUtil.getJarFileUri(
"ClassInJar0", rootCL));
181 final URL ClassInJar2_jarFileURL = JarUtil.getJarFileUri(TempJarCache.getResourceUri(
"sub/ClassInJar2.jar")).toURL();
182 final ClassLoader cl =
new URLClassLoader(
new URL[] { ClassInJar2_jarFileURL }, rootCL);
183 Assert.assertNotNull(cl);
184 validateJarUtil(
"ClassInJar2.jar",
"ClassInJar2", cl);
185 System.err.println(
"XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
196 public void testJarUtilJarInJar03() throws IOException, ClassNotFoundException, IllegalArgumentException, URISyntaxException {
197 System.err.println(
"XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
199 Assert.assertTrue(TempJarCache.initSingleton());
200 Assert.assertTrue(TempCacheReg.isTempJarCacheUsed(
false));
201 Assert.assertTrue(TempJarCache.isInitialized(
false));
207 class CustomClassLoader
extends ClassLoader {
208 CustomClassLoader() {
209 super(
TestJarUtil.this.getClass().getClassLoader());
214 public URL getResource(
final String name) {
215 final URL url = super.getResource(name);
218 URL urlReturn =
null;
221 urlReturn =
new URL(
"bundleresource",
"4.fwk1990213994", 1, url.getFile(),
222 new URLStreamHandler() {
224 protected URLConnection openConnection(
final URL u)
throws IOException {
228 }
catch(
final MalformedURLException e) {
230 Assert.assertTrue(
false);
239 JarUtil.setResolver(
new JarUtil.Resolver() {
241 public URL resolve(
final URL url ) {
242 if( url.getProtocol().equals(
"bundleresource") ) {
244 return new URL( Uri.JAR_SCHEME,
"", url.getFile() );
245 }
catch(
final MalformedURLException e) {
254 final ClassLoader rootCL =
new CustomClassLoader();
257 TempJarCache.addAll(RuntimeProps.class, JarUtil.getJarFileUri(
"ClassInJar0", rootCL));
260 final URL ClassInJar2_jarFileURL = JarUtil.getJarFileUri(TempJarCache.getResourceUri(
"sub/ClassInJar2.jar")).toURL();
261 final ClassLoader cl =
new URLClassLoader(
new URL[] { ClassInJar2_jarFileURL }, rootCL);
262 Assert.assertNotNull(cl);
263 validateJarUtil(
"ClassInJar2.jar",
"ClassInJar2", cl);
264 System.err.println(
"XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
267 public static void main(
final String args[])
throws IOException {
268 final String tstname =
TestJarUtil.class.getName();
269 org.junit.runner.JUnitCore.
main(tstname);
static void main(final String args[])
void testJarUtilJarInJar02()
void testJarUtilJarInJar03()
Tests JarUtil's ability to resolve non-JAR URLs with a custom resolver.
void testJarUtilJarInJar01()