< prev index next >

test/tools/jlink/bindservices/SuggestProviders.java

Print this page




  43  * @test
  44  * @bug 8174826
  45  * @library /lib/testlibrary
  46  * @modules jdk.charsets jdk.compiler jdk.jlink
  47  * @build SuggestProviders CompilerUtils
  48  * @run testng SuggestProviders
  49  */
  50 
  51 public class SuggestProviders {
  52     private static final String JAVA_HOME = System.getProperty("java.home");
  53     private static final String TEST_SRC = System.getProperty("test.src");
  54 
  55     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  56     private static final Path MODS_DIR = Paths.get("mods");
  57 
  58     private static final String MODULE_PATH =
  59         Paths.get(JAVA_HOME, "jmods").toString() +
  60         File.pathSeparator + MODS_DIR.toString();
  61 
  62     // the names of the modules in this test
  63     private static String[] modules = new String[] {"m1", "m2", "m3"};
  64 
  65 
  66     private static boolean hasJmods() {
  67         if (!Files.exists(Paths.get(JAVA_HOME, "jmods"))) {
  68             System.err.println("Test skipped. NO jmods directory");
  69             return false;
  70         }
  71         return true;
  72     }
  73 
  74     /*
  75      * Compiles all modules used by the test
  76      */
  77     @BeforeTest
  78     public void compileAll() throws Throwable {
  79         if (!hasJmods()) return;
  80 
  81         for (String mn : modules) {
  82             Path msrc = SRC_DIR.resolve(mn);
  83             assertTrue(CompilerUtils.compile(msrc, MODS_DIR,


 154 
 155         assertTrue(output.containsAll(expected));
 156     }
 157 
 158     @Test
 159     public void noSuggestProviders() throws Throwable {
 160         if (!hasJmods()) return;
 161 
 162         List<String> output =
 163             JLink.run("--module-path", MODULE_PATH,
 164                       "--add-modules", "m1",
 165                       "--bind-services",
 166                       "--limit-modules", "m1,m2,m3,java.base",
 167                       "--suggest-providers").output();
 168 
 169         String expected = "--bind-services option is specified. No additional providers suggested.";
 170         assertTrue(output.contains(expected));
 171 
 172     }
 173 























































 174     static class JLink {
 175         static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
 176             .orElseThrow(() ->
 177                 new RuntimeException("jlink tool not found")
 178             );
 179 
 180         static JLink run(String... options) {
 181             JLink jlink = new JLink();
 182             assertTrue(jlink.execute(options) == 0);
 183             return jlink;
 184         }
 185 







 186         final List<String> output = new ArrayList<>();
 187         private int execute(String... options) {
 188             System.out.println("jlink " +
 189                 Stream.of(options).collect(Collectors.joining(" ")));
 190 
 191             StringWriter writer = new StringWriter();
 192             PrintWriter pw = new PrintWriter(writer);
 193             int rc = JLINK_TOOL.run(pw, pw, options);
 194             System.out.println(writer.toString());
 195             Stream.of(writer.toString().split("\\v"))
 196                   .map(String::trim)
 197                   .forEach(output::add);
 198             return rc;
 199         }
 200 
 201         boolean contains(String s) {
 202             return output.contains(s);
 203         }
 204 
 205         List<String> output() {


  43  * @test
  44  * @bug 8174826
  45  * @library /lib/testlibrary
  46  * @modules jdk.charsets jdk.compiler jdk.jlink
  47  * @build SuggestProviders CompilerUtils
  48  * @run testng SuggestProviders
  49  */
  50 
  51 public class SuggestProviders {
  52     private static final String JAVA_HOME = System.getProperty("java.home");
  53     private static final String TEST_SRC = System.getProperty("test.src");
  54 
  55     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  56     private static final Path MODS_DIR = Paths.get("mods");
  57 
  58     private static final String MODULE_PATH =
  59         Paths.get(JAVA_HOME, "jmods").toString() +
  60         File.pathSeparator + MODS_DIR.toString();
  61 
  62     // the names of the modules in this test
  63     private static String[] modules = new String[] {"m1", "m2", "m3", "m4"};
  64 
  65 
  66     private static boolean hasJmods() {
  67         if (!Files.exists(Paths.get(JAVA_HOME, "jmods"))) {
  68             System.err.println("Test skipped. NO jmods directory");
  69             return false;
  70         }
  71         return true;
  72     }
  73 
  74     /*
  75      * Compiles all modules used by the test
  76      */
  77     @BeforeTest
  78     public void compileAll() throws Throwable {
  79         if (!hasJmods()) return;
  80 
  81         for (String mn : modules) {
  82             Path msrc = SRC_DIR.resolve(mn);
  83             assertTrue(CompilerUtils.compile(msrc, MODS_DIR,


 154 
 155         assertTrue(output.containsAll(expected));
 156     }
 157 
 158     @Test
 159     public void noSuggestProviders() throws Throwable {
 160         if (!hasJmods()) return;
 161 
 162         List<String> output =
 163             JLink.run("--module-path", MODULE_PATH,
 164                       "--add-modules", "m1",
 165                       "--bind-services",
 166                       "--limit-modules", "m1,m2,m3,java.base",
 167                       "--suggest-providers").output();
 168 
 169         String expected = "--bind-services option is specified. No additional providers suggested.";
 170         assertTrue(output.contains(expected));
 171 
 172     }
 173 
 174     @Test
 175     public void suggestTypeNotRealProvider() throws Throwable {
 176         if (!hasJmods()) return;
 177 
 178         List<String> output =
 179                 JLink.run("--module-path", MODULE_PATH,
 180                           "--add-modules", "m1",
 181                           "--suggest-providers",
 182                           "java.util.List").output();
 183 
 184         System.out.println(output);
 185         List<String> expected = List.of(
 186                 "Services specified in --suggest-providers not used: java.util.List"
 187         );
 188 
 189         assertTrue(output.containsAll(expected));
 190     }
 191 
 192     @Test
 193     public void noOneUsesProvider() throws Throwable {
 194         if (!hasJmods()) return;
 195 
 196         List<String> output =
 197                 JLink.run("--module-path", MODULE_PATH,
 198                           "--add-modules", "m4",
 199                           "--suggest-providers",
 200                           "p4.S").output();
 201 
 202         System.out.println(output);
 203         List<String> expected = List.of(
 204                 "module m4 provides p4.S, not used by any observable module"
 205         );
 206 
 207         assertTrue(output.containsAll(expected));
 208     }
 209 
 210     @Test
 211     public void nonObservableModule() throws Throwable {
 212         if (!hasJmods()) return;
 213 
 214         List<String> output =
 215                 JLink.runWithNonZeroExitCode(
 216                         "--module-path", MODULE_PATH,
 217                         "--add-modules", "nonExistentModule",
 218                         "--suggest-providers",
 219                         "java.nio.charset.spi.CharsetProvider").output();
 220 
 221         System.out.println(output);
 222         List<String> expected = List.of(
 223                 "Error: Module nonExistentModule not found"
 224         );
 225 
 226         assertTrue(output.containsAll(expected));
 227     }
 228 
 229     static class JLink {
 230         static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
 231             .orElseThrow(() ->
 232                 new RuntimeException("jlink tool not found")
 233             );
 234 
 235         static JLink run(String... options) {
 236             JLink jlink = new JLink();
 237             assertTrue(jlink.execute(options) == 0);
 238             return jlink;
 239         }
 240 
 241         static JLink runWithNonZeroExitCode(String... options) {
 242             JLink jlink = new JLink();
 243             assertNotEquals(jlink.execute(options), 0,
 244                     "Exit code should be not 0");
 245             return jlink;
 246         }
 247 
 248         final List<String> output = new ArrayList<>();
 249         private int execute(String... options) {
 250             System.out.println("jlink " +
 251                 Stream.of(options).collect(Collectors.joining(" ")));
 252 
 253             StringWriter writer = new StringWriter();
 254             PrintWriter pw = new PrintWriter(writer);
 255             int rc = JLINK_TOOL.run(pw, pw, options);
 256             System.out.println(writer.toString());
 257             Stream.of(writer.toString().split("\\v"))
 258                   .map(String::trim)
 259                   .forEach(output::add);
 260             return rc;
 261         }
 262 
 263         boolean contains(String s) {
 264             return output.contains(s);
 265         }
 266 
 267         List<String> output() {
< prev index next >