jdk/test/java/lang/module/ModuleDescriptorTest.java
changeset 45004 ea3137042a61
parent 44545 83b611b88ac8
equal deleted inserted replaced
44789:73fd39e0702e 45004:ea3137042a61
    63 import static org.testng.Assert.*;
    63 import static org.testng.Assert.*;
    64 
    64 
    65 @Test
    65 @Test
    66 public class ModuleDescriptorTest {
    66 public class ModuleDescriptorTest {
    67 
    67 
    68     @DataProvider(name = "invalidjavaidentifiers")
    68     @DataProvider(name = "invalidNames")
    69     public Object[][] invalidJavaIdentifiers() {
    69     public Object[][] invalidNames() {
    70         return new Object[][]{
    70         return new Object[][]{
    71 
    71 
    72             { null,             null },
    72             { null,             null },
    73             { "1",              null },
    73             { "1",              null },
    74             { "1foo",           null },
    74             { "1foo",           null },
    81             { "foo.[bar]",      null },
    81             { "foo.[bar]",      null },
    82             { "foo..bar",       null },
    82             { "foo..bar",       null },
    83             { "foo.bar.1",      null },
    83             { "foo.bar.1",      null },
    84             { "foo.bar.1gus",   null },
    84             { "foo.bar.1gus",   null },
    85             { "foo.bar.[gus]",  null },
    85             { "foo.bar.[gus]",  null },
       
    86 
       
    87             { "class",          null },
       
    88             { "interface",      null },
       
    89             { "true",           null },
       
    90             { "false",          null },
       
    91             { "null",           null },
       
    92 
       
    93             { "x.class",        null },
       
    94             { "x.interface",    null },
       
    95             { "x.true",         null },
       
    96             { "x.false",        null },
       
    97             { "x.null",         null },
       
    98 
       
    99             { "class.x",        null },
       
   100             { "interface.x",    null },
       
   101             { "true.x",         null },
       
   102             { "false.x",        null },
       
   103             { "null.x",         null },
       
   104 
       
   105             { "x.class.x",      null },
       
   106             { "x.interface.x",  null },
       
   107             { "x.true.x",       null },
       
   108             { "x.false.x",      null },
       
   109             { "x.null.x",       null },
       
   110 
       
   111             { "_",              null },
    86 
   112 
    87         };
   113         };
    88     }
   114     }
    89 
   115 
    90 
   116 
   197     @Test(expectedExceptions = IllegalArgumentException.class)
   223     @Test(expectedExceptions = IllegalArgumentException.class)
   198     public void testRequiresSelfWithAllModifiers() {
   224     public void testRequiresSelfWithAllModifiers() {
   199         ModuleDescriptor.newModule("m").requires(EnumSet.allOf(Modifier.class), "m");
   225         ModuleDescriptor.newModule("m").requires(EnumSet.allOf(Modifier.class), "m");
   200     }
   226     }
   201 
   227 
   202     @Test(dataProvider = "invalidjavaidentifiers",
   228     @Test(dataProvider = "invalidNames",
   203           expectedExceptions = IllegalArgumentException.class )
   229           expectedExceptions = IllegalArgumentException.class )
   204     public void testRequiresWithBadModuleName(String mn, String ignore) {
   230     public void testRequiresWithBadModuleName(String mn, String ignore) {
   205         requires(EnumSet.noneOf(Modifier.class), mn);
   231         requires(EnumSet.noneOf(Modifier.class), mn);
   206     }
   232     }
   207 
   233 
   404     @Test(expectedExceptions = IllegalArgumentException.class )
   430     @Test(expectedExceptions = IllegalArgumentException.class )
   405     public void testExportsWithEmptySet() {
   431     public void testExportsWithEmptySet() {
   406         ModuleDescriptor.newModule("foo").exports("p", Collections.emptySet());
   432         ModuleDescriptor.newModule("foo").exports("p", Collections.emptySet());
   407     }
   433     }
   408 
   434 
   409     @Test(dataProvider = "invalidjavaidentifiers",
   435     @Test(dataProvider = "invalidNames",
   410           expectedExceptions = IllegalArgumentException.class )
   436           expectedExceptions = IllegalArgumentException.class )
   411     public void testExportsWithBadName(String pn, String ignore) {
   437     public void testExportsWithBadName(String pn, String ignore) {
   412         ModuleDescriptor.newModule("foo").exports(pn);
   438         ModuleDescriptor.newModule("foo").exports(pn);
   413     }
   439     }
   414 
   440 
   566     @Test(expectedExceptions = IllegalArgumentException.class )
   592     @Test(expectedExceptions = IllegalArgumentException.class )
   567     public void testOpensWithEmptySet() {
   593     public void testOpensWithEmptySet() {
   568         ModuleDescriptor.newModule("foo").opens("p", Collections.emptySet());
   594         ModuleDescriptor.newModule("foo").opens("p", Collections.emptySet());
   569     }
   595     }
   570 
   596 
   571     @Test(dataProvider = "invalidjavaidentifiers",
   597     @Test(dataProvider = "invalidNames",
   572             expectedExceptions = IllegalArgumentException.class )
   598             expectedExceptions = IllegalArgumentException.class )
   573     public void testOpensWithBadName(String pn, String ignore) {
   599     public void testOpensWithBadName(String pn, String ignore) {
   574         ModuleDescriptor.newModule("foo").opens(pn);
   600         ModuleDescriptor.newModule("foo").opens(pn);
   575     }
   601     }
   576 
   602 
   662     @Test(expectedExceptions = IllegalArgumentException.class)
   688     @Test(expectedExceptions = IllegalArgumentException.class)
   663     public void testUsesWithSimpleIdentifier() {
   689     public void testUsesWithSimpleIdentifier() {
   664         ModuleDescriptor.newModule("foo").uses("S");
   690         ModuleDescriptor.newModule("foo").uses("S");
   665     }
   691     }
   666 
   692 
   667     @Test(dataProvider = "invalidjavaidentifiers",
   693     @Test(dataProvider = "invalidNames",
   668           expectedExceptions = IllegalArgumentException.class )
   694           expectedExceptions = IllegalArgumentException.class )
   669     public void testUsesWithBadName(String service, String ignore) {
   695     public void testUsesWithBadName(String service, String ignore) {
   670         ModuleDescriptor.newModule("foo").uses(service);
   696         ModuleDescriptor.newModule("foo").uses(service);
   671     }
   697     }
   672 
   698 
   735     @Test(expectedExceptions = IllegalArgumentException.class )
   761     @Test(expectedExceptions = IllegalArgumentException.class )
   736     public void testProvidesWithSimpleIdentifier2() {
   762     public void testProvidesWithSimpleIdentifier2() {
   737         ModuleDescriptor.newModule("foo").provides("p.S", List.of("P"));
   763         ModuleDescriptor.newModule("foo").provides("p.S", List.of("P"));
   738     }
   764     }
   739 
   765 
   740     @Test(dataProvider = "invalidjavaidentifiers",
   766     @Test(dataProvider = "invalidNames",
   741           expectedExceptions = IllegalArgumentException.class )
   767           expectedExceptions = IllegalArgumentException.class )
   742     public void testProvidesWithBadService(String service, String ignore) {
   768     public void testProvidesWithBadService(String service, String ignore) {
   743         ModuleDescriptor.newModule("foo").provides(service, List.of("p.Provider"));
   769         ModuleDescriptor.newModule("foo").provides(service, List.of("p.Provider"));
   744     }
   770     }
   745 
   771 
   746     @Test(dataProvider = "invalidjavaidentifiers",
   772     @Test(dataProvider = "invalidNames",
   747           expectedExceptions = IllegalArgumentException.class )
   773           expectedExceptions = IllegalArgumentException.class )
   748     public void testProvidesWithBadProvider(String provider, String ignore) {
   774     public void testProvidesWithBadProvider(String provider, String ignore) {
   749         List<String> names = new ArrayList<>(); // allows nulls
   775         List<String> names = new ArrayList<>(); // allows nulls
   750         names.add(provider);
   776         names.add(provider);
   751         ModuleDescriptor.newModule("foo").provides("p.Service", names);
   777         ModuleDescriptor.newModule("foo").provides("p.Service", names);
   926                 .build()
   952                 .build()
   927                 .packages();
   953                 .packages();
   928         assertTrue(Objects.equals(packages, Set.of("p1", "p2", "p3", "p4", "p5")));
   954         assertTrue(Objects.equals(packages, Set.of("p1", "p2", "p3", "p4", "p5")));
   929     }
   955     }
   930 
   956 
   931     @Test(dataProvider = "invalidjavaidentifiers",
   957     @Test(dataProvider = "invalidNames",
   932           expectedExceptions = IllegalArgumentException.class )
   958           expectedExceptions = IllegalArgumentException.class )
   933     public void testPackagesWithBadName(String pn, String ignore) {
   959     public void testPackagesWithBadName(String pn, String ignore) {
   934         Set<String> pkgs = new HashSet<>();  // allows nulls
   960         Set<String> pkgs = new HashSet<>();  // allows nulls
   935         pkgs.add(pn);
   961         pkgs.add(pn);
   936         ModuleDescriptor.newModule("foo").packages(pkgs);
   962         ModuleDescriptor.newModule("foo").packages(pkgs);
   941     public void testModuleName() {
   967     public void testModuleName() {
   942         String mn = ModuleDescriptor.newModule("foo").build().name();
   968         String mn = ModuleDescriptor.newModule("foo").build().name();
   943         assertEquals(mn, "foo");
   969         assertEquals(mn, "foo");
   944     }
   970     }
   945 
   971 
   946     @Test(dataProvider = "invalidjavaidentifiers",
   972     @Test(dataProvider = "invalidNames",
   947           expectedExceptions = IllegalArgumentException.class )
   973           expectedExceptions = IllegalArgumentException.class )
   948     public void testBadModuleName(String mn, String ignore) {
   974     public void testBadModuleName(String mn, String ignore) {
   949         ModuleDescriptor.newModule(mn);
   975         ModuleDescriptor.newModule(mn);
   950     }
   976     }
   951 
   977 
  1262     @Test(expectedExceptions = IllegalArgumentException.class)
  1288     @Test(expectedExceptions = IllegalArgumentException.class)
  1263     public void testMainClassWithSimpleIdentifier() {
  1289     public void testMainClassWithSimpleIdentifier() {
  1264         ModuleDescriptor.newModule("foo").mainClass("Main");
  1290         ModuleDescriptor.newModule("foo").mainClass("Main");
  1265     }
  1291     }
  1266 
  1292 
  1267     @Test(dataProvider = "invalidjavaidentifiers",
  1293     @Test(dataProvider = "invalidNames",
  1268           expectedExceptions = IllegalArgumentException.class )
  1294           expectedExceptions = IllegalArgumentException.class )
  1269     public void testMainClassWithBadName(String mainClass, String ignore) {
  1295     public void testMainClassWithBadName(String mainClass, String ignore) {
  1270         Builder builder = ModuleDescriptor.newModule("foo");
  1296         Builder builder = ModuleDescriptor.newModule("foo");
  1271         builder.mainClass(mainClass);
  1297         builder.mainClass(mainClass);
  1272     }
  1298     }