From 722dec76cbfd2277e79043f8ca7b662bc43b8b57 Mon Sep 17 00:00:00 2001 From: araknoid Date: Wed, 30 May 2018 13:50:41 +0200 Subject: [PATCH] Add java.lang.Module API code --- .../java9/modules/ModuleAPIUnitTest.java | 199 ++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 core-java-9/src/test/java/com/baeldung/java9/modules/ModuleAPIUnitTest.java diff --git a/core-java-9/src/test/java/com/baeldung/java9/modules/ModuleAPIUnitTest.java b/core-java-9/src/test/java/com/baeldung/java9/modules/ModuleAPIUnitTest.java new file mode 100644 index 0000000000..aa2fb34753 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/modules/ModuleAPIUnitTest.java @@ -0,0 +1,199 @@ +package com.baeldung.java9.modules; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.collection.IsEmptyCollection.empty; +import static org.junit.Assert.*; + +import java.lang.module.ModuleDescriptor; +import java.lang.module.ModuleDescriptor.*; +import java.sql.Date; +import java.sql.Driver; +import java.util.HashMap; +import java.util.Set; +import java.util.stream.Collectors; + +import org.junit.Before; +import org.junit.Test; + +public class ModuleAPIUnitTest { + + public static final String JAVA_BASE_MODULE_NAME = "java.base"; + + private Module javaBaseModule; + private Module javaSqlModule; + private Module module; + + @Before + public void setUp() { + Class hashMapClass = HashMap.class; + javaBaseModule = hashMapClass.getModule(); + + Class dateClass = Date.class; + javaSqlModule = dateClass.getModule(); + + Class personClass = Person.class; + module = personClass.getModule(); + } + + @Test + public void whenCheckingIfNamed_thenModuleIsNamed() { + assertThat(javaBaseModule.isNamed(), is(true)); + assertThat(javaBaseModule.getName(), is(JAVA_BASE_MODULE_NAME)); + } + + @Test + public void whenCheckingIfNamed_thenModuleIsUnnamed() { + assertThat(module.isNamed(), is(false)); + assertThat(module.getName(), is(nullValue())); + } + + @Test + public void whenExtractingPackagesContainedInAModule_thenModuleContainsOnlyFewOfThem() { + assertTrue(javaBaseModule.getPackages().contains("java.lang.annotation")); + assertFalse(javaBaseModule.getPackages().contains("java.sql")); + } + + @Test + public void whenRetrievingClassLoader_thenClassLoaderIsReturned() { + assertThat( + module.getClassLoader().getClass().getName(), + is("jdk.internal.loader.ClassLoaders$AppClassLoader") + ); + } + + @Test + public void whenGettingAnnotationsPresentOnAModule_thenNoAnnotationsArePresent() { + assertThat(javaBaseModule.getAnnotations().length, is(0)); + } + + @Test + public void whenGettingLayerOfAModule_thenModuleLayerInformationAreAvailable() { + ModuleLayer javaBaseModuleLayer = javaBaseModule.getLayer(); + + assertTrue(javaBaseModuleLayer.configuration().findModule(JAVA_BASE_MODULE_NAME).isPresent()); + assertThat(javaBaseModuleLayer.configuration().modules().size(), is(78)); + assertTrue(javaBaseModuleLayer.parents().get(0).configuration().parents().isEmpty()); + } + + @Test + public void whenRetrievingModuleDescriptor_thenTypeOfModuleIsInferred() { + ModuleDescriptor javaBaseModuleDescriptor = javaBaseModule.getDescriptor(); + ModuleDescriptor javaSqlModuleDescriptor = javaSqlModule.getDescriptor(); + + assertFalse(javaBaseModuleDescriptor.isAutomatic()); + assertFalse(javaBaseModuleDescriptor.isOpen()); + assertFalse(javaSqlModuleDescriptor.isAutomatic()); + assertFalse(javaSqlModuleDescriptor.isOpen()); + } + + @Test + public void givenModuleName_whenBuildingModuleDescriptor_thenBuilt() { + Builder moduleBuilder = ModuleDescriptor.newModule("baeldung.base"); + + ModuleDescriptor moduleDescriptor = moduleBuilder.build(); + + assertThat(moduleDescriptor.name(), is("baeldung.base")); + } + + @Test + public void givenModules_whenAccessingModuleDescriptorRequires_thenRequiresAreReturned() { + Set javaBaseRequires = javaBaseModule.getDescriptor().requires(); + Set javaSqlRequires = javaSqlModule.getDescriptor().requires(); + + Set javaSqlRequiresNames = javaSqlRequires.stream() + .map(Requires::name) + .collect(Collectors.toSet()); + + assertThat(javaBaseRequires, empty()); + assertThat(javaSqlRequires.size(), is(3)); + assertThat(javaSqlRequiresNames, containsInAnyOrder("java.base", "java.xml", "java.logging")); + } + + @Test + public void givenModules_whenAccessingModuleDescriptorProvides_thenProvidesAreReturned() { + Set javaBaseProvides = javaBaseModule.getDescriptor().provides(); + Set javaSqlProvides = javaSqlModule.getDescriptor().provides(); + + Set javaBaseProvidesService = javaBaseProvides.stream() + .map(Provides::service) + .collect(Collectors.toSet()); + + assertThat(javaBaseProvidesService, contains("java.nio.file.spi.FileSystemProvider")); + assertThat(javaSqlProvides, empty()); + } + + @Test + public void givenModules_whenAccessingModuleDescriptorExports_thenExportsAreReturned() { + Set javaBaseExports = javaBaseModule.getDescriptor().exports(); + Set javaSqlExports = javaSqlModule.getDescriptor().exports(); + + Set javaSqlExportsSource = javaSqlExports.stream() + .map(Exports::source) + .collect(Collectors.toSet()); + + assertThat(javaBaseExports.size(), is(108)); + assertThat(javaSqlExports.size(), is(3)); + assertThat(javaSqlExportsSource, containsInAnyOrder("java.sql", "javax.transaction.xa", "javax.sql")); + } + + @Test + public void givenModules_whenAccessingModuleDescriptorUses_thenUsesAreReturned() { + Set javaBaseUses = javaBaseModule.getDescriptor().uses(); + Set javaSqlUses = javaSqlModule.getDescriptor().uses(); + + assertThat(javaBaseUses.size(), is(34)); + assertThat(javaSqlUses, contains("java.sql.Driver")); + } + + @Test + public void givenModules_whenAccessingModuleDescriptorOpen_thenOpenAreReturned() { + Set javaBaseUses = javaBaseModule.getDescriptor().opens(); + Set javaSqlUses = javaSqlModule.getDescriptor().opens(); + + assertThat(javaBaseUses, empty()); + assertThat(javaSqlUses, empty()); + } + + @Test + public void whenAddingReadsToAModule_thenModuleCanReadNewModule() { + Module updatedModule = module.addReads(javaSqlModule); + + assertTrue(updatedModule.canRead(javaSqlModule)); + } + + @Test + public void whenExportingPackage_thenPackageIsExported() { + Module updatedModule = module.addExports("com.baeldung.java9.modules", javaSqlModule); + + assertTrue(updatedModule.isExported("com.baeldung.java9.modules")); + } + + @Test + public void whenOpeningAModulePackage_thenPackagedIsOpened() { + Module updatedModule = module.addOpens("com.baeldung.java9.modules", javaSqlModule); + + assertTrue(updatedModule.isOpen("com.baeldung.java9.modules", javaSqlModule)); + } + + @Test + public void whenAddingUsesToModule_thenUsesIsAdded() { + Module updatedModule = module.addUses(Driver.class); + + assertTrue(updatedModule.canUse(Driver.class)); + } + + private class Person { + private String name; + + public Person(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } +}