1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.dalvik.system;
18
19import dalvik.system.PathClassLoader;
20import java.lang.reflect.Method;
21import java.io.File;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.io.InputStream;
25import libcore.io.Streams;
26import junit.framework.TestCase;
27
28public final class PathClassLoaderTest extends TestCase {
29
30    /**
31     * Make sure we're searching the application library path first.
32     * http://b/issue?id=2933456
33     */
34    public void testLibraryPathSearchOrder() throws IOException {
35        File tmp = new File(System.getProperty("java.io.tmpdir"));
36        File systemLibPath = new File(tmp, "systemLibPath");
37        File applicationLibPath = new File(tmp, "applicationLibPath");
38        makeTempFile(systemLibPath, "libduplicated.so");
39        File applicationLib = makeTempFile(applicationLibPath, "libduplicated.so");
40
41        System.setProperty("java.library.path", systemLibPath.toString());
42        PathClassLoader pathClassLoader = new PathClassLoader(applicationLibPath.toString(),
43                applicationLibPath.toString(), getClass().getClassLoader());
44
45        String path = pathClassLoader.findLibrary("duplicated");
46        assertEquals(applicationLib.toString(), path);
47    }
48
49    private File makeTempFile(File directory, String name) throws IOException {
50        directory.mkdirs();
51        File result = new File(directory, name);
52        FileOutputStream stream = new FileOutputStream(result);
53        stream.close();
54        assertTrue(result.exists());
55        return result;
56    }
57
58    public void testAppUseOfPathClassLoader() throws Exception {
59        // Extract loading-test.jar from the resource.
60        ClassLoader pcl = PathClassLoaderTest.class.getClassLoader();
61        File jar = File.createTempFile("loading-test", ".jar");
62        try (InputStream in = pcl.getResourceAsStream("dalvik/system/loading-test.jar");
63             FileOutputStream out = new FileOutputStream(jar)) {
64          Streams.copy(in, out);
65        }
66
67        // Execute code from the jar file using a PathClassLoader.
68        PathClassLoader cl = new PathClassLoader(jar.getPath(), pcl);
69        Class c = cl.loadClass("test.Test1");
70        Method m = c.getMethod("test", (Class[]) null);
71        String result = (String) m.invoke(null, (Object[]) null);
72        assertSame("blort", result);
73
74        // Clean up the extracted jar file.
75        assertTrue(jar.delete());
76    }
77
78    @Override protected void setUp() throws Exception {
79        super.setUp();
80    }
81
82    @Override protected void tearDown() throws Exception {
83        super.tearDown();
84    }
85}
86