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)
62 public static void init() {
65 setTestSupported(
false);
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");
154 final ClassLoader rootCL = this.getClass().getClassLoader();
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");
175 final ClassLoader rootCL = this.getClass().getClassLoader();
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");
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);
241 public URL resolve(
final URL url ) {
242 if( url.getProtocol().equals(
"bundleresource") ) {
245 }
catch(
final MalformedURLException e) {
254 final ClassLoader rootCL =
new CustomClassLoader();
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()
This class implements an immutable Uri as defined by RFC 2396.
final java.net.URL toURL()
Returns a new URL instance using the encoded input string, new URL(uri.input), i.e.
static final String JAR_SCHEME
{@value}
static Uri getJarFileUri(final String clazzBinName, final ClassLoader cl)
The Class's "com.jogamp.common.GlueGenVersion" Uri jar:sub_protocol:/some/path/gluegen-rt....
static void setResolver(final Resolver r)
Setting a custom Resolver instance.
static boolean isTempJarCacheUsed(final boolean forExecutables)
static boolean isTempFileCacheUsed()
File getTempDir()
Temporary directory for individual files (eg.
boolean isValid(final boolean forExecutables)
static boolean initSingleton()
Documented way to kick off static initialization.
Static Jar file cache handler using an underlying instance of TempFileCache, see getTempFileCache().
static boolean isInitialized(final boolean forExecutables)
static synchronized final void addAll(final Class<?> certClass, final Uri jarUri)
Adds all types, native libraries, class files and other files (resources) if not yet added.
static boolean initSingleton()
Documented way to kick off static initialization.
static synchronized final Uri getResourceUri(final String name)
Similar to ClassLoader#getResource(String).
static final boolean isAvailable
Runtime platform properties derived from PlatformProps and runtime query.
Interface allowing users to provide an URL resolver that will convert custom classloader URLs like Ec...