< prev index next >

jdk/test/tools/jmod/hashes/HashesTest.java

Print this page




  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 Test the recording and checking of module hashes
  27  * @author Andrei Eremeev
  28  * @library /lib/testlibrary
  29  * @modules java.base/jdk.internal.module
  30  *          jdk.jlink/jdk.tools.jlink.internal
  31  *          jdk.jlink/jdk.tools.jmod
  32  *          jdk.compiler
  33  * @build CompilerUtils
  34  * @run testng HashesTest
  35  */
  36 
  37 import java.io.IOException;
  38 import java.io.InputStream;
  39 import java.lang.module.ModuleDescriptor;
  40 import java.lang.module.ModuleFinder;
  41 import java.lang.module.ModuleReader;
  42 import java.lang.module.ModuleReference;
  43 import java.lang.reflect.Method;
  44 import java.nio.file.FileVisitResult;
  45 import java.nio.file.Files;
  46 import java.nio.file.Path;
  47 import java.nio.file.Paths;
  48 import java.nio.file.SimpleFileVisitor;
  49 import java.nio.file.attribute.BasicFileAttributes;
  50 import java.util.ArrayList;
  51 import java.util.Arrays;
  52 import java.util.Collections;
  53 import java.util.List;
  54 import java.util.Optional;
  55 import java.util.Set;

  56 import java.util.stream.Collectors;
  57 
  58 import jdk.internal.module.ConfigurableModuleFinder;
  59 import jdk.internal.module.ModuleHashes;
  60 import org.testng.annotations.BeforeTest;
  61 import org.testng.annotations.Test;
  62 
  63 import static org.testng.Assert.*;
  64 
  65 public class HashesTest {




  66 
  67     private final Path testSrc = Paths.get(System.getProperty("test.src"));
  68     private final Path modSrc = testSrc.resolve("src");
  69     private final Path mods = Paths.get("mods");
  70     private final Path jmods = Paths.get("jmods");
  71     private final String[] modules = new String[] { "m1", "m2", "m3"};
  72 
  73     private static Method hashesMethod;
  74     @BeforeTest
  75     private void setup() throws Exception {
  76         if (Files.exists(jmods)) {
  77             deleteDirectory(jmods);
  78         }
  79         Files.createDirectories(jmods);
  80 
  81         // build m2, m3 required by m1
  82         compileModule("m2", modSrc);
  83         jmod("m2");
  84 
  85         compileModule("m3", modSrc);


 187         Path msrc = src.resolve(moduleName);
 188         assertTrue(CompilerUtils.compile(msrc, mods, "--module-source-path", src.toString()));
 189     }
 190 
 191     private void jmod(String moduleName, String... options) throws IOException {
 192         Path mclasses = mods.resolve(moduleName);
 193         Path outfile = jmods.resolve(moduleName + ".jmod");
 194         List<String> args = new ArrayList<>();
 195         args.add("create");
 196         Collections.addAll(args, options);
 197         Collections.addAll(args, "--class-path", mclasses.toString(),
 198                            outfile.toString());
 199 
 200         if (Files.exists(outfile))
 201             Files.delete(outfile);
 202 
 203         runJmod(args);
 204     }
 205 
 206     private void runJmod(List<String> args) {
 207         int rc = jdk.tools.jmod.Main.run(args.toArray(new String[args.size()]), System.out);
 208         System.out.println("jmod options: " + args.stream().collect(Collectors.joining(" ")));
 209         if (rc != 0) {
 210             throw new AssertionError("Jmod failed: rc = " + rc);
 211         }
 212     }
 213 }


  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 Test the recording and checking of module hashes
  27  * @author Andrei Eremeev
  28  * @library /lib/testlibrary
  29  * @modules java.base/jdk.internal.module
  30  *          jdk.jlink

  31  *          jdk.compiler
  32  * @build CompilerUtils
  33  * @run testng HashesTest
  34  */
  35 
  36 import java.io.IOException;
  37 import java.io.InputStream;
  38 import java.lang.module.ModuleDescriptor;
  39 import java.lang.module.ModuleFinder;
  40 import java.lang.module.ModuleReader;
  41 import java.lang.module.ModuleReference;
  42 import java.lang.reflect.Method;
  43 import java.nio.file.FileVisitResult;
  44 import java.nio.file.Files;
  45 import java.nio.file.Path;
  46 import java.nio.file.Paths;
  47 import java.nio.file.SimpleFileVisitor;
  48 import java.nio.file.attribute.BasicFileAttributes;
  49 import java.util.ArrayList;
  50 import java.util.Arrays;
  51 import java.util.Collections;
  52 import java.util.List;
  53 import java.util.Optional;
  54 import java.util.Set;
  55 import java.util.spi.ToolProvider;
  56 import java.util.stream.Collectors;
  57 
  58 import jdk.internal.module.ConfigurableModuleFinder;
  59 import jdk.internal.module.ModuleHashes;
  60 import org.testng.annotations.BeforeTest;
  61 import org.testng.annotations.Test;
  62 
  63 import static org.testng.Assert.*;
  64 
  65 public class HashesTest {
  66     static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
  67         .orElseThrow(() ->
  68             new RuntimeException("jmod tool not found")
  69         );
  70 
  71     private final Path testSrc = Paths.get(System.getProperty("test.src"));
  72     private final Path modSrc = testSrc.resolve("src");
  73     private final Path mods = Paths.get("mods");
  74     private final Path jmods = Paths.get("jmods");
  75     private final String[] modules = new String[] { "m1", "m2", "m3"};
  76 
  77     private static Method hashesMethod;
  78     @BeforeTest
  79     private void setup() throws Exception {
  80         if (Files.exists(jmods)) {
  81             deleteDirectory(jmods);
  82         }
  83         Files.createDirectories(jmods);
  84 
  85         // build m2, m3 required by m1
  86         compileModule("m2", modSrc);
  87         jmod("m2");
  88 
  89         compileModule("m3", modSrc);


 191         Path msrc = src.resolve(moduleName);
 192         assertTrue(CompilerUtils.compile(msrc, mods, "--module-source-path", src.toString()));
 193     }
 194 
 195     private void jmod(String moduleName, String... options) throws IOException {
 196         Path mclasses = mods.resolve(moduleName);
 197         Path outfile = jmods.resolve(moduleName + ".jmod");
 198         List<String> args = new ArrayList<>();
 199         args.add("create");
 200         Collections.addAll(args, options);
 201         Collections.addAll(args, "--class-path", mclasses.toString(),
 202                            outfile.toString());
 203 
 204         if (Files.exists(outfile))
 205             Files.delete(outfile);
 206 
 207         runJmod(args);
 208     }
 209 
 210     private void runJmod(List<String> args) {
 211         int rc = JMOD_TOOL.run(System.out, System.out, args.toArray(new String[args.size()]));
 212         System.out.println("jmod options: " + args.stream().collect(Collectors.joining(" ")));
 213         if (rc != 0) {
 214             throw new AssertionError("Jmod failed: rc = " + rc);
 215         }
 216     }
 217 }
< prev index next >