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