Merge branch 'master' of https://github.com/eugenp/tutorials into ABA_problem
This commit is contained in:
commit
fd32bd2f4d
@ -15,9 +15,5 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
|
||||
- [Maximum Subarray Problem](https://www.baeldung.com/java-maximum-subarray)
|
||||
- [How to Merge Two Sorted Arrays](https://www.baeldung.com/java-merge-sorted-arrays)
|
||||
- [Median of Stream of Integers using Heap](https://www.baeldung.com/java-stream-integers-median-using-heap)
|
||||
- [Kruskal’s Algorithm for Spanning Trees](https://www.baeldung.com/java-spanning-trees-kruskal)
|
||||
- [Balanced Brackets Algorithm in Java](https://www.baeldung.com/java-balanced-brackets-algorithm)
|
||||
- [Efficiently Merge Sorted Java Sequences](https://www.baeldung.com/java-merge-sorted-sequences)
|
||||
- [Introduction to Greedy Algorithms with Java](https://www.baeldung.com/java-greedy-algorithms)
|
||||
- [The Caesar Cipher in Java](https://www.baeldung.com/java-caesar-cipher)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-4)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-4) [[next -->]](/../algorithms-miscellaneous-6)
|
||||
|
||||
|
@ -25,12 +25,6 @@
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>pl.allegro.finance</groupId>
|
||||
<artifactId>tradukisto</artifactId>
|
||||
|
@ -2,3 +2,9 @@
|
||||
|
||||
- [Boruvka’s Algorithm for Minimum Spanning Trees](https://www.baeldung.com/java-boruvka-algorithm)
|
||||
- [Gradient Descent in Java](https://www.baeldung.com/java-gradient-descent)
|
||||
- [Kruskal’s Algorithm for Spanning Trees](https://www.baeldung.com/java-spanning-trees-kruskal)
|
||||
- [Balanced Brackets Algorithm in Java](https://www.baeldung.com/java-balanced-brackets-algorithm)
|
||||
- [Efficiently Merge Sorted Java Sequences](https://www.baeldung.com/java-merge-sorted-sequences)
|
||||
- [Introduction to Greedy Algorithms with Java](https://www.baeldung.com/java-greedy-algorithms)
|
||||
- [The Caesar Cipher in Java](https://www.baeldung.com/java-caesar-cipher)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-5)
|
||||
|
@ -18,10 +18,35 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-commons</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${org.assertj.core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<guava.version>28.1-jre</guava.version>
|
||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
<junit.platform.version>1.6.0</junit.platform.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
@ -1,13 +1,13 @@
|
||||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
public class HeapNode {
|
||||
|
||||
int element;
|
||||
int arrayIndex;
|
||||
int nextElementIndex = 1;
|
||||
|
||||
public HeapNode(int element, int arrayIndex) {
|
||||
this.element = element;
|
||||
this.arrayIndex = arrayIndex;
|
||||
}
|
||||
}
|
||||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
public class HeapNode {
|
||||
|
||||
int element;
|
||||
int arrayIndex;
|
||||
int nextElementIndex = 1;
|
||||
|
||||
public HeapNode(int element, int arrayIndex) {
|
||||
this.element = element;
|
||||
this.arrayIndex = arrayIndex;
|
||||
}
|
||||
}
|
@ -1,88 +1,88 @@
|
||||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
public class MinHeap {
|
||||
|
||||
HeapNode[] heapNodes;
|
||||
|
||||
public MinHeap(HeapNode heapNodes[]) {
|
||||
this.heapNodes = heapNodes;
|
||||
heapifyFromLastLeafsParent();
|
||||
}
|
||||
|
||||
void heapifyFromLastLeafsParent() {
|
||||
int lastLeafsParentIndex = getParentNodeIndex(heapNodes.length);
|
||||
while (lastLeafsParentIndex >= 0) {
|
||||
heapify(lastLeafsParentIndex);
|
||||
lastLeafsParentIndex--;
|
||||
}
|
||||
}
|
||||
|
||||
void heapify(int index) {
|
||||
int leftNodeIndex = getLeftNodeIndex(index);
|
||||
int rightNodeIndex = getRightNodeIndex(index);
|
||||
int smallestElementIndex = index;
|
||||
if (leftNodeIndex < heapNodes.length && heapNodes[leftNodeIndex].element < heapNodes[index].element) {
|
||||
smallestElementIndex = leftNodeIndex;
|
||||
}
|
||||
if (rightNodeIndex < heapNodes.length && heapNodes[rightNodeIndex].element < heapNodes[smallestElementIndex].element) {
|
||||
smallestElementIndex = rightNodeIndex;
|
||||
}
|
||||
if (smallestElementIndex != index) {
|
||||
swap(index, smallestElementIndex);
|
||||
heapify(smallestElementIndex);
|
||||
}
|
||||
}
|
||||
|
||||
int getParentNodeIndex(int index) {
|
||||
return (index - 1) / 2;
|
||||
}
|
||||
|
||||
int getLeftNodeIndex(int index) {
|
||||
return (2 * index + 1);
|
||||
}
|
||||
|
||||
int getRightNodeIndex(int index) {
|
||||
return (2 * index + 2);
|
||||
}
|
||||
|
||||
HeapNode getRootNode() {
|
||||
return heapNodes[0];
|
||||
}
|
||||
|
||||
void heapifyFromRoot() {
|
||||
heapify(0);
|
||||
}
|
||||
|
||||
void swap(int i, int j) {
|
||||
HeapNode temp = heapNodes[i];
|
||||
heapNodes[i] = heapNodes[j];
|
||||
heapNodes[j] = temp;
|
||||
}
|
||||
|
||||
static int[] merge(int[][] array) {
|
||||
HeapNode[] heapNodes = new HeapNode[array.length];
|
||||
int resultingArraySize = 0;
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
HeapNode node = new HeapNode(array[i][0], i);
|
||||
heapNodes[i] = node;
|
||||
resultingArraySize += array[i].length;
|
||||
}
|
||||
|
||||
MinHeap minHeap = new MinHeap(heapNodes);
|
||||
int[] resultingArray = new int[resultingArraySize];
|
||||
|
||||
for (int i = 0; i < resultingArraySize; i++) {
|
||||
HeapNode root = minHeap.getRootNode();
|
||||
resultingArray[i] = root.element;
|
||||
|
||||
if (root.nextElementIndex < array[root.arrayIndex].length) {
|
||||
root.element = array[root.arrayIndex][root.nextElementIndex++];
|
||||
} else {
|
||||
root.element = Integer.MAX_VALUE;
|
||||
}
|
||||
minHeap.heapifyFromRoot();
|
||||
}
|
||||
return resultingArray;
|
||||
}
|
||||
}
|
||||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
public class MinHeap {
|
||||
|
||||
HeapNode[] heapNodes;
|
||||
|
||||
public MinHeap(HeapNode heapNodes[]) {
|
||||
this.heapNodes = heapNodes;
|
||||
heapifyFromLastLeafsParent();
|
||||
}
|
||||
|
||||
void heapifyFromLastLeafsParent() {
|
||||
int lastLeafsParentIndex = getParentNodeIndex(heapNodes.length);
|
||||
while (lastLeafsParentIndex >= 0) {
|
||||
heapify(lastLeafsParentIndex);
|
||||
lastLeafsParentIndex--;
|
||||
}
|
||||
}
|
||||
|
||||
void heapify(int index) {
|
||||
int leftNodeIndex = getLeftNodeIndex(index);
|
||||
int rightNodeIndex = getRightNodeIndex(index);
|
||||
int smallestElementIndex = index;
|
||||
if (leftNodeIndex < heapNodes.length && heapNodes[leftNodeIndex].element < heapNodes[index].element) {
|
||||
smallestElementIndex = leftNodeIndex;
|
||||
}
|
||||
if (rightNodeIndex < heapNodes.length && heapNodes[rightNodeIndex].element < heapNodes[smallestElementIndex].element) {
|
||||
smallestElementIndex = rightNodeIndex;
|
||||
}
|
||||
if (smallestElementIndex != index) {
|
||||
swap(index, smallestElementIndex);
|
||||
heapify(smallestElementIndex);
|
||||
}
|
||||
}
|
||||
|
||||
int getParentNodeIndex(int index) {
|
||||
return (index - 1) / 2;
|
||||
}
|
||||
|
||||
int getLeftNodeIndex(int index) {
|
||||
return (2 * index + 1);
|
||||
}
|
||||
|
||||
int getRightNodeIndex(int index) {
|
||||
return (2 * index + 2);
|
||||
}
|
||||
|
||||
HeapNode getRootNode() {
|
||||
return heapNodes[0];
|
||||
}
|
||||
|
||||
void heapifyFromRoot() {
|
||||
heapify(0);
|
||||
}
|
||||
|
||||
void swap(int i, int j) {
|
||||
HeapNode temp = heapNodes[i];
|
||||
heapNodes[i] = heapNodes[j];
|
||||
heapNodes[j] = temp;
|
||||
}
|
||||
|
||||
static int[] merge(int[][] array) {
|
||||
HeapNode[] heapNodes = new HeapNode[array.length];
|
||||
int resultingArraySize = 0;
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
HeapNode node = new HeapNode(array[i][0], i);
|
||||
heapNodes[i] = node;
|
||||
resultingArraySize += array[i].length;
|
||||
}
|
||||
|
||||
MinHeap minHeap = new MinHeap(heapNodes);
|
||||
int[] resultingArray = new int[resultingArraySize];
|
||||
|
||||
for (int i = 0; i < resultingArraySize; i++) {
|
||||
HeapNode root = minHeap.getRootNode();
|
||||
resultingArray[i] = root.element;
|
||||
|
||||
if (root.nextElementIndex < array[root.arrayIndex].length) {
|
||||
root.element = array[root.arrayIndex][root.nextElementIndex++];
|
||||
} else {
|
||||
root.element = Integer.MAX_VALUE;
|
||||
}
|
||||
minHeap.heapifyFromRoot();
|
||||
}
|
||||
return resultingArray;
|
||||
}
|
||||
}
|
@ -1,22 +1,22 @@
|
||||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MinHeapUnitTest {
|
||||
|
||||
private final int[][] inputArray = { { 0, 6 }, { 1, 5, 10, 100 }, { 2, 4, 200, 650 } };
|
||||
private final int[] expectedArray = { 0, 1, 2, 4, 5, 6, 10, 100, 200, 650 };
|
||||
|
||||
@Test
|
||||
public void givenSortedArrays_whenMerged_thenShouldReturnASingleSortedarray() {
|
||||
int[] resultArray = MinHeap.merge(inputArray);
|
||||
|
||||
assertThat(resultArray.length, is(equalTo(10)));
|
||||
assertThat(resultArray, is(equalTo(expectedArray)));
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.algorithms.minheapmerge;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MinHeapUnitTest {
|
||||
|
||||
private final int[][] inputArray = { { 0, 6 }, { 1, 5, 10, 100 }, { 2, 4, 200, 650 } };
|
||||
private final int[] expectedArray = { 0, 1, 2, 4, 5, 6, 10, 100, 200, 650 };
|
||||
|
||||
@Test
|
||||
public void givenSortedArrays_whenMerged_thenShouldReturnASingleSortedarray() {
|
||||
int[] resultArray = MinHeap.merge(inputArray);
|
||||
|
||||
assertThat(resultArray.length, is(equalTo(10)));
|
||||
assertThat(resultArray, is(equalTo(expectedArray)));
|
||||
}
|
||||
|
||||
}
|
@ -3,7 +3,7 @@
|
||||
This module contains articles about Apache CXF
|
||||
|
||||
## Relevant Articles:
|
||||
- [Introduction to Apache CXF Aegis Data Binding](https://www.baeldung.com/aegis-data-binding-in-apache-cxf)
|
||||
|
||||
- [Apache CXF Support for RESTful Web Services](https://www.baeldung.com/apache-cxf-rest-api)
|
||||
- [A Guide to Apache CXF with Spring](https://www.baeldung.com/apache-cxf-with-spring)
|
||||
- [Introduction to Apache CXF](https://www.baeldung.com/introduction-to-apache-cxf)
|
||||
|
3
apache-cxf/cxf-aegis/README.md
Normal file
3
apache-cxf/cxf-aegis/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Introduction to Apache CXF Aegis Data Binding](https://www.baeldung.com/aegis-data-binding-in-apache-cxf)
|
@ -10,9 +10,9 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-1</relativePath>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -1,11 +1,14 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authc.*;
|
||||
import org.apache.shiro.authc.AuthenticationException;
|
||||
import org.apache.shiro.authc.IncorrectCredentialsException;
|
||||
import org.apache.shiro.authc.LockedAccountException;
|
||||
import org.apache.shiro.authc.UnknownAccountException;
|
||||
import org.apache.shiro.authc.UsernamePasswordToken;
|
||||
import org.apache.shiro.mgt.DefaultSecurityManager;
|
||||
import org.apache.shiro.mgt.SecurityManager;
|
||||
import org.apache.shiro.realm.Realm;
|
||||
import org.apache.shiro.realm.text.IniRealm;
|
||||
import org.apache.shiro.session.Session;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -1,16 +1,24 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.apache.shiro.authc.*;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.shiro.authc.AuthenticationException;
|
||||
import org.apache.shiro.authc.AuthenticationInfo;
|
||||
import org.apache.shiro.authc.AuthenticationToken;
|
||||
import org.apache.shiro.authc.SimpleAuthenticationInfo;
|
||||
import org.apache.shiro.authc.UnknownAccountException;
|
||||
import org.apache.shiro.authc.UsernamePasswordToken;
|
||||
import org.apache.shiro.authz.AuthorizationInfo;
|
||||
import org.apache.shiro.authz.SimpleAuthorizationInfo;
|
||||
import org.apache.shiro.realm.jdbc.JdbcRealm;
|
||||
import org.apache.shiro.subject.PrincipalCollection;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
public class MyCustomRealm extends JdbcRealm {
|
||||
|
||||
|
@ -1,14 +1,15 @@
|
||||
package com.baeldung.shiro.permissions.custom;
|
||||
|
||||
import com.baeldung.MyCustomRealm;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authc.*;
|
||||
import org.apache.shiro.authc.AuthenticationException;
|
||||
import org.apache.shiro.authc.IncorrectCredentialsException;
|
||||
import org.apache.shiro.authc.LockedAccountException;
|
||||
import org.apache.shiro.authc.UnknownAccountException;
|
||||
import org.apache.shiro.authc.UsernamePasswordToken;
|
||||
import org.apache.shiro.config.Ini;
|
||||
import org.apache.shiro.mgt.DefaultSecurityManager;
|
||||
import org.apache.shiro.mgt.SecurityManager;
|
||||
import org.apache.shiro.realm.Realm;
|
||||
import org.apache.shiro.realm.text.IniRealm;
|
||||
import org.apache.shiro.session.Session;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -1,7 +1,6 @@
|
||||
## Atomikos
|
||||
|
||||
This module contains articles about Atomikos
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Guide Transactions Using Atomikos]()
|
||||
- [A Guide to Atomikos](https://www.baeldung.com/java-atomikos)
|
||||
|
56
aws-app-sync/pom.xml
Normal file
56
aws-app-sync/pom.xml
Normal file
@ -0,0 +1,56 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.6.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>aws-app-sync</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>aws-app-sync</name>
|
||||
|
||||
<description>Spring Boot using AWS App Sync</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,32 @@
|
||||
package com.baeldung.awsappsync;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
public class AppSyncClientHelper {
|
||||
|
||||
static String apiUrl = "https://m4i3b6icrrb7livfbypfspiifi.appsync-api.us-east-2.amazonaws.com";
|
||||
static String apiKey = "da2-bm4rpatkkrc5jfyhvvq7itjeke";
|
||||
static String API_KEY_HEADER = "x-api-key";
|
||||
|
||||
public static WebClient.ResponseSpec getResponseBodySpec(Map<String, Object> requestBody) {
|
||||
return WebClient
|
||||
.builder()
|
||||
.baseUrl(apiUrl)
|
||||
.defaultHeader(API_KEY_HEADER, apiKey)
|
||||
.defaultHeader("Content-Type", "application/json")
|
||||
.build()
|
||||
.method(HttpMethod.POST)
|
||||
.uri("/graphql")
|
||||
.body(BodyInserters.fromValue(requestBody))
|
||||
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
|
||||
.acceptCharset(StandardCharsets.UTF_8)
|
||||
.retrieve();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.awsappsync;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AwsAppSyncApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AwsAppSyncApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package com.baeldung.awsappsync;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
class AwsAppSyncApplicationTests {
|
||||
|
||||
@Test
|
||||
void givenGraphQuery_whenListEvents_thenReturnAllEvents() {
|
||||
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("query", "query ListEvents {"
|
||||
+ " listEvents {"
|
||||
+ " items {"
|
||||
+ " id"
|
||||
+ " name"
|
||||
+ " where"
|
||||
+ " when"
|
||||
+ " description"
|
||||
+ " }"
|
||||
+ " }"
|
||||
+ "}");
|
||||
requestBody.put("variables", "");
|
||||
requestBody.put("operationName", "ListEvents");
|
||||
|
||||
String bodyString = AppSyncClientHelper.getResponseBodySpec(requestBody)
|
||||
.bodyToMono(String.class).block();
|
||||
|
||||
assertNotNull(bodyString);
|
||||
assertTrue(bodyString.contains("My First Event"));
|
||||
assertTrue(bodyString.contains("where"));
|
||||
assertTrue(bodyString.contains("when"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenGraphAdd_whenMutation_thenReturnIdNameDesc() {
|
||||
|
||||
String queryString = "mutation add {"
|
||||
+ " createEvent("
|
||||
+ " name:\"My added GraphQL event\""
|
||||
+ " where:\"Day 2\""
|
||||
+ " when:\"Saturday night\""
|
||||
+ " description:\"Studying GraphQL\""
|
||||
+ " ){"
|
||||
+ " id"
|
||||
+ " name"
|
||||
+ " description"
|
||||
+ " }"
|
||||
+ "}";
|
||||
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("query", queryString);
|
||||
requestBody.put("variables", "");
|
||||
requestBody.put("operationName", "add");
|
||||
|
||||
WebClient.ResponseSpec response = AppSyncClientHelper.getResponseBodySpec(requestBody);
|
||||
|
||||
String bodyString = response.bodyToMono(String.class).block();
|
||||
|
||||
assertNotNull(bodyString);
|
||||
assertTrue(bodyString.contains("My added GraphQL event"));
|
||||
assertFalse(bodyString.contains("where"));
|
||||
assertFalse(bodyString.contains("when"));
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Building Java Applications with Bazel](https://www.baeldung.com/bazel-build-tool)
|
@ -13,4 +13,5 @@ This module contains articles about core Groovy concepts
|
||||
- [Metaprogramming in Groovy](https://www.baeldung.com/groovy-metaprogramming)
|
||||
- [A Quick Guide to Working with Web Services in Groovy](https://www.baeldung.com/groovy-web-services)
|
||||
- [Categories in Groovy](https://www.baeldung.com/groovy-categories)
|
||||
- [How to Determine the Data Type in Groovy](https://www.baeldung.com/groovy-determine-data-type)
|
||||
- [[<-- Prev]](/core-groovy)
|
||||
|
@ -12,4 +12,5 @@ This module contains articles about core Groovy concepts
|
||||
- [Closures in Groovy](https://www.baeldung.com/groovy-closures)
|
||||
- [Converting a String to a Date in Groovy](https://www.baeldung.com/groovy-string-to-date)
|
||||
- [Guide to I/O in Groovy](https://www.baeldung.com/groovy-io)
|
||||
- [[More -->]](/core-groovy-2)
|
||||
- [Convert String to Integer in Groovy](https://www.baeldung.com/groovy-convert-string-to-integer)
|
||||
- [[More -->]](/core-groovy-2)
|
||||
|
@ -8,3 +8,4 @@ This module contains articles about Java 14.
|
||||
- [Java Text Blocks](https://www.baeldung.com/java-text-blocks)
|
||||
- [Pattern Matching for instanceof in Java 14](https://www.baeldung.com/java-pattern-matching-instanceof)
|
||||
- [Helpful NullPointerExceptions in Java 14](https://www.baeldung.com/java-14-nullpointerexception)
|
||||
- [Foreign Memory Access API in Java 14](https://www.baeldung.com/java-foreign-memory-access)
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.java14.helpfulnullpointerexceptions;
|
||||
package com.baeldung.java14.npe;
|
||||
|
||||
public class HelpfulNullPointerException {
|
||||
|
@ -0,0 +1,22 @@
|
||||
package com.baeldung.java14.record;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record Person (String name, String address) {
|
||||
|
||||
public static String UNKWOWN_ADDRESS = "Unknown";
|
||||
public static String UNNAMED = "Unnamed";
|
||||
|
||||
public Person {
|
||||
Objects.requireNonNull(name);
|
||||
Objects.requireNonNull(address);
|
||||
}
|
||||
|
||||
public Person(String name) {
|
||||
this(name, UNKWOWN_ADDRESS);
|
||||
}
|
||||
|
||||
public static Person unnamed(String address) {
|
||||
return new Person(UNNAMED, address);
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package com.baeldung.java14.foreign.api;
|
||||
|
||||
import jdk.incubator.foreign.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
public class ForeignMemoryUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenAValueIsSet_thenAccessTheValue() {
|
||||
long value = 10;
|
||||
MemoryAddress memoryAddress =
|
||||
MemorySegment.allocateNative(8).baseAddress();
|
||||
VarHandle varHandle = MemoryHandles.varHandle(long.class,
|
||||
ByteOrder.nativeOrder());
|
||||
varHandle.set(memoryAddress, value);
|
||||
assertThat(varHandle.get(memoryAddress), is(value));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMultipleValuesAreSet_thenAccessAll() {
|
||||
VarHandle varHandle = MemoryHandles.varHandle(int.class,
|
||||
ByteOrder.nativeOrder());
|
||||
|
||||
try(MemorySegment memorySegment =
|
||||
MemorySegment.allocateNative(100)) {
|
||||
MemoryAddress base = memorySegment.baseAddress();
|
||||
for(int i=0; i<25; i++) {
|
||||
varHandle.set(base.addOffset((i*4)), i);
|
||||
}
|
||||
for(int i=0; i<25; i++) {
|
||||
assertThat(varHandle.get(base.addOffset((i*4))), is(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSetValuesWithMemoryLayout_thenTheyCanBeRetrieved() {
|
||||
SequenceLayout sequenceLayout =
|
||||
MemoryLayout.ofSequence(25,
|
||||
MemoryLayout.ofValueBits(64, ByteOrder.nativeOrder()));
|
||||
VarHandle varHandle =
|
||||
sequenceLayout.varHandle(long.class,
|
||||
MemoryLayout.PathElement.sequenceElement());
|
||||
|
||||
try(MemorySegment memorySegment =
|
||||
MemorySegment.allocateNative(sequenceLayout)) {
|
||||
MemoryAddress base = memorySegment.baseAddress();
|
||||
for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) {
|
||||
varHandle.set(base, i, i);
|
||||
}
|
||||
for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) {
|
||||
assertThat(varHandle.get(base, i), is(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSlicingMemorySegment_thenTheyCanBeAccessedIndividually() {
|
||||
MemoryAddress memoryAddress =
|
||||
MemorySegment.allocateNative(12).baseAddress();
|
||||
MemoryAddress memoryAddress1 =
|
||||
memoryAddress.segment().asSlice(0,4).baseAddress();
|
||||
MemoryAddress memoryAddress2 =
|
||||
memoryAddress.segment().asSlice(4,4).baseAddress();
|
||||
MemoryAddress memoryAddress3 =
|
||||
memoryAddress.segment().asSlice(8,4).baseAddress();
|
||||
|
||||
VarHandle intHandle =
|
||||
MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
|
||||
intHandle.set(memoryAddress1, Integer.MIN_VALUE);
|
||||
intHandle.set(memoryAddress2, 0);
|
||||
intHandle.set(memoryAddress3, Integer.MAX_VALUE);
|
||||
|
||||
assertThat(intHandle.get(memoryAddress1), is(Integer.MIN_VALUE));
|
||||
assertThat(intHandle.get(memoryAddress2), is(0));
|
||||
assertThat(intHandle.get(memoryAddress3), is(Integer.MAX_VALUE));
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.baeldung.java14.helpfulnullpointerexceptions;
|
||||
package com.baeldung.java14.npe;
|
||||
|
||||
import org.junit.Test;
|
||||
|
@ -0,0 +1,150 @@
|
||||
package com.baeldung.java14.record;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PersonTest {
|
||||
|
||||
@Test
|
||||
public void givenSameNameAndAddress_whenEquals_thenPersonsEqual() {
|
||||
|
||||
String name = "John Doe";
|
||||
String address = "100 Linda Ln.";
|
||||
|
||||
Person person1 = new Person(name, address);
|
||||
Person person2 = new Person(name, address);
|
||||
|
||||
assertTrue(person1.equals(person2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentObject_whenEquals_thenNotEqual() {
|
||||
|
||||
Person person = new Person("John Doe", "100 Linda Ln.");
|
||||
|
||||
assertFalse(person.equals(new Object()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentName_whenEquals_thenPersonsNotEqual() {
|
||||
|
||||
String address = "100 Linda Ln.";
|
||||
|
||||
Person person1 = new Person("Jane Doe", address);
|
||||
Person person2 = new Person("John Doe", address);
|
||||
|
||||
assertFalse(person1.equals(person2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentAddress_whenEquals_thenPersonsNotEqual() {
|
||||
|
||||
String name = "John Doe";
|
||||
|
||||
Person person1 = new Person(name, "100 Linda Ln.");
|
||||
Person person2 = new Person(name, "200 London Ave.");
|
||||
|
||||
assertFalse(person1.equals(person2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameNameAndAddress_whenHashCode_thenPersonsEqual() {
|
||||
|
||||
String name = "John Doe";
|
||||
String address = "100 Linda Ln.";
|
||||
|
||||
Person person1 = new Person(name, address);
|
||||
Person person2 = new Person(name, address);
|
||||
|
||||
assertEquals(person1.hashCode(), person2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentObject_whenHashCode_thenNotEqual() {
|
||||
|
||||
Person person = new Person("John Doe", "100 Linda Ln.");
|
||||
|
||||
assertNotEquals(person.hashCode(), new Object().hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentName_whenHashCode_thenPersonsNotEqual() {
|
||||
|
||||
String address = "100 Linda Ln.";
|
||||
|
||||
Person person1 = new Person("Jane Doe", address);
|
||||
Person person2 = new Person("John Doe", address);
|
||||
|
||||
assertNotEquals(person1.hashCode(), person2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentAddress_whenHashCode_thenPersonsNotEqual() {
|
||||
|
||||
String name = "John Doe";
|
||||
|
||||
Person person1 = new Person(name, "100 Linda Ln.");
|
||||
Person person2 = new Person(name, "200 London Ave.");
|
||||
|
||||
assertNotEquals(person1.hashCode(), person2.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidNameAndAddress_whenGetNameAndAddress_thenExpectedValuesReturned() {
|
||||
|
||||
String name = "John Doe";
|
||||
String address = "100 Linda Ln.";
|
||||
|
||||
Person person = new Person(name, address);
|
||||
|
||||
assertEquals(name, person.name());
|
||||
assertEquals(address, person.address());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidNameAndAddress_whenToString_thenCorrectStringReturned() {
|
||||
|
||||
String name = "John Doe";
|
||||
String address = "100 Linda Ln.";
|
||||
|
||||
Person person = new Person(name, address);
|
||||
|
||||
assertEquals("Person[name=" + name + ", address=" + address + "]", person.toString());
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullName_whenConstruct_thenErrorThrown() {
|
||||
new Person(null, "100 Linda Ln.");
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void givenNullAddress_whenConstruct_thenErrorThrown() {
|
||||
new Person("John Doe", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnknownAddress_whenConstructing_thenAddressPopulated() {
|
||||
|
||||
String name = "John Doe";
|
||||
|
||||
Person person = new Person(name);
|
||||
|
||||
assertEquals(name, person.name());
|
||||
assertEquals(Person.UNKWOWN_ADDRESS, person.address());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnnamed_whenConstructingThroughFactory_thenNamePopulated() {
|
||||
|
||||
String address = "100 Linda Ln.";
|
||||
|
||||
Person person = Person.unnamed(address);
|
||||
|
||||
assertEquals(Person.UNNAMED, person.name());
|
||||
assertEquals(address, person.address());
|
||||
}
|
||||
}
|
@ -5,5 +5,5 @@ This module contains articles about sorting arrays in Java
|
||||
### Relevant Articles:
|
||||
- [Sorting Arrays in Java](https://www.baeldung.com/java-sorting-arrays)
|
||||
- [Checking If an Array Is Sorted in Java](https://www.baeldung.com/java-check-sorted-array)
|
||||
- [How to Reverse an Array in Java](http://www.baeldung.com/java-invert-array)
|
||||
- [How to Reverse an Array in Java](https://www.baeldung.com/java-invert-array)
|
||||
- [Arrays.sort vs Arrays.parallelSort](https://www.baeldung.com/java-arrays-sort-vs-parallelsort)
|
||||
|
@ -5,4 +5,5 @@ This module contains articles about Map data structures in Java.
|
||||
### Relevant Articles:
|
||||
- [Java TreeMap vs HashMap](https://www.baeldung.com/java-treemap-vs-hashmap)
|
||||
- [Comparing Two HashMaps in Java](https://www.baeldung.com/java-compare-hashmaps)
|
||||
- [The Map.computeIfAbsent() Method](https://www.baeldung.com/java-map-computeifabsent)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-2)
|
||||
|
@ -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);
|
@ -11,4 +11,5 @@ This module contains articles about advanced topics about multithreading with co
|
||||
- [Guide to Work Stealing in Java](https://www.baeldung.com/java-work-stealing)
|
||||
- [Asynchronous Programming in Java](https://www.baeldung.com/java-asynchronous-programming)
|
||||
- [Java Thread Deadlock and Livelock](https://www.baeldung.com/java-deadlock-livelock)
|
||||
- [Guide to AtomicStampedReference in Java](https://www.baeldung.com/java-atomicstampedreference)
|
||||
- [[<-- previous]](/core-java-modules/core-java-concurrency-advanced-2)
|
||||
|
@ -0,0 +1,60 @@
|
||||
package com.baeldung.deadlockAndLivelock;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class DeadlockExample {
|
||||
|
||||
private Lock lock1 = new ReentrantLock(true);
|
||||
private Lock lock2 = new ReentrantLock(true);
|
||||
|
||||
public static void main(String[] args) {
|
||||
DeadlockExample deadlock = new DeadlockExample();
|
||||
new Thread(deadlock::operation1, "T1").start();
|
||||
new Thread(deadlock::operation2, "T2").start();
|
||||
|
||||
}
|
||||
|
||||
public void operation1() {
|
||||
lock1.lock();
|
||||
print("lock1 acquired, waiting to acquire lock2.");
|
||||
sleep(50);
|
||||
|
||||
lock2.lock();
|
||||
print("lock2 acquired");
|
||||
|
||||
print("executing first operation.");
|
||||
|
||||
lock2.unlock();
|
||||
lock1.unlock();
|
||||
|
||||
}
|
||||
|
||||
public void operation2() {
|
||||
lock2.lock();
|
||||
print("lock2 acquired, waiting to acquire lock1.");
|
||||
sleep(50);
|
||||
|
||||
lock1.lock();
|
||||
print("lock1 acquired");
|
||||
|
||||
print("executing second operation.");
|
||||
|
||||
lock1.unlock();
|
||||
lock2.unlock();
|
||||
}
|
||||
|
||||
public void print(String message) {
|
||||
System.out.println("Thread " + Thread.currentThread()
|
||||
.getName() + ": " + message);
|
||||
}
|
||||
|
||||
public void sleep(long millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package com.baeldung.deadlockAndLivelock;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class LivelockExample {
|
||||
|
||||
private Lock lock1 = new ReentrantLock(true);
|
||||
private Lock lock2 = new ReentrantLock(true);
|
||||
|
||||
public static void main(String[] args) {
|
||||
LivelockExample livelock = new LivelockExample();
|
||||
new Thread(livelock::operation1, "T1").start();
|
||||
new Thread(livelock::operation2, "T2").start();
|
||||
|
||||
}
|
||||
|
||||
public void operation1() {
|
||||
while (true) {
|
||||
tryLock(lock1, 50);
|
||||
print("lock1 acquired, trying to acquire lock2.");
|
||||
sleep(50);
|
||||
|
||||
if (tryLock(lock2)) {
|
||||
print("lock2 acquired.");
|
||||
} else {
|
||||
print("cannot acquire lock2, releasing lock1.");
|
||||
lock1.unlock();
|
||||
continue;
|
||||
}
|
||||
|
||||
print("executing first operation.");
|
||||
break;
|
||||
}
|
||||
lock2.unlock();
|
||||
lock1.unlock();
|
||||
}
|
||||
|
||||
public void operation2() {
|
||||
while (true) {
|
||||
tryLock(lock2, 50);
|
||||
print("lock2 acquired, trying to acquire lock1.");
|
||||
sleep(50);
|
||||
|
||||
if (tryLock(lock1)) {
|
||||
print("lock1 acquired.");
|
||||
} else {
|
||||
print("cannot acquire lock1, releasing lock2.");
|
||||
lock2.unlock();
|
||||
continue;
|
||||
}
|
||||
|
||||
print("executing second operation.");
|
||||
break;
|
||||
}
|
||||
lock1.unlock();
|
||||
lock2.unlock();
|
||||
}
|
||||
|
||||
public void print(String message) {
|
||||
System.out.println("Thread " + Thread.currentThread()
|
||||
.getName() + ": " + message);
|
||||
}
|
||||
|
||||
public void sleep(long millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void tryLock(Lock lock, long millis) {
|
||||
try {
|
||||
lock.tryLock(10, TimeUnit.MILLISECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean tryLock(Lock lock) {
|
||||
return lock.tryLock();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package com.baeldung.lockfree;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class NonBlockingQueue<T> {
|
||||
|
||||
private final AtomicReference<Node<T>> head, tail;
|
||||
private final AtomicInteger size;
|
||||
|
||||
public NonBlockingQueue() {
|
||||
head = new AtomicReference<>(null);
|
||||
tail = new AtomicReference<>(null);
|
||||
size = new AtomicInteger();
|
||||
size.set(0);
|
||||
}
|
||||
|
||||
public void add(T element) {
|
||||
if (element == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
|
||||
Node<T> node = new Node<>(element);
|
||||
Node<T> currentTail;
|
||||
do {
|
||||
currentTail = tail.get();
|
||||
node.setPrevious(currentTail);
|
||||
} while(!tail.compareAndSet(currentTail, node));
|
||||
|
||||
if(node.previous != null) {
|
||||
node.previous.next = node;
|
||||
}
|
||||
|
||||
head.compareAndSet(null, node); //if we are inserting the first element
|
||||
size.incrementAndGet();
|
||||
}
|
||||
|
||||
public T get() {
|
||||
if(head.get() == null) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
Node<T> currentHead;
|
||||
Node<T> nextNode;
|
||||
do {
|
||||
currentHead = head.get();
|
||||
nextNode = currentHead.getNext();
|
||||
} while(!head.compareAndSet(currentHead, nextNode));
|
||||
|
||||
size.decrementAndGet();
|
||||
return currentHead.getValue();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.size.get();
|
||||
}
|
||||
|
||||
private class Node<T> {
|
||||
private final T value;
|
||||
private volatile Node<T> next;
|
||||
private volatile Node<T> previous;
|
||||
|
||||
public Node(T value) {
|
||||
this.value = value;
|
||||
this.next = null;
|
||||
}
|
||||
|
||||
public T getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public Node<T> getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setPrevious(Node<T> previous) {
|
||||
this.previous = previous;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package com.baeldung.exchanger;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.Exchanger;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import org.junit.Test;
|
||||
|
||||
import static java.util.concurrent.CompletableFuture.runAsync;
|
||||
|
||||
|
||||
|
||||
public class ExchangerPipeLineManualTest {
|
||||
|
||||
private static final int BUFFER_SIZE = 100;
|
||||
|
||||
@Test
|
||||
public void givenData_whenPassedThrough_thenCorrect() throws InterruptedException, ExecutionException {
|
||||
|
||||
Exchanger<Queue<String>> readerExchanger = new Exchanger<>();
|
||||
Exchanger<Queue<String>> writerExchanger = new Exchanger<>();
|
||||
int counter = 0;
|
||||
|
||||
Runnable reader = () -> {
|
||||
Queue<String> readerBuffer = new ConcurrentLinkedQueue<>();
|
||||
while (true) {
|
||||
readerBuffer.add(UUID.randomUUID().toString());
|
||||
if (readerBuffer.size() >= BUFFER_SIZE) {
|
||||
try {
|
||||
readerBuffer = readerExchanger.exchange(readerBuffer);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Runnable processor = () -> {
|
||||
Queue<String> processorBuffer = new ConcurrentLinkedQueue<>();
|
||||
Queue<String> writterBuffer = new ConcurrentLinkedQueue<>();
|
||||
try {
|
||||
processorBuffer = readerExchanger.exchange(processorBuffer);
|
||||
while (true) {
|
||||
writterBuffer.add(processorBuffer.poll());
|
||||
if (processorBuffer.isEmpty()) {
|
||||
try {
|
||||
processorBuffer = readerExchanger.exchange(processorBuffer);
|
||||
writterBuffer = writerExchanger.exchange(writterBuffer);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
|
||||
Runnable writer = () -> {
|
||||
Queue<String> writterBuffer = new ConcurrentLinkedQueue<>();
|
||||
try {
|
||||
writterBuffer = writerExchanger.exchange(writterBuffer);
|
||||
while (true) {
|
||||
System.out.println(writterBuffer.poll());
|
||||
if (writterBuffer.isEmpty()) {
|
||||
writterBuffer = writerExchanger.exchange(writterBuffer);
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
|
||||
CompletableFuture.allOf(runAsync(reader), runAsync(processor), runAsync(writer)).get();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.baeldung.exchanger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Exchanger;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import org.junit.Test;
|
||||
|
||||
import static java.util.concurrent.CompletableFuture.runAsync;
|
||||
|
||||
public class ExchangerUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void givenThreads_whenMessageExchanged_thenCorrect() {
|
||||
Exchanger<String> exchanger = new Exchanger<>();
|
||||
|
||||
Runnable taskA = () -> {
|
||||
try {
|
||||
String message = exchanger.exchange("from A");
|
||||
assertEquals("from B", message);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
|
||||
Runnable taskB = () -> {
|
||||
try {
|
||||
String message = exchanger.exchange("from B");
|
||||
assertEquals("from A", message);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
|
||||
CompletableFuture.allOf(runAsync(taskA), runAsync(taskB)).join();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThread_WhenExchangedMessage_thenCorrect() throws InterruptedException, ExecutionException {
|
||||
Exchanger<String> exchanger = new Exchanger<>();
|
||||
|
||||
Runnable runner = () -> {
|
||||
try {
|
||||
String message = exchanger.exchange("from runner");
|
||||
assertEquals("to runner", message);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
|
||||
CompletableFuture<Void> result = CompletableFuture.runAsync(runner);
|
||||
String msg = exchanger.exchange("to runner");
|
||||
assertEquals("from runner", msg);
|
||||
result.join();
|
||||
}
|
||||
|
||||
}
|
@ -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,13 +0,0 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
@ -0,0 +1,41 @@
|
||||
package com.baeldung.weeknumber;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class WeekNumberUsingCalendar {
|
||||
|
||||
public int getWeekNumberFrom(String day, String dateFormat, Locale locale) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||||
|
||||
Calendar calendar = Calendar.getInstance(locale);
|
||||
Date date = sdf.parse(day);
|
||||
calendar.setTime(date);
|
||||
|
||||
return calendar.get(Calendar.WEEK_OF_YEAR);
|
||||
}
|
||||
|
||||
public int getWeekNumberFrom(int year, int month, int day, Locale locale) {
|
||||
Calendar calendar = Calendar.getInstance(locale);
|
||||
calendar.set(year, month, day);
|
||||
|
||||
return calendar.get(Calendar.WEEK_OF_YEAR);
|
||||
}
|
||||
|
||||
public int getWeekNumberFrom(int year, int month, int day, int firstDayOfWeek, int minimalDaysInFirstWeek, Locale locale) {
|
||||
Calendar calendar = Calendar.getInstance(locale);
|
||||
calendar.setFirstDayOfWeek(firstDayOfWeek);
|
||||
calendar.setMinimalDaysInFirstWeek(minimalDaysInFirstWeek);
|
||||
calendar.set(year, month, day);
|
||||
|
||||
return calendar.get(Calendar.WEEK_OF_YEAR);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
System.out.println(calendar.getWeekNumberFrom(2020, 2, 22, Locale.CANADA));
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.weeknumber;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.time.temporal.WeekFields;
|
||||
import java.util.Locale;
|
||||
|
||||
public class WeekNumberUsingLocalDate {
|
||||
|
||||
public Integer getWeekNumberUsingWeekFiedsFrom(String day, String dayFormat, Locale locale) {
|
||||
LocalDate date = LocalDate.parse(day, DateTimeFormatter.ofPattern(dayFormat));
|
||||
|
||||
return date.get(WeekFields.of(locale)
|
||||
.weekOfYear());
|
||||
}
|
||||
|
||||
public Integer getWeekNumberUsinWeekFieldsFrom(int year, int month, int day, Locale locale) {
|
||||
LocalDate date = LocalDate.of(year, month, day);
|
||||
|
||||
return date.get(WeekFields.of(locale)
|
||||
.weekOfYear());
|
||||
}
|
||||
|
||||
public Integer getWeekNumberUsingChronoFieldFrom(int year, int month, int day) {
|
||||
LocalDate date = LocalDate.of(year, month, day);
|
||||
|
||||
return date.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.baeldung.weeknumber;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class WeekNumberUsingCalendarUnitTest {
|
||||
@Test
|
||||
public void givenDateInStringAndDateFormatUsingLocaleItaly_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() throws ParseException {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
|
||||
assertEquals(12, calendar.getWeekNumberFrom("20200322", "yyyyMMdd", Locale.ITALY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInStringAndDateFormatUsingLocaleCanada_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() throws ParseException {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
|
||||
assertEquals(13, calendar.getWeekNumberFrom("20200322", "yyyyMMdd", Locale.CANADA));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInYearMonthDayNumbersLocaleItaly_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
|
||||
assertEquals(12, calendar.getWeekNumberFrom(2020, 2, 22, Locale.ITALY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInYearMonthDayNumbersLocaleItalyChangingWeekCalculationSettings_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
|
||||
assertEquals(13, calendar.getWeekNumberFrom(2020, 2, 22, Calendar.SUNDAY, 4, Locale.ITALY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInYearMonthDayNumbersLocaleCanada_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
|
||||
assertEquals(13, calendar.getWeekNumberFrom(2020, 2, 22, Locale.CANADA));
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.baeldung.weeknumber;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class WeekNumberUsingLocalDateUnitTest {
|
||||
@Test
|
||||
public void givenDateInStringAndDateFormatUsingWeekFieldsWithLocaleItaly_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() {
|
||||
WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate();
|
||||
|
||||
assertEquals(12, localDate.getWeekNumberUsingWeekFiedsFrom("20200322", "yyyyMMdd", Locale.ITALY)
|
||||
.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInStringAndDateFormatUsingWeekFieldsWithLocaleCanada_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() {
|
||||
WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate();
|
||||
|
||||
assertEquals(13, localDate.getWeekNumberUsingWeekFiedsFrom("20200322", "yyyyMMdd", Locale.CANADA)
|
||||
.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInStringAndDateFormatUsingChronoFieds_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() {
|
||||
WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate();
|
||||
|
||||
assertEquals(12, localDate.getWeekNumberUsingChronoFieldFrom(2020, 3, 22)
|
||||
.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInYearMonthDayNumbersUsingWeekFieldsWithLocaleItaly_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() {
|
||||
WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate();
|
||||
|
||||
assertEquals(12, localDate.getWeekNumberUsinWeekFieldsFrom(2020, 3, 22, Locale.ITALY)
|
||||
.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInYearMonthDayNumbersUsingWeekFieldsWithLocaleCanada_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() {
|
||||
WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate();
|
||||
|
||||
assertEquals(13, localDate.getWeekNumberUsinWeekFieldsFrom(2020, 3, 22, Locale.CANADA)
|
||||
.longValue());
|
||||
}
|
||||
}
|
@ -29,6 +29,11 @@
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons.lang3.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
@ -40,6 +45,7 @@
|
||||
|
||||
<properties>
|
||||
<javax.mail.version>1.5.0-b01</javax.mail.version>
|
||||
<commons.lang3.version>3.10</commons.lang3.version>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Arithmetic {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(Arithmetic.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
int result = 30 / 0; // Trying to divide by zero
|
||||
} catch (ArithmeticException e) {
|
||||
LOGGER.error("ArithmeticException caught!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ArrayIndexOutOfBounds {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(ArrayIndexOutOfBounds.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] nums = new int[] { 1, 2, 3 };
|
||||
|
||||
try {
|
||||
int numFromNegativeIndex = nums[-1]; // Trying to access at negative index
|
||||
int numFromGreaterIndex = nums[4]; // Trying to access at greater index
|
||||
int numFromLengthIndex = nums[3]; // Trying to access at index equal to size of the array
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
LOGGER.error("ArrayIndexOutOfBoundsException caught");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
class Animal {
|
||||
|
||||
}
|
||||
|
||||
class Dog extends Animal {
|
||||
|
||||
}
|
||||
|
||||
class Lion extends Animal {
|
||||
|
||||
}
|
||||
|
||||
public class ClassCast {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(ClassCast.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
Animal animalOne = new Dog(); // At runtime the instance is dog
|
||||
Dog bruno = (Dog) animalOne; // Downcasting
|
||||
|
||||
Animal animalTwo = new Lion(); // At runtime the instance is animal
|
||||
Dog tommy = (Dog) animalTwo; // Downcasting
|
||||
} catch (ClassCastException e) {
|
||||
LOGGER.error("ClassCastException caught!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class FileNotFound {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(FileNotFound.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
reader = new BufferedReader(new FileReader(new File("/invalid/file/location")));
|
||||
} catch (FileNotFoundException e) {
|
||||
LOGGER.error("FileNotFoundException caught!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Handler globalExceptionHandler = new Handler();
|
||||
Thread.setDefaultUncaughtExceptionHandler(globalExceptionHandler);
|
||||
new GlobalExceptionHandler().performArithmeticOperation(10, 0);
|
||||
}
|
||||
|
||||
public int performArithmeticOperation(int num1, int num2) {
|
||||
return num1/num2;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Handler implements Thread.UncaughtExceptionHandler {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(Handler.class);
|
||||
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
LOGGER.info("Unhandled exception caught!");
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class IllegalArgument {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(IllegalArgument.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Thread.sleep(-1000);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("IllegalArgumentException caught!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class IllegalState {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(IllegalState.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
List<Integer> intList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
intList.add(i);
|
||||
}
|
||||
|
||||
Iterator<Integer> intListIterator = intList.iterator(); // Initialized with index at -1
|
||||
|
||||
try {
|
||||
intListIterator.remove(); // IllegalStateException
|
||||
} catch (IllegalStateException e) {
|
||||
LOGGER.error("IllegalStateException caught!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
class ChildThread extends Thread {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(ChildThread.class);
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("InterruptedException caught!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class InterruptedExceptionExample {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ChildThread childThread = new ChildThread();
|
||||
childThread.start();
|
||||
childThread.interrupt();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MalformedURL {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(MalformedURL.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
URL baeldungURL = null;
|
||||
|
||||
try {
|
||||
baeldungURL = new URL("malformedurl");
|
||||
} catch (MalformedURLException e) {
|
||||
LOGGER.error("MalformedURLException caught!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class NullPointer {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(NullPointer.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Person personObj = null;
|
||||
|
||||
try {
|
||||
String name = personObj.personName; // Accessing the field of a null object
|
||||
personObj.personName = "Jon Doe"; // Modifying the field of a null object
|
||||
} catch (NullPointerException e) {
|
||||
LOGGER.error("NullPointerException caught!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class Person {
|
||||
|
||||
public String personName;
|
||||
|
||||
public String getPersonName() {
|
||||
return personName;
|
||||
}
|
||||
|
||||
public void setPersonName(String personName) {
|
||||
this.personName = personName;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class NumberFormat {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(NumberFormat.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String str1 = "100ABCD";
|
||||
|
||||
try {
|
||||
int x = Integer.parseInt(str1); // Converting string with inappropriate format
|
||||
int y = Integer.valueOf(str1);
|
||||
} catch (NumberFormatException e) {
|
||||
LOGGER.error("NumberFormatException caught!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ParseExceptionExample {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(ParseExceptionExample.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
DateFormat format = new SimpleDateFormat("MM, dd, yyyy");
|
||||
|
||||
try {
|
||||
format.parse("01, , 2010");
|
||||
} catch (ParseException e) {
|
||||
LOGGER.error("ParseException caught!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class StringIndexOutOfBounds {
|
||||
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(StringIndexOutOfBounds.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String str = "Hello World";
|
||||
|
||||
try {
|
||||
char charAtNegativeIndex = str.charAt(-1); // Trying to access at negative index
|
||||
char charAtLengthIndex = str.charAt(11); // Trying to access at index equal to size of the string
|
||||
} catch (StringIndexOutOfBoundsException e) {
|
||||
LOGGER.error("StringIndexOutOfBoundsException caught");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package com.baeldung.exceptions.rootcausefinder;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Utility class to find root cause exceptions.
|
||||
*/
|
||||
public class RootCauseFinder {
|
||||
|
||||
public static Throwable findCauseUsingPlainJava(Throwable throwable) {
|
||||
Objects.requireNonNull(throwable);
|
||||
Throwable rootCause = throwable;
|
||||
while (rootCause.getCause() != null) {
|
||||
rootCause = rootCause.getCause();
|
||||
}
|
||||
return rootCause;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the age of a person from a given date.
|
||||
*/
|
||||
static class AgeCalculator {
|
||||
|
||||
private AgeCalculator() {
|
||||
}
|
||||
|
||||
public static int calculateAge(String birthDate) throws CalculationException {
|
||||
if (birthDate == null || birthDate.isEmpty()) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
try {
|
||||
return Period
|
||||
.between(parseDate(birthDate), LocalDate.now())
|
||||
.getYears();
|
||||
} catch (DateParseException ex) {
|
||||
throw new CalculationException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static LocalDate parseDate(String birthDateAsString) throws DateParseException {
|
||||
|
||||
LocalDate birthDate;
|
||||
try {
|
||||
birthDate = LocalDate.parse(birthDateAsString);
|
||||
} catch (DateTimeParseException ex) {
|
||||
throw new InvalidFormatException(birthDateAsString, ex);
|
||||
}
|
||||
|
||||
if (birthDate.isAfter(LocalDate.now())) {
|
||||
throw new DateOutOfRangeException(birthDateAsString);
|
||||
}
|
||||
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class CalculationException extends Exception {
|
||||
|
||||
CalculationException(DateParseException ex) {
|
||||
super(ex);
|
||||
}
|
||||
}
|
||||
|
||||
static class DateParseException extends Exception {
|
||||
|
||||
DateParseException(String input) {
|
||||
super(input);
|
||||
}
|
||||
|
||||
DateParseException(String input, Throwable thr) {
|
||||
super(input, thr);
|
||||
}
|
||||
}
|
||||
|
||||
static class InvalidFormatException extends DateParseException {
|
||||
|
||||
InvalidFormatException(String input, Throwable thr) {
|
||||
super("Invalid date format: " + input, thr);
|
||||
}
|
||||
}
|
||||
|
||||
static class DateOutOfRangeException extends DateParseException {
|
||||
|
||||
DateOutOfRangeException(String date) {
|
||||
super("Date out of range: " + date);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||
import ch.qos.logback.core.Appender;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class GlobalExceptionHandlerUnitTest {
|
||||
|
||||
@Mock
|
||||
private Appender<ILoggingEvent> mockAppender;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.addAppender(mockAppender);
|
||||
|
||||
Handler globalExceptionHandler = new Handler();
|
||||
Thread.setDefaultUncaughtExceptionHandler(globalExceptionHandler);
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() {
|
||||
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||
logger.detachAppender(mockAppender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenArithmeticException_thenUseUncaughtExceptionHandler() throws InterruptedException {
|
||||
|
||||
Thread globalExceptionHandlerThread = new Thread() {
|
||||
public void run() {
|
||||
GlobalExceptionHandler globalExceptionHandlerObj = new GlobalExceptionHandler();
|
||||
globalExceptionHandlerObj.performArithmeticOperation(99, 0);
|
||||
}
|
||||
};
|
||||
|
||||
globalExceptionHandlerThread.start();
|
||||
globalExceptionHandlerThread.join();
|
||||
|
||||
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||
LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||
|
||||
assertThat(loggingEvent.getLevel()).isEqualTo(Level.INFO);
|
||||
assertThat(loggingEvent.getFormattedMessage()).isEqualTo("Unhandled exception caught!");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
package com.baeldung.exceptions.rootcausefinder;
|
||||
|
||||
import com.baeldung.exceptions.rootcausefinder.RootCauseFinder.CalculationException;
|
||||
import com.baeldung.exceptions.rootcausefinder.RootCauseFinder.DateOutOfRangeException;
|
||||
import com.google.common.base.Throwables;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static com.baeldung.exceptions.rootcausefinder.RootCauseFinder.AgeCalculator;
|
||||
import static com.baeldung.exceptions.rootcausefinder.RootCauseFinder.findCauseUsingPlainJava;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests the {@link RootCauseFinder}.
|
||||
*/
|
||||
public class RootCauseFinderUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenBirthDate_whenCalculatingAge_thenAgeReturned() {
|
||||
try {
|
||||
int age = AgeCalculator.calculateAge("1990-01-01");
|
||||
Assertions.assertEquals(1990, LocalDate
|
||||
.now()
|
||||
.minus(age, ChronoUnit.YEARS)
|
||||
.getYear());
|
||||
} catch (CalculationException e) {
|
||||
Assertions.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWrongFormatDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("010102");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(findCauseUsingPlainJava(ex) instanceof DateTimeParseException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutOfRangeDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("2020-04-04");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(findCauseUsingPlainJava(ex) instanceof DateOutOfRangeException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge(null);
|
||||
} catch (Exception ex) {
|
||||
assertTrue(findCauseUsingPlainJava(ex) instanceof IllegalArgumentException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWrongFormatDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("010102");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(ExceptionUtils.getRootCause(ex) instanceof DateTimeParseException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutOfRangeDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("2020-04-04");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(ExceptionUtils.getRootCause(ex) instanceof DateOutOfRangeException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWrongFormatDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("010102");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(Throwables.getRootCause(ex) instanceof DateTimeParseException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutOfRangeDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
|
||||
try {
|
||||
AgeCalculator.calculateAge("2020-04-04");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(Throwables.getRootCause(ex) instanceof DateOutOfRangeException);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -11,13 +11,26 @@ import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Scanner;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@ -46,6 +59,18 @@ public class JavaInputStreamToXUnitTest {
|
||||
assertEquals(textBuilder.toString(), originalString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingJava8_whenConvertingAnInputStreamToAString_thenCorrect() {
|
||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||
|
||||
final String text = new BufferedReader(new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())))
|
||||
.lines()
|
||||
.collect(Collectors.joining("\n"));
|
||||
|
||||
assertThat(text, equalTo(originalString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||
|
@ -0,0 +1,95 @@
|
||||
package com.baeldung.exceptions;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Utility class to find root cause exceptions.
|
||||
*/
|
||||
public class RootCauseFinder {
|
||||
|
||||
public static Throwable findCauseUsingPlainJava(Throwable throwable) {
|
||||
Objects.requireNonNull(throwable);
|
||||
Throwable rootCause = throwable;
|
||||
while (rootCause.getCause() != null) {
|
||||
rootCause = rootCause.getCause();
|
||||
}
|
||||
return rootCause;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the age of a person from a given date.
|
||||
*/
|
||||
static class AgeCalculator {
|
||||
|
||||
private AgeCalculator() {
|
||||
}
|
||||
|
||||
public static int calculateAge(String birthDate) throws CalculationException {
|
||||
if (birthDate == null || birthDate.isEmpty()) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
try {
|
||||
return Period
|
||||
.between(parseDate(birthDate), LocalDate.now())
|
||||
.getYears();
|
||||
} catch (DateParseException ex) {
|
||||
throw new CalculationException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static LocalDate parseDate(String birthDateAsString) throws DateParseException {
|
||||
|
||||
LocalDate birthDate;
|
||||
try {
|
||||
birthDate = LocalDate.parse(birthDateAsString);
|
||||
} catch (DateTimeParseException ex) {
|
||||
throw new InvalidFormatException(birthDateAsString, ex);
|
||||
}
|
||||
|
||||
if (birthDate.isAfter(LocalDate.now())) {
|
||||
throw new DateOutOfRangeException(birthDateAsString);
|
||||
}
|
||||
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class CalculationException extends Exception {
|
||||
|
||||
CalculationException(DateParseException ex) {
|
||||
super(ex);
|
||||
}
|
||||
}
|
||||
|
||||
static class DateParseException extends Exception {
|
||||
|
||||
DateParseException(String input) {
|
||||
super(input);
|
||||
}
|
||||
|
||||
DateParseException(String input, Throwable thr) {
|
||||
super(input, thr);
|
||||
}
|
||||
}
|
||||
|
||||
static class InvalidFormatException extends DateParseException {
|
||||
|
||||
InvalidFormatException(String input, Throwable thr) {
|
||||
super("Invalid date format: " + input, thr);
|
||||
}
|
||||
}
|
||||
|
||||
static class DateOutOfRangeException extends DateParseException {
|
||||
|
||||
DateOutOfRangeException(String date) {
|
||||
super("Date out of range: " + date);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
target/
|
||||
.idea/
|
||||
bin/
|
||||
*.iml
|
@ -1,18 +0,0 @@
|
||||
## Core Java Lang OOP (Part 2)
|
||||
|
||||
This module contains articles about Object-oriented programming (OOP) in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- [Generic Constructors in Java](https://www.baeldung.com/java-generic-constructors)
|
||||
- [Cannot Reference “X” Before Supertype Constructor Has Been Called](https://www.baeldung.com/java-cannot-reference-x-before-supertype-constructor-error)
|
||||
- [Anonymous Classes in Java](https://www.baeldung.com/java-anonymous-classes)
|
||||
- [Raw Types in Java](https://www.baeldung.com/raw-types-java)
|
||||
- [Marker Interfaces in Java](https://www.baeldung.com/java-marker-interfaces)
|
||||
- [Java equals() and hashCode() Contracts](https://www.baeldung.com/java-equals-hashcode-contracts)
|
||||
- [Immutable Objects in Java](https://www.baeldung.com/java-immutable-object)
|
||||
- [Inheritance and Composition (Is-a vs Has-a relationship) in Java](https://www.baeldung.com/java-inheritance-composition)
|
||||
- [A Guide to Constructors in Java](https://www.baeldung.com/java-constructors)
|
||||
- [Static and Default Methods in Interfaces in Java](https://www.baeldung.com/java-static-default-methods)
|
||||
- [Java Copy Constructor](https://www.baeldung.com/java-copy-constructor)
|
||||
- [Abstract Classes in Java](https://www.baeldung.com/java-abstract-class)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang-oop)[[More -->]](/core-java-modules/core-java-lang-oop-3)
|
@ -1,51 +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-lang-oop-2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-lang-oop-2</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>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>nl.jqno.equalsverifier</groupId>
|
||||
<artifactId>equalsverifier</artifactId>
|
||||
<version>${equalsverifier.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-oop-2</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
<equalsverifier.version>3.0.3</equalsverifier.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -1,16 +0,0 @@
|
||||
## Core Java Lang OOP (Part 3)
|
||||
|
||||
This module contains articles about Object-oriented programming (OOP) in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- [Pass-By-Value as a Parameter Passing Mechanism in Java](https://www.baeldung.com/java-pass-by-value-or-pass-by-reference)
|
||||
- [Access Modifiers in Java](https://www.baeldung.com/java-access-modifiers)
|
||||
- [Guide to the super Java Keyword](https://www.baeldung.com/java-super)
|
||||
- [Guide to the this Java Keyword](https://www.baeldung.com/java-this)
|
||||
- [Java ‘public’ Access Modifier](https://www.baeldung.com/java-public-keyword)
|
||||
- [Composition, Aggregation, and Association in Java](https://www.baeldung.com/java-composition-aggregation-association)
|
||||
- [Nested Classes in Java](https://www.baeldung.com/java-nested-classes)
|
||||
- [A Guide to Inner Interfaces in Java](https://www.baeldung.com/java-inner-interfaces)
|
||||
- [Java Classes and Objects](https://www.baeldung.com/java-classes-objects)
|
||||
- [Java Interfaces](https://www.baeldung.com/java-interfaces)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang-oop-2)[[More -->]](/core-java-modules/core-java-lang-oop-4)
|
@ -1,60 +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-lang-oop-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-lang-oop-3</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>
|
||||
<!-- logging -->
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-oop-3</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -1,9 +0,0 @@
|
||||
## Core Java Lang OOP (Part 4)
|
||||
|
||||
This module contains articles about Object-oriented programming (OOP) in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- [Static and Dynamic Binding in Java](https://www.baeldung.com/java-static-dynamic-binding)
|
||||
- [Methods in Java](https://www.baeldung.com/java-methods)
|
||||
- [Java ‘private’ Access Modifier](https://www.baeldung.com/java-private-keyword)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang-oop-3)
|
@ -1,60 +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-lang-oop-4</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-lang-oop-4</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>
|
||||
<!-- logging -->
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-oop-4</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,8 @@
|
||||
## Core Java Lang OOP - Constructors
|
||||
|
||||
This module contains article about constructors in Java
|
||||
|
||||
### Relevant Articles:
|
||||
- [A Guide to Constructors in Java](https://www.baeldung.com/java-constructors)
|
||||
- [Java Copy Constructor](https://www.baeldung.com/java-copy-constructor)
|
||||
- [Cannot Reference “X” Before Supertype Constructor Has Been Called](https://www.baeldung.com/java-cannot-reference-x-before-supertype-constructor-error)
|
28
core-java-modules/core-java-lang-oop-constructors/pom.xml
Normal file
28
core-java-modules/core-java-lang-oop-constructors/pom.xml
Normal file
@ -0,0 +1,28 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>core-java-lang-oop-constructors</artifactId>
|
||||
<name>core-java-lang-oop-constructors</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
</project>
|
@ -6,7 +6,7 @@ class BankAccount {
|
||||
String name;
|
||||
LocalDateTime opened;
|
||||
double balance;
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s, %s, %f", this.name, this.opened.toString(), this.balance);
|
||||
@ -47,14 +47,13 @@ class BankAccountCopyConstructor extends BankAccount {
|
||||
this.opened = opened;
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
|
||||
public BankAccountCopyConstructor(BankAccount other) {
|
||||
this.name = other.name;
|
||||
this.opened = LocalDateTime.now();
|
||||
this.balance = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
class BankAccountChainedConstructors extends BankAccount {
|
||||
public BankAccountChainedConstructors(String name, LocalDateTime opened, double balance) {
|
||||
this.name = name;
|
||||
@ -65,4 +64,4 @@ class BankAccountChainedConstructors extends BankAccount {
|
||||
public BankAccountChainedConstructors(String name) {
|
||||
this(name, LocalDateTime.now(), 0.0f);
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user