[BAEL-3936] Merged with upstream and fixed conflicts

This commit is contained in:
Kostas Karavitis
2020-04-28 20:35:04 +03:00
155 changed files with 2738 additions and 609 deletions
@@ -1,4 +1,4 @@
package com.baeldung.java14.helpfulnullpointerexceptions;
package com.baeldung.java14.npe;
public class HelpfulNullPointerException {
@@ -4,4 +4,5 @@
### Relevant Articles:
- [Using a Mutex Object in Java](https://www.baeldung.com/java-mutex)
- [Testing Multi-Threaded Code in Java] (https://www.baeldung.com/java-testing-multithreaded)
@@ -15,6 +15,38 @@
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.thread-weaver</groupId>
<artifactId>threadweaver</artifactId>
<version>0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.tempus-fugit</groupId>
<artifactId>tempus-fugit</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.multithreadedtc</groupId>
<artifactId>multithreadedtc</artifactId>
<version>1.01</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jcstress</groupId>
<artifactId>jcstress-core</artifactId>
<version>0.5</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-concurrency-2</finalName>
@@ -24,6 +56,51 @@
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>${javac.target}</compilerVersion>
<source>${javac.target}</source>
<target>${javac.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>main</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>jcstress</finalName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jcstress.Main</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/TestList</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<javac.target>1.8</javac.target>
</properties>
</project>
@@ -1,5 +1,6 @@
package com.baeldung.concurrent;
import org.junit.Ignore;
import org.junit.Test;
import edu.umd.cs.mtc.MultithreadedTestCase;
@@ -25,9 +26,10 @@ public class MyCounterMultithreadedTCUnitTest extends MultithreadedTestCase {
@SuppressWarnings("deprecation")
@Override
public void finish() {
assertEquals(2, counter.getCount());
assertEquals(2, counter.getCount());
}
@Ignore
@Test
public void testCounter() throws Throwable {
TestFramework.runManyTimes(new MyCounterMultithreadedTCUnitTest(), 1000);
@@ -6,6 +6,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.Ignore;
import org.junit.Test;
public class MyCounterSimpleUnitTest {
@@ -18,7 +19,8 @@ public class MyCounterSimpleUnitTest {
assertEquals(500, counter.getCount());
}
// @Test
@Ignore
@Test
public void testCounterWithConcurrency() throws InterruptedException {
int numberOfThreads = 100;
ExecutorService service = Executors.newFixedThreadPool(10);
@@ -34,7 +36,8 @@ public class MyCounterSimpleUnitTest {
assertEquals(numberOfThreads, counter.getCount());
}
// @Test
@Ignore
@Test
public void testSummationWithConcurrencyAndWait() throws InterruptedException {
int numberOfThreads = 2;
ExecutorService service = Executors.newFixedThreadPool(10);
@@ -3,6 +3,7 @@ package com.baeldung.concurrent;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
@@ -20,6 +21,7 @@ public class MyCounterTempusFugitUnitTest {
private static MyCounter counter = new MyCounter();
@Ignore
@Test
@Concurrent(count = 2)
@Repeating(repetition = 10)
@@ -2,6 +2,7 @@ package com.baeldung.concurrent;
import static org.junit.Assert.assertEquals;
import org.junit.Ignore;
import org.junit.Test;
import com.google.testing.threadtester.AnnotatedTestRunner;
@@ -34,6 +35,7 @@ public class MyCounterThreadWeaverUnitTest {
assertEquals(2, counter.getCount());
}
@Ignore
@Test
public void testCounter() {
new AnnotatedTestRunner().runTests(this.getClass(), MyCounter.class);
@@ -1,7 +0,0 @@
=========
## Core Java Concurrency Testing Examples
### Relevant Articles:
- [Testing Multi-Threaded Code in Java](https://www.baeldung.com/java-testing-multithreaded)
@@ -1,93 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-concurrency-testing</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-concurrency-testing</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.thread-weaver</groupId>
<artifactId>threadweaver</artifactId>
<version>0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.tempus-fugit</groupId>
<artifactId>tempus-fugit</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.googlecode.multithreadedtc</groupId>
<artifactId>multithreadedtc</artifactId>
<version>1.01</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jcstress</groupId>
<artifactId>jcstress-core</artifactId>
<version>0.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>${javac.target}</compilerVersion>
<source>${javac.target}</source>
<target>${javac.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>main</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>jcstress</finalName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jcstress.Main</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/TestList</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -1,19 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -1,13 +0,0 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear
@@ -11,4 +11,5 @@ This module contains articles about string operations.
- [Case-Insensitive String Matching in Java](https://www.baeldung.com/java-case-insensitive-string-matching)
- [L-Trim and R-Trim Alternatives in Java](https://www.baeldung.com/java-trim-alternatives)
- [Java Convert PDF to Base64](https://www.baeldung.com/java-convert-pdf-to-base64)
- [Encode a String to UTF-8 in Java](https://www.baeldung.com/java-string-encode-utf-8)
- More articles: [[<-- prev]](../core-java-string-operations)
@@ -0,0 +1,73 @@
package com.baeldung.encodetoutf8;
import static org.junit.Assert.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.apache.commons.codec.binary.StringUtils;
import org.junit.Test;
public class StringEncodeUnitTest {
@Test
public void givenGermanAsciiString_whenComparing_thenCompareNotEquals() {
String germanString = "Entwickeln Sie mit Vergnügen";
byte[] germanBytes = germanString.getBytes();
String asciiEncodedString = new String(germanBytes, StandardCharsets.US_ASCII);
assertNotEquals(germanString, asciiEncodedString);
}
@Test
public void givenUsAsciiString_whenComparing_thenCompareNotEquals() {
String englishString = "Develop with pleasure";
byte[] englishBytes = englishString.getBytes();
String asciiEncondedEnglishString = new String(englishBytes, StandardCharsets.US_ASCII);
assertEquals(englishString, asciiEncondedEnglishString);
}
/*
* ApacheCommonsCodecEncode
*/
@Test
public void givenSomeUnencodedString_whenApacheCommonsCodecEncode_thenCompareEquals() {
String rawString = "Entwickeln Sie mit Vergnügen";
byte[] bytes = StringUtils.getBytesUtf8(rawString);
String utf8EncodedString = StringUtils.newStringUtf8(bytes);
assertEquals(rawString, utf8EncodedString);
}
/*
* CoreJavaEncode
*/
@Test
public void givenSomeUnencodedString_whenCoreJavaEncode_thenCompareEquals() {
String rawString = "Entwickeln Sie mit Vergnügen";
byte[] bytes = rawString.getBytes(StandardCharsets.UTF_8);
String utf8EncodedString = new String(bytes, StandardCharsets.UTF_8);
assertEquals(rawString, utf8EncodedString);
}
/*
* Java7StandardCharsetsEncode
*/
@Test
public void givenSomeUnencodedString_whenJava7StandardCharsetsEncode_thenCompareEquals() {
String rawString = "Entwickeln Sie mit Vergnügen";
ByteBuffer buffer = StandardCharsets.UTF_8.encode(rawString);
String utf8EncodedString = StandardCharsets.UTF_8.decode(buffer)
.toString();
assertEquals(rawString, utf8EncodedString);
}
}
@@ -96,7 +96,7 @@
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<asspectj.version>1.8.9</asspectj.version>
<powermock.version>2.0.0-RC.4</powermock.version>
<powermock.version>2.0.0</powermock.version>
<jmockit.version>1.44</jmockit.version>
<!-- plugins -->
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
@@ -0,0 +1,23 @@
package com.baeldung.objectclass;
import lombok.Data;
@Data
public class Borrower extends User {
private double totalLoanAmount;
public double requestLoan(double amount) {
totalLoanAmount = amount;
return totalLoanAmount;
}
public double increaseLoan(double increaseBy) {
return totalLoanAmount + increaseBy;
}
public double payLoan(double amount) {
return totalLoanAmount - amount;
}
}
@@ -0,0 +1,20 @@
package com.baeldung.objectclass;
public class Lender extends User {
private double totalInvestmentAmount;
public double invest(double amount) {
totalInvestmentAmount = amount;
return totalInvestmentAmount;
}
public double increaseInvestment(double increaseBy) {
return totalInvestmentAmount + increaseBy;
}
public double collectDividends() {
return totalInvestmentAmount * 0.07;
}
}
@@ -0,0 +1,12 @@
package com.baeldung.objectclass;
import lombok.Data;
@Data
public class User {
private String firstName;
private String lastName;
}
@@ -0,0 +1,74 @@
package com.baeldung.objectclass;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test;
public class CreditAppUnitTest {
@Test
public void givenLender_whenInstanceOf_thenReturnTrue() {
User lender = new Lender();
assertTrue(lender instanceof Lender);
assertTrue(lender instanceof User);
}
@Test
public void givenUser_whenInstanceOfLender_thenDowncast() {
User user = new Lender();
Lender lender = null;
if(user instanceof Lender) {
lender = (Lender) user;
}
assertNotNull(lender);
}
@Test
public void givenUser_whenIsInstanceOfLender_thenDowncast() {
User user = new Lender();
Lender lender = null;
if(Lender.class.isInstance(user)) {
lender = (Lender) user;
}
assertNotNull(lender);
}
@Test
public void givenBorrower_whenLoanAmountIsDouble_thenRequestLoan() {
Borrower borrower = new Borrower();
double amount = 100.0;
//if(amount instanceof Double) // Compilation error, no autoboxing
if(Double.class.isInstance(amount)) {
borrower.requestLoan(amount);
}
assertEquals(100, borrower.getTotalLoanAmount());
}
@Test
public void givenBorrower_whenLoanAmountIsNotString_thenRequestLoan() {
Borrower borrower = new Borrower();
Double amount = 100.0;
//if(amount instanceof String) // Compilation error, incompatible operands
if(!String.class.isInstance(amount)) {
borrower.requestLoan(amount);
}
assertEquals(100, borrower.getTotalLoanAmount());
}
@Test
public void givenLender_whenGetClass_thenEqualsLenderType() {
User lender = new Lender();
assertEquals(Lender.class, lender.getClass());
assertNotEquals(User.class, lender.getClass());
}
}
@@ -0,0 +1,59 @@
package com.baeldung.foldvsreduce
import org.junit.Test
import org.junit.jupiter.api.assertThrows
import java.lang.RuntimeException
import kotlin.test.assertEquals
class FoldAndReduceTest {
@Test
fun testReduceLimitations() {
val numbers: List<Int> = listOf(1, 2, 3)
val sum: Number = numbers.reduce { acc, next -> acc + next }
assertEquals(6, sum)
val emptyList = listOf<Int>()
assertThrows<RuntimeException> { emptyList.reduce { acc, next -> acc + next } }
// doesn't compile
// val sum = numbers.reduce { acc, next -> acc.toLong() + next.toLong()}
}
@Test
fun testFold() {
val numbers: List<Int> = listOf(1, 2, 3)
val sum: Int = numbers.fold(0, { acc, next -> acc + next })
assertEquals(6, sum)
//change result type
val sumLong: Long = numbers.fold(0L, { acc, next -> acc + next.toLong() })
assertEquals(6L, sumLong)
val emptyList = listOf<Int>()
val emptySum = emptyList.fold(0, { acc, next -> acc + next })
assertEquals(0, emptySum)
//power of changing result type
val (even, odd) = numbers.fold(Pair(listOf<Int>(), listOf<Int>()), { acc, next ->
if (next % 2 == 0) Pair(acc.first + next, acc.second)
else Pair(acc.first, acc.second + next)
})
assertEquals(listOf(2), even)
assertEquals(listOf(1, 3), odd)
}
@Test
fun testVariationsOfFold() {
val numbers = listOf(1, 2, 3)
val reversed = numbers.foldRight(listOf<Int>(), { next, acc -> acc + next})
assertEquals(listOf(3,2,1), reversed)
val reversedIndexes = numbers.foldRightIndexed(listOf<Int>(), { i, _, acc -> acc + i })
assertEquals(listOf(2,1,0), reversedIndexes)
}
}
+3
View File
@@ -0,0 +1,3 @@
/.idea
/.gradle
/build
+38
View File
@@ -0,0 +1,38 @@
plugins {
id 'java-library'
id 'application'
}
apply plugin: 'application'
mainClassName = 'employee.EmployeeApp'
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
println 'This is executed during configuration phase'
task configured {
println 'The project is configured'
}
task wrapper(type: Wrapper){
gradleVersion = '5.3.1'
}
repositories {
jcenter()
}
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.10'
testImplementation('junit:junit:4.13')
testRuntime('junit:junit:4.13')
}
test {
useJUnit()
}
@@ -0,0 +1,9 @@
package employee;
public class Employee {
String name;
String emailAddress;
int yearOfBirth;
}
@@ -0,0 +1,16 @@
package employee;
public class EmployeeApp {
public static void main(String[] args){
Employee employee = new Employee();
employee.name = "John";
employee.emailAddress = "[email protected]";
employee.yearOfBirth = 1978;
System.out.println("Name: " + employee.name);
System.out.println("Email Address: " + employee.emailAddress);
System.out.println("Year Of Birth:" + employee.yearOfBirth);
}
}
@@ -0,0 +1,31 @@
package employee;
import employee.Employee;
import org.junit.*;
import static org.junit.Assert.*;
public class EmployeeAppTest {
@Test
public void testData(){
Employee testEmp = this.getEmployeeTest();
assertEquals(testEmp.name, "John");
assertEquals(testEmp.emailAddress, "[email protected]");
assertEquals(testEmp.yearOfBirth, 1978);
}
private Employee getEmployeeTest(){
Employee employee = new Employee();
employee.name = "John";
employee.emailAddress = "[email protected]";
employee.yearOfBirth = 1978;
return employee;
}
}
+1 -1
View File
@@ -1,10 +1,10 @@
rootProject.name = 'gradletutorial'
include 'greeting-library'
include 'greeting-library-java'
include 'greeter'
include 'gradletaskdemo'
include 'junit5'
include 'gradle-employee-app'
println 'This will be executed during the initialization phase.'
+3 -4
View File
@@ -256,10 +256,9 @@
</execution>
</executions>
<configuration>
<wsdlDirectory>src/main/resources</wsdlDirectory>
<wsdlFiles>
<wsdlFile>country.wsdl</wsdlFile>
</wsdlFiles>
<wsdlUrls>
<wsdlUrl>http://localhost:8888/ws/country?wsdl</wsdlUrl>
</wsdlUrls>
<keep>true</keep>
<packageName>com.baeldung.soap.ws.client.generated</packageName>
<sourceDestDir>src/main/java</sourceDestDir>
@@ -12,11 +12,11 @@ import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.3.2
* JAX-WS RI 2.2.9-b130926.1035
* Generated source version: 2.2
*
*/
@WebServiceClient(name = "CountryServiceImplService", targetNamespace = "http://server.ws.soap.baeldung.com/", wsdlLocation = "file:src/main/resources/country.wsdl")
@WebServiceClient(name = "CountryServiceImplService", targetNamespace = "http://server.ws.soap.baeldung.com/", wsdlLocation = "http://localhost:8888/ws/country?wsdl")
public class CountryServiceImplService extends Service {
private final static URL COUNTRYSERVICEIMPLSERVICE_WSDL_LOCATION;
@@ -27,7 +27,7 @@ public class CountryServiceImplService extends Service {
URL url = null;
WebServiceException e = null;
try {
url = new URL("file:src/main/resources/country.wsdl");
url = new URL("http://localhost:8888/ws/country?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}

Some files were not shown because too many files have changed in this diff Show More