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  * @requires vm.aot
  27  * @modules jdk.aot/jdk.tools.jaotc
  28  *          jdk.aot/jdk.tools.jaotc.collect
  29  *          jdk.aot/jdk.tools.jaotc.collect.jar
  30  * @compile ../Utils.java
  31  * @compile ../FakeFileSupport.java
  32  * @compile ../FakeSearchPath.java
  33  *
  34  * @run junit/othervm jdk.tools.jaotc.test.collect.jar.JarSourceProviderTest
  35  */
  36 
  37 package jdk.tools.jaotc.test.collect.jar;
  38 
  39 import jdk.tools.jaotc.collect.ClassSource;
  40 import jdk.tools.jaotc.collect.jar.JarSourceProvider;
  41 import jdk.tools.jaotc.test.collect.FakeFileSupport;
  42 import jdk.tools.jaotc.test.collect.FakeSearchPath;
  43 import org.junit.Assert;
  44 import org.junit.Before;
  45 import org.junit.Test;
  46 
  47 import java.nio.file.Path;
  48 import java.nio.file.Paths;
  49 import java.nio.file.ProviderNotFoundException;
  50 import java.util.Set;
  51 
  52 import static jdk.tools.jaotc.test.collect.Utils.mkpath;
  53 import static jdk.tools.jaotc.test.collect.Utils.set;
  54 
  55 public class JarSourceProviderTest {
  56 
  57     private FakeFileSupport fileSupport;
  58     private JarSourceProvider target;
  59 
  60     @Before
  61     public void setUp() throws Exception {
  62         fileSupport = new FakeFileSupport(set(), set());
  63         target = new JarSourceProvider(fileSupport);
  64     }
  65 
  66     @Test
  67     public void itShouldUseSearchPathToFindPath() {
  68         Set<String> visited = set();
  69         JarSourceProvider target = new JarSourceProvider(fileSupport);
  70         FakeSearchPath searchPath = new FakeSearchPath(null);
  71         ClassSource source = target.findSource("hello", searchPath);
  72 
  73         Assert.assertEquals(set("hello"), searchPath.entries);
  74     }
  75 
  76     @Test
  77     public void itShouldReturnNullIfPathIsNull() {
  78         JarSourceProvider target = new JarSourceProvider(fileSupport);
  79         ClassSource source = target.findSource("foobar", new FakeSearchPath(null));
  80         Assert.assertNull(source);
  81     }
  82 
  83     @Test
  84     public void itShouldReturnNullIfPathIsDirectory() {
  85         fileSupport.addDirectory("hello/foobar");
  86         ClassSource source = target.findSource("foobar", new FakeSearchPath("hello/foobar"));
  87 
  88         Assert.assertNull(source);
  89         Assert.assertEquals(set(mkpath("hello/foobar")), fileSupport.getCheckedDirectory());
  90     }
  91 
  92     @Test
  93     public void itShouldReturnNullIfUnableToMakeJarFileSystem() {
  94         fileSupport.setJarFileSystemRoot(null);
  95         ClassSource result = target.findSource("foobar", new FakeSearchPath("foo/bar"));
  96 
  97         Assert.assertEquals(set(mkpath("foo/bar")), fileSupport.getCheckedJarFileSystemRoots());
  98         Assert.assertNull(result);
  99     }
 100 
 101     @Test
 102     public void itShouldReturnNullIfNotValidJarProvider() {
 103         fileSupport = new FakeFileSupport(set(), set()) {
 104 
 105             @Override
 106             public Path getJarFileSystemRoot(Path jarFile) {
 107                 super.getJarFileSystemRoot(jarFile);
 108                 throw new ProviderNotFoundException();
 109             }
 110         };
 111         fileSupport.setJarFileSystemRoot(null);
 112         target = new JarSourceProvider(fileSupport);
 113 
 114         ClassSource result = target.findSource("foobar", new FakeSearchPath("foo/bar"));
 115 
 116         Assert.assertEquals(set(mkpath("foo/bar")), fileSupport.getCheckedJarFileSystemRoots());
 117         Assert.assertNull(result);
 118     }
 119 
 120     @Test
 121     public void itShouldReturnSourceWhenAllIsValid() {
 122         fileSupport.setJarFileSystemRoot(Paths.get("some/bar"));
 123         ClassSource result = target.findSource("foobar", new FakeSearchPath("this/bar"));
 124 
 125         Assert.assertEquals(set(mkpath("this/bar")), fileSupport.getClassloaderPaths());
 126         Assert.assertEquals("jar:" + mkpath("this/bar"), result.toString());
 127     }
 128 }