From a802b9816ac5c0cb0fd236cc7f25ed4fdb1349ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thiago=20Henrique=20H=C3=BCpner?= Date: Thu, 23 Jun 2022 09:41:06 +0000 Subject: [PATCH] 8287760: --do-not-resolve-by-default gets overwritten if --warn-if-resolved flags is used Reviewed-by: lancea, alanb, cstein --- .../sun/tools/jar/GNUStyleOptions.java | 4 +- test/jdk/tools/jar/modularJar/Basic.java | 138 +++++++++++++++++- 2 files changed, 139 insertions(+), 3 deletions(-) diff --git a/src/jdk.jartool/share/classes/sun/tools/jar/GNUStyleOptions.java b/src/jdk.jartool/share/classes/sun/tools/jar/GNUStyleOptions.java index 8686210b209bd..b526f80cb35ca 100644 --- a/src/jdk.jartool/share/classes/sun/tools/jar/GNUStyleOptions.java +++ b/src/jdk.jartool/share/classes/sun/tools/jar/GNUStyleOptions.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -178,7 +178,7 @@ void process(Main jartool, String opt, String arg) { void process(Main jartool, String opt, String arg) throws BadArgs { ModuleResolution mres = ModuleResolution.empty(); if (jartool.moduleResolution.doNotResolveByDefault()) { - mres.withDoNotResolveByDefault(); + mres = mres.withDoNotResolveByDefault(); } if (arg.equals("deprecated")) { jartool.moduleResolution = mres.withDeprecated(); diff --git a/test/jdk/tools/jar/modularJar/Basic.java b/test/jdk/tools/jar/modularJar/Basic.java index 177ae2a84534d..80e4e96a2b676 100644 --- a/test/jdk/tools/jar/modularJar/Basic.java +++ b/test/jdk/tools/jar/modularJar/Basic.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,12 +23,14 @@ import java.io.*; import java.lang.module.ModuleDescriptor; +import java.lang.module.ModuleFinder; import java.lang.reflect.Method; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; import java.util.function.Consumer; +import java.util.function.Predicate; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarInputStream; @@ -38,6 +40,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import jdk.internal.module.ModuleReferenceImpl; +import jdk.internal.module.ModuleResolution; import jdk.test.lib.util.FileUtils; import jdk.test.lib.JDKToolFinder; import org.testng.annotations.BeforeTest; @@ -53,6 +57,7 @@ * @library /test/lib * @modules jdk.compiler * jdk.jartool + * java.base/jdk.internal.module * @build jdk.test.lib.Platform * jdk.test.lib.util.FileUtils * jdk.test.lib.JDKToolFinder @@ -942,6 +947,137 @@ public void updateFooModuleVersion() throws IOException { } } + @DataProvider(name = "resolutionWarnings") + public Object[][] resolutionWarnings() { + return new Object[][] { + {"incubating", (Predicate) ModuleResolution::hasIncubatingWarning}, + {"deprecated", (Predicate) ModuleResolution::hasDeprecatedWarning}, + {"deprecated-for-removal", + (Predicate) ModuleResolution::hasDeprecatedForRemovalWarning} + }; + } + + /** + * Validate that you can create a jar only specifying --warn-if-resolved + * @throws IOException + */ + @Test(dataProvider = "resolutionWarnings") + public void shouldAddWarnIfResolved(String resolutionName, + Predicate hasWarning) throws IOException { + Path mp = Paths.get("moduleWarnIfResolved-" + resolutionName); + createTestDir(mp); + Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); + Path modularJar = mp.resolve(FOO.moduleName + ".jar"); + + jar("--create", + "--file=" + modularJar.toString(), + "--main-class=" + FOO.mainClass, + "--warn-if-resolved=" + resolutionName, + "--no-manifest", + "-C", modClasses.toString(), ".") + .assertSuccess(); + + ModuleReferenceImpl moduleReference = ModuleFinder.of(modularJar) + .find(FOO.moduleName) + .map(ModuleReferenceImpl.class::cast) + .orElseThrow(); + ModuleResolution moduleResolution = moduleReference.moduleResolution(); + + assertTrue(hasWarning.test(moduleResolution)); + } + + /** + * Validate that you can create a jar only specifying --do-not-resolve-by-default + * @throws IOException + */ + @Test + public void shouldAddDoNotResolveByDefault() throws IOException { + Path mp = Paths.get("moduleDoNotResolveByDefault"); + createTestDir(mp); + Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); + Path modularJar = mp.resolve(FOO.moduleName + ".jar"); + + jar("--create", + "--file=" + modularJar.toString(), + "--main-class=" + FOO.mainClass, + "--do-not-resolve-by-default", + "--no-manifest", + "-C", modClasses.toString(), ".") + .assertSuccess(); + + ModuleReferenceImpl moduleReference = ModuleFinder.of(modularJar) + .find(FOO.moduleName) + .map(ModuleReferenceImpl.class::cast) + .orElseThrow(); + ModuleResolution moduleResolution = moduleReference.moduleResolution(); + + assertTrue(moduleResolution.doNotResolveByDefault()); + } + + /** + * Validate that you can create a jar specifying --warn-if-resolved and + * --do-not-resolve-by-default + * @throws IOException + */ + @Test(dataProvider = "resolutionWarnings") + public void shouldAddWarnIfResolvedAndDoNotResolveByDefault(String resolutionName, + Predicate hasWarning) throws IOException { + Path mp = Paths.get("moduleResolutionWarnThenNotResolve-" + resolutionName); + createTestDir(mp); + Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); + Path modularJar = mp.resolve(FOO.moduleName + ".jar"); + + jar("--create", + "--file=" + modularJar.toString(), + "--main-class=" + FOO.mainClass, + "--warn-if-resolved=" + resolutionName, + "--do-not-resolve-by-default", + "--no-manifest", + "-C", modClasses.toString(), ".") + .assertSuccess(); + + ModuleReferenceImpl moduleReference = ModuleFinder.of(modularJar) + .find(FOO.moduleName) + .map(ModuleReferenceImpl.class::cast) + .orElseThrow(); + ModuleResolution moduleResolution = moduleReference.moduleResolution(); + + assertTrue(hasWarning.test(moduleResolution)); + assertTrue(moduleResolution.doNotResolveByDefault()); + } + + /** + * Validate that you can create a jar specifying --do-not-resolve-by-default and + * --warn-if-resolved + * @throws IOException + */ + @Test(dataProvider = "resolutionWarnings") + public void shouldAddResolutionDoNotResolveByDefaultAndWarnIfResolved(String resolutionName, + Predicate hasWarning) throws IOException { + Path mp = Paths.get("moduleResolutionNotResolveThenWarn-" + resolutionName); + createTestDir(mp); + Path modClasses = MODULE_CLASSES.resolve(FOO.moduleName); + Path modularJar = mp.resolve(FOO.moduleName + ".jar"); + + jar("--create", + "--file=" + modularJar.toString(), + "--main-class=" + FOO.mainClass, + "--do-not-resolve-by-default", + "--warn-if-resolved=" + resolutionName, + "--no-manifest", + "-C", modClasses.toString(), ".") + .assertSuccess(); + + ModuleReferenceImpl moduleReference = ModuleFinder.of(modularJar) + .find(FOO.moduleName) + .map(ModuleReferenceImpl.class::cast) + .orElseThrow(); + ModuleResolution moduleResolution = moduleReference.moduleResolution(); + + assertTrue(hasWarning.test(moduleResolution)); + assertTrue(moduleResolution.doNotResolveByDefault()); + } + @DataProvider(name = "autoNames") public Object[][] autoNames() { return new Object[][] {