Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
74acf44f1b
|
@ -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)
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Introduction to Apache CXF Aegis Data Binding](https://www.baeldung.com/aegis-data-binding-in-apache-cxf)
|
|
@ -10,8 +10,8 @@ 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 apiUrl = "<INSERT API URL HERE>";
|
||||
static String apiKey = "<INSERT API KEY HERE>";
|
||||
static String API_KEY_HEADER = "x-api-key";
|
||||
|
||||
public static WebClient.ResponseSpec getResponseBodySpec(Map<String, Object> requestBody) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.baeldung.awsappsync;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
@ -10,6 +11,7 @@ import java.util.Map;
|
|||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
@Disabled
|
||||
class AwsAppSyncApplicationTests {
|
||||
|
||||
@Test
|
||||
|
|
|
@ -16,10 +16,6 @@
|
|||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-8-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>${project.parent.version}</version>
|
||||
<name>core-java-8-datetime</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>${project.parent.version}</version>
|
||||
<name>core-java-8-datetime</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-8</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -9,3 +9,4 @@ This module contains articles about the improvements to core Java features intro
|
|||
- [Java 9 Stream API Improvements](https://www.baeldung.com/java-9-stream-api)
|
||||
- [Java 9 java.util.Objects Additions](https://www.baeldung.com/java-9-objects-new)
|
||||
- [Java 9 CompletableFuture API Improvements](https://www.baeldung.com/java-9-completablefuture)
|
||||
- [Java InputStream to Byte Array and ByteBuffer](https://www.baeldung.com/convert-input-stream-to-array-of-bytes)
|
||||
|
|
|
@ -33,6 +33,11 @@
|
|||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.java9.io.conversion;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
public class InputStreamToByteArrayUnitTest {
|
||||
|
||||
@Test
|
||||
public final void givenUsingPlainJavaOnFixedSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
|
||||
final byte[] targetArray = new byte[initialStream.available()];
|
||||
initialStream.read(targetArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingPlainJavaOnUnknownSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
|
||||
|
||||
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
int nRead;
|
||||
final byte[] data = new byte[1024];
|
||||
while ((nRead = is.read(data, 0, data.length)) != -1) {
|
||||
buffer.write(data, 0, nRead);
|
||||
}
|
||||
|
||||
buffer.flush();
|
||||
final byte[] byteArray = buffer.toByteArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava9_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
|
||||
|
||||
byte[] data = is.readAllBytes();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingGuava_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = ByteSource.wrap(new byte[] { 0, 1, 2 })
|
||||
.openStream();
|
||||
final byte[] targetArray = ByteStreams.toByteArray(initialStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingCommonsIO_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
|
||||
final byte[] targetArray = IOUtils.toByteArray(initialStream);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.inputstreamtobytes;
|
||||
package com.baeldung.java9.io.conversion;
|
||||
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.io.ByteStreams;
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-9-streams</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -10,10 +10,10 @@
|
|||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
|
|
@ -7,12 +7,11 @@
|
|||
<artifactId>core-java-collections-2</artifactId>
|
||||
<name>core-java-collections-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-array-list</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-list-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-list-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-list</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -7,12 +7,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-maps-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -7,12 +7,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-maps-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -6,12 +6,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-maps</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-set</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-concurrency-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-concurrency-advanced-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -11,4 +11,6 @@ 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)
|
||||
- [The ABA Problem in Concurrency](https://www.baeldung.com/cs/aba-concurrency)
|
||||
- [[<-- previous]](/core-java-modules/core-java-concurrency-advanced-2)
|
||||
|
|
|
@ -9,12 +9,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-concurrency-advanced-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -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> writerBuffer = new ConcurrentLinkedQueue<>();
|
||||
try {
|
||||
processorBuffer = readerExchanger.exchange(processorBuffer);
|
||||
while (true) {
|
||||
writerBuffer.add(processorBuffer.poll());
|
||||
if (processorBuffer.isEmpty()) {
|
||||
try {
|
||||
processorBuffer = readerExchanger.exchange(processorBuffer);
|
||||
writerBuffer = writerExchanger.exchange(writerBuffer);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
|
||||
Runnable writer = () -> {
|
||||
Queue<String> writerBuffer = new ConcurrentLinkedQueue<>();
|
||||
try {
|
||||
writerBuffer = writerExchanger.exchange(writerBuffer);
|
||||
while (true) {
|
||||
System.out.println(writerBuffer.poll());
|
||||
if (writerBuffer.isEmpty()) {
|
||||
writerBuffer = writerExchanger.exchange(writerBuffer);
|
||||
}
|
||||
}
|
||||
} 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-concurrency-advanced</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-concurrency-basic-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-concurrency-basic</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -23,7 +23,12 @@
|
|||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
|
@ -42,6 +47,8 @@
|
|||
<properties>
|
||||
<jmh.version>1.21</jmh.version>
|
||||
<guava.version>28.2-jre</guava.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,66 @@
|
|||
package com.baeldung.concurrent.queue;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
|
||||
@FixMethodOrder
|
||||
public class TestConcurrentLinkedQueue {
|
||||
|
||||
@Test
|
||||
public void givenThereIsExistingCollection_WhenAddedIntoQueue_ThenShouldContainElements() {
|
||||
Collection<Integer> elements = Arrays.asList(1, 2, 3, 4, 5);
|
||||
ConcurrentLinkedQueue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>(elements);
|
||||
assertThat(concurrentLinkedQueue).containsExactly(1, 2, 3, 4, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueueIsEmpty_WhenAccessingTheQueue_ThenQueueReturnsNull() throws InterruptedException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(1);
|
||||
ConcurrentLinkedQueue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
|
||||
executorService.submit(() -> assertNull("Retrieve object is null", concurrentLinkedQueue.poll()));
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
executorService.awaitTermination(1, TimeUnit.SECONDS);
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProducerOffersElementInQueue_WhenConsumerPollsQueue_ThenItRetrievesElement() throws Exception {
|
||||
int element = 1;
|
||||
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
ConcurrentLinkedQueue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
|
||||
Runnable offerTask = () -> concurrentLinkedQueue.offer(element);
|
||||
|
||||
Callable<Integer> pollTask = () -> {
|
||||
while (concurrentLinkedQueue.peek() != null) {
|
||||
return concurrentLinkedQueue.poll()
|
||||
.intValue();
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
executorService.submit(offerTask);
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
|
||||
Future<Integer> returnedElement = executorService.submit(pollTask);
|
||||
assertThat(returnedElement.get()
|
||||
.intValue(), is(equalTo(element)));
|
||||
executorService.awaitTermination(1, TimeUnit.SECONDS);
|
||||
executorService.shutdown();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package com.baeldung.concurrent.queue;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
|
||||
@FixMethodOrder
|
||||
public class TestLinkedBlockingQueue {
|
||||
|
||||
@Test
|
||||
public void givenThereIsExistingCollection_WhenAddedIntoQueue_ThenShouldContainElements() {
|
||||
Collection<Integer> elements = Arrays.asList(1, 2, 3, 4, 5);
|
||||
LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<>(elements);
|
||||
assertThat(linkedBlockingQueue).containsExactly(1, 2, 3, 4, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueueIsEmpty_WhenAccessingTheQueue_ThenThreadBlocks() throws InterruptedException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(1);
|
||||
LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<>();
|
||||
executorService.submit(() -> {
|
||||
try {
|
||||
linkedBlockingQueue.take();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
executorService.awaitTermination(1, TimeUnit.SECONDS);
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProducerPutsElementInQueue_WhenConsumerAccessQueue_ThenItRetrieve() {
|
||||
int element = 10;
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||
LinkedBlockingQueue<Integer> linkedBlockingQueue = new LinkedBlockingQueue<>();
|
||||
Runnable putTask = () -> {
|
||||
try {
|
||||
linkedBlockingQueue.put(element);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
};
|
||||
|
||||
Callable<Integer> takeTask = () -> {
|
||||
try {
|
||||
return linkedBlockingQueue.take();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
executorService.submit(putTask);
|
||||
Future<Integer> returnElement = executorService.submit(takeTask);
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
assertThat(returnElement.get()
|
||||
.intValue(), is(equalTo(element)));
|
||||
executorService.awaitTermination(1, TimeUnit.SECONDS);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
executorService.shutdown();
|
||||
}
|
||||
}
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-concurrency-collections</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>${project.parent.version}</version>
|
||||
<name>core-java-date-operations-1</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>${project.parent.version}</version>
|
||||
<name>core-java-date-operations-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>${project.parent.version}</version>
|
||||
<name>core-java-datetime-conversion</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>${project.parent.version}</version>
|
||||
<name>core-java-datetime-string</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -9,3 +9,6 @@ This module contains articles about core java exceptions
|
|||
- [java.net.UnknownHostException: Invalid Hostname for Server](https://www.baeldung.com/java-unknownhostexception)
|
||||
- [How to Handle Java SocketException](https://www.baeldung.com/java-socketexception)
|
||||
- [Java Suppressed Exceptions](https://www.baeldung.com/java-suppressed-exceptions)
|
||||
- [Java – Try with Resources](https://www.baeldung.com/java-try-with-resources)
|
||||
- [Java Global Exception Handler](https://www.baeldung.com/java-global-exception-handler)
|
||||
- [How to Find an Exception’s Root Cause in Java](https://www.baeldung.com/java-exception-root-cause)
|
||||
|
|
|
@ -7,12 +7,11 @@
|
|||
<artifactId>core-java-exceptions-2</artifactId>
|
||||
<name>core-java-exceptions-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -23,6 +22,11 @@
|
|||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons.lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<description> </description>
|
||||
|
@ -30,6 +34,7 @@
|
|||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<commons.lang3.version>3.10</commons.lang3.version>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.rootcausefinder;
|
||||
package com.baeldung.rootcausefinder;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.exceptions.globalexceptionhandler;
|
||||
package com.baeldung.globalexceptionhandler;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
|
@ -1,7 +1,7 @@
|
|||
package com.baeldung.exceptions.rootcausefinder;
|
||||
package com.baeldung.rootcausefinder;
|
||||
|
||||
import com.baeldung.exceptions.rootcausefinder.RootCauseFinder.CalculationException;
|
||||
import com.baeldung.exceptions.rootcausefinder.RootCauseFinder.DateOutOfRangeException;
|
||||
import com.baeldung.rootcausefinder.RootCauseFinder.CalculationException;
|
||||
import com.baeldung.rootcausefinder.RootCauseFinder.DateOutOfRangeException;
|
||||
import com.google.common.base.Throwables;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
@ -11,8 +11,7 @@ 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 com.baeldung.rootcausefinder.RootCauseFinder.AgeCalculator;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
|
@ -38,7 +37,7 @@ public class RootCauseFinderUnitTest {
|
|||
try {
|
||||
AgeCalculator.calculateAge("010102");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(findCauseUsingPlainJava(ex) instanceof DateTimeParseException);
|
||||
assertTrue(RootCauseFinder.findCauseUsingPlainJava(ex) instanceof DateTimeParseException);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -47,7 +46,7 @@ public class RootCauseFinderUnitTest {
|
|||
try {
|
||||
AgeCalculator.calculateAge("2020-04-04");
|
||||
} catch (CalculationException ex) {
|
||||
assertTrue(findCauseUsingPlainJava(ex) instanceof DateOutOfRangeException);
|
||||
assertTrue(RootCauseFinder.findCauseUsingPlainJava(ex) instanceof DateOutOfRangeException);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,7 +55,7 @@ public class RootCauseFinderUnitTest {
|
|||
try {
|
||||
AgeCalculator.calculateAge(null);
|
||||
} catch (Exception ex) {
|
||||
assertTrue(findCauseUsingPlainJava(ex) instanceof IllegalArgumentException);
|
||||
assertTrue(RootCauseFinder.findCauseUsingPlainJava(ex) instanceof IllegalArgumentException);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,9 +12,5 @@ This module contains articles about core java exceptions
|
|||
- [“Sneaky Throws” in Java](https://www.baeldung.com/java-sneaky-throws)
|
||||
- [The StackOverflowError in Java](https://www.baeldung.com/java-stack-overflow-error)
|
||||
- [Checked and Unchecked Exceptions in Java](https://www.baeldung.com/java-checked-unchecked-exceptions)
|
||||
- [Java – Try with Resources](https://www.baeldung.com/java-try-with-resources)
|
||||
- [Java Global Exception Handler](https://www.baeldung.com/java-global-exception-handler)
|
||||
- [Common Java Exceptions](https://www.baeldung.com/java-common-exceptions)
|
||||
- [How to Find an Exception’s Root Cause in Java](https://www.baeldung.com/java-exception-root-cause)
|
||||
- [Is It a Bad Practice to Catch Throwable?](https://www.baeldung.com/java-catch-throwable-bad-practice)
|
||||
- [[Next -->]](/core-java-modules/core-java-exceptions-2)
|
|
@ -9,12 +9,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-exceptions</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-function</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-io-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-io-apis</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -4,6 +4,5 @@ This module contains articles about core Java input/output(IO) conversions.
|
|||
|
||||
### Relevant Articles:
|
||||
- [Java InputStream to String](https://www.baeldung.com/convert-input-stream-to-string)
|
||||
- [Java InputStream to Byte Array and ByteBuffer](https://www.baeldung.com/convert-input-stream-to-array-of-bytes)
|
||||
- [Java – Write an InputStream to a File](https://www.baeldung.com/convert-input-stream-to-a-file)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-io-conversions)
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-io-conversions-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -2,7 +2,6 @@ package com.baeldung.inputstreamtostring;
|
|||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.io.CharStreams;
|
||||
import com.google.common.io.Files;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
@ -11,13 +10,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 +58,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);
|
||||
|
@ -127,42 +151,6 @@ public class JavaInputStreamToXUnitTest {
|
|||
assertThat(result, equalTo(originalString));
|
||||
}
|
||||
|
||||
// tests - InputStream to byte[]
|
||||
|
||||
@Test
|
||||
public final void givenUsingPlainJavaOnFixedSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
|
||||
final byte[] targetArray = new byte[initialStream.available()];
|
||||
initialStream.read(targetArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingPlainJavaOnUnknownSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
|
||||
|
||||
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
int nRead;
|
||||
final byte[] data = new byte[1024];
|
||||
while ((nRead = is.read(data, 0, data.length)) != -1) {
|
||||
buffer.write(data, 0, nRead);
|
||||
}
|
||||
|
||||
buffer.flush();
|
||||
final byte[] byteArray = buffer.toByteArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingGuava_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = ByteSource.wrap(new byte[] { 0, 1, 2 }).openStream();
|
||||
final byte[] targetArray = ByteStreams.toByteArray(initialStream);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingCommonsIO_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
|
||||
final byte[] targetArray = IOUtils.toByteArray(initialStream);
|
||||
}
|
||||
|
||||
// tests - InputStream to File
|
||||
|
||||
@Test
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-io-conversions</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-io</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-jar</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
|
|
|
@ -10,10 +10,10 @@
|
|||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-lambdas</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -8,19 +8,23 @@
|
|||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-lang-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.9</version>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
|
@ -65,6 +69,8 @@
|
|||
<jmh-generator.version>1.19</jmh-generator.version>
|
||||
<assertj.version>3.12.2</assertj.version>
|
||||
<commons.beanutils.version>1.9.4</commons.beanutils.version>
|
||||
<commons-lang3.version>3.10</commons-lang3.version>
|
||||
<guava.version>29.0-jre</guava.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.comparing;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PersonWithEquals {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private LocalDate birthDate;
|
||||
|
||||
public PersonWithEquals(String firstName, String lastName) {
|
||||
if (firstName == null || lastName == null) {
|
||||
throw new NullPointerException("Names can't be null");
|
||||
}
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public PersonWithEquals(String firstName, String lastName, LocalDate birthDate) {
|
||||
this(firstName, lastName);
|
||||
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
public String firstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String lastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public LocalDate birthDate() {
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
PersonWithEquals that = (PersonWithEquals) o;
|
||||
return firstName.equals(that.firstName) &&
|
||||
lastName.equals(that.lastName) &&
|
||||
Objects.equals(birthDate, that.birthDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(firstName, lastName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.comparing;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PersonWithEqualsAndComparable implements Comparable<PersonWithEqualsAndComparable> {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private LocalDate birthDate;
|
||||
|
||||
public PersonWithEqualsAndComparable(String firstName, String lastName) {
|
||||
if (firstName == null || lastName == null) {
|
||||
throw new NullPointerException("Names can't be null");
|
||||
}
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public PersonWithEqualsAndComparable(String firstName, String lastName, LocalDate birthDate) {
|
||||
this(firstName, lastName);
|
||||
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
PersonWithEqualsAndComparable that = (PersonWithEqualsAndComparable) o;
|
||||
return firstName.equals(that.firstName) &&
|
||||
lastName.equals(that.lastName) &&
|
||||
Objects.equals(birthDate, that.birthDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(firstName, lastName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(PersonWithEqualsAndComparable o) {
|
||||
int lastNamesComparison = this.lastName.compareTo(o.lastName);
|
||||
if (lastNamesComparison == 0) {
|
||||
int firstNamesComparison = this.firstName.compareTo(o.firstName);
|
||||
if (firstNamesComparison == 0) {
|
||||
if (this.birthDate != null && o.birthDate != null) {
|
||||
return this.birthDate.compareTo(o.birthDate);
|
||||
} else if (this.birthDate != null) {
|
||||
return 1;
|
||||
} else if (o.birthDate != null) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
return firstNamesComparison;
|
||||
}
|
||||
} else {
|
||||
return lastNamesComparison;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.comparing;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Comparator;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PersonWithEqualsAndComparableUsingComparator implements Comparable<PersonWithEqualsAndComparableUsingComparator> {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private LocalDate birthDate;
|
||||
|
||||
public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName) {
|
||||
if (firstName == null || lastName == null) {
|
||||
throw new NullPointerException("Names can't be null");
|
||||
}
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public PersonWithEqualsAndComparableUsingComparator(String firstName, String lastName, LocalDate birthDate) {
|
||||
this(firstName, lastName);
|
||||
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
public String firstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String lastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public LocalDate birthDate() {
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
PersonWithEqualsAndComparableUsingComparator that = (PersonWithEqualsAndComparableUsingComparator) o;
|
||||
return firstName.equals(that.firstName) &&
|
||||
lastName.equals(that.lastName) &&
|
||||
Objects.equals(birthDate, that.birthDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(firstName, lastName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(PersonWithEqualsAndComparableUsingComparator o) {
|
||||
return Comparator.comparing(PersonWithEqualsAndComparableUsingComparator::lastName)
|
||||
.thenComparing(PersonWithEqualsAndComparableUsingComparator::firstName)
|
||||
.thenComparing(PersonWithEqualsAndComparableUsingComparator::birthDate, Comparator.nullsLast(Comparator.naturalOrder()))
|
||||
.compare(this, o);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.comparing;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PersonWithEqualsAndWrongComparable implements Comparable<PersonWithEqualsAndWrongComparable> {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private LocalDate birthDate;
|
||||
|
||||
public PersonWithEqualsAndWrongComparable(String firstName, String lastName) {
|
||||
if (firstName == null || lastName == null) {
|
||||
throw new NullPointerException("Names can't be null");
|
||||
}
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public PersonWithEqualsAndWrongComparable(String firstName, String lastName, LocalDate birthDate) {
|
||||
this(firstName, lastName);
|
||||
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
PersonWithEqualsAndWrongComparable that = (PersonWithEqualsAndWrongComparable) o;
|
||||
return firstName.equals(that.firstName) &&
|
||||
lastName.equals(that.lastName) &&
|
||||
Objects.equals(birthDate, that.birthDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(firstName, lastName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(PersonWithEqualsAndWrongComparable o) {
|
||||
return this.lastName.compareTo(o.lastName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.comparing;
|
||||
|
||||
public class PersonWithoutEquals {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public PersonWithoutEquals(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.comparing;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ApacheCommonsObjectUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoStringsWithSameValues_whenApacheCommonsEqualityMethods_thenEqualsTrueNotEqualsFalse() {
|
||||
String a = new String("Hello!");
|
||||
String b = new String("Hello!");
|
||||
|
||||
assertThat(ObjectUtils.equals(a, b)).isTrue();
|
||||
assertThat(ObjectUtils.notEqual(a, b)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoStringsWithDifferentValues_whenApacheCommonsEqualityMethods_thenEqualsFalseNotEqualsTrue() {
|
||||
String a = new String("Hello!");
|
||||
String b = new String("Hello World!");
|
||||
|
||||
assertThat(ObjectUtils.equals(a, b)).isFalse();
|
||||
assertThat(ObjectUtils.notEqual(a, b)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoStringsWithConsecutiveValues_whenApacheCommonsCompare_thenNegative() {
|
||||
String first = new String("Hello!");
|
||||
String second = new String("How are you?");
|
||||
|
||||
assertThat(ObjectUtils.compare(first, second)).isNegative();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoStringsWithSameValues_whenApacheCommonsEqualityMethods_thenEqualsFalseNotEqualsTrue() {
|
||||
String first = new String("Hello!");
|
||||
String second = new String("Hello!");
|
||||
|
||||
assertThat(ObjectUtils.compare(first, second)).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoStringsWithConsecutiveValues_whenApacheCommonsCompareReversed_thenPositive() {
|
||||
String first = new String("Hello!");
|
||||
String second = new String("How are you?");
|
||||
|
||||
assertThat(ObjectUtils.compare(second, first)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoStringsOneNull_whenApacheCommonsCompare_thenPositive() {
|
||||
String first = new String("Hello!");
|
||||
String second = null;
|
||||
|
||||
assertThat(ObjectUtils.compare(first, second, false)).isPositive();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
package com.baeldung.comparing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ComparableInterfaceUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoConsecutiveStrings_whenCompareTo_thenNegative() {
|
||||
String first = "Google";
|
||||
String second = "Microsoft";
|
||||
|
||||
assertThat(first.compareTo(second)).isNegative();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoEqualsStrings_whenCompareTo_thenZero() {
|
||||
String first = "Google";
|
||||
String second = "Google";
|
||||
|
||||
assertThat(first.compareTo(second)).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoConsecutiveStrings_whenReversedCompareTo_thenPositive() {
|
||||
String first = "Google";
|
||||
String second = "Microsoft";
|
||||
|
||||
assertThat(second.compareTo(first)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndWrongComparableAndConsecutiveLastNames_whenCompareTo_thenNegative() {
|
||||
PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndWrongComparable joe = new PersonWithEqualsAndWrongComparable("Joe", "Portman");
|
||||
|
||||
assertThat(richard.compareTo(joe)).isNegative();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndWrongComparableAndSameLastNames_whenReversedCompareTo_thenZero() {
|
||||
PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndWrongComparable mike = new PersonWithEqualsAndWrongComparable("Mike", "Jefferson");
|
||||
|
||||
assertThat(richard.compareTo(mike)).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndWrongComparableAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() {
|
||||
PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndWrongComparable joe = new PersonWithEqualsAndWrongComparable("Joe", "Portman");
|
||||
|
||||
assertThat(joe.compareTo(richard)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndWrongComparableAndSameLastNames_whenSortedSet_thenProblem() {
|
||||
PersonWithEqualsAndWrongComparable richard = new PersonWithEqualsAndWrongComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndWrongComparable mike = new PersonWithEqualsAndWrongComparable("Mike", "Jefferson");
|
||||
|
||||
SortedSet<PersonWithEqualsAndWrongComparable> people = new TreeSet<>();
|
||||
people.add(richard);
|
||||
people.add(mike);
|
||||
|
||||
assertThat(people).containsExactly(richard);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableAndConsecutiveLastNames_whenCompareTo_thenNegative() {
|
||||
PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparable joe = new PersonWithEqualsAndComparable("Joe", "Portman");
|
||||
|
||||
assertThat(richard.compareTo(joe)).isNegative();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableAndSameLastNames_whenReversedCompareTo_thenZero() {
|
||||
PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparable mike = new PersonWithEqualsAndComparable("Mike", "Jefferson");
|
||||
|
||||
assertThat(richard.compareTo(mike)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() {
|
||||
PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparable joe = new PersonWithEqualsAndComparable("Joe", "Portman");
|
||||
|
||||
assertThat(joe.compareTo(richard)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableAndSameLastNames_whenSortedSet_thenProblem() {
|
||||
PersonWithEqualsAndComparable richard = new PersonWithEqualsAndComparable("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparable mike = new PersonWithEqualsAndComparable("Mike", "Jefferson");
|
||||
|
||||
SortedSet<PersonWithEqualsAndComparable> people = new TreeSet<>();
|
||||
people.add(richard);
|
||||
people.add(mike);
|
||||
|
||||
assertThat(people).containsExactly(mike, richard);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package com.baeldung.comparing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ComparatorInterfaceUnitTest {
|
||||
|
||||
@Test
|
||||
void givenListOfTwoPersonWithEqualsAndComparatorByFirstName_whenSort_thenSortedByFirstNames() {
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals allan = new PersonWithEquals("Allan", "Dale");
|
||||
|
||||
List<PersonWithEquals> people = new ArrayList<>();
|
||||
people.add(joe);
|
||||
people.add(allan);
|
||||
|
||||
Comparator<PersonWithEquals> compareByFirstNames = new Comparator<PersonWithEquals>() {
|
||||
@Override
|
||||
public int compare(PersonWithEquals o1, PersonWithEquals o2) {
|
||||
return o1.firstName().compareTo(o2.firstName());
|
||||
}
|
||||
};
|
||||
people.sort(compareByFirstNames);
|
||||
|
||||
assertThat(people).containsExactly(allan, joe);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfTwoPersonWithEqualsAndComparatorByFirstNameFunctionalStyle_whenSort_thenSortedByFirstNames() {
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals allan = new PersonWithEquals("Allan", "Dale");
|
||||
|
||||
List<PersonWithEquals> people = new ArrayList<>();
|
||||
people.add(joe);
|
||||
people.add(allan);
|
||||
|
||||
Comparator<PersonWithEquals> compareByFirstNames = Comparator.comparing(PersonWithEquals::firstName);
|
||||
people.sort(compareByFirstNames);
|
||||
|
||||
assertThat(people).containsExactly(allan, joe);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableUsingComparatorAndConsecutiveLastNames_whenCompareTo_thenNegative() {
|
||||
PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparableUsingComparator joe = new PersonWithEqualsAndComparableUsingComparator("Joe", "Portman");
|
||||
|
||||
assertThat(richard.compareTo(joe)).isNegative();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableUsingComparatorAndSameLastNames_whenReversedCompareTo_thenZero() {
|
||||
PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparableUsingComparator mike = new PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson");
|
||||
|
||||
assertThat(richard.compareTo(mike)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableUsingComparatorAndConsecutiveLastNames_whenReversedCompareTo_thenPositive() {
|
||||
PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparableUsingComparator joe = new PersonWithEqualsAndComparableUsingComparator("Joe", "Portman");
|
||||
|
||||
assertThat(joe.compareTo(richard)).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsAndComparableUsingComparatorAndSameLastNames_whenSortedSet_thenProblem() {
|
||||
PersonWithEqualsAndComparableUsingComparator richard = new PersonWithEqualsAndComparableUsingComparator("Richard", "Jefferson");
|
||||
PersonWithEqualsAndComparableUsingComparator mike = new PersonWithEqualsAndComparableUsingComparator("Mike", "Jefferson");
|
||||
|
||||
SortedSet<PersonWithEqualsAndComparableUsingComparator> people = new TreeSet<>();
|
||||
people.add(richard);
|
||||
people.add(mike);
|
||||
|
||||
assertThat(people).containsExactly(mike, richard);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
package com.baeldung.comparing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class EqualityOperatorUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoIntsWithSameValues_whenEqualityOperators_thenConsideredSame() {
|
||||
int a = 1;
|
||||
int b = 1;
|
||||
|
||||
assertThat(a == b).isTrue();
|
||||
assertThat(a != b).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoIntsWithDifferentValues_whenEqualityOperators_thenNotConsideredSame() {
|
||||
int a = 1;
|
||||
int b = 2;
|
||||
|
||||
assertThat(a == b).isFalse();
|
||||
assertThat(a != b).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoIntsWithSameValuesOneWrapped_whenEqualityOperators_thenConsideredSame() {
|
||||
int a = 1;
|
||||
Integer b = new Integer(1);
|
||||
|
||||
assertThat(a == b).isTrue();
|
||||
assertThat(a != b).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoIntsWithDifferentValuesOneWrapped_whenEqualityOperators_thenNotConsideredSame() {
|
||||
int a = 1;
|
||||
Integer b = new Integer(2);
|
||||
|
||||
assertThat(a == b).isFalse();
|
||||
assertThat(a != b).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoIntegersWithSameValues_whenEqualityOperators_thenNotConsideredSame() {
|
||||
Integer a = new Integer(1);
|
||||
Integer b = new Integer(1);
|
||||
|
||||
assertThat(a == b).isFalse();
|
||||
assertThat(a != b).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoIntegersWithDifferentValues_whenEqualityOperators_thenNotConsideredSame() {
|
||||
Integer a = new Integer(1);
|
||||
Integer b = new Integer(2);
|
||||
|
||||
assertThat(a == b).isFalse();
|
||||
assertThat(a != b).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoIntegersWithSameReference_whenEqualityOperators_thenConsideredSame() {
|
||||
Integer a = new Integer(1);
|
||||
Integer b = a;
|
||||
|
||||
assertThat(a == b).isTrue();
|
||||
assertThat(a != b).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoIntegersFromValueOfWithSameValues_whenEqualityOperators_thenConsideredSame() {
|
||||
Integer a = Integer.valueOf(1);
|
||||
Integer b = Integer.valueOf(1);
|
||||
|
||||
assertThat(a == b).isTrue();
|
||||
assertThat(a != b).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoStringsWithSameValues_whenEqualityOperators_thenNotConsideredSame() {
|
||||
String a = new String("Hello!");
|
||||
String b = new String("Hello!");
|
||||
|
||||
assertThat(a == b).isFalse();
|
||||
assertThat(a != b).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoStringsFromLiteralsWithSameValues_whenEqualityOperators_thenConsideredSame() {
|
||||
String a = "Hello!";
|
||||
String b = "Hello!";
|
||||
|
||||
assertThat(a == b).isTrue();
|
||||
assertThat(a != b).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoNullObjects_whenEqualityOperators_thenConsideredSame() {
|
||||
Object a = null;
|
||||
Object b = null;
|
||||
|
||||
assertThat(a == b).isTrue();
|
||||
assertThat(a != b).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoObjectsOneNull_whenEqualityOperators_thenNotConsideredSame() {
|
||||
Object a = null;
|
||||
Object b = "Hello!";
|
||||
|
||||
assertThat(a == b).isFalse();
|
||||
assertThat(a != b).isTrue();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.comparing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class EqualsMethodUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoIntegersWithSameValue_whenEquals_thenTrue() {
|
||||
Integer a = new Integer(1);
|
||||
Integer b = new Integer(1);
|
||||
|
||||
assertThat(a.equals(b)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoStringsWithSameValue_whenEquals_thenTrue() {
|
||||
String a = new String("Hello!");
|
||||
String b = new String("Hello!");
|
||||
|
||||
assertThat(a.equals(b)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoStringsWithDifferentValue_whenEquals_thenFalse() {
|
||||
String a = new String("Hello!");
|
||||
String b = new String("Hello World!");
|
||||
|
||||
assertThat(a.equals(b)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoObjectsFirstNull_whenEquals_thenNullPointerExceptionThrown() {
|
||||
Object a = null;
|
||||
Object b = new String("Hello!");
|
||||
|
||||
assertThrows(NullPointerException.class, () -> a.equals(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoObjectsSecondNull_whenEquals_thenFalse() {
|
||||
Object a = new String("Hello!");
|
||||
Object b = null;
|
||||
|
||||
assertThat(a.equals(b)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithoutEqualsWithSameNames_whenEquals_thenFalse() {
|
||||
PersonWithoutEquals joe = new PersonWithoutEquals("Joe", "Portman");
|
||||
PersonWithoutEquals joeAgain = new PersonWithoutEquals("Joe", "Portman");
|
||||
|
||||
assertThat(joe.equals(joeAgain)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWithEqualsWithSameNames_whenEquals_thenTrue() {
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals joeAgain = new PersonWithEquals("Joe", "Portman");
|
||||
|
||||
assertThat(joe.equals(joeAgain)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoPersonWittEqualsWithDifferentNames_whenEquals_thenFalse() {
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
PersonWithEquals natalie = new PersonWithEquals("Natalie", "Portman");
|
||||
|
||||
assertThat(joe.equals(natalie)).isFalse();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.comparing;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.collect.ComparisonChain;
|
||||
import com.google.common.primitives.Ints;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class GuavaUnitTest {
|
||||
|
||||
@Nested
|
||||
class ObjectsEqualMethod {
|
||||
@Test
|
||||
void givenTwoStringsWithSameValues_whenObjectsEqualMethods_thenTrue() {
|
||||
String a = new String("Hello!");
|
||||
String b = new String("Hello!");
|
||||
|
||||
assertThat(Objects.equal(a, b)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoStringsWithDifferentValues_whenObjectsEqualMethods_thenFalse() {
|
||||
String a = new String("Hello!");
|
||||
String b = new String("Hello World!");
|
||||
|
||||
assertThat(Objects.equal(a, b)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ComparisonMethods {
|
||||
@Test
|
||||
void givenTwoIntsWithConsecutiveValues_whenIntsCompareMethods_thenNegative() {
|
||||
int first = 1;
|
||||
int second = 2;
|
||||
assertThat(Ints.compare(first, second)).isNegative();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoIntsWithSameValues_whenIntsCompareMethods_thenZero() {
|
||||
int first = 1;
|
||||
int second = 1;
|
||||
|
||||
assertThat(Ints.compare(first, second)).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoIntsWithConsecutiveValues_whenIntsCompareMethodsReversed_thenNegative() {
|
||||
int first = 1;
|
||||
int second = 2;
|
||||
|
||||
assertThat(Ints.compare(second, first)).isPositive();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ComparisonChainClass {
|
||||
@Test
|
||||
void givenTwoPersonWithEquals_whenComparisonChainByLastNameThenFirstName_thenSortedJoeFirstAndNatalieSecond() {
|
||||
PersonWithEquals natalie = new PersonWithEquals("Natalie", "Portman");
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
|
||||
int comparisonResult = ComparisonChain.start()
|
||||
.compare(natalie.lastName(), joe.lastName())
|
||||
.compare(natalie.firstName(), joe.firstName())
|
||||
.result();
|
||||
|
||||
assertThat(comparisonResult).isPositive();
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue