jaulib v1.3.0
Jau Support Library (C++, Java, ..)
TestTempJarCache.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2011 Gothel Software e.K.
5 * Copyright (c) 2011 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package jau.test.pkg.cache;
28
29import java.io.File;
30import java.io.IOException;
31import java.lang.reflect.Method;
32import java.net.URISyntaxException;
33import java.net.URL;
34import java.net.URLClassLoader;
35import java.util.jar.JarFile;
36
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;
50import org.junit.Test;
51import org.junit.runners.MethodSorters;
52
53import jau.test.junit.util.JunitTracer;
54import jau.test.net.URIDumpUtil;
55
56@FixMethodOrder(MethodSorters.NAME_ASCENDING)
57public class TestTempJarCache extends JunitTracer {
58 static TempFileCache fileCache;
59
60 static class TestClassLoader extends URLClassLoader {
61 public TestClassLoader(final URL[] urls) {
62 super(urls);
63 }
64 public TestClassLoader(final URL[] urls, final ClassLoader parent) {
65 super(urls, parent);
66 }
67 }
68
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());
74
75 Assert.assertEquals(TempFileCache.getBaseDir(), TempFileCache.getBaseDir());
76 Assert.assertEquals(TempFileCache.getRootDir(), TempFileCache.getRootDir());
77
78 if(shallBeSame) {
79 Assert.assertTrue("file caches are not equal", fileCache2.getTempDir().equals(fileCache3.getTempDir()));
80 } else {
81 Assert.assertFalse("file caches are equal", fileCache2.getTempDir().equals(fileCache3.getTempDir()));
82 }
83 // also verify with diff classloader/reflection method,
84 // to proof that methodology is valid!
85 final ClassLoader cl = fileCache2.getClass().getClassLoader();
86 assertTempFileCachesIndividualInstances(shallBeSame, fileCache2, cl, fileCache3, cl);
87 }
88
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);
92
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);
98
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);
104
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);
109
110 if(shallBeSame) {
111 Assert.assertTrue("file caches are not equal", tempDir2.equals(tempDir3));
112 } else {
113 Assert.assertFalse("file caches are equal", tempDir2.equals(tempDir3));
114 }
115 }
116
117 @BeforeClass
118 public static void init() {
119 // may already been initialized by other test
120 // Assert.assertFalse(TempCacheReg.isTempFileCacheUsed());
121 Assert.assertTrue(TempFileCache.initSingleton());
122 Assert.assertTrue(TempCacheReg.isTempFileCacheUsed());
123
124 fileCache = new TempFileCache();
125 Assert.assertTrue(fileCache.isValid(false));
126 System.err.println("tmp dir: "+fileCache.getTempDir());
127 }
128
129 @Test
130 public void testTempFileCache01FileExist() throws IOException {
131 Assert.assertTrue(fileCache.getTempDir().exists());
132 Assert.assertTrue(fileCache.getTempDir().isDirectory());
133 }
134
135 @Test
136 public void testTempFileCache02Instances() throws IOException {
137 final TempFileCache fileCache2 = new TempFileCache();
138 final TempFileCache fileCache3 = new TempFileCache();
139
140 assertTempFileCachesIndividualInstances(false, fileCache2, fileCache3);
141 }
142
143 @Test
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());
153 }
154
155 @Test
156 public void testJarUtil01b() throws IOException {
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());
162 }
163
164 @Test
165 public void testTempJarCache00Init() throws IOException {
166 // may already been initialized by other test
167 // Assert.assertFalse(TempCacheReg.isTempJarCacheUsed());
168 // Assert.assertFalse(TempJarCache.isInitialized());
169 Assert.assertTrue(TempJarCache.initSingleton());
170 Assert.assertTrue(TempCacheReg.isTempJarCacheUsed(false));
171 Assert.assertTrue(TempJarCache.isInitialized(false));
172 }
173
174 @Test
175 public void testTempJarCache01LoadAllTestManifestAndClass() throws IOException, SecurityException, IllegalArgumentException, URISyntaxException {
176 if(AndroidVersion.isAvailable) { System.err.println("n/a on Android"); return; }
177
178 final ClassLoader cl = getClass().getClassLoader();
180
181 File f0 = new File(TempJarCache.getTempFileCache().getTempDir(), "META-INF/MANIFEST.MF");
182 Assert.assertTrue(f0.exists());
183
184 File f1 = new File(TempJarCache.findResource("META-INF/MANIFEST.MF"));
185 Assert.assertTrue(f1.exists());
186 Assert.assertEquals(f0, f1);
187
188 f0 = new File(TempJarCache.getTempFileCache().getTempDir(), IOUtil.getClassFileName(RuntimeProps.class.getName()));
189 Assert.assertTrue(f0.exists());
190
191 f1 = new File(TempJarCache.findResource(IOUtil.getClassFileName(RuntimeProps.class.getName())));
192 Assert.assertTrue(f1.exists());
193 Assert.assertEquals(f0, f1);
194 }
195
196 @Test
197 public void testTempJarCache02AddNativeLibs() throws IOException, IllegalArgumentException, URISyntaxException {
198 if(AndroidVersion.isAvailable) { System.err.println("n/a on Android"); return; }
199 // FIXME ?? final Uri.Encoded nativeJarName = Uri.Encoded.cast("jaulib_jni-natives-"+PlatformProps.os_and_arch+".jar");
200 final Uri.Encoded nativeJarName = Uri.Encoded.cast("jaulib-fat.jar");
201 final String libBaseName = "jaulib_jni_jni";
202 final ClassLoader cl = getClass().getClassLoader();
203
204 final Uri jarUri = JarUtil.getJarUri(TempJarCache.class.getName(), cl);
205 Assert.assertNotNull(jarUri);
206 System.err.println("1 - jarUri:");
207 URIDumpUtil.showUri(jarUri);
208
209 final Uri jarFileUri = jarUri.getContainedUri();
210 Assert.assertNotNull(jarFileUri);
211 System.err.println("2 - jarFileUri:");
212 URIDumpUtil.showUri(jarFileUri);
213
214 final Uri jarFileDir = jarFileUri.getParent();
215 Assert.assertNotNull(jarFileDir);
216 System.err.println("3 - jarFileDir:");
217 URIDumpUtil.showUri(jarFileDir);
218
219 final Uri nativeJarURI = JarUtil.getJarFileUri(jarFileDir, nativeJarName);
220 System.err.println("4 - nativeJarURI:");
221 URIDumpUtil.showUri(nativeJarURI);
222
223 TempJarCache.addNativeLibs(TempJarCache.class, nativeJarURI, null /* nativeLibraryPath */);
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());
229 }
230
231 @Test
232 public void testTempJarCache04aSameClassLoader() throws IOException {
233 assertTempFileCachesIndividualInstances(true, TempJarCache.getTempFileCache(), TempJarCache.getTempFileCache());
234
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);
239 }
240
241 @Test
242 public void testTempJarCache04bDiffClassLoader() throws IOException, IllegalArgumentException, URISyntaxException {
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);
248
249 Assert.assertFalse(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", new Class<?>[] { Boolean.TYPE }, new Object[] { Boolean.FALSE }, cl2)
250 ).booleanValue());
251 Assert.assertFalse(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", new Class<?>[] { Boolean.TYPE }, new Object[] { Boolean.FALSE }, cl3)
252 ).booleanValue());
253 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "initSingleton", null, null, cl2)
254 ).booleanValue());
255 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "initSingleton", null, null, cl3)
256 ).booleanValue());
257 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", new Class<?>[] { Boolean.TYPE }, new Object[] { Boolean.FALSE }, cl2)
258 ).booleanValue());
259 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", new Class<?>[] { Boolean.TYPE }, new Object[] { Boolean.FALSE }, cl3)
260 ).booleanValue());
261
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);
264
265 assertTempFileCachesIndividualInstances(false, fileCache2, cl2, fileCache3, cl3);
266 }
267
268 public static void main(final String args[]) throws IOException {
269 final String tstname = TestTempJarCache.class.getName();
270 org.junit.runner.JUnitCore.main(tstname);
271 }
272
273}
static void showUri(final Uri uri)
static void main(final String args[])
static String getClassFileName(final String clazzBinName)
Definition: IOUtil.java:397
Utility methods to simplify reflection access.
static final< R > R callStaticMethod(final Method method, final Object ... args)
static final Class<?> getClass(final String clazzName, final boolean initializeClazz, final ClassLoader cl)
Loads and returns the class or null.
static final< R > R callMethod(final Object instance, final Method method, final Object ... args)
static final Method getMethod(final Class<?> clazz, final String methodName, final Class<?> ... argTypes)
Immutable RFC3986 encoded string.
Definition: Uri.java:301
static Encoded cast(final String encoded)
Casts the given encoded String by creating a new Encoded instance.
Definition: Uri.java:310
This class implements an immutable Uri as defined by RFC 2396.
Definition: Uri.java:162
final Uri getParent()
Returns this Uri's parent directory Uri.
Definition: Uri.java:1641
final java.net.URL toURL()
Returns a new URL instance using the encoded input string, new URL(uri.input), i.e.
Definition: Uri.java:1331
final Uri getContainedUri()
If this instance's schemeSpecificPart contains a Uri itself, a sub-Uri, return schemeSpecificPart + #...
Definition: Uri.java:1414
static Uri getJarFileUri(final String clazzBinName, final ClassLoader cl)
The Class's "com.jogamp.common.GlueGenVersion" Uri jar:sub_protocol:/some/path/gluegen-rt....
Definition: JarUtil.java:304
static Uri getJarUri(final String clazzBinName, final ClassLoader cl)
The Class's "com.jogamp.common.GlueGenVersion" Uri jar:sub_protocol:/some/path/gluegen-rt....
Definition: JarUtil.java:140
static JarFile getJarFile(final String clazzBinName, final ClassLoader cl)
Definition: JarUtil.java:379
static final int extract(final File dest, final Map< String, String > nativeLibMap, final JarFile jarFile, final String nativeLibraryPath, final boolean extractNativeLibraries, final boolean extractClassFiles, final boolean extractOtherFiles)
Extract the files of the given jar file.
Definition: JarUtil.java:541
static boolean isTempJarCacheUsed(final boolean forExecutables)
static boolean isTempFileCacheUsed()
File getTempDir()
Temporary directory for individual files (eg.
static File getRootDir()
Root temp directory for this JVM instance.
boolean isValid(final boolean forExecutables)
static File getBaseDir()
Base temp directory used by TempFileCache.
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 synchronized final String findResource(final String name)
TODO class access pending needs Classloader.defineClass(..) access, ie.
static synchronized final boolean addNativeLibs(final Class<?> certClass, final Uri jarUri, final String nativeLibraryPath)
Adds native libraries, if not yet added.
static boolean initSingleton()
Documented way to kick off static initialization.
static synchronized final String findLibrary(final String libName)
If isInitialized(true) is false due to lack of executable support only, this method always returns fa...
static TempFileCache getTempFileCache()
static final boolean isAvailable
Static JNI Native Libraries handler.
Definition: JNILibrary.java:47
static final String isValidNativeLibraryName(final String libName, final boolean isLowerCaseAlready)
Comparison of prefix and suffix of the given libName's basename is performed case insensitive
Runtime platform properties derived from PlatformProps and runtime query.