private static Builder toBuilder(ModuleDescriptor descriptor) { String name = descriptor.name(); Set<Modifier> modifiers = descriptor.isAutomatic()? Set.of(Modifier.AUTOMATIC): descriptor.isOpen()? Set.of(Modifier.OPEN): Set.of(); Builder builder = ModuleDescriptor.newModule(name, modifiers); descriptor.requires().forEach(builder::requires); descriptor.exports().forEach(builder::exports); descriptor.opens().forEach(builder::opens); HashSet<String> packages = new HashSet<>(descriptor.packages()); descriptor.exports().forEach(export -> packages.remove(export.source())); descriptor.opens().forEach(open -> packages.remove(open.source())); builder.packages(packages); descriptor.uses().forEach(builder::uses); descriptor.provides().forEach(builder::provides); return builder; }
/** * Creates Unnamed/modular jar files for TestClient and TestClassLoader. */ private static void setUp() throws Exception { if (ARTIFACT_DIR.toFile().exists()) { System.out.println("Skipping setup: Artifacts already exists."); return; } // Generate unnamed handler jar file. JarUtils.createJarFile(H_JAR, TEST_CLASSES, "handler/TestCallbackHandler.class"); // Generate unnamed client jar file. JarUtils.createJarFile(C_JAR, TEST_CLASSES, "login/TestLoginModule.class", "login/JaasClientWithDefaultHandler.class"); Builder mBuilder = ModuleDescriptor.newModule("mh"); // Modular jar exports package to let the handler type accessible. generateJar(H_JAR, MH_JAR, mBuilder.exports("handler").build()); mBuilder = ModuleDescriptor.newModule("mc").exports("login") .requires("jdk.security.auth"); // Generate modular client jar file to use automatic handler jar. generateJar(C_JAR, AMC_JAR, mBuilder.build()); // Generate modular client jar file to use modular handler jar. generateJar(C_JAR, MC_JAR, mBuilder.requires("mh").build()); }
public void testExportsToTargets() { Set<String> targets = new HashSet<>(); targets.add("bar"); targets.add("gus"); Exports e = new Builder("foo") .exports("p", targets) .build() .exports() .iterator() .next(); assertEquals(e, e); assertEquals(e.source(), "p"); assertTrue(e.isQualified()); assertTrue(e.targets().size() == 2); assertTrue(e.targets().contains("bar")); assertTrue(e.targets().contains("gus")); }
public void testProvides() { Set<String> pns = new HashSet<>(); pns.add("q.P1"); pns.add("q.P2"); Map<String, Provides> map = new Builder("foo") .provides("p.S", pns) .build() .provides(); assertTrue(map.size() == 1); Provides p = map.values().iterator().next(); assertEquals(p, p); assertTrue(p.providers().size() == 2); assertTrue(p.providers().contains("q.P1")); assertTrue(p.providers().contains("q.P2")); }
private ModuleDescriptor newModule(String name, String vs) { JavaLangModuleAccess JLMA = SharedSecrets.getJavaLangModuleAccess(); Builder builder = JLMA.newModuleBuilder(name, false, Set.of()); if (vs != null) builder.version(vs); builder.requires("java.base"); ByteBuffer bb = ModuleInfoWriter.toByteBuffer(builder.build()); return ModuleDescriptor.read(bb); }
private Requires requires(Set<Modifier> mods, String mn) { return new Builder("m") .requires(mods, mn) .build() .requires() .iterator() .next(); }
private Exports exports(String pn) { return new Builder("foo") .exports(pn) .build() .exports() .iterator() .next(); }
private Exports exports(String pn, String target) { return new Builder("foo") .exports(pn, target) .build() .exports() .iterator() .next(); }
public void testExportsToString() { String s = new Builder("foo") .exports("p1", "bar") .build() .exports() .iterator() .next() .toString(); assertTrue(s.contains("p1")); assertTrue(s.contains("bar")); }
public void testUses() { Set<String> uses = new Builder("foo") .uses("p.S") .uses("q.S") .build() .uses(); assertTrue(uses.size() == 2); assertTrue(uses.contains("p.S")); assertTrue(uses.contains("q.S")); }
private Provides provides(String st, String pc) { return new Builder("foo") .provides("p.S", pc) .build() .provides() .values() .iterator() .next(); }
public void testConceals() { Set<String> conceals = new Builder("foo").conceals("p").conceals("q").build().conceals(); assertTrue(conceals.size() == 2); assertTrue(conceals.contains("p")); assertTrue(conceals.contains("q")); }
public void testPackages() { Set<String> packages = new Builder("foo").exports("p").conceals("q").build().packages(); assertTrue(packages.size() == 2); assertTrue(packages.contains("p")); assertTrue(packages.contains("q")); }
public void testCompare() { ModuleDescriptor md1 = new Builder("foo").build(); ModuleDescriptor md2 = new Builder("bar").build(); int n = "foo".compareTo("bar"); assertTrue(md1.compareTo(md2) == n); assertTrue(md2.compareTo(md1) == -n); }
void addExport(Builder builder) { if (friends.isEmpty()) { builder.exports(name); } else { builder.exports(name, friends); } }
private static ModuleDescriptor createModuleDescriptor(ModuleNode moduleNode, Path moduleInfoPath) { Set<Modifier> modifiers = (moduleNode.access & ACC_OPEN) != 0? Set.of(Modifier.OPEN): Set.of(); ModuleDescriptor.Builder builder = ModuleDescriptor.newModule(moduleNode.name, modifiers); moduleNode.requires.forEach(require -> builder.requires(requireModifiers(require.access), require.module)); moduleNode.exports.forEach(export -> { if (export.modules.isEmpty()) { builder.exports(export.packaze); } else { builder.exports(export.packaze, export.modules.stream().collect(toSet())); } }); moduleNode.opens.forEach(open -> { if (open.modules.isEmpty()) { builder.opens(open.packaze); } else { builder.opens(open.packaze, open.modules.stream().collect(toSet())); } }); moduleNode.uses.forEach(builder::uses); moduleNode.provides.forEach(provide -> builder.provides(provide.service, provide.providers)); Path moduleDirectory = moduleInfoPath.getParent(); Set<String> javaPackages = findJavaPackages(moduleDirectory); javaPackages.removeAll(moduleNode.exports.stream().map(export -> export.packaze).collect(Collectors.toList())); javaPackages.removeAll(moduleNode.opens.stream().map(export -> export.packaze).collect(Collectors.toList())); builder.packages(javaPackages); ModuleDescriptor descriptor = builder.build(); //System.out.println(descriptor.name() + " " + descriptor.packages()); return descriptor; }
/** * Creates Unnamed/modular jar files for TestClient and TestClassLoader. */ private static void setUp() throws Exception { if (ARTIFACT_DIR.toFile().exists()) { System.out.println("Skipping setup: Artifacts already exists."); return; } // Generate unnamed provider jar file. JarUtils.createJarFile(P_JAR, TEST_CLASSES, "p/TestProvider.class"); // Generate unnamed client jar file. JarUtils.createJarFile(C_JAR, TEST_CLASSES, "c/TestClient.class"); // Generate unnamed provider jar files with META-INF descriptor. generateJar(P_JAR, PD_JAR, null, true); Builder mBuilder = ModuleDescriptor.newModule("mp").exports("p"); // Modular provider defined as META-INF service. generateJar(P_JAR, MPD_JAR, mBuilder.build(), true); // Modular jar exports package to let the provider type accessible. generateJar(P_JAR, MP_JAR, mBuilder.build(), false); mBuilder = ModuleDescriptor.newModule("mp") .provides("java.security.Provider", Arrays.asList(P_TYPE)); // Modular provider Service in module-info does not need to export // its package. generateJar(P_JAR, MSP_JAR, mBuilder.build(), false); // Modular provider Service in module-info also have META-INF descriptor generateJar(P_JAR, MSPD_JAR, mBuilder.build(), true); mBuilder = ModuleDescriptor.newModule("mc").exports("c"); // Generate modular client jar file to use automatic provider jar. generateJar(C_JAR, AMC_JAR, mBuilder.build(), false); // Generate modular client jar file to use modular provider jar. generateJar(C_JAR, MC_JAR, mBuilder.requires("mp").build(), false); mBuilder = ModuleDescriptor.newModule("mc").exports("c") .uses("java.security.Provider"); // Generate modular client jar file to use automatic provider service. generateJar(C_JAR, AMCS_JAR, mBuilder.build(), false); // Generate modular client jar file using modular provider service. generateJar(C_JAR, MCS_JAR, mBuilder.requires("mp").build(), false); }
/** * Returns a Builder that does not validate module names. */ private Builder newBuilder(String mn) { return SharedSecrets.getJavaLangModuleAccess() .newModuleBuilder(mn, false, Set.of()); }
@Test(dataProvider = "invalidNames", expectedExceptions = IllegalArgumentException.class ) public void testMainClassWithBadName(String mainClass, String ignore) { Builder builder = ModuleDescriptor.newModule("foo"); builder.mainClass(mainClass); }
/** * Creates Unnamed/modular jar files for TestClient and TestClassLoader. */ private static void setUp() throws Exception { if (ARTIFACT_DIR.toFile().exists()) { System.out.println("Skipping setup: Artifacts already exists."); return; } // Generate unnamed login module jar file. JarUtils.createJarFile(L_JAR, TEST_CLASSES, "login/TestLoginModule.class"); // Generate unnamed client jar. JarUtils.createJarFile(C_JAR, TEST_CLASSES, "client/JaasClient.class", "client/JaasClient$MyCallbackHandler.class"); Builder mBuilder = ModuleDescriptor.newModule("ml") .requires("jdk.security.auth"); // Modular jar exports package to let the login module type accessible. generateJar(L_JAR, ML_JAR, mBuilder.exports("login").build()); mBuilder = ModuleDescriptor.newModule("ml") .requires("jdk.security.auth") .provides("javax.security.auth.spi.LoginModule", Arrays.asList(L_TYPE)); // Modular login module as Service in module-info does not need to // export service package. generateJar(L_JAR, MSL_JAR, mBuilder.build()); mBuilder = ModuleDescriptor.newModule("mc").exports("client") .requires("jdk.security.auth"); // Generate modular client jar to use automatic login module jar. generateJar(C_JAR, AMC_JAR, mBuilder.build()); // Generate modular client jar to use modular login module jar. generateJar(C_JAR, MC_JAR, mBuilder.requires("ml").build()); mBuilder = ModuleDescriptor.newModule("mc").exports("client") .requires("jdk.security.auth") .uses("javax.security.auth.spi.LoginModule"); // Generate modular client jar to use automatic login module service. generateJar(C_JAR, AMCS_JAR, mBuilder.build()); // Generate modular client jar using modular login module service. generateJar(C_JAR, MCS_JAR, mBuilder.requires("ml").build()); }
public void testRequiresWithRequires() { Requires r1 = requires(null, "foo"); ModuleDescriptor descriptor = new Builder("m").requires(r1).build(); Requires r2 = descriptor.requires().iterator().next(); assertEquals(r1, r2); }
@Test(expectedExceptions = IllegalStateException.class) public void testRequiresWithDuplicatesRequires() { Requires r = requires(null, "foo"); new Builder("m").requires(r).requires(r); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testRequiresSelfWithRequires() { Requires r = requires(null, "m"); new Builder("m").requires(r); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testRequiresSelfWithNoModifier() { new Builder("m").requires("m"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testRequiresSelfWithOneModifier() { new Builder("m").requires(PUBLIC, "m"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testRequiresSelfWithAllModifiers() { new Builder("m").requires(EnumSet.allOf(Modifier.class), "m"); }
@Test(expectedExceptions = NullPointerException.class) public void testRequiresWithNullRequires() { new Builder("m").requires((Requires) null); }
public void testExportsExports() { Exports e1 = exports("p"); ModuleDescriptor descriptor = new Builder("m").exports(e1).build(); Exports e2 = descriptor.exports().iterator().next(); assertEquals(e1, e2); }
@Test(expectedExceptions = IllegalStateException.class) public void testExportsWithDuplicate1() { Exports e = exports("p"); new Builder("foo").exports(e).exports(e); }
@Test(expectedExceptions = IllegalStateException.class) public void testExportsWithDuplicate2() { new Builder("foo").exports("p").exports("p"); }
@Test(expectedExceptions = IllegalStateException.class) public void testExportsWithConcealedPackage() { new Builder("foo").conceals("p").exports("p"); }
@Test(expectedExceptions = IllegalStateException.class) public void testExportsToTargetWithConcealedPackage() { new Builder("foo").conceals("p").exports("p", "bar"); }
@Test(expectedExceptions = IllegalArgumentException.class ) public void testExportsWithEmptySet() { new Builder("foo").exports("p", Collections.emptySet()); }
@Test(dataProvider = "invalidjavaidentifiers", expectedExceptions = IllegalArgumentException.class ) public void testExportsWithBadName(String pn, String ignore) { new Builder("foo").exports(pn); }
@Test(expectedExceptions = NullPointerException.class ) public void testExportsWithNullExports() { new Builder("foo").exports((Exports)null); }
@Test(expectedExceptions = IllegalArgumentException.class ) public void testExportsWithNullTarget() { new Builder("foo").exports("p", (String) null); }
@Test(expectedExceptions = NullPointerException.class ) public void testExportsWithNullTargets() { new Builder("foo").exports("p", (Set<String>) null); }
@Test(expectedExceptions = IllegalStateException.class) public void testUsesWithDuplicate() { new Builder("foo").uses("p.S").uses("p.S"); }
@Test(dataProvider = "invalidjavaidentifiers", expectedExceptions = IllegalArgumentException.class ) public void testUsesWithBadName(String service, String ignore) { new Builder("foo").uses(service); }