GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestTempJarCache.java
Go to the documentation of this file.
1/**
2 * Copyright 2011 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.common.util;
30
31import java.io.File;
32import java.io.IOException;
33import java.lang.reflect.Method;
34import java.net.URISyntaxException;
35import java.net.URL;
36import java.net.URLClassLoader;
37import java.util.jar.JarFile;
38
39import org.junit.Assert;
40import org.junit.BeforeClass;
41import org.junit.FixMethodOrder;
42import org.junit.Test;
43import org.junit.runners.MethodSorters;
44
45import com.jogamp.common.GlueGenVersion;
46import com.jogamp.common.net.URIDumpUtil;
47import com.jogamp.common.net.Uri;
48import com.jogamp.common.os.AndroidVersion;
49import com.jogamp.common.os.NativeLibrary;
50import com.jogamp.common.os.Platform;
51import com.jogamp.common.util.cache.TempCacheReg;
52import com.jogamp.common.util.cache.TempFileCache;
53import com.jogamp.common.util.cache.TempJarCache;
54import com.jogamp.junit.util.SingletonJunitCase;
55
56@FixMethodOrder(MethodSorters.NAME_ASCENDING)
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(GlueGenVersion.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(GlueGenVersion.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(GlueGenVersion.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
189 Assert.assertTrue(f0.exists());
190
191 f1 = new File(TempJarCache.findResource(IOUtil.getClassFileName(GlueGenVersion.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 final Uri.Encoded nativeJarName = Uri.Encoded.cast("gluegen-rt-natives-"+Platform.getOSAndArch()+".jar");
200 final String libBaseName = "gluegen_rt";
201 final ClassLoader cl = getClass().getClassLoader();
202
203 final Uri jarUri = JarUtil.getJarUri(TempJarCache.class.getName(), cl);
204 Assert.assertNotNull(jarUri);
205 System.err.println("1 - jarUri:");
206 URIDumpUtil.showUri(jarUri);
207
208 final Uri jarFileUri = jarUri.getContainedUri();
209 Assert.assertNotNull(jarFileUri);
210 System.err.println("2 - jarFileUri:");
211 URIDumpUtil.showUri(jarFileUri);
212
213 final Uri jarFileDir = jarFileUri.getParent();
214 Assert.assertNotNull(jarFileDir);
215 System.err.println("3 - jarFileDir:");
216 URIDumpUtil.showUri(jarFileDir);
217
218 final Uri nativeJarURI = JarUtil.getJarFileUri(jarFileDir, nativeJarName);
219 System.err.println("4 - nativeJarURI:");
220 URIDumpUtil.showUri(nativeJarURI);
221
222 TempJarCache.addNativeLibs(TempJarCache.class, nativeJarURI, null /* nativeLibraryPath */);
223 final String libFullPath = TempJarCache.findLibrary(libBaseName);
224 Assert.assertNotNull(libFullPath);
225 Assert.assertEquals(libBaseName, NativeLibrary.isValidNativeLibraryName(libFullPath, true));
226 final File f = new File(libFullPath);
227 Assert.assertTrue(f.exists());
228 }
229
230 @Test
231 public void testTempJarCache04aSameClassLoader() throws IOException {
232 assertTempFileCachesIndividualInstances(true, TempJarCache.getTempFileCache(), TempJarCache.getTempFileCache());
233
234 final ClassLoader cl = getClass().getClassLoader();
235 final TempFileCache fileCache2 = (TempFileCache) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "getTempFileCache", null, null, cl);
236 final TempFileCache fileCache3 = (TempFileCache) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "getTempFileCache", null, null, cl);
237 assertTempFileCachesIndividualInstances(true, fileCache2, fileCache3);
238 }
239
240 @Test
241 public void testTempJarCache04bDiffClassLoader() throws IOException, IllegalArgumentException, URISyntaxException {
242 if(AndroidVersion.isAvailable) { System.err.println("n/a on Android"); return; }
243 final URL[] urls = new URL[] { JarUtil.getJarFileUri(TempJarCache.class.getName(), getClass().getClassLoader()).toURL() };
244 System.err.println("url: "+urls[0]);
245 final ClassLoader cl2 = new TestClassLoader(urls, null);
246 final ClassLoader cl3 = new TestClassLoader(urls, null);
247
248 Assert.assertFalse(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", new Class<?>[] { Boolean.TYPE }, new Object[] { Boolean.FALSE }, cl2)
249 ).booleanValue());
250 Assert.assertFalse(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", new Class<?>[] { Boolean.TYPE }, new Object[] { Boolean.FALSE }, cl3)
251 ).booleanValue());
252 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "initSingleton", null, null, cl2)
253 ).booleanValue());
254 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "initSingleton", null, null, cl3)
255 ).booleanValue());
256 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", new Class<?>[] { Boolean.TYPE }, new Object[] { Boolean.FALSE }, cl2)
257 ).booleanValue());
258 Assert.assertTrue(( (Boolean) ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "isInitialized", new Class<?>[] { Boolean.TYPE }, new Object[] { Boolean.FALSE }, cl3)
259 ).booleanValue());
260
261 final Object fileCache2 = ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "getTempFileCache", null, null, cl2);
262 final Object fileCache3 = ReflectionUtil.callStaticMethod(TempJarCache.class.getName(), "getTempFileCache", null, null, cl3);
263
264 assertTempFileCachesIndividualInstances(false, fileCache2, cl2, fileCache3, cl3);
265 }
266
267 public static void main(final String args[]) throws IOException {
268 final String tstname = TestTempJarCache.class.getName();
269 org.junit.runner.JUnitCore.main(tstname);
270 }
271
272}
static void showUri(final Uri uri)
Immutable RFC3986 encoded string.
Definition: Uri.java:296
static Encoded cast(final String encoded)
Casts the given encoded String by creating a new Encoded instance.
Definition: Uri.java:305
This class implements an immutable Uri as defined by RFC 2396.
Definition: Uri.java:160
final java.net.URL toURL()
Returns a new URL instance using the encoded input string, new URL(uri.input), i.e.
Definition: Uri.java:1369
final Uri getContainedUri()
If this instance's schemeSpecificPart contains a Uri itself, a sub-Uri, return schemeSpecificPart + #...
Definition: Uri.java:1437
final Uri getParent()
Returns this Uri's parent directory Uri.
Definition: Uri.java:1664
Provides low-level, relatively platform-independent access to shared ("native") libraries.
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
Utility class for querying platform specific properties.
Definition: Platform.java:58
static String getOSAndArch()
Returns the GlueGen common name for the currently running OSType and CPUType as implemented in the bu...
Definition: Platform.java:454
static String getClassFileName(final String clazzBinName)
Definition: IOUtil.java:463
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:549
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:139
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:303
static JarFile getJarFile(final String clazzBinName, final ClassLoader cl)
Definition: JarUtil.java:378
static final Method getMethod(final Class<?> clazz, final String methodName, final Class<?> ... argTypes)
static final Object callStaticMethod(final String clazzName, final String methodName, final Class<?>[] argTypes, final Object[] args, final ClassLoader cl)
static final Class<?> getClass(final String clazzName, final boolean initializeClazz, final ClassLoader cl)
Loads and returns the class or null.
static final Object callMethod(final Object instance, final Method method, final Object ... args)
static void main(final String args[])
static boolean isTempJarCacheUsed(final boolean forExecutables)
File getTempDir()
Temporary directory for individual files (eg.
boolean isValid(final boolean forExecutables)
static boolean initSingleton()
Documented way to kick off static initialization.
static File getRootDir()
Root temp directory for this JVM instance.
static File getBaseDir()
Base temp directory used by TempFileCache.
Static Jar file cache handler using an underlying instance of TempFileCache, see getTempFileCache().
static synchronized final String findResource(final String name)
TODO class access pending needs Classloader.defineClass(..) access, ie.
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 isInitialized(final boolean forExecutables)
static boolean initSingleton()
Documented way to kick off static initialization.
static synchronized final boolean addNativeLibs(final Class<?> certClass, final Uri jarUri, final String nativeLibraryPath)
Adds native libraries, if not yet added.
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...