Skip to content

Commit

Permalink
Merge branch 'master' into BAEL-1689
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored Jun 6, 2018
2 parents b7b5b50 + be608ae commit 9fd6025
Show file tree
Hide file tree
Showing 82 changed files with 836 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class CayenneAdvancedOperationIntegrationTest {
public class CayenneAdvancedOperationLiveTest {
private static ObjectContext context = null;

@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static org.junit.Assert.assertNull;


public class CayenneOperationIntegrationTest {
public class CayenneOperationLiveTest {
private static ObjectContext context = null;

@BeforeClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@RunWith(SpringRunner.class)
@SpringBootTest
public class AzureApplicationTests {
public class AzureApplicationIntegrationTest {

@Test
public void contextLoads() {
Expand Down
10 changes: 10 additions & 0 deletions core-java-8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
18 changes: 18 additions & 0 deletions core-java-8/src/main/java/com/baeldung/reflect/Person.java
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;
}
}
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");
}
}
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException {
bw.newLine();
bw.close();

assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n");
assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void whenReadTwoFilesWithSequenceInputStream_thenCorrect() throws IOExcep

@Test
public void whenReadUTFEncodedFile_thenCorrect() throws IOException {
final String expected_value = "�空";
final String expected_value = "青空";
final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("src/test/resources/test_read7.in"), "UTF-8"));
final String currentLine = reader.readLine();
reader.close();
Expand Down
5 changes: 5 additions & 0 deletions core-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@
<artifactId>mail</artifactId>
<version>${javax.mail.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
</dependencies>

<build>
Expand Down
49 changes: 49 additions & 0 deletions core-java/src/main/java/com/baeldung/array/JaggedArray.java
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];
}

}
Loading

0 comments on commit 9fd6025

Please sign in to comment.