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