1 /*
   2  * Copyright (c) 2016, 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  * @summary Tests jdeps --generate-open-module option
  27  * @library ../lib
  28  * @build CompilerUtils JdepsUtil JdepsRunner
  29  * @modules jdk.jdeps/com.sun.tools.jdeps
  30  * @run testng GenOpenModule
  31  */
  32 
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.lang.module.ModuleDescriptor;
  36 
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 
  41 import java.util.stream.Stream;
  42 
  43 import org.testng.annotations.BeforeTest;
  44 import org.testng.annotations.Test;
  45 
  46 import static org.testng.Assert.assertEquals;
  47 import static org.testng.Assert.assertTrue;
  48 
  49 public class GenOpenModule extends GenModuleInfo {
  50     private static final String MODULE_INFO = "module-info.class";
  51 
  52     private static final Path MODS_DIR = Paths.get("mods");
  53     private static final Path LIBS_DIR = Paths.get("libs");
  54     private static final Path DEST_DIR = Paths.get("moduleinfosrc");
  55     private static final Path NEW_MODS_DIR = Paths.get("new_mods");
  56 
  57     /**
  58      * Compiles all modules used by the test
  59      */
  60     @BeforeTest
  61     public void compileAll() throws Exception {
  62 
  63         compileModules(MODS_DIR);
  64 
  65         createJARFiles(MODS_DIR, LIBS_DIR);
  66     }
  67 
  68     @Test
  69     public void test() throws IOException {
  70         Stream<String> files = MODULES.stream()
  71                 .map(mn -> LIBS_DIR.resolve(mn + ".jar"))
  72                 .map(Path::toString);
  73 
  74         Stream<String> options = Stream.concat(
  75             Stream.of("--generate-open-module", DEST_DIR.toString()), files);
  76         JdepsRunner.run(options.toArray(String[]::new));
  77 
  78         // check file exists
  79         MODULES.stream()
  80              .map(mn -> DEST_DIR.resolve(mn).resolve("module-info.java"))
  81              .forEach(f -> assertTrue(Files.exists(f)));
  82 
  83         // copy classes to a temporary directory
  84         // and then compile new module-info.java
  85         copyClasses(MODS_DIR, NEW_MODS_DIR);
  86         compileNewGenModuleInfo(DEST_DIR, NEW_MODS_DIR);
  87 
  88         for (String mn : MODULES) {
  89             Path p1 = NEW_MODS_DIR.resolve(mn).resolve(MODULE_INFO);
  90             Path p2 = MODS_DIR.resolve(mn).resolve(MODULE_INFO);
  91 
  92             try (InputStream in1 = Files.newInputStream(p1);
  93                  InputStream in2 = Files.newInputStream(p2)) {
  94                 verify(ModuleDescriptor.read(in1),
  95                        ModuleDescriptor.read(in2));
  96             }
  97         }
  98     }
  99 
 100     /*
 101      * Verify the dependences
 102      */
 103     private void verify(ModuleDescriptor openModule, ModuleDescriptor md) {
 104         System.out.println("verifying: " + openModule.name());
 105         assertTrue(openModule.isOpen());
 106         assertTrue(!md.isOpen());
 107         assertEquals(openModule.name(), md.name());
 108         assertEquals(openModule.requires(), md.requires());
 109         assertTrue(openModule.exports().isEmpty());
 110         assertEquals(openModule.provides(), md.provides());
 111     }
 112 }