27package jau.test.pkg.cache;
30import java.io.IOException;
31import java.lang.reflect.Method;
32import java.net.URISyntaxException;
34import java.net.URLClassLoader;
35import java.util.jar.JarFile;
37import org.jau.io.IOUtil;
38import org.jau.lang.ReflectionUtil;
39import org.jau.net.Uri;
40import org.jau.pkg.JarUtil;
41import org.jau.pkg.cache.TempCacheReg;
42import org.jau.pkg.cache.TempFileCache;
43import org.jau.pkg.cache.TempJarCache;
44import org.jau.sys.AndroidVersion;
45import org.jau.sys.JNILibrary;
46import org.jau.sys.RuntimeProps;
47import org.junit.Assert;
48import org.junit.BeforeClass;
49import org.junit.FixMethodOrder;
51import org.junit.runners.MethodSorters;
53import jau.test.junit.util.JunitTracer;
54import jau.test.net.URIDumpUtil;
56@FixMethodOrder(MethodSorters.NAME_ASCENDING)
58 static TempFileCache fileCache;
60 static class TestClassLoader
extends URLClassLoader {
61 public TestClassLoader(
final URL[] urls) {
64 public TestClassLoader(
final URL[] urls,
final ClassLoader parent) {
69 static void assertTempFileCachesIndividualInstances(
final boolean shallBeSame,
final TempFileCache fileCache2,
final TempFileCache fileCache3) {
70 Assert.assertTrue(fileCache2.getTempDir().exists());
71 Assert.assertTrue(fileCache2.getTempDir().isDirectory());
72 Assert.assertTrue(fileCache3.getTempDir().exists());
73 Assert.assertTrue(fileCache3.getTempDir().isDirectory());
75 Assert.assertEquals(TempFileCache.getBaseDir(), TempFileCache.getBaseDir());
76 Assert.assertEquals(TempFileCache.getRootDir(), TempFileCache.getRootDir());
79 Assert.assertTrue(
"file caches are not equal", fileCache2.getTempDir().equals(fileCache3.getTempDir()));
81 Assert.assertFalse(
"file caches are equal", fileCache2.getTempDir().equals(fileCache3.getTempDir()));
85 final ClassLoader cl = fileCache2.getClass().getClassLoader();
86 assertTempFileCachesIndividualInstances(shallBeSame, fileCache2, cl, fileCache3, cl);
89 static void assertTempFileCachesIndividualInstances(
final boolean shallBeSame,
final Object fileCache2,
final ClassLoader cl2,
final Object fileCache3,
final ClassLoader cl3) {
90 final Class<?> fileCacheClazz2 = ReflectionUtil.getClass(TempFileCache.class.getName(),
false, cl2);
91 final Class<?> fileCacheClazz3 = ReflectionUtil.getClass(TempFileCache.class.getName(),
false, cl3);
93 final Method fc2GetBaseDir = ReflectionUtil.getMethod(fileCacheClazz2 ,
"getBaseDir");
94 final Method fc3GetBaseDir = ReflectionUtil.getMethod(fileCacheClazz3 ,
"getBaseDir");
95 final Object baseDir2 = ReflectionUtil.callMethod(fileCache2, fc2GetBaseDir);
96 final Object baseDir3 = ReflectionUtil.callMethod(fileCache3, fc3GetBaseDir);
97 Assert.assertEquals(baseDir2, baseDir3);
99 final Method fc2GetRootDir = ReflectionUtil.getMethod(fileCacheClazz2 ,
"getRootDir");
100 final Method fc3GetRootDir = ReflectionUtil.getMethod(fileCacheClazz3 ,
"getRootDir");
101 final Object rootDir2 = ReflectionUtil.callMethod(fileCache2, fc2GetRootDir);
102 final Object rootDir3 = ReflectionUtil.callMethod(fileCache3, fc3GetRootDir);
103 Assert.assertEquals(rootDir2, rootDir3);
105 final Method fc2GetTempDir = ReflectionUtil.getMethod(fileCacheClazz2 ,
"getTempDir");
106 final Method fc3GetTempDir = ReflectionUtil.getMethod(fileCacheClazz3 ,
"getTempDir");
107 final Object tempDir2 = ReflectionUtil.callMethod(fileCache2, fc2GetTempDir);
108 final Object tempDir3 = ReflectionUtil.callMethod(fileCache3, fc3GetTempDir);
111 Assert.assertTrue(
"file caches are not equal", tempDir2.equals(tempDir3));
113 Assert.assertFalse(
"file caches are equal", tempDir2.equals(tempDir3));
121 Assert.assertTrue(TempFileCache.initSingleton());
122 Assert.assertTrue(TempCacheReg.isTempFileCacheUsed());
124 fileCache =
new TempFileCache();
125 Assert.assertTrue(fileCache.isValid(
false));
126 System.err.println(
"tmp dir: "+fileCache.getTempDir());
131 Assert.assertTrue(fileCache.getTempDir().exists());
132 Assert.assertTrue(fileCache.getTempDir().isDirectory());
137 final TempFileCache fileCache2 =
new TempFileCache();
138 final TempFileCache fileCache3 =
new TempFileCache();
140 assertTempFileCachesIndividualInstances(
false, fileCache2, fileCache3);
144 public void testJarUtil01a() throws IOException, IllegalArgumentException, URISyntaxException {
145 if(AndroidVersion.isAvailable) { System.err.println(
"n/a on Android");
return; }
146 final JarFile jarFile = JarUtil.getJarFile(RuntimeProps.class.getName(),
this.getClass().getClassLoader());
147 Assert.assertNotNull(jarFile);
148 JarUtil.extract(fileCache.getTempDir(),
null, jarFile,
null,
false,
true,
true);
149 File f =
new File(fileCache.getTempDir(),
"META-INF/MANIFEST.MF");
150 Assert.assertTrue(f.exists());
151 f =
new File(fileCache.getTempDir(), IOUtil.getClassFileName(RuntimeProps.class.getName()));
152 Assert.assertTrue(f.exists());
157 if(AndroidVersion.isAvailable) { System.err.println(
"n/a on Android");
return; }
158 File f =
new File(fileCache.getTempDir(),
"META-INF/MANIFEST.MF");
159 Assert.assertTrue(f.exists());
160 f =
new File(fileCache.getTempDir(), IOUtil.getClassFileName(RuntimeProps.class.getName()));
161 Assert.assertTrue(f.exists());
169 Assert.assertTrue(TempJarCache.initSingleton());
170 Assert.assertTrue(TempCacheReg.isTempJarCacheUsed(
false));
171 Assert.assertTrue(TempJarCache.isInitialized(
false));
176 if(AndroidVersion.isAvailable) { System.err.println(
"n/a on Android");
return; }
178 final ClassLoader cl = getClass().getClassLoader();
179 TempJarCache.addAll(RuntimeProps.class, JarUtil.getJarFileUri(RuntimeProps.class.getName(), cl));
181 File f0 =
new File(TempJarCache.getTempFileCache().getTempDir(),
"META-INF/MANIFEST.MF");
182 Assert.assertTrue(f0.exists());
184 File f1 =
new File(TempJarCache.findResource(
"META-INF/MANIFEST.MF"));
185 Assert.assertTrue(f1.exists());
186 Assert.assertEquals(f0, f1);
188 f0 =
new File(TempJarCache.getTempFileCache().getTempDir(), IOUtil.getClassFileName(RuntimeProps.class.getName()));
189 Assert.assertTrue(f0.exists());
191 f1 =
new File(TempJarCache.findResource(IOUtil.getClassFileName(RuntimeProps.class.getName())));
192 Assert.assertTrue(f1.exists());
193 Assert.assertEquals(f0, f1);
198 if(AndroidVersion.isAvailable) { System.err.println(
"n/a on Android");
return; }
200 final Uri.Encoded nativeJarName = Uri.Encoded.cast(
"jaulib-fat.jar");
201 final String libBaseName =
"jaulib_jni_jni";
202 final ClassLoader cl = getClass().getClassLoader();
204 final Uri jarUri = JarUtil.getJarUri(TempJarCache.class.getName(), cl);
205 Assert.assertNotNull(jarUri);
206 System.err.println(
"1 - jarUri:");
209 final Uri jarFileUri = jarUri.getContainedUri();
210 Assert.assertNotNull(jarFileUri);
211 System.err.println(
"2 - jarFileUri:");
214 final Uri jarFileDir = jarFileUri.getParent();
215 Assert.assertNotNull(jarFileDir);
216 System.err.println(
"3 - jarFileDir:");
219 final Uri nativeJarURI = JarUtil.getJarFileUri(jarFileDir, nativeJarName);
220 System.err.println(
"4 - nativeJarURI:");
223 TempJarCache.addNativeLibs(TempJarCache.class, nativeJarURI,
null );
224 final String libFullPath = TempJarCache.findLibrary(libBaseName);
225 Assert.assertNotNull(libFullPath);
226 Assert.assertEquals(libBaseName, JNILibrary.isValidNativeLibraryName(libFullPath,
true));
227 final File f =
new File(libFullPath);
228 Assert.assertTrue(f.exists());
233 assertTempFileCachesIndividualInstances(
true, TempJarCache.getTempFileCache(), TempJarCache.getTempFileCache());
235 final ClassLoader cl = getClass().getClassLoader();
236 final TempFileCache fileCache2 = (TempFileCache) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(),
"getTempFileCache",
null,
null, cl);
237 final TempFileCache fileCache3 = (TempFileCache) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(),
"getTempFileCache",
null,
null, cl);
238 assertTempFileCachesIndividualInstances(
true, fileCache2, fileCache3);
243 if(AndroidVersion.isAvailable) { System.err.println(
"n/a on Android");
return; }
244 final URL[] urls =
new URL[] { JarUtil.getJarFileUri(TempJarCache.class.getName(), getClass().getClassLoader()).toURL() };
245 System.err.println(
"url: "+urls[0]);
246 final ClassLoader cl2 =
new TestClassLoader(urls,
null);
247 final ClassLoader cl3 =
new TestClassLoader(urls,
null);
249 Assert.assertFalse(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(),
"isInitialized",
new Class<?>[] { Boolean.TYPE },
new Object[] { Boolean.FALSE }, cl2)
251 Assert.assertFalse(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(),
"isInitialized",
new Class<?>[] { Boolean.TYPE },
new Object[] { Boolean.FALSE }, cl3)
253 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(),
"initSingleton",
null,
null, cl2)
255 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(),
"initSingleton",
null,
null, cl3)
257 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(),
"isInitialized",
new Class<?>[] { Boolean.TYPE },
new Object[] { Boolean.FALSE }, cl2)
259 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(),
"isInitialized",
new Class<?>[] { Boolean.TYPE },
new Object[] { Boolean.FALSE }, cl3)
262 final Object fileCache2 = ReflectionUtil.callStaticMethod(TempJarCache.class.getName(),
"getTempFileCache",
null,
null, cl2);
263 final Object fileCache3 = ReflectionUtil.callStaticMethod(TempJarCache.class.getName(),
"getTempFileCache",
null,
null, cl3);
265 assertTempFileCachesIndividualInstances(
false, fileCache2, cl2, fileCache3, cl3);
268 public static void main(
final String args[])
throws IOException {
270 org.junit.runner.JUnitCore.
main(tstname);
static void showUri(final Uri uri)
void testTempJarCache00Init()
void testTempJarCache04bDiffClassLoader()
void testTempFileCache01FileExist()
void testTempJarCache01LoadAllTestManifestAndClass()
void testTempJarCache04aSameClassLoader()
static void main(final String args[])
void testTempFileCache02Instances()
void testTempJarCache02AddNativeLibs()