1af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson/*
2af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson * Copyright (C) 2010 The Android Open Source Project
3af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson *
4af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson * you may not use this file except in compliance with the License.
6af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson * You may obtain a copy of the License at
7af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson *
8af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson *      http://www.apache.org/licenses/LICENSE-2.0
9af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson *
10af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson * See the License for the specific language governing permissions and
14af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson * limitations under the License.
15af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson */
16af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson
17af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilsonpackage libcore.dalvik.system;
18af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson
19af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilsonimport dalvik.system.PathClassLoader;
20af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilsonimport java.io.File;
21af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilsonimport java.io.FileOutputStream;
22af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilsonimport java.io.IOException;
23af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilsonimport junit.framework.TestCase;
24af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson
25af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilsonpublic final class PathClassLoaderTest extends TestCase {
26af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson
27af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson    /**
28af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson     * Make sure we're searching the application library path first.
29af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson     * http://b/issue?id=2933456
30af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson     */
31af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson    public void testLibraryPathSearchOrder() throws IOException {
32af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        File tmp = new File(System.getProperty("java.io.tmpdir"));
33af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        File systemLibPath = new File(tmp, "systemLibPath");
34af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        File applicationLibPath = new File(tmp, "applicationLibPath");
35af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        makeTempFile(systemLibPath, "libduplicated.so");
36af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        File applicationLib = makeTempFile(applicationLibPath, "libduplicated.so");
37af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson
38af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        System.setProperty("java.library.path", systemLibPath.toString());
39af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        PathClassLoader pathClassLoader = new PathClassLoader(applicationLibPath.toString(),
40af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson                applicationLibPath.toString(), getClass().getClassLoader());
41af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson
42af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        String path = pathClassLoader.findLibrary("duplicated");
43af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        assertEquals(applicationLib.toString(), path);
44af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson    }
45af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson
46af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson    private File makeTempFile(File directory, String name) throws IOException {
47af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        directory.mkdirs();
48af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        File result = new File(directory, name);
49af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        FileOutputStream stream = new FileOutputStream(result);
50af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        stream.close();
51af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        assertTrue(result.exists());
52af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        return result;
53af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson    }
54af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson
55af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson    @Override protected void setUp() throws Exception {
56af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        super.setUp();
57af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson    }
58af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson
59af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson    @Override protected void tearDown() throws Exception {
60af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson        super.tearDown();
61af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson    }
62af7e77337d97ca5961159a7298e3f47a1c507db3Jesse Wilson}
63