1 /* 2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @requires vm.aot 27 * @modules jdk.aot/jdk.tools.jaotc 28 * jdk.aot/jdk.tools.jaotc.collect 29 * 30 * @build jdk.tools.jaotc.test.collect.Utils 31 * @build jdk.tools.jaotc.test.collect.FakeFileSupport 32 * @run junit/othervm jdk.tools.jaotc.test.collect.SearchPathTest 33 */ 34 35 36 37 package jdk.tools.jaotc.test.collect; 38 39 import static jdk.tools.jaotc.test.collect.Utils.mkpath; 40 import static jdk.tools.jaotc.test.collect.Utils.mkpaths; 41 import static jdk.tools.jaotc.test.collect.Utils.set; 42 import static org.junit.Assert.assertEquals; 43 import static org.junit.Assert.assertNull; 44 import static org.junit.Assert.assertSame; 45 46 import java.nio.file.FileSystem; 47 import java.nio.file.FileSystems; 48 import java.nio.file.Path; 49 import java.nio.file.Paths; 50 51 import org.junit.Before; 52 import org.junit.Test; 53 54 import jdk.tools.jaotc.collect.SearchPath; 55 56 public class SearchPathTest { 57 private FakeFileSupport fileSupport; 58 private FileSystem fs; 59 60 @Before 61 public void setUp() throws Exception { 62 fs = FileSystems.getDefault(); 63 } 64 65 @Test 66 public void itShouldUsePathIfPathIsAbsoluteAndExisting() { 67 fileSupport = new FakeFileSupport(mkpaths("/foo"), set()); 68 SearchPath target = new SearchPath(fileSupport); 69 Path foo = Paths.get(mkpath("/foo")); 70 Path result = target.find(fs, foo); 71 assertSame(result, foo); 72 } 73 74 @Test 75 public void itShouldReturnNullIfPathIsAbsoluteAndNonExisting() { 76 fileSupport = new FakeFileSupport(set(), set()); 77 SearchPath target = new SearchPath(fileSupport); 78 Path result = target.find(fs, Paths.get(mkpath("/bar"))); 79 assertNull(result); 80 } 81 82 @Test 83 public void itShouldUseRelativeExisting() { 84 fileSupport = new FakeFileSupport(mkpaths("hello", "tmp/hello", "search/hello"), set()); 85 SearchPath target = new SearchPath(fileSupport); 86 target.add("search"); 87 Path hello = Paths.get("hello"); 88 Path result = target.find(fs, hello, "tmp"); 89 assertSame(result, hello); 90 } 91 92 @Test 93 public void itShouldSearchDefaultsBeforeSearchPaths() { 94 fileSupport = new FakeFileSupport(mkpaths("bar/foobar"), set()); 95 SearchPath target = new SearchPath(fileSupport); 96 Path result = target.find(fs, Paths.get("foobar"), "default1", "bar"); 97 assertEquals(mkpath("bar/foobar"), result.toString()); 98 assertEquals(mkpaths("foobar", "default1/foobar", "bar/foobar"), fileSupport.getCheckedExists()); 99 } 100 101 @Test 102 public void itShouldUseSearchPathsIfNotInDefaults() { 103 fileSupport = new FakeFileSupport(mkpaths("bar/tmp/foobar"), set()); 104 SearchPath target = new SearchPath(fileSupport); 105 target.add("foo/tmp", "bar/tmp"); 106 107 Path result = target.find(fs, Paths.get("foobar"), "foo", "bar"); 108 assertEquals(mkpath("bar/tmp/foobar"), result.toString()); 109 assertEquals(mkpaths("foobar", "foo/foobar", "bar/foobar", "bar/tmp/foobar", "foo/tmp/foobar"), fileSupport.getCheckedExists()); 110 } 111 112 @Test 113 public void itShouldReturnNullIfNoExistingPathIsFound() { 114 fileSupport = new FakeFileSupport(set(), set()); 115 SearchPath target = new SearchPath(fileSupport); 116 target.add("dir1", "dir2"); 117 118 Path result = target.find(fs, Paths.get("entry"), "dir3", "dir4"); 119 assertNull(result); 120 assertEquals(mkpaths("entry", "dir1/entry", "dir2/entry", "dir3/entry", "dir4/entry"), fileSupport.getCheckedExists()); 121 } 122 }