forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into BAEL-1689
- Loading branch information
Showing
82 changed files
with
836 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
core-java-8/src/main/java/com/baeldung/reflect/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.baeldung.reflect; | ||
|
||
public class Person { | ||
|
||
private String fullName; | ||
|
||
public Person(String fullName) { | ||
this.fullName = fullName; | ||
} | ||
|
||
public void setFullName(String fullName) { | ||
this.fullName = fullName; | ||
} | ||
|
||
public String getFullName() { | ||
return fullName; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
core-java-8/src/test/java/com/baeldung/reflect/MethodParamNameUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.baeldung.reflect; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.lang.reflect.Parameter; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.junit.Test; | ||
|
||
public class MethodParamNameUnitTest { | ||
|
||
@Test | ||
public void whenGetConstructorParams_thenOk() | ||
throws NoSuchMethodException, SecurityException { | ||
List<Parameter> parameters | ||
= Arrays.asList(Person.class.getConstructor(String.class).getParameters()); | ||
Optional<Parameter> parameter | ||
= parameters.stream().filter(Parameter::isNamePresent).findFirst(); | ||
assertThat(parameter.get().getName()).isEqualTo("fullName"); | ||
} | ||
|
||
@Test | ||
public void whenGetMethodParams_thenOk() | ||
throws NoSuchMethodException, SecurityException { | ||
List<Parameter> parameters | ||
= Arrays.asList( | ||
Person.class.getMethod("setFullName", String.class).getParameters()); | ||
Optional<Parameter> parameter | ||
= parameters.stream().filter(Parameter::isNamePresent).findFirst(); | ||
assertThat(parameter.get().getName()).isEqualTo("fullName"); | ||
} | ||
} |
199 changes: 199 additions & 0 deletions
199
core-java-9/src/test/java/com/baeldung/java9/modules/ModuleAPIUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HashMap> hashMapClass = HashMap.class; | ||
javaBaseModule = hashMapClass.getModule(); | ||
|
||
Class<Date> dateClass = Date.class; | ||
javaSqlModule = dateClass.getModule(); | ||
|
||
Class<Person> 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<Requires> javaBaseRequires = javaBaseModule.getDescriptor().requires(); | ||
Set<Requires> javaSqlRequires = javaSqlModule.getDescriptor().requires(); | ||
|
||
Set<String> 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<Provides> javaBaseProvides = javaBaseModule.getDescriptor().provides(); | ||
Set<Provides> javaSqlProvides = javaSqlModule.getDescriptor().provides(); | ||
|
||
Set<String> 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<Exports> javaBaseExports = javaBaseModule.getDescriptor().exports(); | ||
Set<Exports> javaSqlExports = javaSqlModule.getDescriptor().exports(); | ||
|
||
Set<String> 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<String> javaBaseUses = javaBaseModule.getDescriptor().uses(); | ||
Set<String> javaSqlUses = javaSqlModule.getDescriptor().uses(); | ||
|
||
assertThat(javaBaseUses.size(), is(34)); | ||
assertThat(javaSqlUses, contains("java.sql.Driver")); | ||
} | ||
|
||
@Test | ||
public void givenModules_whenAccessingModuleDescriptorOpen_thenOpenAreReturned() { | ||
Set<Opens> javaBaseUses = javaBaseModule.getDescriptor().opens(); | ||
Set<Opens> 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
core-java/src/main/java/com/baeldung/array/JaggedArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.baeldung.array; | ||
|
||
import java.util.Arrays; | ||
import java.util.Scanner; | ||
|
||
public class JaggedArray { | ||
|
||
int[][] shortHandFormInitialization() { | ||
int[][] jaggedArr = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } }; | ||
return jaggedArr; | ||
} | ||
|
||
int[][] declarationAndThenInitialization() { | ||
int[][] jaggedArr = new int[3][]; | ||
jaggedArr[0] = new int[] { 1, 2 }; | ||
jaggedArr[1] = new int[] { 3, 4, 5 }; | ||
jaggedArr[2] = new int[] { 6, 7, 8, 9 }; | ||
return jaggedArr; | ||
} | ||
|
||
int[][] declarationAndThenInitializationUsingUserInputs() { | ||
int[][] jaggedArr = new int[3][]; | ||
jaggedArr[0] = new int[2]; | ||
jaggedArr[1] = new int[3]; | ||
jaggedArr[2] = new int[4]; | ||
initializeElements(jaggedArr); | ||
return jaggedArr; | ||
} | ||
|
||
void initializeElements(int[][] jaggedArr) { | ||
Scanner sc = new Scanner(System.in); | ||
for (int outer = 0; outer < jaggedArr.length; outer++) { | ||
for (int inner = 0; inner < jaggedArr[outer].length; inner++) { | ||
jaggedArr[outer][inner] = sc.nextInt(); | ||
} | ||
} | ||
} | ||
|
||
void printElements(int[][] jaggedArr) { | ||
for (int index = 0; index < jaggedArr.length; index++) { | ||
System.out.println(Arrays.toString(jaggedArr[index])); | ||
} | ||
} | ||
|
||
int[] getElementAtGivenIndex(int[][] jaggedArr, int index) { | ||
return jaggedArr[index]; | ||
} | ||
|
||
} |
Oops, something went wrong.