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  *          jdk.aot/jdk.tools.jaotc.collect.module
  30  * @compile ../Utils.java
  31  * @run junit/othervm jdk.tools.jaotc.test.collect.module.ModuleSourceProviderTest
  32  */
  33 
  34 
  35 
  36 package jdk.tools.jaotc.test.collect.module;
  37 
  38 import static jdk.tools.jaotc.test.collect.Utils.mkpath;
  39 import static org.junit.Assert.assertEquals;
  40 
  41 import java.io.IOException;
  42 import java.nio.file.FileSystem;
  43 import java.nio.file.FileSystems;
  44 import java.nio.file.Path;
  45 import java.nio.file.Paths;
  46 import java.util.function.BiFunction;
  47 
  48 import org.junit.Before;
  49 import org.junit.Test;
  50 
  51 import jdk.tools.jaotc.collect.FileSupport;
  52 import jdk.tools.jaotc.collect.module.ModuleSource;
  53 import jdk.tools.jaotc.collect.module.ModuleSourceProvider;
  54 
  55 public class ModuleSourceProviderTest {
  56     private ClassLoader classLoader;
  57     private ModuleSourceProvider target;
  58     private FileSupport fileSupport;
  59     private BiFunction<Path, Path, Path> getSubDirectory = null;
  60 
  61     @Before
  62     public void setUp() {
  63         classLoader = new FakeClassLoader();
  64         fileSupport = new FileSupport() {
  65 
  66             @Override
  67             public boolean isDirectory(Path path) {
  68                 return true;
  69             }
  70 
  71             @Override
  72             public Path getSubDirectory(FileSystem fileSystem, Path root, Path path) throws IOException {
  73                 if (getSubDirectory == null) {
  74                     throw new IOException("Nope");
  75                 }
  76                 return getSubDirectory.apply(root, path);
  77             }
  78         };
  79         target = new ModuleSourceProvider(FileSystems.getDefault(), classLoader, fileSupport);
  80     }
  81 
  82     @Test
  83     public void itShouldUseFileSupport() {
  84         getSubDirectory = (root, path) -> {
  85             if (root.toString().equals("modules") && path.toString().equals("test.module")) {
  86                 return Paths.get("modules/test.module");
  87             }
  88             return null;
  89         };
  90 
  91         ModuleSource source = (ModuleSource) target.findSource("test.module", null);
  92         assertEquals(mkpath("modules/test.module"), source.getModulePath().toString());
  93         assertEquals("module:" + mkpath("modules/test.module"), source.toString());
  94     }
  95 
  96     private static class FakeClassLoader extends ClassLoader {
  97         @Override
  98         public Class<?> loadClass(String name) throws ClassNotFoundException {
  99             return null;
 100         }
 101     }
 102 }