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