Merge branch 'master' of https://github.com/eugenp/tutorials into task/BAEL-20665

# Conflicts:
#	core-java-modules/core-java-jndi/pom.xml
This commit is contained in:
Dhawal Kapil
2020-01-21 20:02:01 +05:30
97 changed files with 1158 additions and 202 deletions
+3 -2
View File
@@ -42,12 +42,12 @@
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId>
<version>10.0.0</version>
<version>${eclipse.collections.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections-api</artifactId>
<version>10.0.0</version>
<version>${eclipse.collections.version}</version>
</dependency>
</dependencies>
@@ -108,6 +108,7 @@
<assertj.version>3.11.1</assertj.version>
<uberjar.name>benchmarks</uberjar.name>
<jmh.version>1.22</jmh.version>
<eclipse.collections.version>10.0.0</eclipse.collections.version>
</properties>
</project>
+2 -1
View File
@@ -41,7 +41,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<version>${surefire.plugin.version}</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
@@ -53,6 +53,7 @@
<maven.compiler.source.version>13</maven.compiler.source.version>
<maven.compiler.target.version>13</maven.compiler.target.version>
<assertj.version>3.6.1</assertj.version>
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
</properties>
</project>
@@ -0,0 +1,27 @@
package com.baeldung.newfeatures;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class SwitchExpressionsWithYieldUnitTest {
@Test
@SuppressWarnings("preview")
public void whenSwitchingOnOperationSquareMe_thenWillReturnSquare() {
var me = 4;
var operation = "squareMe";
var result = switch (operation) {
case "doubleMe" -> {
yield me * 2;
}
case "squareMe" -> {
yield me * me;
}
default -> me;
};
assertEquals(16, result);
}
}
@@ -0,0 +1,39 @@
package com.baeldung.newfeatures;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class TextBlocksUnitTest {
private static final String JSON_STRING = "{\r\n" + "\"name\" : \"Baeldung\",\r\n" + "\"website\" : \"https://www.%s.com/\"\r\n" + "}";
@SuppressWarnings("preview")
private static final String TEXT_BLOCK_JSON = """
{
"name" : "Baeldung",
"website" : "https://www.%s.com/"
}
""";
@Test
public void whenTextBlocks_thenStringOperationsWork() {
assertThat(TEXT_BLOCK_JSON.contains("Baeldung")).isTrue();
assertThat(TEXT_BLOCK_JSON.indexOf("www")).isGreaterThan(0);
assertThat(TEXT_BLOCK_JSON.length()).isGreaterThan(0);
}
@SuppressWarnings("removal")
@Test
public void whenTextBlocks_thenFormattedWorksAsFormat() {
assertThat(TEXT_BLOCK_JSON.formatted("baeldung")
.contains("www.baeldung.com")).isTrue();
assertThat(String.format(JSON_STRING, "baeldung")
.contains("www.baeldung.com")).isTrue();
}
}
+2 -1
View File
@@ -36,7 +36,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<version>${surefire.plugin.version}</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
@@ -47,6 +47,7 @@
<properties>
<maven.compiler.source.version>14</maven.compiler.source.version>
<maven.compiler.target.version>14</maven.compiler.target.version>
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
</properties>
</project>
+2 -1
View File
@@ -51,7 +51,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<version>${shade.plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
@@ -79,6 +79,7 @@
<commons-lang3.version>3.9</commons-lang3.version>
<!-- testing -->
<assertj-core.version>3.10.0</assertj-core.version>
<shade.plugin.version>3.2.0</shade.plugin.version>
</properties>
</project>
+4 -2
View File
@@ -183,8 +183,8 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>${source.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
</plugins>
@@ -373,6 +373,8 @@
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
<spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
</properties>
</project>
+4 -2
View File
@@ -207,8 +207,8 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>${source.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
</plugins>
@@ -397,6 +397,8 @@
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
<spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
</properties>
</project>
+27 -6
View File
@@ -18,7 +18,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.1</version>
<version>${jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -30,29 +30,50 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.9.RELEASE</version>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.9.RELEASE</version>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.9.RELEASE</version>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.9.RELEASE</version>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
<version>${h2.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${source.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<spring.version>5.0.9.RELEASE</spring.version>
<h2.version>1.4.199</h2.version>
<jupiter.version>5.5.1</jupiter.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
</properties>
</project>
@@ -16,7 +16,7 @@
<dependency>
<groupId>com.baeldung.servicemodule</groupId>
<artifactId>servicemodule</artifactId>
<version>1.0</version>
<version>${servicemodule.version}</version>
</dependency>
</dependencies>
@@ -29,4 +29,8 @@
</plugins>
</build>
<properties>
<servicemodule.version>1.0</servicemodule.version>
</properties>
</project>
@@ -19,10 +19,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<version>${compiler.plugin.version}</version>
<configuration>
<source>11</source>
<target>11</target>
<source>${source.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
</plugins>
@@ -31,6 +31,9 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<compiler.plugin.version>3.8.0</compiler.plugin.version>
<source.version>11</source.version>
<target.version>11</target.version>
</properties>
</project>
@@ -17,12 +17,12 @@
<dependency>
<groupId>com.baeldung.servicemodule</groupId>
<artifactId>servicemodule</artifactId>
<version>1.0</version>
<version>${servicemodule.version}</version>
</dependency>
<dependency>
<groupId>com.baeldung.providermodule</groupId>
<artifactId>providermodule</artifactId>
<version>1.0</version>
<version>${providermodule.version}</version>
</dependency>
</dependencies>
@@ -34,5 +34,10 @@
</plugin>
</plugins>
</build>
<properties>
<servicemodule.version>1.0</servicemodule.version>
<providermodule.version>1.0</providermodule.version>
</properties>
</project>
@@ -20,14 +20,20 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<version>${compiler.plugin.version}</version>
<configuration>
<source>11</source>
<target>11</target>
<source>${source.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<compiler.plugin.version>3.8.0</compiler.plugin.version>
<source.version>11</source.version>
<target.version>11</target.version>
</properties>
</project>
@@ -17,7 +17,7 @@
<dependency>
<groupId>com.baeldung.servicemodule</groupId>
<artifactId>servicemodule</artifactId>
<version>1.0</version>
<version>${servicemodule.version}</version>
</dependency>
</dependencies>
@@ -30,4 +30,9 @@
</plugins>
</build>
<properties>
<servicemodule.version>1.0</servicemodule.version>
</properties>
</project>
+2 -1
View File
@@ -18,7 +18,7 @@
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
<version>${commons.beanutils.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
@@ -57,6 +57,7 @@
<jmh-core.version>1.19</jmh-core.version>
<jmh-generator.version>1.19</jmh-generator.version>
<assertj.version>3.12.2</assertj.version>
<commons.beanutils.version>1.9.4</commons.beanutils.version>
</properties>
</project>
@@ -1,6 +1,8 @@
package com.baeldung.optional;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class Person {
private String name;
@@ -21,7 +23,7 @@ public class Person {
}
public Optional<Integer> getAge() {
return Optional.ofNullable(age);
return Optional.of(age);
}
public void setAge(int age) {
@@ -36,4 +38,37 @@ public class Person {
return Optional.ofNullable(password);
}
public static List<Person> search(List<Person> people, String name, Optional<Integer> age) {
// Null checks for people and name
return people.stream()
.filter(p -> p.getName().equals(name))
.filter(p -> p.getAge().get() >= age.orElse(0))
.collect(Collectors.toList());
}
public static List<Person> search(List<Person> people, String name, Integer age) {
// Null checks for people and name
final Integer ageFilter = age != null ? age : 0;
return people.stream()
.filter(p -> p.getName().equals(name))
.filter(p -> p.getAge().get() >= ageFilter)
.collect(Collectors.toList());
}
public static List<Person> search(List<Person> people, String name) {
return doSearch(people, name, 0);
}
public static List<Person> search(List<Person> people, String name, int age) {
return doSearch(people, name, age);
}
private static List<Person> doSearch(List<Person> people, String name, int age) {
// Null checks for people and name
return people.stream()
.filter(p -> p.getName().equals(name))
.filter(p -> p.getAge().get().intValue() >= age)
.collect(Collectors.toList());
}
}
@@ -37,8 +37,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>${source.version}</source>
<target>${target.version}</target>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
@@ -48,5 +48,7 @@
<properties>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<assertj-core.version>3.10.0</assertj-core.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
</properties>
</project>
@@ -0,0 +1,7 @@
package com.baeldung.reflection.exception.invocationtarget;
public class InvocationTargetExample {
public int divideByZeroExample() {
return 1 / 0;
}
}
@@ -0,0 +1,23 @@
package com.baeldung.reflection.exception.invocationtarget;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
public class InvocationTargetUnitTest {
@Test
public void whenCallingMethodThrowsException_thenAssertCauseOfInvocationTargetException() throws Exception {
InvocationTargetExample targetExample = new InvocationTargetExample();
Method method = InvocationTargetExample.class.getMethod("divideByZeroExample");
Exception exception = assertThrows(InvocationTargetException.class, () -> method.invoke(targetExample));
assertEquals(ArithmeticException.class, exception.getCause().getClass());
}
}
@@ -25,7 +25,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
<version>${guava.version}</version>
</dependency>
<dependency>
@@ -62,7 +62,7 @@
<properties>
<commons-lang3.version>3.8.1</commons-lang3.version>
<assertj.version>3.6.1</assertj.version>
<guava.version>27.0.1-jre</guava.version>
<guava.version>28.1-jre</guava.version>
<junit-jupiter-api.version>5.3.1</junit-jupiter-api.version>
</properties>
@@ -0,0 +1,63 @@
package com.baeldung.regex.matcher;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
public class MatcherUnitTest {
@Test
public void whenFindFourDigitWorks_thenCorrect() {
Pattern stringPattern = Pattern.compile("\\d\\d\\d\\d");
Matcher m = stringPattern.matcher("goodbye 2019 and welcome 2020");
assertTrue(m.find());
assertEquals(8, m.start());
assertEquals("2019", m.group());
assertEquals(12, m.end());
assertTrue(m.find());
assertEquals(25, m.start());
assertEquals("2020", m.group());
assertEquals(29, m.end());
assertFalse(m.find());
}
@Test
public void givenStartIndex_whenFindFourDigitWorks_thenCorrect() {
Pattern stringPattern = Pattern.compile("\\d\\d\\d\\d");
Matcher m = stringPattern.matcher("goodbye 2019 and welcome 2020");
assertTrue(m.find(20));
assertEquals(25, m.start());
assertEquals("2020", m.group());
assertEquals(29, m.end());
}
@Test
public void whenMatchFourDigitWorks_thenFail() {
Pattern stringPattern = Pattern.compile("\\d\\d\\d\\d");
Matcher m = stringPattern.matcher("goodbye 2019 and welcome 2020");
assertFalse(m.matches());
}
@Test
public void whenMatchFourDigitWorks_thenCorrect() {
Pattern stringPattern = Pattern.compile("\\d\\d\\d\\d");
Matcher m = stringPattern.matcher("2019");
assertTrue(m.matches());
assertEquals(0, m.start());
assertEquals("2019", m.group());
assertEquals(4, m.end());
assertTrue(m.matches());// matches will always return the same return
}
}
+4 -2
View File
@@ -207,8 +207,8 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>${source.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
</plugins>
@@ -397,6 +397,8 @@
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
<spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
</properties>
</project>
@@ -17,17 +17,17 @@
<dependency>
<groupId>com.baeldung.entitymodule</groupId>
<artifactId>entitymodule</artifactId>
<version>1.0</version>
<version>${entitymodule.version}</version>
</dependency>
<dependency>
<groupId>com.baeldung.daomodule</groupId>
<artifactId>daomodule</artifactId>
<version>1.0</version>
<version>${daomodule.version}</version>
</dependency>
<dependency>
<groupId>com.baeldung.userdaomodule</groupId>
<artifactId>userdaomodule</artifactId>
<version>1.0</version>
<version>${userdaomodule.version}</version>
</dependency>
</dependencies>
@@ -43,6 +43,9 @@
<properties>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
<entitymodule.version>1.0</entitymodule.version>
<daomodule.version>1.0</daomodule.version>
<userdaomodule.version>1.0</userdaomodule.version>
</properties>
</project>
@@ -45,10 +45,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<version>${compiler.plugin.version}</version>
<configuration>
<source>1.9</source>
<target>1.9</target>
<source>${source.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
</plugins>
@@ -56,6 +56,9 @@
</build>
<properties>
<compiler.plugin.version>3.8.0</compiler.plugin.version>
<source.version>1.9</source.version>
<target.version>1.9</target.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<assertj-core.version>3.12.2</assertj-core.version>
</properties>
@@ -17,12 +17,12 @@
<dependency>
<groupId>com.baeldung.entitymodule</groupId>
<artifactId>entitymodule</artifactId>
<version>1.0</version>
<version>${entitymodule.version}</version>
</dependency>
<dependency>
<groupId>com.baeldung.daomodule</groupId>
<artifactId>daomodule</artifactId>
<version>1.0</version>
<version>${daomodule.version}</version>
</dependency>
</dependencies>
@@ -38,6 +38,8 @@
<properties>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
<entitymodule.version>1.0</entitymodule.version>
<daomodule.version>1.0</daomodule.version>
</properties>
</project>
+11 -4
View File
@@ -29,16 +29,16 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<version>${compiler.plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>${source.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<version>${dependency.plugin.version}</version>
<executions>
<execution>
<id>copy-dependencies</id>
@@ -69,5 +69,12 @@
</plugin>
</plugins>
</build>
<properties>
<dependency.plugin.version>3.1.1</dependency.plugin.version>
<compiler.plugin.version>3.8.0</compiler.plugin.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
</properties>
</project>