1 /* 2 * Copyright (c) 2017, 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 * @key 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 package jdk.tools.jaotc.test.collect; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 40 import java.nio.file.FileSystem; 41 import java.nio.file.FileSystems; 42 import java.nio.file.Path; 43 import java.nio.file.Paths; 44 45 import jdk.tools.jaotc.collect.*; 46 47 import static jdk.tools.jaotc.test.collect.Utils.set; 48 import static jdk.tools.jaotc.test.collect.Utils.mkpath; 49 import static jdk.tools.jaotc.test.collect.Utils.mkpaths; 50 import static org.junit.Assert.*; 51 52 public class SearchPathTest { 53 private FakeFileSupport fileSupport; 54 private FileSystem fs; 55 56 @Before 57 public void setUp() throws Exception { 58 fs = FileSystems.getDefault(); 59 } 60 61 @Test 62 public void itShouldUsePathIfPathIsAbsoluteAndExisting() { 63 fileSupport = new FakeFileSupport(mkpaths("/foo"), set()); 64 SearchPath target = new SearchPath(fileSupport); 65 Path foo = Paths.get(mkpath("/foo")); 66 Path result = target.find(fs, foo); 67 assertSame(result, foo); 68 } 69 70 @Test 71 public void itShouldReturnNullIfPathIsAbsoluteAndNonExisting() { 72 fileSupport = new FakeFileSupport(set(), set()); 73 SearchPath target = new SearchPath(fileSupport); 74 Path result = target.find(fs, Paths.get(mkpath("/bar"))); 75 assertNull(result); 76 } 77 78 @Test 79 public void itShouldUseRelativeExisting() { 80 fileSupport = new FakeFileSupport(mkpaths("hello", "tmp/hello", "search/hello"), set()); 81 SearchPath target = new SearchPath(fileSupport); 82 target.add("search"); 83 Path hello = Paths.get("hello"); 84 Path result = target.find(fs, hello, "tmp"); 85 assertSame(result, hello); 86 } 87 88 @Test 89 public void itShouldSearchDefaultsBeforeSearchPaths() { 90 fileSupport = new FakeFileSupport(mkpaths("bar/foobar"), set()); 91 SearchPath target = new SearchPath(fileSupport); 92 Path result = target.find(fs, Paths.get("foobar"), "default1", "bar"); 93 assertEquals(mkpath("bar/foobar"), result.toString()); 94 assertEquals(mkpaths("foobar", "default1/foobar", "bar/foobar"), fileSupport.getCheckedExists()); 95 } 96 97 @Test 98 public void itShouldUseSearchPathsIfNotInDefaults() { 99 fileSupport = new FakeFileSupport(mkpaths("bar/tmp/foobar"), set()); 100 SearchPath target = new SearchPath(fileSupport); 101 target.add("foo/tmp", "bar/tmp"); 102 103 Path result = target.find(fs, Paths.get("foobar"), "foo", "bar"); 104 assertEquals(mkpath("bar/tmp/foobar"), result.toString()); 105 assertEquals(mkpaths("foobar", "foo/foobar", "bar/foobar", "bar/tmp/foobar", "foo/tmp/foobar"), fileSupport.getCheckedExists()); 106 } 107 108 @Test 109 public void itShouldReturnNullIfNoExistingPathIsFound() { 110 fileSupport = new FakeFileSupport(set(), set()); 111 SearchPath target = new SearchPath(fileSupport); 112 target.add("dir1", "dir2"); 113 114 Path result = target.find(fs, Paths.get("entry"), "dir3", "dir4"); 115 assertNull(result); 116 assertEquals(mkpaths("entry", "dir1/entry", "dir2/entry", "dir3/entry", "dir4/entry"), fileSupport.getCheckedExists()); 117 } 118 }