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.io.File;
21import java.io.FileOutputStream;
22import java.io.IOException;
23import junit.framework.TestCase;
24
25public final class PathClassLoaderTest extends TestCase {
26
27    /**
28     * Make sure we're searching the application library path first.
29     * http://b/issue?id=2933456
30     */
31    public void testLibraryPathSearchOrder() throws IOException {
32        File tmp = new File(System.getProperty("java.io.tmpdir"));
33        File systemLibPath = new File(tmp, "systemLibPath");
34        File applicationLibPath = new File(tmp, "applicationLibPath");
35        makeTempFile(systemLibPath, "libduplicated.so");
36        File applicationLib = makeTempFile(applicationLibPath, "libduplicated.so");
37
38        System.setProperty("java.library.path", systemLibPath.toString());
39        PathClassLoader pathClassLoader = new PathClassLoader(applicationLibPath.toString(),
40                applicationLibPath.toString(), getClass().getClassLoader());
41
42        String path = pathClassLoader.findLibrary("duplicated");
43        assertEquals(applicationLib.toString(), path);
44    }
45
46    private File makeTempFile(File directory, String name) throws IOException {
47        directory.mkdirs();
48        File result = new File(directory, name);
49        FileOutputStream stream = new FileOutputStream(result);
50        stream.close();
51        assertTrue(result.exists());
52        return result;
53    }
54
55    @Override protected void setUp() throws Exception {
56        super.setUp();
57    }
58
59    @Override protected void tearDown() throws Exception {
60        super.tearDown();
61    }
62}
63