diff --git a/algorithms-miscellaneous-5/pom.xml b/algorithms-miscellaneous-5/pom.xml index 9d0326241a..615cf03467 100644 --- a/algorithms-miscellaneous-5/pom.xml +++ b/algorithms-miscellaneous-5/pom.xml @@ -21,10 +21,10 @@ ${commons-codec.version} - org.apache.commons - commons-math3 - ${commons-math3.version} - + org.apache.commons + commons-math3 + ${commons-math3.version} + pl.allegro.finance tradukisto diff --git a/apache-cxf/README.md b/apache-cxf/README.md index f825b85bb3..bedd19a91a 100644 --- a/apache-cxf/README.md +++ b/apache-cxf/README.md @@ -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) diff --git a/apache-cxf/cxf-aegis/README.md b/apache-cxf/cxf-aegis/README.md new file mode 100644 index 0000000000..1cdb6efbb5 --- /dev/null +++ b/apache-cxf/cxf-aegis/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Introduction to Apache CXF Aegis Data Binding](https://www.baeldung.com/aegis-data-binding-in-apache-cxf) diff --git a/apache-shiro/pom.xml b/apache-shiro/pom.xml index d519ba42af..3df6283437 100644 --- a/apache-shiro/pom.xml +++ b/apache-shiro/pom.xml @@ -10,9 +10,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../parent-boot-1 + ../parent-boot-2 diff --git a/apache-shiro/src/main/java/com/baeldung/Main.java b/apache-shiro/src/main/java/com/baeldung/Main.java index 5e341f251b..99515bb705 100644 --- a/apache-shiro/src/main/java/com/baeldung/Main.java +++ b/apache-shiro/src/main/java/com/baeldung/Main.java @@ -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; diff --git a/apache-shiro/src/main/java/com/baeldung/MyCustomRealm.java b/apache-shiro/src/main/java/com/baeldung/MyCustomRealm.java index 8d792c76a5..6d7c01d96e 100644 --- a/apache-shiro/src/main/java/com/baeldung/MyCustomRealm.java +++ b/apache-shiro/src/main/java/com/baeldung/MyCustomRealm.java @@ -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 { diff --git a/apache-shiro/src/main/java/com/baeldung/shiro/permissions/custom/Main.java b/apache-shiro/src/main/java/com/baeldung/shiro/permissions/custom/Main.java index a373122d6c..a902a24388 100644 --- a/apache-shiro/src/main/java/com/baeldung/shiro/permissions/custom/Main.java +++ b/apache-shiro/src/main/java/com/baeldung/shiro/permissions/custom/Main.java @@ -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; diff --git a/atomikos/README.md b/atomikos/README.md index 19f2e871d4..f9129233ec 100644 --- a/atomikos/README.md +++ b/atomikos/README.md @@ -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) diff --git a/aws-app-sync/pom.xml b/aws-app-sync/pom.xml new file mode 100644 index 0000000000..1f915978ab --- /dev/null +++ b/aws-app-sync/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.6.RELEASE + + + + com.baeldung + aws-app-sync + 0.0.1-SNAPSHOT + aws-app-sync + + Spring Boot using AWS App Sync + + + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + org.springframework.boot + spring-boot-starter-webflux + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/aws-app-sync/src/main/java/com/baeldung/awsappsync/AppSyncClientHelper.java b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AppSyncClientHelper.java new file mode 100644 index 0000000000..f66ac1841b --- /dev/null +++ b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AppSyncClientHelper.java @@ -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 = ""; + static String apiKey = ""; + static String API_KEY_HEADER = "x-api-key"; + + public static WebClient.ResponseSpec getResponseBodySpec(Map 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(); + } + +} diff --git a/aws-app-sync/src/main/java/com/baeldung/awsappsync/AwsAppSyncApplication.java b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AwsAppSyncApplication.java new file mode 100644 index 0000000000..19fffd2994 --- /dev/null +++ b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AwsAppSyncApplication.java @@ -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); + } + +} diff --git a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java new file mode 100644 index 0000000000..2338cc29a1 --- /dev/null +++ b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java @@ -0,0 +1,74 @@ +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; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +@Disabled +class AwsAppSyncApplicationTests { + + @Test + void givenGraphQuery_whenListEvents_thenReturnAllEvents() { + + Map 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 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")); + } +} diff --git a/bazel/bazelapp/README.md b/bazel/bazelapp/README.md deleted file mode 100644 index 528f797c21..0000000000 --- a/bazel/bazelapp/README.md +++ /dev/null @@ -1,3 +0,0 @@ -### Relevant Articles: - -- [Building Java Applications with Bazel](https://www.baeldung.com/bazel-build-tool) diff --git a/cas/cas-secured-app/pom.xml b/cas/cas-secured-app/pom.xml index 426d65c32b..bcce82c94c 100644 --- a/cas/cas-secured-app/pom.xml +++ b/cas/cas-secured-app/pom.xml @@ -16,10 +16,6 @@ ../../parent-boot-2 - - 2.2.6.RELEASE - - org.springframework.boot diff --git a/core-groovy-2/README.md b/core-groovy-2/README.md index 95a00a1f5b..9f81ac6c16 100644 --- a/core-groovy-2/README.md +++ b/core-groovy-2/README.md @@ -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) diff --git a/core-groovy/README.md b/core-groovy/README.md index 25a0aece3a..f852b3ccf2 100644 --- a/core-groovy/README.md +++ b/core-groovy/README.md @@ -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) \ No newline at end of file +- [Convert String to Integer in Groovy](https://www.baeldung.com/groovy-convert-string-to-integer) +- [[More -->]](/core-groovy-2) diff --git a/core-java-modules/core-java-14/README.md b/core-java-modules/core-java-14/README.md index 13bb468b30..dc80800054 100644 --- a/core-java-modules/core-java-14/README.md +++ b/core-java-modules/core-java-14/README.md @@ -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) diff --git a/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/record/Person.java b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/record/Person.java new file mode 100644 index 0000000000..33243c4ecf --- /dev/null +++ b/core-java-modules/core-java-14/src/main/java/com/baeldung/java14/record/Person.java @@ -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); + } +} diff --git a/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/foreign/api/ForeignMemoryUnitTest.java b/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/foreign/api/ForeignMemoryUnitTest.java new file mode 100644 index 0000000000..b2264f30e6 --- /dev/null +++ b/core-java-modules/core-java-14/src/test/java/com/baeldung/java14/foreign/api/ForeignMemoryUnitTest.java @@ -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; iguava ${guava.version} + + commons-io + commons-io + ${commons-io.version} + org.junit.platform junit-platform-runner diff --git a/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteArrayUnitTest.java b/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteArrayUnitTest.java new file mode 100644 index 0000000000..b64709be09 --- /dev/null +++ b/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteArrayUnitTest.java @@ -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); + } +} diff --git a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtobytes/InputStreamToByteBufferUnitTest.java b/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteBufferUnitTest.java similarity index 97% rename from core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtobytes/InputStreamToByteBufferUnitTest.java rename to core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteBufferUnitTest.java index c10aaae22a..f97352ac27 100644 --- a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtobytes/InputStreamToByteBufferUnitTest.java +++ b/core-java-modules/core-java-9-improvements/src/test/java/com/baeldung/java9/io/conversion/InputStreamToByteBufferUnitTest.java @@ -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; diff --git a/core-java-modules/core-java-collections-maps-3/README.md b/core-java-modules/core-java-collections-maps-3/README.md index 64a3b75d83..7386f7e9b7 100644 --- a/core-java-modules/core-java-collections-maps-3/README.md +++ b/core-java-modules/core-java-collections-maps-3/README.md @@ -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) diff --git a/core-java-modules/core-java-concurrency-2/README.md b/core-java-modules/core-java-concurrency-2/README.md index ab7eebc26a..23471a237e 100644 --- a/core-java-modules/core-java-concurrency-2/README.md +++ b/core-java-modules/core-java-concurrency-2/README.md @@ -4,5 +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) +- [Testing Multi-Threaded Code in Java](https://www.baeldung.com/java-testing-multithreaded) diff --git a/core-java-modules/core-java-concurrency-advanced-3/README.md b/core-java-modules/core-java-concurrency-advanced-3/README.md index b11cde5158..dfd264116c 100644 --- a/core-java-modules/core-java-concurrency-advanced-3/README.md +++ b/core-java-modules/core-java-concurrency-advanced-3/README.md @@ -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) diff --git a/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/lockfree/NonBlockingQueue.java b/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/lockfree/NonBlockingQueue.java new file mode 100644 index 0000000000..9140dc287d --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-3/src/main/java/com/baeldung/lockfree/NonBlockingQueue.java @@ -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 { + + private final AtomicReference> 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 node = new Node<>(element); + Node 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 currentHead; + Node 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 { + private final T value; + private volatile Node next; + private volatile Node previous; + + public Node(T value) { + this.value = value; + this.next = null; + } + + public T getValue() { + return value; + } + + public Node getNext() { + return next; + } + + public void setPrevious(Node previous) { + this.previous = previous; + } + } +} diff --git a/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/exchanger/ExchangerPipeLineManualTest.java b/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/exchanger/ExchangerPipeLineManualTest.java new file mode 100644 index 0000000000..af8b8b9aa1 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/exchanger/ExchangerPipeLineManualTest.java @@ -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> readerExchanger = new Exchanger<>(); + Exchanger> writerExchanger = new Exchanger<>(); + int counter = 0; + + Runnable reader = () -> { + Queue 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 processorBuffer = new ConcurrentLinkedQueue<>(); + Queue 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 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(); + } + +} diff --git a/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/exchanger/ExchangerUnitTest.java b/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/exchanger/ExchangerUnitTest.java new file mode 100644 index 0000000000..ec567a3563 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/exchanger/ExchangerUnitTest.java @@ -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 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 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 result = CompletableFuture.runAsync(runner); + String msg = exchanger.exchange("to runner"); + assertEquals("from runner", msg); + result.join(); + } + +} diff --git a/core-java-modules/core-java-concurrency-collections-2/pom.xml b/core-java-modules/core-java-concurrency-collections-2/pom.xml index 65a91c9a9c..7fdd348dc5 100644 --- a/core-java-modules/core-java-concurrency-collections-2/pom.xml +++ b/core-java-modules/core-java-concurrency-collections-2/pom.xml @@ -23,7 +23,12 @@ jmh-generator-annprocess ${jmh.version} - + + org.assertj + assertj-core + ${assertj.version} + test + src @@ -42,6 +47,8 @@ 1.21 28.2-jre + + 3.6.1 \ No newline at end of file diff --git a/core-java-modules/core-java-concurrency-collections-2/src/test/java/com/baeldung/concurrent/queue/TestConcurrentLinkedQueue.java b/core-java-modules/core-java-concurrency-collections-2/src/test/java/com/baeldung/concurrent/queue/TestConcurrentLinkedQueue.java new file mode 100644 index 0000000000..c61becc366 --- /dev/null +++ b/core-java-modules/core-java-concurrency-collections-2/src/test/java/com/baeldung/concurrent/queue/TestConcurrentLinkedQueue.java @@ -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 elements = Arrays.asList(1, 2, 3, 4, 5); + ConcurrentLinkedQueue 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 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 concurrentLinkedQueue = new ConcurrentLinkedQueue<>(); + Runnable offerTask = () -> concurrentLinkedQueue.offer(element); + + Callable pollTask = () -> { + while (concurrentLinkedQueue.peek() != null) { + return concurrentLinkedQueue.poll() + .intValue(); + } + return null; + }; + + executorService.submit(offerTask); + TimeUnit.SECONDS.sleep(1); + + Future returnedElement = executorService.submit(pollTask); + assertThat(returnedElement.get() + .intValue(), is(equalTo(element))); + executorService.awaitTermination(1, TimeUnit.SECONDS); + executorService.shutdown(); + } +} diff --git a/core-java-modules/core-java-concurrency-collections-2/src/test/java/com/baeldung/concurrent/queue/TestLinkedBlockingQueue.java b/core-java-modules/core-java-concurrency-collections-2/src/test/java/com/baeldung/concurrent/queue/TestLinkedBlockingQueue.java new file mode 100644 index 0000000000..7a78bc7b3b --- /dev/null +++ b/core-java-modules/core-java-concurrency-collections-2/src/test/java/com/baeldung/concurrent/queue/TestLinkedBlockingQueue.java @@ -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 elements = Arrays.asList(1, 2, 3, 4, 5); + LinkedBlockingQueue 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 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 linkedBlockingQueue = new LinkedBlockingQueue<>(); + Runnable putTask = () -> { + try { + linkedBlockingQueue.put(element); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }; + + Callable takeTask = () -> { + try { + return linkedBlockingQueue.take(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return null; + }; + + executorService.submit(putTask); + Future 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(); + } +} diff --git a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingCalendar.java b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingCalendar.java new file mode 100644 index 0000000000..db32421aa4 --- /dev/null +++ b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingCalendar.java @@ -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)); + } +} diff --git a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingLocalDate.java b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingLocalDate.java new file mode 100644 index 0000000000..4e1b7be3be --- /dev/null +++ b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingLocalDate.java @@ -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); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingCalendarUnitTest.java b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingCalendarUnitTest.java new file mode 100644 index 0000000000..587a459277 --- /dev/null +++ b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingCalendarUnitTest.java @@ -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)); + } +} diff --git a/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingLocalDateUnitTest.java b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingLocalDateUnitTest.java new file mode 100644 index 0000000000..9b98222ece --- /dev/null +++ b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingLocalDateUnitTest.java @@ -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()); + } +} diff --git a/core-java-modules/core-java-exceptions-2/README.md b/core-java-modules/core-java-exceptions-2/README.md index 1b8457acc4..46ffd490be 100644 --- a/core-java-modules/core-java-exceptions-2/README.md +++ b/core-java-modules/core-java-exceptions-2/README.md @@ -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) diff --git a/core-java-modules/core-java-exceptions-2/pom.xml b/core-java-modules/core-java-exceptions-2/pom.xml index cf8de3d5b6..915ec1da69 100644 --- a/core-java-modules/core-java-exceptions-2/pom.xml +++ b/core-java-modules/core-java-exceptions-2/pom.xml @@ -23,6 +23,11 @@ ${assertj-core.version} test + + org.apache.commons + commons-lang3 + ${commons.lang3.version} + @@ -30,6 +35,7 @@ UTF-8 + 3.10 3.10.0 diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/Arithmetic.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/Arithmetic.java similarity index 87% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/Arithmetic.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/Arithmetic.java index db29198b39..57d022eb13 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/Arithmetic.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/Arithmetic.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/ArrayIndexOutOfBounds.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/ArrayIndexOutOfBounds.java similarity index 92% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/ArrayIndexOutOfBounds.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/ArrayIndexOutOfBounds.java index 54c95f224c..b7c8bb1875 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/ArrayIndexOutOfBounds.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/ArrayIndexOutOfBounds.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/ClassCast.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/ClassCast.java similarity index 92% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/ClassCast.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/ClassCast.java index 8f8a6cf9e6..1ef8399d3d 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/ClassCast.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/ClassCast.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/FileNotFound.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/FileNotFound.java similarity index 91% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/FileNotFound.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/FileNotFound.java index a9f2e5ee84..a94e294016 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/FileNotFound.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/FileNotFound.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import java.io.BufferedReader; import java.io.File; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/GlobalExceptionHandler.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/GlobalExceptionHandler.java similarity index 92% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/GlobalExceptionHandler.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/GlobalExceptionHandler.java index f2e89f44e3..4d3f7c1a98 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/GlobalExceptionHandler.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/GlobalExceptionHandler.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/IllegalArgument.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/IllegalArgument.java similarity index 87% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/IllegalArgument.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/IllegalArgument.java index d54757dfac..cb7f981e17 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/IllegalArgument.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/IllegalArgument.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/IllegalState.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/IllegalState.java similarity index 92% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/IllegalState.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/IllegalState.java index 0a812d2b82..105ca155b7 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/IllegalState.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/IllegalState.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import java.util.ArrayList; import java.util.Iterator; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/InterruptedExceptionExample.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/InterruptedExceptionExample.java similarity index 91% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/InterruptedExceptionExample.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/InterruptedExceptionExample.java index d0c8bb2cd0..3b37168013 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/InterruptedExceptionExample.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/InterruptedExceptionExample.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/MalformedURL.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/MalformedURL.java similarity index 89% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/MalformedURL.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/MalformedURL.java index 9a02f005fd..cc1f39ea69 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/MalformedURL.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/MalformedURL.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import java.net.MalformedURLException; import java.net.URL; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/NullPointer.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/NullPointer.java similarity index 93% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/NullPointer.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/NullPointer.java index 445cbecdc8..6d6a1706a6 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/NullPointer.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/NullPointer.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/NumberFormat.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/NumberFormat.java similarity index 90% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/NumberFormat.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/NumberFormat.java index 576fe51f78..7497d023ee 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/NumberFormat.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/NumberFormat.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/ParseExceptionExample.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/ParseExceptionExample.java similarity index 90% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/ParseExceptionExample.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/ParseExceptionExample.java index e3b3e04b10..7def606786 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/ParseExceptionExample.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/ParseExceptionExample.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import java.text.DateFormat; import java.text.ParseException; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/StringIndexOutOfBounds.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/StringIndexOutOfBounds.java similarity index 91% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/StringIndexOutOfBounds.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/StringIndexOutOfBounds.java index 0ee132e568..8652926777 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/globalexceptionhandler/StringIndexOutOfBounds.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/globalexceptionhandler/StringIndexOutOfBounds.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/rootcausefinder/RootCauseFinder.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/rootcausefinder/RootCauseFinder.java similarity index 98% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/rootcausefinder/RootCauseFinder.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/rootcausefinder/RootCauseFinder.java index 06610f3874..cb04902dfa 100644 --- a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/exceptions/rootcausefinder/RootCauseFinder.java +++ b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/rootcausefinder/RootCauseFinder.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.rootcausefinder; +package com.baeldung.rootcausefinder; import java.time.LocalDate; import java.time.Period; diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/trywithresource/AutoCloseableMain.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/trywithresource/AutoCloseableMain.java similarity index 100% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/trywithresource/AutoCloseableMain.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/trywithresource/AutoCloseableMain.java diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/trywithresource/AutoCloseableResourcesFirst.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/trywithresource/AutoCloseableResourcesFirst.java similarity index 100% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/trywithresource/AutoCloseableResourcesFirst.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/trywithresource/AutoCloseableResourcesFirst.java diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/trywithresource/AutoCloseableResourcesSecond.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/trywithresource/AutoCloseableResourcesSecond.java similarity index 100% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/trywithresource/AutoCloseableResourcesSecond.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/trywithresource/AutoCloseableResourcesSecond.java diff --git a/core-java-modules/core-java-exceptions/src/main/java/com/baeldung/trywithresource/MyResource.java b/core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/trywithresource/MyResource.java similarity index 100% rename from core-java-modules/core-java-exceptions/src/main/java/com/baeldung/trywithresource/MyResource.java rename to core-java-modules/core-java-exceptions-2/src/main/java/com/baeldung/trywithresource/MyResource.java diff --git a/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/globalexceptionhandler/GlobalExceptionHandlerUnitTest.java b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/globalexceptionhandler/GlobalExceptionHandlerUnitTest.java similarity index 97% rename from core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/globalexceptionhandler/GlobalExceptionHandlerUnitTest.java rename to core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/globalexceptionhandler/GlobalExceptionHandlerUnitTest.java index 74ceb3b442..83347f9d9d 100644 --- a/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/globalexceptionhandler/GlobalExceptionHandlerUnitTest.java +++ b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/globalexceptionhandler/GlobalExceptionHandlerUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.exceptions.globalexceptionhandler; +package com.baeldung.globalexceptionhandler; import org.junit.After; import org.junit.Before; diff --git a/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/rootcausefinder/RootCauseFinderUnitTest.java b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/rootcausefinder/RootCauseFinderUnitTest.java similarity index 80% rename from core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/rootcausefinder/RootCauseFinderUnitTest.java rename to core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/rootcausefinder/RootCauseFinderUnitTest.java index f42388857a..ccf14c4cba 100644 --- a/core-java-modules/core-java-exceptions/src/test/java/com/baeldung/exceptions/rootcausefinder/RootCauseFinderUnitTest.java +++ b/core-java-modules/core-java-exceptions-2/src/test/java/com/baeldung/rootcausefinder/RootCauseFinderUnitTest.java @@ -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); } } diff --git a/core-java-modules/core-java-exceptions/README.md b/core-java-modules/core-java-exceptions/README.md index b7222540e9..5f47aa69fb 100644 --- a/core-java-modules/core-java-exceptions/README.md +++ b/core-java-modules/core-java-exceptions/README.md @@ -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) \ No newline at end of file diff --git a/core-java-modules/core-java-io-conversions-2/README.md b/core-java-modules/core-java-io-conversions-2/README.md index 4a28bf37c5..5cb9c21c54 100644 --- a/core-java-modules/core-java-io-conversions-2/README.md +++ b/core-java-modules/core-java-io-conversions-2/README.md @@ -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) diff --git a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtostring/JavaInputStreamToXUnitTest.java b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtostring/JavaInputStreamToXUnitTest.java index eb8c39f2d9..c34c32891f 100644 --- a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtostring/JavaInputStreamToXUnitTest.java +++ b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/inputstreamtostring/JavaInputStreamToXUnitTest.java @@ -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 diff --git a/core-java-modules/core-java-lang-2/pom.xml b/core-java-modules/core-java-lang-2/pom.xml index 5aa80ce3df..21a63d8091 100644 --- a/core-java-modules/core-java-lang-2/pom.xml +++ b/core-java-modules/core-java-lang-2/pom.xml @@ -20,7 +20,12 @@ org.apache.commons commons-lang3 - 3.9 + ${commons-lang3.version} + + + com.google.guava + guava + ${guava.version} commons-beanutils @@ -65,6 +70,8 @@ 1.19 3.12.2 1.9.4 + 3.10 + 29.0-jre diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEquals.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEquals.java new file mode 100644 index 0000000000..e3a61fc05a --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEquals.java @@ -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); + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparable.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparable.java new file mode 100644 index 0000000000..5611ce8a09 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparable.java @@ -0,0 +1,62 @@ +package com.baeldung.comparing; + +import java.time.LocalDate; +import java.util.Objects; + +public class PersonWithEqualsAndComparable implements Comparable { + 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; + } + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparableUsingComparator.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparableUsingComparator.java new file mode 100644 index 0000000000..ed322cb353 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndComparableUsingComparator.java @@ -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 { + 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); + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndWrongComparable.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndWrongComparable.java new file mode 100644 index 0000000000..e0bdaa413a --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithEqualsAndWrongComparable.java @@ -0,0 +1,44 @@ +package com.baeldung.comparing; + +import java.time.LocalDate; +import java.util.Objects; + +public class PersonWithEqualsAndWrongComparable implements Comparable { + 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); + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithoutEquals.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithoutEquals.java new file mode 100644 index 0000000000..bb4c6b958b --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/comparing/PersonWithoutEquals.java @@ -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; + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ApacheCommonsObjectUtilsUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ApacheCommonsObjectUtilsUnitTest.java new file mode 100644 index 0000000000..33b8dcb3fc --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ApacheCommonsObjectUtilsUnitTest.java @@ -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(); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparableInterfaceUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparableInterfaceUnitTest.java new file mode 100644 index 0000000000..281c4a0201 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparableInterfaceUnitTest.java @@ -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 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 people = new TreeSet<>(); + people.add(richard); + people.add(mike); + + assertThat(people).containsExactly(mike, richard); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparatorInterfaceUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparatorInterfaceUnitTest.java new file mode 100644 index 0000000000..769ae60bed --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ComparatorInterfaceUnitTest.java @@ -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 people = new ArrayList<>(); + people.add(joe); + people.add(allan); + + Comparator compareByFirstNames = new Comparator() { + @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 people = new ArrayList<>(); + people.add(joe); + people.add(allan); + + Comparator 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 people = new TreeSet<>(); + people.add(richard); + people.add(mike); + + assertThat(people).containsExactly(mike, richard); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualityOperatorUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualityOperatorUnitTest.java new file mode 100644 index 0000000000..ebcf83ef5b --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualityOperatorUnitTest.java @@ -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(); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java new file mode 100644 index 0000000000..a69ac38916 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/EqualsMethodUnitTest.java @@ -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(); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java new file mode 100644 index 0000000000..5c8591e134 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/GuavaUnitTest.java @@ -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(); + } + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java new file mode 100644 index 0000000000..5ac89da2be --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/comparing/ObjectsEqualsStaticMethodUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.comparing; + +import org.junit.jupiter.api.Test; + +import java.util.Objects; + +import static org.assertj.core.api.Assertions.assertThat; + +class ObjectsEqualsStaticMethodUnitTest { + + @Test + void givenTwoPersonWithEqualsWithSameNames_whenObjectsEquals_thenTrue() { + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); + PersonWithEquals joeAgain = new PersonWithEquals("Joe", "Portman"); + + assertThat(Objects.equals(joe, joeAgain)).isTrue(); + } + + @Test + void givenTwoPersonWithEqualsWithDifferentNames_whenObjectsEquals_thenFalse() { + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); + PersonWithEquals natalie = new PersonWithEquals("Natalie", "Portman"); + + assertThat(Objects.equals(joe, natalie)).isFalse(); + } + + @Test + void givenTwoPersonWithEqualsFirstNull_whenObjectsEquals_thenFalse() { + PersonWithEquals nobody = null; + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); + + assertThat(Objects.equals(nobody, joe)).isFalse(); + } + + @Test + void givenTwoObjectsSecondtNull_whenObjectsEquals_thenFalse() { + PersonWithEquals joe = new PersonWithEquals("Joe", "Portman"); + PersonWithEquals nobody = null; + + assertThat(Objects.equals(joe, nobody)).isFalse(); + } + + @Test + void givenTwoObjectsNull_whenObjectsEquals_thenTrue() { + PersonWithEquals nobody = null; + PersonWithEquals nobodyAgain = null; + + assertThat(Objects.equals(nobody, nobodyAgain)).isTrue(); + } +} diff --git a/core-java-modules/core-java-lang-oop-2/.gitignore b/core-java-modules/core-java-lang-oop-2/.gitignore deleted file mode 100644 index 36aba1c242..0000000000 --- a/core-java-modules/core-java-lang-oop-2/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -target/ -.idea/ -bin/ -*.iml \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-2/README.md b/core-java-modules/core-java-lang-oop-2/README.md deleted file mode 100644 index c48a3f5cd9..0000000000 --- a/core-java-modules/core-java-lang-oop-2/README.md +++ /dev/null @@ -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) diff --git a/core-java-modules/core-java-lang-oop-2/pom.xml b/core-java-modules/core-java-lang-oop-2/pom.xml deleted file mode 100644 index ccacaf7116..0000000000 --- a/core-java-modules/core-java-lang-oop-2/pom.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - 4.0.0 - core-java-lang-oop-2 - 0.1.0-SNAPSHOT - core-java-lang-oop-2 - jar - - - com.baeldung - parent-java - 0.0.1-SNAPSHOT - ../../parent-java - - - - - - org.assertj - assertj-core - ${assertj-core.version} - test - - - nl.jqno.equalsverifier - equalsverifier - ${equalsverifier.version} - test - - - - - core-java-lang-oop-2 - - - src/main/resources - true - - - - - - - 3.10.0 - 3.0.3 - - - diff --git a/core-java-modules/core-java-lang-oop-3/README.md b/core-java-modules/core-java-lang-oop-3/README.md deleted file mode 100644 index 3a0e588ad4..0000000000 --- a/core-java-modules/core-java-lang-oop-3/README.md +++ /dev/null @@ -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) diff --git a/core-java-modules/core-java-lang-oop-3/pom.xml b/core-java-modules/core-java-lang-oop-3/pom.xml deleted file mode 100644 index cc9b473d03..0000000000 --- a/core-java-modules/core-java-lang-oop-3/pom.xml +++ /dev/null @@ -1,60 +0,0 @@ - - - 4.0.0 - core-java-lang-oop-3 - 0.1.0-SNAPSHOT - core-java-lang-oop-3 - jar - - - com.baeldung - parent-java - 0.0.1-SNAPSHOT - ../../parent-java - - - - - - log4j - log4j - ${log4j.version} - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - - - - org.assertj - assertj-core - ${assertj-core.version} - test - - - com.h2database - h2 - ${h2.version} - test - - - - - core-java-lang-oop-3 - - - src/main/resources - true - - - - - - 3.10.0 - - - diff --git a/core-java-modules/core-java-lang-oop-4/README.md b/core-java-modules/core-java-lang-oop-4/README.md deleted file mode 100644 index 51650dc1f6..0000000000 --- a/core-java-modules/core-java-lang-oop-4/README.md +++ /dev/null @@ -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) \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-4/pom.xml b/core-java-modules/core-java-lang-oop-4/pom.xml deleted file mode 100644 index 3c7e4f446d..0000000000 --- a/core-java-modules/core-java-lang-oop-4/pom.xml +++ /dev/null @@ -1,60 +0,0 @@ - - - 4.0.0 - core-java-lang-oop-4 - 0.1.0-SNAPSHOT - core-java-lang-oop-4 - jar - - - com.baeldung - parent-java - 0.0.1-SNAPSHOT - ../../parent-java - - - - - - log4j - log4j - ${log4j.version} - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - - - - org.assertj - assertj-core - ${assertj-core.version} - test - - - com.h2database - h2 - ${h2.version} - test - - - - - core-java-lang-oop-4 - - - src/main/resources - true - - - - - - 3.10.0 - - - diff --git a/core-java-modules/core-java-lang-oop-constructors/README.md b/core-java-modules/core-java-lang-oop-constructors/README.md new file mode 100644 index 0000000000..0082969807 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/README.md @@ -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) \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-constructors/pom.xml b/core-java-modules/core-java-lang-oop-constructors/pom.xml new file mode 100644 index 0000000000..76507103ea --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/pom.xml @@ -0,0 +1,28 @@ + + + + core-java-modules + com.baeldung.core-java-modules + 1.0.0-SNAPSHOT + + 4.0.0 + + core-java-lang-oop-constructors + core-java-lang-oop-constructors + jar + + + + org.assertj + assertj-core + ${assertj-core.version} + test + + + + + 3.10.0 + + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/constructors/BankAccount.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/BankAccount.java similarity index 99% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/constructors/BankAccount.java rename to core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/BankAccount.java index b78b5937e1..b198492129 100644 --- a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/constructors/BankAccount.java +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/BankAccount.java @@ -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); } -} +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/constructors/Transaction.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/Transaction.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/constructors/Transaction.java rename to core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/constructors/Transaction.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Employee.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/copyconstructor/Employee.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Employee.java rename to core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/copyconstructor/Employee.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Manager.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/copyconstructor/Manager.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Manager.java rename to core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/copyconstructor/Manager.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/supertypecompilerexception/MyClass.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/supertypecompilerexception/MyClass.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/supertypecompilerexception/MyClass.java rename to core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/supertypecompilerexception/MyClass.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution1.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution1.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution1.java rename to core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution1.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution2.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution2.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution2.java rename to core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution2.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution3.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution3.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution3.java rename to core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/supertypecompilerexception/MyClassSolution3.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/supertypecompilerexception/MyException.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/supertypecompilerexception/MyException.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/supertypecompilerexception/MyException.java rename to core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/supertypecompilerexception/MyException.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/constructors/ConstructorUnitTest.java b/core-java-modules/core-java-lang-oop-constructors/src/test/java/com/baeldung/constructors/ConstructorUnitTest.java similarity index 88% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/constructors/ConstructorUnitTest.java rename to core-java-modules/core-java-lang-oop-constructors/src/test/java/com/baeldung/constructors/ConstructorUnitTest.java index 274fe77764..e207afec57 100644 --- a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/constructors/ConstructorUnitTest.java +++ b/core-java-modules/core-java-lang-oop-constructors/src/test/java/com/baeldung/constructors/ConstructorUnitTest.java @@ -1,20 +1,19 @@ package com.baeldung.constructors; +import org.assertj.core.api.Assertions; import org.junit.Test; import java.time.LocalDateTime; import java.time.Month; -import java.util.logging.Logger; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; public class ConstructorUnitTest { - final static Logger LOGGER = Logger.getLogger(ConstructorUnitTest.class.getName()); - + @Test public void givenNoExplicitContructor_whenUsed_thenFails() { BankAccount account = new BankAccount(); - assertThatThrownBy(() -> { + Assertions.assertThatThrownBy(() -> { account.toString(); }).isInstanceOf(Exception.class); } @@ -22,7 +21,7 @@ public class ConstructorUnitTest { @Test public void givenNoArgumentConstructor_whenUsed_thenSucceeds() { BankAccountEmptyConstructor account = new BankAccountEmptyConstructor(); - assertThatCode(() -> { + Assertions.assertThatCode(() -> { account.toString(); }).doesNotThrowAnyException(); } @@ -33,7 +32,7 @@ public class ConstructorUnitTest { BankAccountParameterizedConstructor account = new BankAccountParameterizedConstructor("Tom", opened, 1000.0f); - assertThatCode(() -> { + Assertions.assertThatCode(() -> { account.toString(); }).doesNotThrowAnyException(); } diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/EmployeeUnitTest.java b/core-java-modules/core-java-lang-oop-constructors/src/test/java/com/baeldung/copyconstructor/EmployeeUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/EmployeeUnitTest.java rename to core-java-modules/core-java-lang-oop-constructors/src/test/java/com/baeldung/copyconstructor/EmployeeUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/ManagerUnitTest.java b/core-java-modules/core-java-lang-oop-constructors/src/test/java/com/baeldung/copyconstructor/ManagerUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/ManagerUnitTest.java rename to core-java-modules/core-java-lang-oop-constructors/src/test/java/com/baeldung/copyconstructor/ManagerUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-generics/README.md b/core-java-modules/core-java-lang-oop-generics/README.md new file mode 100644 index 0000000000..f0213c5659 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/README.md @@ -0,0 +1,8 @@ +## Core Java Lang OOP - Generics + +This module contains articles about generics in Java + +### Relevant Articles: +- [Generic Constructors in Java](https://www.baeldung.com/java-generic-constructors) +- [Type Erasure in Java Explained](https://www.baeldung.com/java-type-erasure) +- [Raw Types in Java](https://www.baeldung.com/raw-types-java) diff --git a/core-java-modules/core-java-lang-oop-generics/pom.xml b/core-java-modules/core-java-lang-oop-generics/pom.xml new file mode 100644 index 0000000000..ae141ecda2 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/pom.xml @@ -0,0 +1,16 @@ + + + + core-java-modules + com.baeldung.core-java-modules + 1.0.0-SNAPSHOT + + 4.0.0 + + core-java-lang-oop-generics + core-java-lang-oop-generics + jar + + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/generics/Entry.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/Entry.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/generics/Entry.java rename to core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/Entry.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/generics/GenericEntry.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/GenericEntry.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/generics/GenericEntry.java rename to core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/GenericEntry.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/generics/MapEntry.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/MapEntry.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/generics/MapEntry.java rename to core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/MapEntry.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/generics/Product.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/Product.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/generics/Product.java rename to core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/Product.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/generics/Rankable.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/Rankable.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/generics/Rankable.java rename to core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/generics/Rankable.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/rawtype/RawTypeDemo.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/rawtype/RawTypeDemo.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/rawtype/RawTypeDemo.java rename to core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/rawtype/RawTypeDemo.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/typeerasure/ArrayContentPrintUtil.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/ArrayContentPrintUtil.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/typeerasure/ArrayContentPrintUtil.java rename to core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/ArrayContentPrintUtil.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/typeerasure/BoundStack.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/BoundStack.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/typeerasure/BoundStack.java rename to core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/BoundStack.java diff --git a/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/Example.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/Example.java new file mode 100644 index 0000000000..f816fd6d16 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/Example.java @@ -0,0 +1,13 @@ +package com.baeldung.typeerasure; + +public class Example { + + public static boolean containsElement(E [] elements, E element){ + for (E e : elements){ + if(e.equals(element)){ + return true; + } + } + return false; + } +} diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/typeerasure/IntegerStack.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/IntegerStack.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/typeerasure/IntegerStack.java rename to core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/IntegerStack.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/typeerasure/Stack.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/Stack.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/typeerasure/Stack.java rename to core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/typeerasure/Stack.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/generics/GenericConstructorUnitTest.java b/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/generics/GenericConstructorUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/generics/GenericConstructorUnitTest.java rename to core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/generics/GenericConstructorUnitTest.java diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/typeerasure/TypeErasureUnitTest.java b/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/typeerasure/TypeErasureUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/typeerasure/TypeErasureUnitTest.java rename to core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/typeerasure/TypeErasureUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-inheritance/README.md b/core-java-modules/core-java-lang-oop-inheritance/README.md new file mode 100644 index 0000000000..c87bdf13d7 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/README.md @@ -0,0 +1,14 @@ +## Core Java Lang OOP - Inheritance + +This module contains articles about inheritance in Java + +### Relevant Articles: +- [Java Interfaces](https://www.baeldung.com/java-interfaces) +- [Abstract Classes in Java](https://www.baeldung.com/java-abstract-class) +- [A Guide to Inner Interfaces in Java](https://www.baeldung.com/java-inner-interfaces) +- [Guide to the super Java Keyword](https://www.baeldung.com/java-super) +- [Anonymous Classes in Java](https://www.baeldung.com/java-anonymous-classes) +- [Polymorphism in Java](https://www.baeldung.com/java-polymorphism) +- [Guide to Inheritance in Java](https://www.baeldung.com/java-inheritance) +- [Object Type Casting in Java](https://www.baeldung.com/java-type-casting) +- [Variable and Method Hiding in Java](https://www.baeldung.com/java-variable-method-hiding) diff --git a/core-java-modules/core-java-lang-oop-inheritance/pom.xml b/core-java-modules/core-java-lang-oop-inheritance/pom.xml new file mode 100644 index 0000000000..a48b28a289 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/pom.xml @@ -0,0 +1,28 @@ + + + + core-java-modules + com.baeldung.core-java-modules + 1.0.0-SNAPSHOT + + 4.0.0 + + core-java-lang-oop-inheritance + core-java-lang-oop-inheritance + jar + + + + org.assertj + assertj-core + ${assertj-core.version} + test + + + + + 3.10.0 + + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/abstractclasses/application/Application.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/application/Application.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/abstractclasses/application/Application.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/application/Application.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/abstractclasses/filereaders/BaseFileReader.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/filereaders/BaseFileReader.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/abstractclasses/filereaders/BaseFileReader.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/filereaders/BaseFileReader.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/abstractclasses/filereaders/LowercaseFileReader.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/filereaders/LowercaseFileReader.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/abstractclasses/filereaders/LowercaseFileReader.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/filereaders/LowercaseFileReader.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/abstractclasses/filereaders/UppercaseFileReader.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/filereaders/UppercaseFileReader.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/abstractclasses/filereaders/UppercaseFileReader.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/filereaders/UppercaseFileReader.java diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/BoardGame.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/BoardGame.java new file mode 100644 index 0000000000..e0395cec7e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/BoardGame.java @@ -0,0 +1,9 @@ +package com.baeldung.abstractclasses.overview; + +public abstract class BoardGame { + //... field declarations, constructors + + public abstract void play(); + + //... concrete methods +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/Checkers.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/Checkers.java new file mode 100644 index 0000000000..5c911649c8 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/abstractclasses/overview/Checkers.java @@ -0,0 +1,8 @@ +package com.baeldung.abstractclasses.overview; + +public class Checkers extends BoardGame { + @Override + public void play() { + //... implementation + } +} diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/anonymous/Book.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/anonymous/Book.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/anonymous/Book.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/anonymous/Book.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/anonymous/Main.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/anonymous/Main.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/anonymous/Main.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/anonymous/Main.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/Animal.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/Animal.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/Animal.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/Animal.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/AnimalFeeder.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/AnimalFeeder.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/AnimalFeeder.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/AnimalFeeder.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/AnimalFeederGeneric.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/AnimalFeederGeneric.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/AnimalFeederGeneric.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/AnimalFeederGeneric.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/Cat.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/Cat.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/Cat.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/Cat.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/Dog.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/Dog.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/Dog.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/Dog.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/Mew.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/Mew.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/casting/Mew.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/casting/Mew.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/ArmoredCar.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/ArmoredCar.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/ArmoredCar.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/ArmoredCar.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/BMW.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/BMW.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/BMW.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/BMW.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/Car.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/Car.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/Car.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/Car.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/Employee.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/Employee.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/Employee.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/Employee.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/Floatable.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/Floatable.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/Floatable.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/Floatable.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/Flyable.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/Flyable.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/Flyable.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/Flyable.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/SpaceCar.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/SpaceCar.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/SpaceCar.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/SpaceCar.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/SpaceTraveller.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/SpaceTraveller.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/inheritance/SpaceTraveller.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/inheritance/SpaceTraveller.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/innerinterfaces/CommaSeparatedCustomers.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/innerinterfaces/CommaSeparatedCustomers.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/innerinterfaces/CommaSeparatedCustomers.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/innerinterfaces/CommaSeparatedCustomers.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/innerinterfaces/Customer.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/innerinterfaces/Customer.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/innerinterfaces/Customer.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/innerinterfaces/Customer.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/Box.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Box.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/Box.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Box.java diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Computer.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Computer.java new file mode 100644 index 0000000000..94d39e2448 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Computer.java @@ -0,0 +1,9 @@ +package com.baeldung.interfaces; + +public class Computer implements Electronic { + + @Override + public int getElectricityUse() { + return 1000; + } +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Electronic.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Electronic.java new file mode 100644 index 0000000000..fcaf67dc3a --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Electronic.java @@ -0,0 +1,22 @@ +package com.baeldung.interfaces; + +public interface Electronic { + // Constant variable + String LED = "LED"; + + // Abstract method + int getElectricityUse(); + + // Static method + static boolean isEnergyEfficient(String electtronicType) { + if (electtronicType.equals(LED)) { + return true; + } + return false; + } + + //Default method + default void printDescription() { + System.out.println("Electronic Description"); + } +} diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/Employee.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Employee.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/Employee.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/Employee.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/EmployeeSalaryComparator.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/EmployeeSalaryComparator.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/EmployeeSalaryComparator.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/EmployeeSalaryComparator.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/HasColor.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/HasColor.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/HasColor.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/HasColor.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/multiinheritance/Car.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/multiinheritance/Car.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/multiinheritance/Car.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/multiinheritance/Car.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/multiinheritance/Fly.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/multiinheritance/Fly.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/multiinheritance/Fly.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/multiinheritance/Fly.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/multiinheritance/Transform.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/multiinheritance/Transform.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/multiinheritance/Transform.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/multiinheritance/Transform.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/multiinheritance/Vehicle.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/multiinheritance/Vehicle.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/multiinheritance/Vehicle.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/multiinheritance/Vehicle.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/polymorphysim/Circle.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/polymorphysim/Circle.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/polymorphysim/Circle.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/polymorphysim/Circle.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/polymorphysim/MainTestClass.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/polymorphysim/MainTestClass.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/polymorphysim/MainTestClass.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/polymorphysim/MainTestClass.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/polymorphysim/Shape.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/polymorphysim/Shape.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/polymorphysim/Shape.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/polymorphysim/Shape.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/polymorphysim/Square.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/polymorphysim/Square.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/interfaces/polymorphysim/Square.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/interfaces/polymorphysim/Square.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/polymorphism/FileManager.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/polymorphism/FileManager.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/polymorphism/FileManager.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/polymorphism/FileManager.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/polymorphism/GenericFile.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/polymorphism/GenericFile.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/polymorphism/GenericFile.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/polymorphism/GenericFile.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/polymorphism/ImageFile.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/polymorphism/ImageFile.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/polymorphism/ImageFile.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/polymorphism/ImageFile.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/polymorphism/TextFile.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/polymorphism/TextFile.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/polymorphism/TextFile.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/polymorphism/TextFile.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/KeywordDemo.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/superkeyword/KeywordDemo.java similarity index 50% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/KeywordDemo.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/superkeyword/KeywordDemo.java index fd608b424c..0e5bd489bb 100644 --- a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/KeywordDemo.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/superkeyword/KeywordDemo.java @@ -1,7 +1,4 @@ -package com.baeldung.keyword; - -import com.baeldung.keyword.superkeyword.SuperSub; -import com.baeldung.keyword.thiskeyword.KeywordUnitTest; +package com.baeldung.superkeyword; /** * Created by Gebruiker on 5/14/2018. @@ -9,8 +6,6 @@ import com.baeldung.keyword.thiskeyword.KeywordUnitTest; public class KeywordDemo { public static void main(String[] args) { - KeywordUnitTest keyword = new KeywordUnitTest(); - SuperSub child = new SuperSub("message from the child class"); } } diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/superkeyword/SuperBase.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/superkeyword/SuperBase.java similarity index 87% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/superkeyword/SuperBase.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/superkeyword/SuperBase.java index a5304fcef9..ec6a90cb06 100644 --- a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/superkeyword/SuperBase.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/superkeyword/SuperBase.java @@ -1,4 +1,4 @@ -package com.baeldung.keyword.superkeyword; +package com.baeldung.superkeyword; /** * Created by Gebruiker on 5/14/2018. diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/superkeyword/SuperSub.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/superkeyword/SuperSub.java similarity index 91% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/superkeyword/SuperSub.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/superkeyword/SuperSub.java index 83bc04ad0f..ada52e4ff2 100644 --- a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/superkeyword/SuperSub.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/superkeyword/SuperSub.java @@ -1,4 +1,4 @@ -package com.baeldung.keyword.superkeyword; +package com.baeldung.superkeyword; /** * Created by Gebruiker on 5/15/2018. diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/method/BaseMethodClass.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/method/BaseMethodClass.java similarity index 71% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/method/BaseMethodClass.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/method/BaseMethodClass.java index 46ed5fd99f..7e6b08c000 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/method/BaseMethodClass.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/method/BaseMethodClass.java @@ -1,4 +1,4 @@ -package com.baeldung.scope.method; +package com.baeldung.variableandmethodhiding.method; public class BaseMethodClass { diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/method/ChildMethodClass.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/method/ChildMethodClass.java similarity index 74% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/method/ChildMethodClass.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/method/ChildMethodClass.java index 1d0cff2d6b..30db54cd84 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/method/ChildMethodClass.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/method/ChildMethodClass.java @@ -1,4 +1,4 @@ -package com.baeldung.scope.method; +package com.baeldung.variableandmethodhiding.method; public class ChildMethodClass extends BaseMethodClass { diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/method/MethodHidingDemo.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/method/MethodHidingDemo.java similarity index 70% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/method/MethodHidingDemo.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/method/MethodHidingDemo.java index 7e0b3ed146..ce1feac665 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/method/MethodHidingDemo.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/method/MethodHidingDemo.java @@ -1,4 +1,4 @@ -package com.baeldung.scope.method; +package com.baeldung.variableandmethodhiding.method; public class MethodHidingDemo { diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/ChildVariable.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/ChildVariable.java similarity index 81% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/ChildVariable.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/ChildVariable.java index 5730e5e282..83434f7dd3 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/ChildVariable.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/ChildVariable.java @@ -1,4 +1,4 @@ -package com.baeldung.scope.variable; +package com.baeldung.variableandmethodhiding.variable; /** * Created by Gebruiker on 5/7/2018. diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/HideVariable.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/HideVariable.java similarity index 89% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/HideVariable.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/HideVariable.java index 8243fdb249..98a29573b8 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/HideVariable.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/HideVariable.java @@ -1,4 +1,4 @@ -package com.baeldung.scope.variable; +package com.baeldung.variableandmethodhiding.variable; /** * Created by Gebruiker on 5/6/2018. diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/ParentVariable.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/ParentVariable.java similarity index 80% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/ParentVariable.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/ParentVariable.java index 7f116b955e..ef17305746 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/ParentVariable.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/ParentVariable.java @@ -1,4 +1,4 @@ -package com.baeldung.scope.variable; +package com.baeldung.variableandmethodhiding.variable; /** * Created by Gebruiker on 5/7/2018. diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/VariableHidingDemo.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/VariableHidingDemo.java similarity index 89% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/VariableHidingDemo.java rename to core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/VariableHidingDemo.java index 1ad71bd966..0fd83ad55e 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/scope/variable/VariableHidingDemo.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/variableandmethodhiding/variable/VariableHidingDemo.java @@ -1,4 +1,4 @@ -package com.baeldung.scope.variable; +package com.baeldung.variableandmethodhiding.variable; /** * Created by Gebruiker on 5/6/2018. diff --git a/core-java-modules/core-java-lang-oop-2/src/main/resources/files/test.txt b/core-java-modules/core-java-lang-oop-inheritance/src/main/resources/files/test.txt similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/resources/files/test.txt rename to core-java-modules/core-java-lang-oop-inheritance/src/main/resources/files/test.txt diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/abstractclasses/test/LowercaseFileReaderUnitTest.java b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/abstractclasses/LowercaseFileReaderUnitTest.java similarity index 94% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/abstractclasses/test/LowercaseFileReaderUnitTest.java rename to core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/abstractclasses/LowercaseFileReaderUnitTest.java index 45e16f0d25..8e970fc0c7 100644 --- a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/abstractclasses/test/LowercaseFileReaderUnitTest.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/abstractclasses/LowercaseFileReaderUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.abstractclasses.test; +package com.baeldung.abstractclasses; import com.baeldung.abstractclasses.filereaders.BaseFileReader; import com.baeldung.abstractclasses.filereaders.LowercaseFileReader; diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/abstractclasses/test/UppercaseFileReaderUnitTest.java b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/abstractclasses/UppercaseFileReaderUnitTest.java similarity index 94% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/abstractclasses/test/UppercaseFileReaderUnitTest.java rename to core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/abstractclasses/UppercaseFileReaderUnitTest.java index dc4df900e4..e7b83a97e6 100644 --- a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/abstractclasses/test/UppercaseFileReaderUnitTest.java +++ b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/abstractclasses/UppercaseFileReaderUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.abstractclasses.test; +package com.baeldung.abstractclasses; import com.baeldung.abstractclasses.filereaders.BaseFileReader; import com.baeldung.abstractclasses.filereaders.UppercaseFileReader; diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/casting/CastingUnitTest.java b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/casting/CastingUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/casting/CastingUnitTest.java rename to core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/casting/CastingUnitTest.java diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/inheritance/AppUnitTest.java b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/inheritance/AppUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/inheritance/AppUnitTest.java rename to core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/inheritance/AppUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/innerinterfaces/InnerInterfaceUnitTest.java b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/innerinterfaces/InnerInterfaceUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/innerinterfaces/InnerInterfaceUnitTest.java rename to core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/innerinterfaces/InnerInterfaceUnitTest.java diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/polymorphism/PolymorphismUnitTest.java b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/polymorphism/PolymorphismUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/polymorphism/PolymorphismUnitTest.java rename to core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/polymorphism/PolymorphismUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-methods/README.md b/core-java-modules/core-java-lang-oop-methods/README.md new file mode 100644 index 0000000000..fa474c9795 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/README.md @@ -0,0 +1,9 @@ +## Core Java Lang OOP - Methods + +This module contains articles about methods in Java + +### Relevant Articles: +- [Methods in Java](https://www.baeldung.com/java-methods) +- [Method Overloading and Overriding in Java](https://www.baeldung.com/java-method-overload-override) +- [Java equals() and hashCode() Contracts](https://www.baeldung.com/java-equals-hashcode-contracts) +- [Guide to hashCode() in Java](https://www.baeldung.com/java-hashcode) diff --git a/core-java-modules/core-java-lang-oop-methods/pom.xml b/core-java-modules/core-java-lang-oop-methods/pom.xml new file mode 100644 index 0000000000..3590b85454 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/pom.xml @@ -0,0 +1,49 @@ + + + + core-java-modules + com.baeldung.core-java-modules + 1.0.0-SNAPSHOT + + 4.0.0 + + core-java-lang-oop-methods + core-java-lang-oop-methods + jar + + + + org.projectlombok + lombok + ${lombok.version} + + + commons-lang + commons-lang + ${commons-lang.version} + + + + org.assertj + assertj-core + ${assertj-core.version} + test + + + nl.jqno.equalsverifier + equalsverifier + ${equalsverifier.version} + test + + + + + 1.18.12 + 2.6 + + 3.10.0 + 3.0.3 + + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/Money.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/equalshashcode/Money.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/Money.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/equalshashcode/Money.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/Team.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/equalshashcode/Team.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/Team.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/equalshashcode/Team.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/Voucher.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/equalshashcode/Voucher.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/Voucher.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/equalshashcode/Voucher.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/WrongTeam.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/equalshashcode/WrongTeam.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/WrongTeam.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/equalshashcode/WrongTeam.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/WrongVoucher.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/equalshashcode/WrongVoucher.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/WrongVoucher.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/equalshashcode/WrongVoucher.java diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/apachecommons/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/apachecommons/User.java new file mode 100644 index 0000000000..4be082019a --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/apachecommons/User.java @@ -0,0 +1,42 @@ +package com.baeldung.hashcode.apachecommons; + +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class User { + + private final Logger logger = LoggerFactory.getLogger(User.class); + private long id; + private String name; + private String email; + + public User(long id, String name, String email) { + this.id = id; + this.name = name; + this.email = email; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null) + return false; + if (this.getClass() != o.getClass()) + return false; + User user = (User) o; + return id == user.id && (name.equals(user.name) && email.equals(user.email)); + } + + @Override + public int hashCode() { + return new HashCodeBuilder(17, 37). + append(id). + append(name). + append(email). + toHashCode(); + } + // getters and setters here + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/eclipse/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/eclipse/User.java new file mode 100644 index 0000000000..e852eef96e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/eclipse/User.java @@ -0,0 +1,42 @@ +package com.baeldung.hashcode.eclipse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class User { + + private final Logger logger = LoggerFactory.getLogger(User.class); + private long id; + private String name; + private String email; + + public User(long id, String name, String email) { + this.id = id; + this.name = name; + this.email = email; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null) + return false; + if (this.getClass() != o.getClass()) + return false; + User user = (User) o; + return id == user.id && (name.equals(user.name) && email.equals(user.email)); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((email == null) ? 0 : email.hashCode()); + result = prime * result + (int) (id ^ (id >>> 32)); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + // getters and setters here + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/improved/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/improved/User.java new file mode 100644 index 0000000000..773a2c7e45 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/improved/User.java @@ -0,0 +1,37 @@ +package com.baeldung.hashcode.improved; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class User { + + private final Logger logger = LoggerFactory.getLogger(User.class); + private long id; + private String name; + private String email; + + public User(long id, String name, String email) { + this.id = id; + this.name = name; + this.email = email; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null) + return false; + if (this.getClass() != o.getClass()) + return false; + User user = (User) o; + return id == user.id && (name.equals(user.name) && email.equals(user.email)); + } + + @Override + public int hashCode() { + return (int) id * name.hashCode() * email.hashCode(); + } + // getters and setters here + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/intellij/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/intellij/User.java new file mode 100644 index 0000000000..f7bf1ab735 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/intellij/User.java @@ -0,0 +1,40 @@ +package com.baeldung.hashcode.intellij; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class User { + + private final Logger logger = LoggerFactory.getLogger(User.class); + private long id; + private String name; + private String email; + + public User(long id, String name, String email) { + this.id = id; + this.name = name; + this.email = email; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null) + return false; + if (this.getClass() != o.getClass()) + return false; + User user = (User) o; + return id == user.id && (name.equals(user.name) && email.equals(user.email)); + } + + @Override + public int hashCode() { + int result = (int) (id ^ (id >>> 32)); + result = 31 * result + name.hashCode(); + result = 31 * result + email.hashCode(); + return result; + } + // getters and setters here + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/lombok/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/lombok/User.java new file mode 100644 index 0000000000..e34246ce24 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/lombok/User.java @@ -0,0 +1,22 @@ +package com.baeldung.hashcode.lombok; + +import lombok.EqualsAndHashCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@EqualsAndHashCode +public class User { + + private final Logger logger = LoggerFactory.getLogger(User.class); + private long id; + private String name; + private String email; + + public User(long id, String name, String email) { + this.id = id; + this.name = name; + this.email = email; + } + // getters and setters here + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/naive/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/naive/User.java new file mode 100644 index 0000000000..b0a33ad3e9 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/naive/User.java @@ -0,0 +1,37 @@ +package com.baeldung.hashcode.naive; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class User { + + private final Logger logger = LoggerFactory.getLogger(User.class); + private long id; + private String name; + private String email; + + public User(long id, String name, String email) { + this.id = id; + this.name = name; + this.email = email; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null) + return false; + if (this.getClass() != o.getClass()) + return false; + User user = (User) o; + return id == user.id && (name.equals(user.name) && email.equals(user.email)); + } + + @Override + public int hashCode() { + return 1; + } + // getters and setters here + +} diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/hashcode/entities/User.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/standard/User.java similarity index 89% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/hashcode/entities/User.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/standard/User.java index 524f176e6b..f8fd5cedbd 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/hashcode/entities/User.java +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/hashcode/standard/User.java @@ -1,4 +1,4 @@ -package com.baeldung.hashcode.entities; +package com.baeldung.hashcode.standard; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,7 +34,6 @@ public class User { hash = 31 * hash + (int) id; hash = 31 * hash + (name == null ? 0 : name.hashCode()); hash = 31 * hash + (email == null ? 0 : email.hashCode()); - logger.info("hashCode() method called - Computed hash: " + hash); return hash; } // getters and setters here diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java diff --git a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/basicmethods/PersonName.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/methods/PersonName.java similarity index 96% rename from core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/basicmethods/PersonName.java rename to core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/methods/PersonName.java index 43bbf0dd62..d1f3f58b8c 100644 --- a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/basicmethods/PersonName.java +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/methods/PersonName.java @@ -1,4 +1,4 @@ -package com.baeldung.basicmethods; +package com.baeldung.methods; import java.io.FileWriter; import java.io.IOException; diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/equalshashcode/MoneyUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/equalshashcode/MoneyUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/equalshashcode/MoneyUnitTest.java rename to core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/equalshashcode/MoneyUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java similarity index 96% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java rename to core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java index 7dfc6d47a3..308004452b 100644 --- a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/equalshashcode/TeamUnitTest.java @@ -38,7 +38,7 @@ public class TeamUnitTest { } @Test - public void equalsContract() { + public void equalsHashCodeContracts() { EqualsVerifier.forClass(Team.class).verify(); } diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/hashcode/entities/UserUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/hashcode/UserUnitTest.java similarity index 90% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/hashcode/entities/UserUnitTest.java rename to core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/hashcode/UserUnitTest.java index 44ea7efed1..51475acabf 100644 --- a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/hashcode/entities/UserUnitTest.java +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/hashcode/UserUnitTest.java @@ -1,5 +1,6 @@ -package com.baeldung.hashcode.entities; +package com.baeldung.hashcode; +import com.baeldung.hashcode.standard.User; import org.junit.After; import org.junit.Assert; import org.junit.Before; diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/hashcode/application/ApplicationUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/hashcode/application/ApplicationUnitTest.java similarity index 93% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/hashcode/application/ApplicationUnitTest.java rename to core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/hashcode/application/ApplicationUnitTest.java index 49857f355a..18b2d4d570 100644 --- a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/hashcode/application/ApplicationUnitTest.java +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/hashcode/application/ApplicationUnitTest.java @@ -1,6 +1,6 @@ package com.baeldung.hashcode.application; -import com.baeldung.hashcode.entities.User; +import com.baeldung.hashcode.standard.User; import org.junit.Test; import java.util.HashMap; diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/methodoverloadingoverriding/MethodOverloadingUnitTest.java similarity index 95% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java rename to core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/methodoverloadingoverriding/MethodOverloadingUnitTest.java index 081a30c34a..476e70618f 100644 --- a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/methodoverloadingoverriding/MethodOverloadingUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.methodoverloadingoverriding.test; +package com.baeldung.methodoverloadingoverriding; import com.baeldung.methodoverloadingoverriding.util.Multiplier; import org.junit.BeforeClass; diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/methodoverloadingoverriding/MethodOverridingUnitTest.java similarity index 97% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java rename to core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/methodoverloadingoverriding/MethodOverridingUnitTest.java index 554ac121bc..f4142d7382 100644 --- a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/methodoverloadingoverriding/MethodOverridingUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.methodoverloadingoverriding.test; +package com.baeldung.methodoverloadingoverriding; import com.baeldung.methodoverloadingoverriding.model.Car; import com.baeldung.methodoverloadingoverriding.model.Vehicle; diff --git a/core-java-modules/core-java-lang-oop-modifiers/README.md b/core-java-modules/core-java-lang-oop-modifiers/README.md new file mode 100644 index 0000000000..eef905fa0e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-modifiers/README.md @@ -0,0 +1,12 @@ +## Core Java Lang OOP - Modifiers + +This module contains articles about modifiers in Java + +### Relevant Articles: +- [Access Modifiers in Java](https://www.baeldung.com/java-access-modifiers) +- [Java ‘public’ Access Modifier](https://www.baeldung.com/java-public-keyword) +- [Java ‘private’ Access Modifier](https://www.baeldung.com/java-private-keyword) +- [The “final” Keyword in Java](https://www.baeldung.com/java-final) +- [A Guide to the Static Keyword in Java](https://www.baeldung.com/java-static) +- [Static and Default Methods in Interfaces in Java](https://www.baeldung.com/java-static-default-methods) +- [The strictfp Keyword in Java](https://www.baeldung.com/java-strictfp) \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-modifiers/pom.xml b/core-java-modules/core-java-lang-oop-modifiers/pom.xml new file mode 100644 index 0000000000..615e20690f --- /dev/null +++ b/core-java-modules/core-java-lang-oop-modifiers/pom.xml @@ -0,0 +1,34 @@ + + + + core-java-modules + com.baeldung.core-java-modules + 1.0.0-SNAPSHOT + + 4.0.0 + + core-java-lang-oop-modifiers + core-java-lang-oop-modifiers + jar + + + + org.assertj + assertj-core + ${assertj-core.version} + test + + + com.h2database + h2 + ${h2.version} + test + + + + + 3.10.0 + + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/Public.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/Public.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/Public.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/Public.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/SubClass.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/SubClass.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/SubClass.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/SubClass.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/SuperPublic.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/SuperPublic.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/SuperPublic.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/SuperPublic.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/another/AnotherPublic.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/another/AnotherPublic.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/another/AnotherPublic.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/another/AnotherPublic.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/another/AnotherSubClass.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/another/AnotherSubClass.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/another/AnotherSubClass.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/another/AnotherSubClass.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/another/AnotherSuperPublic.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/another/AnotherSuperPublic.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/another/AnotherSuperPublic.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/accessmodifiers/another/AnotherSuperPublic.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/application/Application.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/application/Application.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/application/Application.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/application/Application.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Alarm.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Alarm.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Alarm.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Alarm.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Car.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Car.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Car.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Car.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Motorbike.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Motorbike.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Motorbike.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Motorbike.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/MultiAlarmCar.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/MultiAlarmCar.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/MultiAlarmCar.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/MultiAlarmCar.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Vehicle.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Vehicle.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Vehicle.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/defaultstaticinterfacemethods/model/Vehicle.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/finalkeyword/BlackCat.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/finalkeyword/BlackCat.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/finalkeyword/BlackCat.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/finalkeyword/BlackCat.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/finalkeyword/BlackDog.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/finalkeyword/BlackDog.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/finalkeyword/BlackDog.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/finalkeyword/BlackDog.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/finalkeyword/Cat.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/finalkeyword/Cat.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/finalkeyword/Cat.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/finalkeyword/Cat.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/finalkeyword/Dog.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/finalkeyword/Dog.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/finalkeyword/Dog.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/finalkeyword/Dog.java diff --git a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/core/privatemodifier/Employee.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/privatemodifier/Employee.java similarity index 96% rename from core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/core/privatemodifier/Employee.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/privatemodifier/Employee.java index 4598400e93..a0f7829d51 100644 --- a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/core/privatemodifier/Employee.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/privatemodifier/Employee.java @@ -1,4 +1,4 @@ -package com.baeldung.core.privatemodifier; +package com.baeldung.privatemodifier; public class Employee { diff --git a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/core/privatemodifier/ExampleClass.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/privatemodifier/ExampleClass.java similarity index 84% rename from core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/core/privatemodifier/ExampleClass.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/privatemodifier/ExampleClass.java index 8659b4ad9c..c87980fbe8 100644 --- a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/core/privatemodifier/ExampleClass.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/privatemodifier/ExampleClass.java @@ -1,4 +1,4 @@ -package com.baeldung.core.privatemodifier; +package com.baeldung.privatemodifier; public class ExampleClass { diff --git a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/core/privatemodifier/PublicOuterClass.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/privatemodifier/PublicOuterClass.java similarity index 90% rename from core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/core/privatemodifier/PublicOuterClass.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/privatemodifier/PublicOuterClass.java index 8a9e8a8eb3..082d08b034 100644 --- a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/core/privatemodifier/PublicOuterClass.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/privatemodifier/PublicOuterClass.java @@ -1,4 +1,4 @@ -package com.baeldung.core.privatemodifier; +package com.baeldung.privatemodifier; public class PublicOuterClass { diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/publicmodifier/ListOfThree.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/publicmodifier/ListOfThree.java similarity index 92% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/publicmodifier/ListOfThree.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/publicmodifier/ListOfThree.java index 2ded0ba5d3..0e3449f451 100644 --- a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/publicmodifier/ListOfThree.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/publicmodifier/ListOfThree.java @@ -1,4 +1,4 @@ -package com.baeldung.accessmodifiers.publicmodifier; +package com.baeldung.publicmodifier; import java.util.AbstractList; import java.util.Arrays; diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/publicmodifier/SpecialCharacters.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/publicmodifier/SpecialCharacters.java similarity index 61% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/publicmodifier/SpecialCharacters.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/publicmodifier/SpecialCharacters.java index 5556e9aa57..633308e707 100644 --- a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/publicmodifier/SpecialCharacters.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/publicmodifier/SpecialCharacters.java @@ -1,4 +1,4 @@ -package com.baeldung.accessmodifiers.publicmodifier; +package com.baeldung.publicmodifier; public class SpecialCharacters { diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/publicmodifier/Student.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/publicmodifier/Student.java similarity index 96% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/publicmodifier/Student.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/publicmodifier/Student.java index 83a0dcb30f..5aa80286bf 100644 --- a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/accessmodifiers/publicmodifier/Student.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/publicmodifier/Student.java @@ -1,4 +1,4 @@ -package com.baeldung.accessmodifiers.publicmodifier; +package com.baeldung.publicmodifier; import java.math.BigDecimal; import java.sql.Connection; diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/staticdemo/Car.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/Car.java similarity index 96% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/staticdemo/Car.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/Car.java index cdb3806c35..950f008dcd 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/staticdemo/Car.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/Car.java @@ -1,4 +1,4 @@ -package com.baeldung.staticdemo; +package com.baeldung.staticmodifier; /** * This class demonstrates the use of static fields and static methods diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/staticdemo/Singleton.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/Singleton.java similarity index 88% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/staticdemo/Singleton.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/Singleton.java index de75af9d9d..6e7dcf7c60 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/staticdemo/Singleton.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/Singleton.java @@ -1,4 +1,4 @@ -package com.baeldung.staticdemo; +package com.baeldung.staticmodifier; public class Singleton { private Singleton() {} diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/staticdemo/StaticBlock.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/StaticBlockDemo.java similarity index 81% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/staticdemo/StaticBlock.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/StaticBlockDemo.java index fde7afb090..637bb5e82a 100644 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/staticdemo/StaticBlock.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/staticmodifier/StaticBlockDemo.java @@ -1,9 +1,9 @@ -package com.baeldung.staticdemo; +package com.baeldung.staticmodifier; import java.util.LinkedList; import java.util.List; -public class StaticBlock { +public class StaticBlockDemo { private static List ranks = new LinkedList<>(); static { @@ -23,6 +23,6 @@ public class StaticBlock { } public static void setRanks(List ranks) { - StaticBlock.ranks = ranks; + StaticBlockDemo.ranks = ranks; } } diff --git a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/strictfpUsage/Circle.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/strictfpUsage/Circle.java similarity index 100% rename from core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/strictfpUsage/Circle.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/strictfpUsage/Circle.java diff --git a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/strictfpUsage/ScientificCalculator.java b/core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/strictfpUsage/ScientificCalculator.java similarity index 100% rename from core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/strictfpUsage/ScientificCalculator.java rename to core-java-modules/core-java-lang-oop-modifiers/src/main/java/com/baeldung/strictfpUsage/ScientificCalculator.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/defaultistaticinterfacemethods/test/StaticDefaulInterfaceMethodUnitTest.java b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/defaultstaticinterfacemethods/StaticDefaulInterfaceMethodUnitTest.java similarity index 97% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/defaultistaticinterfacemethods/test/StaticDefaulInterfaceMethodUnitTest.java rename to core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/defaultstaticinterfacemethods/StaticDefaulInterfaceMethodUnitTest.java index 7d4b06908a..0dffe8f80e 100644 --- a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/defaultistaticinterfacemethods/test/StaticDefaulInterfaceMethodUnitTest.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/defaultstaticinterfacemethods/StaticDefaulInterfaceMethodUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.defaultistaticinterfacemethods.test; +package com.baeldung.defaultstaticinterfacemethods; import com.baeldung.defaultstaticinterfacemethods.model.Car; import com.baeldung.defaultstaticinterfacemethods.model.Motorbike; diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/finalkeyword/FinalUnitTest.java b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/finalkeyword/FinalUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/finalkeyword/FinalUnitTest.java rename to core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/finalkeyword/FinalUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/accessmodifiers/PublicAccessModifierUnitTest.java b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/publicmodifier/PublicAccessModifierUnitTest.java similarity index 94% rename from core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/accessmodifiers/PublicAccessModifierUnitTest.java rename to core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/publicmodifier/PublicAccessModifierUnitTest.java index ed8fb4f45a..8c014e703b 100644 --- a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/accessmodifiers/PublicAccessModifierUnitTest.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/publicmodifier/PublicAccessModifierUnitTest.java @@ -1,7 +1,5 @@ -package com.baeldung.accessmodifiers; +package com.baeldung.publicmodifier; -import com.baeldung.accessmodifiers.publicmodifier.ListOfThree; -import com.baeldung.accessmodifiers.publicmodifier.Student; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance.Lifecycle; diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/staticdemo/CarIntegrationTest.java b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/CarUnitTest.java similarity index 80% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/staticdemo/CarIntegrationTest.java rename to core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/CarUnitTest.java index 3150627269..f55955caa8 100644 --- a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/staticdemo/CarIntegrationTest.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/CarUnitTest.java @@ -1,10 +1,10 @@ -package com.baeldung.staticdemo; +package com.baeldung.staticmodifier; import static org.junit.Assert.*; import org.junit.Test; -public class CarIntegrationTest { +public class CarUnitTest { @Test public void whenNumberOfCarObjectsInitialized_thenStaticCounterIncreases() { new Car("Jaguar", "V8"); diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/staticdemo/SingletonIntegrationTest.java b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/SingletonUnitTest.java similarity index 81% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/staticdemo/SingletonIntegrationTest.java rename to core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/SingletonUnitTest.java index 28d864073a..6371fd4961 100644 --- a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/staticdemo/SingletonIntegrationTest.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/SingletonUnitTest.java @@ -1,9 +1,9 @@ -package com.baeldung.staticdemo; +package com.baeldung.staticmodifier; import org.junit.Assert; import org.junit.Test; -public class SingletonIntegrationTest { +public class SingletonUnitTest { @Test public void givenStaticInnerClass_whenMultipleTimesInstanceCalled_thenOnlyOneTimeInitialized() { diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/staticdemo/StaticBlockIntegrationTest.java b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/StaticBlockDemoUnitTest.java similarity index 72% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/staticdemo/StaticBlockIntegrationTest.java rename to core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/StaticBlockDemoUnitTest.java index f98e3e14db..f31a8e8895 100644 --- a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/staticdemo/StaticBlockIntegrationTest.java +++ b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/staticmodifier/StaticBlockDemoUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.staticdemo; +package com.baeldung.staticmodifier; import static org.hamcrest.collection.IsIterableContainingInOrder.contains; import static org.junit.Assert.assertThat; @@ -7,11 +7,11 @@ import java.util.List; import org.junit.Test; -public class StaticBlockIntegrationTest { +public class StaticBlockDemoUnitTest { @Test public void whenAddedListElementsThroughStaticBlock_thenEnsureCorrectOrder() { - List actualList = StaticBlock.getRanks(); + List actualList = StaticBlockDemo.getRanks(); assertThat(actualList, contains("Lieutenant", "Captain", "Major", "Colonel", "General")); } } diff --git a/core-java-modules/core-java-lang-oop-4/src/test/java/com/baeldung/strictfpUsage/ScientificCalculatorUnitTest.java b/core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/strictfpUsage/ScientificCalculatorUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-4/src/test/java/com/baeldung/strictfpUsage/ScientificCalculatorUnitTest.java rename to core-java-modules/core-java-lang-oop-modifiers/src/test/java/com/baeldung/strictfpUsage/ScientificCalculatorUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-others/README.md b/core-java-modules/core-java-lang-oop-others/README.md new file mode 100644 index 0000000000..d3909c0014 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/README.md @@ -0,0 +1,8 @@ +## Core Java Lang OOP - Others + +This module contains articles about Object Oriented Programming (OOP) in Java + +### Relevant Articles: +- [Object-Oriented-Programming Concepts in Java](https://www.baeldung.com/java-oop) +- [Static and Dynamic Binding in Java](https://www.baeldung.com/java-static-dynamic-binding) +- [Pass-By-Value as a Parameter Passing Mechanism in Java](https://www.baeldung.com/java-pass-by-value-or-pass-by-reference) diff --git a/core-java-modules/core-java-lang-oop-others/pom.xml b/core-java-modules/core-java-lang-oop-others/pom.xml new file mode 100644 index 0000000000..8eab301748 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/pom.xml @@ -0,0 +1,16 @@ + + + + core-java-modules + com.baeldung.core-java-modules + 1.0.0-SNAPSHOT + + 4.0.0 + + core-java-lang-oop-others + core-java-lang-oop-others + jar + + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/binding/Animal.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/binding/Animal.java similarity index 100% rename from core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/binding/Animal.java rename to core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/binding/Animal.java diff --git a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/binding/AnimalActivity.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/binding/AnimalActivity.java similarity index 90% rename from core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/binding/AnimalActivity.java rename to core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/binding/AnimalActivity.java index 1bd36123e3..a30c3acd00 100644 --- a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/binding/AnimalActivity.java +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/binding/AnimalActivity.java @@ -15,7 +15,7 @@ public class AnimalActivity { logger.info("Animal is sleeping"); } - public static void sleep(Cat cat) { + public static void sleep(Dog dog) { logger.info("Cat is sleeping"); } @@ -30,7 +30,7 @@ public class AnimalActivity { //assigning a dog object to reference of type Animal - Animal catAnimal = new Cat(); + Animal catAnimal = new Dog(); catAnimal.makeNoise(); diff --git a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/binding/Cat.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/binding/Dog.java similarity index 68% rename from core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/binding/Cat.java rename to core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/binding/Dog.java index bbe740e412..b6924569d0 100644 --- a/core-java-modules/core-java-lang-oop-4/src/main/java/com/baeldung/binding/Cat.java +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/binding/Dog.java @@ -6,9 +6,9 @@ import org.slf4j.LoggerFactory; /** * Created by madhumita.g on 25-07-2018. */ -public class Cat extends Animal { +public class Dog extends Animal { - final static Logger logger = LoggerFactory.getLogger(Cat.class); + final static Logger logger = LoggerFactory.getLogger(Dog.class); public void makeNoise() { diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/ArmoredCar.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/ArmoredCar.java new file mode 100644 index 0000000000..29cba3702a --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/ArmoredCar.java @@ -0,0 +1,13 @@ +package com.baeldung.oop; + +public class ArmoredCar extends Car { + private int bulletProofWindows; + + public ArmoredCar(String type, String model, String color) { + super(type, model, color); + } + + public void remoteStartCar() { + // this vehicle can be started by using a remote control + } +} diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/Car.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/Car.java new file mode 100644 index 0000000000..8ea4d779b6 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/Car.java @@ -0,0 +1,58 @@ +package com.baeldung.oop; + +public class Car extends Vehicle { + + private String type; + private String color; + private int speed; + private int numberOfGears; + + public Car(String type, String model, String color) { + super(4, model); + this.type = type; + this.color = color; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public int getSpeed() { + return speed; + } + + public void setSpeed(int speed) { + this.speed = speed; + } + + public int increaseSpeed(int increment) { + if (increment > 0) { + this.speed += increment; + } else { + System.out.println("Increment can't be negative."); + } + return this.speed; + } + + public int decreaseSpeed(int decrement) { + if (decrement > 0 && decrement <= this.speed) { + this.speed -= decrement; + } else { + System.out.println("Decrement can't be negative or greater than current speed."); + } + return this.speed; + } + + public void openDoors() { + // process to open the doors + } + + @Override + public void honk() { + // produces car-specific honk + } +} diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/GenericFile.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/GenericFile.java new file mode 100644 index 0000000000..812a3f1d63 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/GenericFile.java @@ -0,0 +1,63 @@ +package com.baeldung.oop; + +import java.util.Date; + +public class GenericFile { + private String name; + private String extension; + private Date dateCreated; + private String version; + private byte[] content; + + public GenericFile() { + this.setDateCreated(new Date()); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getExtension() { + return extension; + } + + public void setExtension(String extension) { + this.extension = extension; + } + + public Date getDateCreated() { + return dateCreated; + } + + public void setDateCreated(Date dateCreated) { + this.dateCreated = dateCreated; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public byte[] getContent() { + return content; + } + + public void setContent(byte[] content) { + this.content = content; + } + + public String getFileInfo() { + return "Generic File Impl"; + } + + public Object read() { + return content; + } +} diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/ImageFile.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/ImageFile.java new file mode 100644 index 0000000000..2e2b9b4129 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/ImageFile.java @@ -0,0 +1,41 @@ +package com.baeldung.oop; + +public class ImageFile extends GenericFile { + private int height; + private int width; + + public ImageFile(String name, int height, int width, byte[] content, String version) { + this.setHeight(height); + this.setWidth(width); + this.setContent(content); + this.setName(name); + this.setVersion(version); + this.setExtension(".jpg"); + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public String getFileInfo() { + return "Image File Impl"; + } + + public String read() { + return this.getContent() + .toString(); + } + +} diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/TextFile.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/TextFile.java new file mode 100644 index 0000000000..0d7b8e4192 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/TextFile.java @@ -0,0 +1,44 @@ +package com.baeldung.oop; + +public class TextFile extends GenericFile { + private int wordCount; + + public TextFile(String name, String content, String version) { + String[] words = content.split(" "); + this.setWordCount(words.length > 0 ? words.length : 1); + this.setContent(content.getBytes()); + this.setName(name); + this.setVersion(version); + this.setExtension(".txt"); + } + + public int getWordCount() { + return wordCount; + } + + public void setWordCount(int wordCount) { + this.wordCount = wordCount; + } + + public String getFileInfo() { + return "Text File Impl"; + } + + public String read() { + return this.getContent() + .toString(); + } + + public String read(int limit) { + return this.getContent() + .toString() + .substring(0, limit); + } + + public String read(int start, int stop) { + return this.getContent() + .toString() + .substring(start, stop); + } + +} diff --git a/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/Vehicle.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/Vehicle.java new file mode 100644 index 0000000000..d5ef05bc31 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/oop/Vehicle.java @@ -0,0 +1,23 @@ +package com.baeldung.oop; + +public class Vehicle { + private int wheels; + private String model; + + public Vehicle(int wheels, String model) { + this.wheels = wheels; + this.model = model; + } + + public void start() { + // the process of starting the vehicle + } + + public void stop() { + // process to stop the vehicle + } + + public void honk() { + // produces a default honk + } +} diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/parameterpassing/NonPrimitives.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/parameterpassing/NonPrimitives.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/parameterpassing/NonPrimitives.java rename to core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/parameterpassing/NonPrimitives.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/parameterpassing/Primitives.java b/core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/parameterpassing/Primitives.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/parameterpassing/Primitives.java rename to core-java-modules/core-java-lang-oop-others/src/main/java/com/baeldung/parameterpassing/Primitives.java diff --git a/core-java-modules/core-java-lang-oop-4/src/test/java/com/baeldung/binding/AnimalActivityUnitTest.java b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/binding/AnimalActivityUnitTest.java similarity index 96% rename from core-java-modules/core-java-lang-oop-4/src/test/java/com/baeldung/binding/AnimalActivityUnitTest.java rename to core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/binding/AnimalActivityUnitTest.java index 41c67ff389..6ef9b51818 100644 --- a/core-java-modules/core-java-lang-oop-4/src/test/java/com/baeldung/binding/AnimalActivityUnitTest.java +++ b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/binding/AnimalActivityUnitTest.java @@ -62,9 +62,9 @@ public class AnimalActivityUnitTest { @Test public void givenDogReference__whenRefersCatObject_shouldCallFunctionWithAnimalParam() { - Cat cat = new Cat(); + Dog dog = new Dog(); - AnimalActivity.sleep(cat); + AnimalActivity.sleep(dog); verify(mockAppender).doAppend(captorLoggingEvent.capture()); @@ -79,7 +79,7 @@ public class AnimalActivityUnitTest { @Test public void givenAnimaReference__whenRefersDogObject_shouldCallFunctionWithAnimalParam() { - Animal cat = new Cat(); + Animal cat = new Dog(); AnimalActivity.sleep(cat); diff --git a/core-java-modules/core-java-lang-oop-4/src/test/java/com/baeldung/binding/AnimalUnitTest.java b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/binding/AnimalUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-4/src/test/java/com/baeldung/binding/AnimalUnitTest.java rename to core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/binding/AnimalUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-4/src/test/java/com/baeldung/binding/CatUnitTest.java b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/binding/DogUnitTest.java similarity index 94% rename from core-java-modules/core-java-lang-oop-4/src/test/java/com/baeldung/binding/CatUnitTest.java rename to core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/binding/DogUnitTest.java index 76ccfb7719..977c5d65e6 100644 --- a/core-java-modules/core-java-lang-oop-4/src/test/java/com/baeldung/binding/CatUnitTest.java +++ b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/binding/DogUnitTest.java @@ -22,7 +22,7 @@ import static org.mockito.Mockito.verify; * Created by madhumita.g on 01-08-2018. */ @RunWith(MockitoJUnitRunner.class) -public class CatUnitTest { +public class DogUnitTest { @Mock private Appender mockAppender; @@ -45,9 +45,9 @@ public class CatUnitTest { @Test public void makeNoiseTest() { - Cat cat = new Cat(); + Dog dog = new Dog(); - cat.makeNoise(); + dog.makeNoise(); verify(mockAppender).doAppend(captorLoggingEvent.capture()); diff --git a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/parameterpassing/NonPrimitivesUnitTest.java b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/parameterpassing/NonPrimitivesUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/parameterpassing/NonPrimitivesUnitTest.java rename to core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/parameterpassing/NonPrimitivesUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/parameterpassing/PrimitivesUnitTest.java b/core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/parameterpassing/PrimitivesUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/parameterpassing/PrimitivesUnitTest.java rename to core-java-modules/core-java-lang-oop-others/src/test/java/com/baeldung/parameterpassing/PrimitivesUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-patterns/README.md b/core-java-modules/core-java-lang-oop-patterns/README.md new file mode 100644 index 0000000000..178a556a96 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/README.md @@ -0,0 +1,9 @@ +## Core Java Lang OOP - Patterns + +This module contains articles about Object-oriented programming (OOP) patterns in Java + +### Relevant Articles: +- [Composition, Aggregation, and Association in Java](https://www.baeldung.com/java-composition-aggregation-association) +- [Inheritance and Composition (Is-a vs Has-a relationship) in Java](https://www.baeldung.com/java-inheritance-composition) +- [Immutable Objects in Java](https://www.baeldung.com/java-immutable-object) +- [How to Make a Deep Copy of an Object in Java](https://www.baeldung.com/java-deep-copy) diff --git a/core-java-modules/core-java-lang-oop/pom.xml b/core-java-modules/core-java-lang-oop-patterns/pom.xml similarity index 55% rename from core-java-modules/core-java-lang-oop/pom.xml rename to core-java-modules/core-java-lang-oop-patterns/pom.xml index 4415784f85..0102ef2653 100644 --- a/core-java-modules/core-java-lang-oop/pom.xml +++ b/core-java-modules/core-java-lang-oop-patterns/pom.xml @@ -1,20 +1,17 @@ - - 4.0.0 - core-java-lang-oop - 0.1.0-SNAPSHOT - core-java-lang-oop - jar - + - com.baeldung - parent-java - 0.0.1-SNAPSHOT - ../../parent-java + core-java-modules + com.baeldung.core-java-modules + 1.0.0-SNAPSHOT + 4.0.0 + + core-java-lang-oop-patterns + core-java-lang-oop-patterns + jar @@ -22,8 +19,6 @@ commons-lang3 ${commons-lang3.version} - - com.fasterxml.jackson.core jackson-databind @@ -34,7 +29,7 @@ gson ${gson.version} - + org.assertj assertj-core @@ -43,20 +38,8 @@ - - core-java-lang-oop - - - src/main/resources - true - - - - 2.8.2 - 3.10.0 - - + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/deepcopy/Address.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/deepcopy/Address.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/deepcopy/Address.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/deepcopy/Address.java diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/deepcopy/User.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/deepcopy/User.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/deepcopy/User.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/deepcopy/User.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/immutableobjects/Currency.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/immutableobjects/Currency.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/immutableobjects/Currency.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/immutableobjects/Currency.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/immutableobjects/Money.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/immutableobjects/Money.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/immutableobjects/Money.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/immutableobjects/Money.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/application/Application.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/application/Application.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/application/Application.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/application/Application.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Actress.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Actress.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Actress.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Actress.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Computer.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Computer.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Computer.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Computer.java diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/ComputerBuilder.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/ComputerBuilder.java new file mode 100644 index 0000000000..4d8574e93f --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/ComputerBuilder.java @@ -0,0 +1,13 @@ +package com.baeldung.inheritancecomposition.model; + +public abstract class ComputerBuilder { + + public final void buildComputer() { + addProcessor(); + addMemory(); + } + + public abstract void addProcessor(); + + public abstract void addMemory(); +} diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Memory.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Memory.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Memory.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Memory.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Person.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Person.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Person.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Person.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Processor.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Processor.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Processor.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Processor.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/SoundCard.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/SoundCard.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/SoundCard.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/SoundCard.java diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardComputerBuilder.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardComputerBuilder.java new file mode 100644 index 0000000000..76c4732ace --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardComputerBuilder.java @@ -0,0 +1,13 @@ +package com.baeldung.inheritancecomposition.model; + +public class StandardComputerBuilder extends ComputerBuilder { + @Override + public void addProcessor() { + // method implementation + } + + @Override + public void addMemory() { + // method implementation + } +} diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/StandardMemory.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardMemory.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/StandardMemory.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardMemory.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/StandardProcessor.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardProcessor.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/StandardProcessor.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardProcessor.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/StandardSoundCard.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardSoundCard.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/StandardSoundCard.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/StandardSoundCard.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Waitress.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Waitress.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/inheritancecomposition/model/Waitress.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/inheritancecomposition/model/Waitress.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/aggregation/Car.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/aggregation/Car.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/aggregation/Car.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/aggregation/Car.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/aggregation/CarWithStaticInnerWheel.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/aggregation/CarWithStaticInnerWheel.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/aggregation/CarWithStaticInnerWheel.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/aggregation/CarWithStaticInnerWheel.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/aggregation/Wheel.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/aggregation/Wheel.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/aggregation/Wheel.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/aggregation/Wheel.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/association/Child.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/association/Child.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/association/Child.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/association/Child.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/association/Mother.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/association/Mother.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/association/Mother.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/association/Mother.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/composition/Building.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/composition/Building.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/composition/Building.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/composition/Building.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/composition/BuildingWithDefinitionRoomInMethod.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/composition/BuildingWithDefinitionRoomInMethod.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/composition/BuildingWithDefinitionRoomInMethod.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/composition/BuildingWithDefinitionRoomInMethod.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/university/Department.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/university/Department.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/university/Department.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/university/Department.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/university/Professor.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/university/Professor.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/university/Professor.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/university/Professor.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/university/University.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/university/University.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/relationships/university/University.java rename to core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/relationships/university/University.java diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/deepcopy/DeepCopyUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/deepcopy/DeepCopyUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/deepcopy/DeepCopyUnitTest.java rename to core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/deepcopy/DeepCopyUnitTest.java diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/deepcopy/ShallowCopyUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/deepcopy/ShallowCopyUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/deepcopy/ShallowCopyUnitTest.java rename to core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/deepcopy/ShallowCopyUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/immutableobjects/ImmutableObjectsUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/immutableobjects/ImmutableObjectsUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/immutableobjects/ImmutableObjectsUnitTest.java rename to core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/immutableobjects/ImmutableObjectsUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/inheritancecomposition/ActressUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/inheritancecomposition/ActressUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/inheritancecomposition/ActressUnitTest.java rename to core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/inheritancecomposition/ActressUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/inheritancecomposition/CompositionUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/inheritancecomposition/CompositionUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/inheritancecomposition/CompositionUnitTest.java rename to core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/inheritancecomposition/CompositionUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/inheritancecomposition/InheritanceUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/inheritancecomposition/InheritanceUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/inheritancecomposition/InheritanceUnitTest.java rename to core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/inheritancecomposition/InheritanceUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/inheritancecomposition/PersonUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/inheritancecomposition/PersonUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/inheritancecomposition/PersonUnitTest.java rename to core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/inheritancecomposition/PersonUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/inheritancecomposition/WaitressUnitTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/inheritancecomposition/WaitressUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/inheritancecomposition/WaitressUnitTest.java rename to core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/inheritancecomposition/WaitressUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-types/README.md b/core-java-modules/core-java-lang-oop-types/README.md new file mode 100644 index 0000000000..80344c70fa --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types/README.md @@ -0,0 +1,9 @@ +## Core Java Lang OOP - Types + +This module contains articles about types in Java + +### Relevant Articles: +- [Java Classes and Objects](https://www.baeldung.com/java-classes-objects) +- [Guide to the this Java Keyword](https://www.baeldung.com/java-this) +- [Nested Classes in Java](https://www.baeldung.com/java-nested-classes) +- [Marker Interfaces in Java](https://www.baeldung.com/java-marker-interfaces) diff --git a/core-java-modules/core-java-lang-oop-types/pom.xml b/core-java-modules/core-java-lang-oop-types/pom.xml new file mode 100644 index 0000000000..f73434a9ff --- /dev/null +++ b/core-java-modules/core-java-lang-oop-types/pom.xml @@ -0,0 +1,15 @@ + + + + core-java-modules + com.baeldung.core-java-modules + 1.0.0-SNAPSHOT + + 4.0.0 + + core-java-lang-oop-types + core-java-lang-oop-types + jar + \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/markerinterface/DeletableShape.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/markerinterface/DeletableShape.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/markerinterface/DeletableShape.java rename to core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/markerinterface/DeletableShape.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/markerinterface/Rectangle.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/markerinterface/Rectangle.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/markerinterface/Rectangle.java rename to core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/markerinterface/Rectangle.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/markerinterface/Shape.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/markerinterface/Shape.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/markerinterface/Shape.java rename to core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/markerinterface/Shape.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/markerinterface/ShapeDao.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/markerinterface/ShapeDao.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/markerinterface/ShapeDao.java rename to core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/markerinterface/ShapeDao.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/objects/Car.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/objects/Car.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/objects/Car.java rename to core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/objects/Car.java diff --git a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/thiskeyword/KeywordUnitTest.java b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/thiskeyword/Keyword.java similarity index 64% rename from core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/thiskeyword/KeywordUnitTest.java rename to core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/thiskeyword/Keyword.java index 35fd7358af..a09a1ac8e1 100644 --- a/core-java-modules/core-java-lang-oop-3/src/main/java/com/baeldung/keyword/thiskeyword/KeywordUnitTest.java +++ b/core-java-modules/core-java-lang-oop-types/src/main/java/com/baeldung/thiskeyword/Keyword.java @@ -1,17 +1,17 @@ -package com.baeldung.keyword.thiskeyword; +package com.baeldung.thiskeyword; -public class KeywordUnitTest { +public class Keyword { private String name; private int age; - public KeywordUnitTest() { + public Keyword() { this("John", 27); this.printMessage(); printInstance(this); } - public KeywordUnitTest(String name, int age) { + public Keyword(String name, int age) { this.name = name; this.age = age; } @@ -20,11 +20,11 @@ public class KeywordUnitTest { System.out.println("invoked by this"); } - public void printInstance(KeywordUnitTest thisKeyword) { + public void printInstance(Keyword thisKeyword) { System.out.println(thisKeyword); } - public KeywordUnitTest getCurrentInstance() { + public Keyword getCurrentInstance() { return this; } @@ -33,8 +33,8 @@ public class KeywordUnitTest { boolean isInnerClass = true; public ThisInnerClass() { - KeywordUnitTest thisKeyword = KeywordUnitTest.this; - String outerString = KeywordUnitTest.this.name; + Keyword thisKeyword = Keyword.this; + String outerString = Keyword.this.name; System.out.println(this.isInnerClass); } } diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/markerinterface/MarkerInterfaceUnitTest.java b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/markerinterface/MarkerInterfaceUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/markerinterface/MarkerInterfaceUnitTest.java rename to core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/markerinterface/MarkerInterfaceUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/nestedclass/AnonymousInner.java b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/AnonymousInnerUnitTest.java similarity index 91% rename from core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/nestedclass/AnonymousInner.java rename to core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/AnonymousInnerUnitTest.java index 9fa8ee9cd5..98917b9e05 100644 --- a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/nestedclass/AnonymousInner.java +++ b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/AnonymousInnerUnitTest.java @@ -6,7 +6,7 @@ abstract class SimpleAbstractClass { abstract void run(); } -public class AnonymousInner { +public class AnonymousInnerUnitTest { @Test public void run() { diff --git a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/nestedclass/Enclosing.java b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/Enclosing.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/nestedclass/Enclosing.java rename to core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/Enclosing.java diff --git a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/nestedclass/NewEnclosing.java b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/NewEnclosing.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/nestedclass/NewEnclosing.java rename to core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/NewEnclosing.java diff --git a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/nestedclass/NewOuter.java b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/NewOuter.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/nestedclass/NewOuter.java rename to core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/NewOuter.java diff --git a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/nestedclass/Outer.java b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/Outer.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/nestedclass/Outer.java rename to core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/nestedclass/Outer.java diff --git a/core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/objects/ObjectsUnitTest.java b/core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/objects/ObjectsUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-3/src/test/java/com/baeldung/objects/ObjectsUnitTest.java rename to core-java-modules/core-java-lang-oop-types/src/test/java/com/baeldung/objects/ObjectsUnitTest.java diff --git a/core-java-modules/core-java-lang-oop/.gitignore b/core-java-modules/core-java-lang-oop/.gitignore deleted file mode 100644 index 3de4cc647e..0000000000 --- a/core-java-modules/core-java-lang-oop/.gitignore +++ /dev/null @@ -1,26 +0,0 @@ -*.class - -0.* - -#folders# -/target -/neoDb* -/data -/src/main/webapp/WEB-INF/classes -*/META-INF/* -.resourceCache - -# Packaged files # -*.jar -*.war -*.ear - -# Files generated by integration tests -*.txt -backup-pom.xml -/bin/ -/temp - -#IntelliJ specific -.idea/ -*.iml \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop/README.md b/core-java-modules/core-java-lang-oop/README.md deleted file mode 100644 index 2be3d0cab1..0000000000 --- a/core-java-modules/core-java-lang-oop/README.md +++ /dev/null @@ -1,17 +0,0 @@ -## Core Java Lang OOP - -This module contains articles about Object-oriented programming (OOP) in Java - -### Relevant Articles: -- [Guide to hashCode() in Java](https://www.baeldung.com/java-hashcode) -- [A Guide to the Static Keyword in Java](https://www.baeldung.com/java-static) -- [Polymorphism in Java](https://www.baeldung.com/java-polymorphism) -- [Method Overloading and Overriding in Java](https://www.baeldung.com/java-method-overload-override) -- [How to Make a Deep Copy of an Object in Java](https://www.baeldung.com/java-deep-copy) -- [Guide to Inheritance in Java](https://www.baeldung.com/java-inheritance) -- [Object Type Casting in Java](https://www.baeldung.com/java-type-casting) -- [The “final” Keyword in Java](https://www.baeldung.com/java-final) -- [Type Erasure in Java Explained](https://www.baeldung.com/java-type-erasure) -- [Variable and Method Hiding in Java](https://www.baeldung.com/java-variable-method-hiding) -- [Object-Oriented-Programming Concepts in Java](https://www.baeldung.com/java-oop) -- [[More -->]](/core-java-modules/core-java-lang-oop-2) diff --git a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/initializationguide/User.java b/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/initializationguide/User.java deleted file mode 100644 index 1d9a872d69..0000000000 --- a/core-java-modules/core-java-lang-oop/src/main/java/com/baeldung/initializationguide/User.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.baeldung.initializationguide; - -import java.io.Serializable; - -public class User implements Serializable, Cloneable { - private static final long serialVersionUID = 1L; - static String forum; - private String name; - private int id; - - { - id = 0; - System.out.println("Instance Initializer"); - } - - static { - forum = "Java"; - System.out.println("Static Initializer"); - } - - public User(String name, int id) { - super(); - this.name = name; - this.id = id; - } - - public User() { - System.out.println("Constructor"); - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - @Override - protected Object clone() throws CloneNotSupportedException { - return this; - } - -} - diff --git a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/initializationguide/UserUnitTest.java b/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/initializationguide/UserUnitTest.java deleted file mode 100644 index a26b602609..0000000000 --- a/core-java-modules/core-java-lang-oop/src/test/java/com/baeldung/initializationguide/UserUnitTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.initializationguide; -import org.junit.Before; -import org.junit.Test; - -import static org.assertj.core.api.Assertions.*; - -import java.lang.reflect.InvocationTargetException; - -public class UserUnitTest { - - @Test - public void givenUserInstance_whenIntializedWithNew_thenInstanceIsNotNull() { - User user = new User("Alice", 1); - assertThat(user).isNotNull(); - } - - @Test - public void givenUserInstance_whenInitializedWithReflection_thenInstanceIsNotNull() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { - User user = User.class.getConstructor(String.class, int.class) - .newInstance("Alice", 2); - assertThat(user).isNotNull(); - } - - @Test - public void givenUserInstance_whenCopiedWithClone_thenExactMatchIsCreated() throws CloneNotSupportedException { - User user = new User("Alice", 3); - User clonedUser = (User) user.clone(); - assertThat(clonedUser).isEqualTo(user); - } - - @Test - public void givenUserInstance_whenValuesAreNotInitialized_thenUserNameAndIdReturnDefault() { - User user = new User(); - assertThat(user.getName()).isNull(); - assertThat(user.getId() == 0); - } -} diff --git a/core-java-modules/core-java-lang-oop/src/test/resources/.gitignore b/core-java-modules/core-java-lang-oop/src/test/resources/.gitignore deleted file mode 100644 index 83c05e60c8..0000000000 --- a/core-java-modules/core-java-lang-oop/src/test/resources/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -*.class - -#folders# -/target -/neoDb* -/data -/src/main/webapp/WEB-INF/classes -*/META-INF/* - -# Packaged files # -*.jar -*.war -*.ear \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/entities/ComplexClass.java b/core-java-modules/core-java-lang/src/main/java/com/baeldung/equalshashcode/entities/ComplexClass.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/entities/ComplexClass.java rename to core-java-modules/core-java-lang/src/main/java/com/baeldung/equalshashcode/entities/ComplexClass.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/entities/PrimitiveClass.java b/core-java-modules/core-java-lang/src/main/java/com/baeldung/equalshashcode/entities/PrimitiveClass.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/entities/PrimitiveClass.java rename to core-java-modules/core-java-lang/src/main/java/com/baeldung/equalshashcode/entities/PrimitiveClass.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/entities/Rectangle.java b/core-java-modules/core-java-lang/src/main/java/com/baeldung/equalshashcode/entities/Rectangle.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/entities/Rectangle.java rename to core-java-modules/core-java-lang/src/main/java/com/baeldung/equalshashcode/entities/Rectangle.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/entities/Shape.java b/core-java-modules/core-java-lang/src/main/java/com/baeldung/equalshashcode/entities/Shape.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/entities/Shape.java rename to core-java-modules/core-java-lang/src/main/java/com/baeldung/equalshashcode/entities/Shape.java diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/entities/Square.java b/core-java-modules/core-java-lang/src/main/java/com/baeldung/equalshashcode/entities/Square.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/equalshashcode/entities/Square.java rename to core-java-modules/core-java-lang/src/main/java/com/baeldung/equalshashcode/entities/Square.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/equalshashcode/entities/ComplexClassUnitTest.java b/core-java-modules/core-java-lang/src/test/java/com/baeldung/equalshashcode/entities/ComplexClassUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/equalshashcode/entities/ComplexClassUnitTest.java rename to core-java-modules/core-java-lang/src/test/java/com/baeldung/equalshashcode/entities/ComplexClassUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/equalshashcode/entities/PrimitiveClassUnitTest.java b/core-java-modules/core-java-lang/src/test/java/com/baeldung/equalshashcode/entities/PrimitiveClassUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/equalshashcode/entities/PrimitiveClassUnitTest.java rename to core-java-modules/core-java-lang/src/test/java/com/baeldung/equalshashcode/entities/PrimitiveClassUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/equalshashcode/entities/SquareClassUnitTest.java b/core-java-modules/core-java-lang/src/test/java/com/baeldung/equalshashcode/entities/SquareClassUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/equalshashcode/entities/SquareClassUnitTest.java rename to core-java-modules/core-java-lang/src/test/java/com/baeldung/equalshashcode/entities/SquareClassUnitTest.java diff --git a/core-java-modules/core-java-networking-2/README.md b/core-java-modules/core-java-networking-2/README.md index 120b111ff5..662d97252e 100644 --- a/core-java-modules/core-java-networking-2/README.md +++ b/core-java-modules/core-java-networking-2/README.md @@ -11,4 +11,5 @@ This module contains articles about networking in Java - [Sending Emails with Java](https://www.baeldung.com/java-email) - [Authentication with HttpUrlConnection](https://www.baeldung.com/java-http-url-connection) - [Download a File from an URL in Java](https://www.baeldung.com/java-download-file) +- [Handling java.net.ConnectException](https://www.baeldung.com/java-net-connectexception) - [[<-- Prev]](/core-java-modules/core-java-networking) diff --git a/core-java-modules/core-java-regex/README.md b/core-java-modules/core-java-regex/README.md index 6fdea9f2ca..8830af2c2d 100644 --- a/core-java-modules/core-java-regex/README.md +++ b/core-java-modules/core-java-regex/README.md @@ -10,3 +10,4 @@ - [Difference Between Java Matcher find() and matches()](https://www.baeldung.com/java-matcher-find-vs-matches) - [How to Use Regular Expressions to Replace Tokens in Strings](https://www.baeldung.com/java-regex-token-replacement) - [Regular Expressions \s and \s+ in Java](https://www.baeldung.com/java-regex-s-splus) +- [Validate Phone Numbers With Java Regex](https://www.baeldung.com/java-regex-validate-phone-numbers) diff --git a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/RegexUnitTest.java b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/RegexUnitTest.java index b3c3a91a09..77052b79ac 100644 --- a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/RegexUnitTest.java +++ b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/RegexUnitTest.java @@ -26,7 +26,6 @@ public class RegexUnitTest { while (matcher.find()) matches++; assertEquals(matches, 2); - } @Test @@ -452,7 +451,6 @@ public class RegexUnitTest { Matcher matcher = pattern.matcher("dogs are friendly"); assertTrue(matcher.lookingAt()); assertFalse(matcher.matches()); - } @Test @@ -460,7 +458,6 @@ public class RegexUnitTest { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher("dog"); assertTrue(matcher.matches()); - } @Test @@ -469,7 +466,6 @@ public class RegexUnitTest { Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly"); String newStr = matcher.replaceFirst("cat"); assertEquals("cats are domestic animals, dogs are friendly", newStr); - } @Test @@ -478,7 +474,6 @@ public class RegexUnitTest { Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly"); String newStr = matcher.replaceAll("cat"); assertEquals("cats are domestic animals, cats are friendly", newStr); - } public synchronized static int runTest(String regex, String text) { diff --git a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/phonenumbers/PhoneNumbersRegexUnitTest.java b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/phonenumbers/PhoneNumbersRegexUnitTest.java new file mode 100644 index 0000000000..11bf1618b6 --- /dev/null +++ b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/phonenumbers/PhoneNumbersRegexUnitTest.java @@ -0,0 +1,129 @@ +package com.baeldung.regex.phonenumbers; + +import static org.junit.Assert.*; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.junit.Test; + +public class PhoneNumbersRegexUnitTest { + + @Test + public void whenMatchesTenDigitsNumber_thenCorrect() { + Pattern pattern = Pattern.compile("^\\d{10}$"); + Matcher matcher = pattern.matcher("2055550125"); + assertTrue(matcher.matches()); + } + + @Test + public void whenMOreThanTenDigits_thenNotCorrect() { + Pattern pattern = Pattern.compile("^\\d{10}$"); + Matcher matcher = pattern.matcher("20555501251"); + assertFalse(matcher.matches()); + } + + @Test + public void whenMatchesTenDigitsNumberWhitespacesDotHyphen_thenCorrect() { + Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); + Matcher matcher = pattern.matcher("202 555 0125"); + assertTrue(matcher.matches()); + } + + @Test + public void whenIncludesBracket_thenNotCorrect() { + Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); + Matcher matcher = pattern.matcher("202]555 0125"); + assertFalse(matcher.matches()); + } + + @Test + public void whenNotStartsWithBatchesOfThreeDigits_thenNotCorrect() { + Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); + Matcher matcher = pattern.matcher("2021 555 0125"); + assertFalse(matcher.matches()); + } + + @Test + public void whenLastPartWithNoFourDigits_thenNotCorrect() { + Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); + Matcher matcher = pattern.matcher("202 555 012"); + assertFalse(matcher.matches()); + } + + @Test + public void whenMatchesTenDigitsNumberParenthesis_thenCorrect() { + Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); + Matcher matcher = pattern.matcher("(202) 555-0125"); + assertTrue(matcher.matches()); + } + + @Test + public void whenJustOpeningParenthesis_thenNotCorrect() { + Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); + Matcher matcher = pattern.matcher("(202 555-0125"); + assertFalse(matcher.matches()); + } + + @Test + public void whenJustClosingParenthesis_thenNotCorrect() { + Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); + Matcher matcher = pattern.matcher("202) 555-0125"); + assertFalse(matcher.matches()); + } + + @Test + public void whenMatchesTenDigitsNumberPrefix_thenCorrect() { + Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); + Matcher matcher = pattern.matcher("+111 (202) 555-0125"); + assertTrue(matcher.matches()); + } + + @Test + public void whenIncorrectPrefix_thenNotCorrect() { + Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); + Matcher matcher = pattern.matcher("-111 (202) 555-0125"); + assertFalse(matcher.matches()); + } + + @Test + public void whenTooLongPrefix_thenNotCorrect() { + Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); + Matcher matcher = pattern.matcher("+1111 (202) 555-0125"); + assertFalse(matcher.matches()); + } + + @Test + public void whenMatchesPhoneNumber_thenCorrect() { + String patterns + = "^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$" + + "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$" + + "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?)(\\d{2}[ ]?){2}\\d{2}$"; + + String[] validPhoneNumbers + = {"2055550125","202 555 0125", "(202) 555-0125", "+111 (202) 555-0125", "636 856 789", "+111 636 856 789", "636 85 67 89", "+111 636 85 67 89"}; + + Pattern pattern = Pattern.compile(patterns); + for(String phoneNumber : validPhoneNumbers) { + Matcher matcher = pattern.matcher(phoneNumber); + assertTrue(matcher.matches()); + } + } + + @Test + public void whenNotMatchesPhoneNumber_thenNotCorrect() { + String patterns + = "^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$" + + "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$" + + "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?)(\\d{2}[ ]?){2}\\d{2}$"; + + String[] invalidPhoneNumbers + = {"20555501251","202]555 0125", "2021 555 012", "(202 555-0125", "202) 555-0125", "-111 (202) 555-0125", "+1111 (202) 555-0125", "636 85 789", "636 85 67 893"}; + + Pattern pattern = Pattern.compile(patterns); + for(String phoneNumber : invalidPhoneNumbers) { + Matcher matcher = pattern.matcher(phoneNumber); + assertFalse(matcher.matches()); + } + } +} diff --git a/core-java-modules/core-java-streams-3/README.md b/core-java-modules/core-java-streams-3/README.md index 05c4b99900..65713aa04f 100644 --- a/core-java-modules/core-java-streams-3/README.md +++ b/core-java-modules/core-java-streams-3/README.md @@ -10,4 +10,5 @@ This module contains articles about the Stream API in Java. - [Primitive Type Streams in Java 8](https://www.baeldung.com/java-8-primitive-streams) - [Debugging Java 8 Streams with IntelliJ](https://www.baeldung.com/intellij-debugging-java-streams) - [Add BigDecimals using the Stream API](https://www.baeldung.com/java-stream-add-bigdecimals) +- [Should We Close a Java Stream?](https://www.baeldung.com/java-stream-close) - More articles: [[<-- prev>]](/../core-java-streams-2) diff --git a/core-java-modules/core-java-string-operations-2/src/test/java/com/baeldung/pdf/base64/EncodeDecodeUnitTest.java b/core-java-modules/core-java-string-operations-2/src/test/java/com/baeldung/pdf/base64/EncodeDecodeUnitTest.java index 0fb61ea121..0df6f58136 100644 --- a/core-java-modules/core-java-string-operations-2/src/test/java/com/baeldung/pdf/base64/EncodeDecodeUnitTest.java +++ b/core-java-modules/core-java-string-operations-2/src/test/java/com/baeldung/pdf/base64/EncodeDecodeUnitTest.java @@ -4,8 +4,10 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Paths; @@ -39,6 +41,27 @@ public class EncodeDecodeUnitTest { } + @Test + public void givenJavaBase64_whenEncodedStream_thenDecodedStreamOK() throws IOException { + + try (OutputStream os = java.util.Base64.getEncoder().wrap(new FileOutputStream(OUT_FILE)); + FileInputStream fis = new FileInputStream(IN_FILE)) { + byte[] bytes = new byte[1024]; + int read; + while ((read = fis.read(bytes)) > -1) { + os.write(bytes, 0, read); + } + } + + byte[] encoded = java.util.Base64.getEncoder().encode(inFileBytes); + byte[] encodedOnDisk = Files.readAllBytes(Paths.get(OUT_FILE)); + assertArrayEquals(encoded, encodedOnDisk); + + byte[] decoded = java.util.Base64.getDecoder().decode(encoded); + byte[] decodedOnDisk = java.util.Base64.getDecoder().decode(encodedOnDisk); + assertArrayEquals(decoded, decodedOnDisk); + } + @Test public void givenApacheCommons_givenJavaBase64_whenEncoded_thenDecodedOK() throws IOException { diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index b7454cc737..c6cc3726e1 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -85,10 +85,14 @@ core-java-lang-2 core-java-lang-math core-java-lang-math-2 - core-java-lang-oop - core-java-lang-oop-2 - core-java-lang-oop-3 - core-java-lang-oop-4 + core-java-lang-oop-constructors + core-java-lang-oop-patterns + core-java-lang-oop-generics + core-java-lang-oop-modifiers + core-java-lang-oop-types + core-java-lang-oop-inheritance + core-java-lang-oop-methods + core-java-lang-oop-others core-java-lang-operators core-java-lang-syntax core-java-lang-syntax-2 diff --git a/core-kotlin-modules/core-kotlin-collections/README.md b/core-kotlin-modules/core-kotlin-collections/README.md index f0da2b4cfd..66f15e7419 100644 --- a/core-kotlin-modules/core-kotlin-collections/README.md +++ b/core-kotlin-modules/core-kotlin-collections/README.md @@ -9,3 +9,4 @@ This module contains articles about core Kotlin collections. - [Converting a List to Map in Kotlin](https://www.baeldung.com/kotlin-list-to-map) - [Filtering Kotlin Collections](https://www.baeldung.com/kotlin-filter-collection) - [Collection Transformations in Kotlin](https://www.baeldung.com/kotlin-collection-transformations) +- [Difference between fold and reduce in Kotlin](https://www.baeldung.com/kotlin/fold-vs-reduce) diff --git a/core-kotlin-modules/core-kotlin-lang-2/src/main/kotlin/com/baeldung/ifelseexpression/IfElseExpressionExample.kt b/core-kotlin-modules/core-kotlin-lang-2/src/main/kotlin/com/baeldung/ifelseexpression/IfElseExpressionExample.kt new file mode 100644 index 0000000000..f4e42a4f4f --- /dev/null +++ b/core-kotlin-modules/core-kotlin-lang-2/src/main/kotlin/com/baeldung/ifelseexpression/IfElseExpressionExample.kt @@ -0,0 +1,86 @@ +package com.baeldung.ifelseexpression + +fun ifStatementUsage(): String { + val number = 15 + + if (number > 0) { + return "Positive number" + } + return "Positive number not found" +} + +fun ifElseStatementUsage(): String { + val number = -50 + + if (number > 0) { + return "Positive number" + } else { + return "Negative number" + } +} + +fun ifElseExpressionUsage(): String { + val number = -50 + + val result = if (number > 0) { + "Positive number" + } else { + "Negative number" + } + return result +} + +fun ifElseExpressionSingleLineUsage(): String { + val number = -50 + val result = if (number > 0) "Positive number" else "Negative number" + + return result +} + +fun ifElseMultipleExpressionUsage(): Int { + val x = 24 + val y = 73 + + val result = if (x > y) { + println("$x is greater than $y") + x + } else { + println("$x is less than or equal to $y") + y + } + return result +} + +fun ifElseLadderExpressionUsage(): String { + val number = 60 + + val result = if (number < 0) { + "Negative number" + } else if (number in 0..9) { + "Single digit number" + } else if (number in 10..99) { + "Double digit number" + } else { + "Number has more digits" + } + return result +} + +fun ifElseNestedExpressionUsage(): Int { + val x = 37 + val y = 89 + val z = 6 + + val result = if (x > y) { + if (x > z) + x + else + z + } else { + if (y > z) + y + else + z + } + return result +} \ No newline at end of file diff --git a/core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/ifelseexpression/IfElseExpressionExampleTest.kt b/core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/ifelseexpression/IfElseExpressionExampleTest.kt new file mode 100644 index 0000000000..266e41e07b --- /dev/null +++ b/core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/ifelseexpression/IfElseExpressionExampleTest.kt @@ -0,0 +1,43 @@ +package com.baeldung.ifelseexpression + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotEquals + +class IfElseExpressionExampleTest { + + @Test + fun givenNumber_whenIfStatementCalled_thenReturnsString() { + assertEquals("Positive number", ifStatementUsage()) + } + + @Test + fun givenNumber_whenIfElseStatementCalled_thenReturnsString() { + assertEquals("Negative number", ifElseStatementUsage()) + } + + @Test + fun givenNumber_whenIfElseExpressionCalled_thenReturnsString() { + assertEquals("Negative number", ifElseExpressionUsage()) + } + + @Test + fun givenNumber_whenIfElseExpressionSingleLineCalled_thenReturnsString() { + assertEquals("Negative number", ifElseExpressionSingleLineUsage()) + } + + @Test + fun givenNumber_whenIfElseMultipleExpressionCalled_thenReturnsNumber() { + assertEquals(73, ifElseMultipleExpressionUsage()) + } + + @Test + fun givenNumber_whenIfElseLadderExpressionCalled_thenReturnsString() { + assertEquals("Double digit number", ifElseLadderExpressionUsage()) + } + + @Test + fun givenNumber_whenIfElseNestedExpressionCalled_thenReturnsNumber() { + assertEquals(89, ifElseNestedExpressionUsage()) + } +} \ No newline at end of file diff --git a/core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/late/LateInitUnitTest.kt b/core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/late/LateInitUnitTest.kt new file mode 100644 index 0000000000..c99e438742 --- /dev/null +++ b/core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/late/LateInitUnitTest.kt @@ -0,0 +1,22 @@ +package com.baeldung.late + +import org.junit.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class LateInitUnitTest { + + private lateinit var answer: String + + @Test(expected = UninitializedPropertyAccessException::class) + fun givenLateInit_WhenNotInitialized_ShouldThrowAnException() { + answer.length + } + + @Test + fun givenLateInit_TheIsInitialized_ReturnsTheInitializationStatus() { + assertFalse { this::answer.isInitialized } + answer = "42" + assertTrue { this::answer.isInitialized } + } +} diff --git a/ddd/pom.xml b/ddd/pom.xml index 7f3c417b71..1253f2ac48 100644 --- a/ddd/pom.xml +++ b/ddd/pom.xml @@ -96,7 +96,6 @@ 1.0.1 - 2.0.6.RELEASE diff --git a/gradle/gradle-employee-app/README.md b/gradle/gradle-employee-app/README.md new file mode 100644 index 0000000000..1bf7c5e8dd --- /dev/null +++ b/gradle/gradle-employee-app/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Building a Java Application With Gradle](https://www.baeldung.com/gradle-building-a-java-app) diff --git a/java-collections-conversions-2/README.md b/java-collections-conversions-2/README.md index 761e56253e..11dddadc5c 100644 --- a/java-collections-conversions-2/README.md +++ b/java-collections-conversions-2/README.md @@ -4,4 +4,5 @@ This module contains articles about conversions among Collection types and array ### Relevant Articles: - [Array to String Conversions](https://www.baeldung.com/java-array-to-string) -- More articles: [[<-- prev]](../java-collections-conversions) \ No newline at end of file +- [Mapping Lists with ModelMapper](https://www.baeldung.com/java-modelmapper-lists) +- More articles: [[<-- prev]](../java-collections-conversions) diff --git a/java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java b/java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java index 6527d35742..57579e948f 100644 --- a/java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java +++ b/java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java @@ -6,6 +6,7 @@ import org.apache.commons.collections4.MapUtils; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; public class ConvertListToMapService { @@ -21,7 +22,7 @@ public class ConvertListToMapService { } public Map convertListAfterJava8(List list) { - Map map = list.stream().collect(Collectors.toMap(Animal::getId, animal -> animal)); + Map map = list.stream().collect(Collectors.toMap(Animal::getId, Function.identity())); return map; } diff --git a/java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java b/java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java index ad2ab2a756..b8134de08a 100644 --- a/java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java +++ b/java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java @@ -53,14 +53,14 @@ public class CollectionToArrayListUnitTest { verifyShallowCopy(srcCollection, newList); } - + /** * Section 5. Deep Copy */ @Test public void whenUsingDeepCopy_thenVerifyDeepCopy() { ArrayList newList = srcCollection.stream() - .map(foo -> foo.deepCopy()) + .map(Foo::deepCopy) .collect(toCollection(ArrayList::new)); verifyDeepCopy(srcCollection, newList); @@ -83,13 +83,13 @@ public class CollectionToArrayListUnitTest { * @param a * @param b */ - private void verifyShallowCopy(Collection a, Collection b) { + private void verifyShallowCopy(Collection a, Collection b) { assertEquals("Collections have different lengths", a.size(), b.size()); Iterator iterA = a.iterator(); Iterator iterB = b.iterator(); while (iterA.hasNext()) { - // use '==' to test instance identity - assertTrue("Foo instances differ!", iterA.next() == iterB.next()); + // test instance identity + assertSame("Foo instances differ!", iterA.next(), iterB.next()); } } @@ -98,7 +98,7 @@ public class CollectionToArrayListUnitTest { * @param a * @param b */ - private void verifyDeepCopy(Collection a, Collection b) { + private void verifyDeepCopy(Collection a, Collection b) { assertEquals("Collections have different lengths", a.size(), b.size()); Iterator iterA = a.iterator(); Iterator iterB = b.iterator(); @@ -106,7 +106,7 @@ public class CollectionToArrayListUnitTest { Foo nextA = iterA.next(); Foo nextB = iterB.next(); // should not be same instance - assertFalse("Foo instances are the same!", nextA == nextB); + assertNotSame("Foo instances are the same!", nextA, nextB); // but should have same content assertFalse("Foo instances have different content!", fooDiff(nextA, nextB)); } diff --git a/java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java b/java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java index 4d6cba7d27..7d94f88d21 100644 --- a/java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java +++ b/java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java @@ -23,7 +23,7 @@ public class ConvertIteratorToListServiceUnitTest { Iterator iterator; @Before - public void setUp() throws Exception { + public void setUp() { iterator = Arrays.asList(1, 2, 3) .iterator(); } @@ -31,7 +31,7 @@ public class ConvertIteratorToListServiceUnitTest { @Test public void givenAnIterator_whenConvertIteratorToListUsingWhileLoop_thenReturnAList() { - List actualList = new ArrayList(); + List actualList = new ArrayList<>(); // Convert Iterator to List using while loop dsf while (iterator.hasNext()) { @@ -44,7 +44,7 @@ public class ConvertIteratorToListServiceUnitTest { @Test public void givenAnIterator_whenConvertIteratorToListAfterJava8_thenReturnAList() { - List actualList = new ArrayList(); + List actualList = new ArrayList<>(); // Convert Iterator to List using Java 8 iterator.forEachRemaining(actualList::add); diff --git a/java-collections-maps-3/README.md b/java-collections-maps-3/README.md new file mode 100644 index 0000000000..4da8547824 --- /dev/null +++ b/java-collections-maps-3/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Java Map With Case-Insensitive Keys](https://www.baeldung.com/java-map-with-case-insensitive-keys) diff --git a/jee-7/README.md b/jee-7/README.md index adaee67d74..88359a81ec 100644 --- a/jee-7/README.md +++ b/jee-7/README.md @@ -11,3 +11,4 @@ This module contains articles about JEE 7. - [Introduction to Testing with Arquillian](https://www.baeldung.com/arquillian) - [Java EE 7 Batch Processing](https://www.baeldung.com/java-ee-7-batch-processing) - [The Difference Between CDI and EJB Singleton](https://www.baeldung.com/jee-cdi-vs-ejb-singleton) +- [Invoking a SOAP Web Service in Java](https://www.baeldung.com/java-soap-web-service) diff --git a/jsoup/README.md b/jsoup/README.md index 271d04194d..690afe3099 100644 --- a/jsoup/README.md +++ b/jsoup/README.md @@ -4,6 +4,7 @@ This module contains articles about jsoup. ### Relevant Articles: - [Parsing HTML in Java with Jsoup](https://www.baeldung.com/java-with-jsoup) +- [How to add proxy support to Jsoup?](https://www.baeldung.com/java-jsoup-proxy) ### Build the Project diff --git a/jsoup/src/test/java/com/baeldung/jsonproxy/JsoupProxyLiveTest.java b/jsoup/src/test/java/com/baeldung/jsonproxy/JsoupProxyLiveTest.java new file mode 100644 index 0000000000..931464c4db --- /dev/null +++ b/jsoup/src/test/java/com/baeldung/jsonproxy/JsoupProxyLiveTest.java @@ -0,0 +1,27 @@ +package com.baeldung.jsonproxy; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Proxy; + +import org.jsoup.Jsoup; +import org.junit.Test; + +public class JsoupProxyLiveTest { + + @Test + public void whenUsingHostAndPort_thenConnect() throws IOException { + Jsoup.connect("https://spring.io/blog") + .proxy("200.216.227.141", 53281) + .get(); + } + + @Test + public void whenUsingProxyClass_thenConnect() throws IOException { + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("200.216.227.141", 53281)); + + Jsoup.connect("https://spring.io/blog") + .proxy(proxy) + .get(); + } +} diff --git a/kaniko/README.md b/kaniko/README.md new file mode 100644 index 0000000000..a69a3cb683 --- /dev/null +++ b/kaniko/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [An Introduction to Kaniko](https://www.baeldung.com/ops/kaniko) diff --git a/language-interop/README.md b/language-interop/README.md new file mode 100644 index 0000000000..a28c4a5405 --- /dev/null +++ b/language-interop/README.md @@ -0,0 +1,5 @@ +## Language Interop + +This module contains articles about Java interop with other language integrations. + +### Relevant Articles: diff --git a/language-interop/pom.xml b/language-interop/pom.xml new file mode 100644 index 0000000000..bdf872a076 --- /dev/null +++ b/language-interop/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + language-interop + 0.0.1-SNAPSHOT + language-interop + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.python + jython-slim + ${jython.version} + + + org.apache.commons + commons-exec + ${commons-exec.version} + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + language-interop + + + src/main/resources + true + + + src/test/resources + true + + + + + + 2.7.2 + 1.3 + 3.6.1 + + + diff --git a/language-interop/src/main/java/com/baeldung/language/interop/python/ScriptEngineManagerUtils.java b/language-interop/src/main/java/com/baeldung/language/interop/python/ScriptEngineManagerUtils.java new file mode 100644 index 0000000000..7bf07cf598 --- /dev/null +++ b/language-interop/src/main/java/com/baeldung/language/interop/python/ScriptEngineManagerUtils.java @@ -0,0 +1,34 @@ +package com.baeldung.language.interop.python; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.script.ScriptEngineFactory; +import javax.script.ScriptEngineManager; + +public class ScriptEngineManagerUtils { + + private static final Logger LOGGER = LoggerFactory.getLogger(ScriptEngineManagerUtils.class); + + private ScriptEngineManagerUtils() { + } + + public static void listEngines() { + ScriptEngineManager manager = new ScriptEngineManager(); + List engines = manager.getEngineFactories(); + + for (ScriptEngineFactory engine : engines) { + LOGGER.info("Engine name: {}", engine.getEngineName()); + LOGGER.info("Version: {}", engine.getEngineVersion()); + LOGGER.info("Language: {}", engine.getLanguageName()); + + LOGGER.info("Short Names:"); + for (String names : engine.getNames()) { + LOGGER.info(names); + } + } + } + +} diff --git a/core-java-modules/core-java-lang-oop/src/main/resources/logback.xml b/language-interop/src/main/resources/logback.xml similarity index 57% rename from core-java-modules/core-java-lang-oop/src/main/resources/logback.xml rename to language-interop/src/main/resources/logback.xml index 56af2d397e..7d900d8ea8 100644 --- a/core-java-modules/core-java-lang-oop/src/main/resources/logback.xml +++ b/language-interop/src/main/resources/logback.xml @@ -7,12 +7,6 @@ - - - - - - diff --git a/language-interop/src/test/java/com/baeldung/language/interop/python/JavaPythonInteropUnitTest.java b/language-interop/src/test/java/com/baeldung/language/interop/python/JavaPythonInteropUnitTest.java new file mode 100644 index 0000000000..50e7403470 --- /dev/null +++ b/language-interop/src/test/java/com/baeldung/language/interop/python/JavaPythonInteropUnitTest.java @@ -0,0 +1,131 @@ +package com.baeldung.language.interop.python; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.util.List; +import java.util.stream.Collectors; + +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.SimpleScriptContext; + +import org.apache.commons.exec.CommandLine; +import org.apache.commons.exec.DefaultExecutor; +import org.apache.commons.exec.ExecuteException; +import org.apache.commons.exec.PumpStreamHandler; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.python.core.PyException; +import org.python.core.PyObject; +import org.python.util.PythonInterpreter; + +public class JavaPythonInteropUnitTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void givenPythonScript_whenPythonProcessInvoked_thenSuccess() throws Exception { + ProcessBuilder processBuilder = new ProcessBuilder("python", resolvePythonScriptPath("hello.py")); + processBuilder.redirectErrorStream(true); + + Process process = processBuilder.start(); + List results = readProcessOutput(process.getInputStream()); + + assertThat("Results should not be empty", results, is(not(empty()))); + assertThat("Results should contain output of script: ", results, hasItem(containsString("Hello Baeldung Readers!!"))); + + int exitCode = process.waitFor(); + assertEquals("No errors should be detected", 0, exitCode); + } + + @Test + public void givenPythonScriptEngineIsAvailable_whenScriptInvoked_thenOutputDisplayed() throws Exception { + StringWriter output = new StringWriter(); + ScriptContext context = new SimpleScriptContext(); + context.setWriter(output); + + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByName("python"); + engine.eval(new FileReader(resolvePythonScriptPath("hello.py")), context); + assertEquals("Should contain script output: ", "Hello Baeldung Readers!!", output.toString() + .trim()); + } + + @Test + public void givenPythonInterpreter_whenPrintExecuted_thenOutputDisplayed() { + try (PythonInterpreter pyInterp = new PythonInterpreter()) { + StringWriter output = new StringWriter(); + pyInterp.setOut(output); + + pyInterp.exec("print('Hello Baeldung Readers!!')"); + assertEquals("Should contain script output: ", "Hello Baeldung Readers!!", output.toString() + .trim()); + } + } + + @Test + public void givenPythonInterpreter_whenNumbersAdded_thenOutputDisplayed() { + try (PythonInterpreter pyInterp = new PythonInterpreter()) { + pyInterp.exec("x = 10+10"); + PyObject x = pyInterp.get("x"); + assertEquals("x: ", 20, x.asInt()); + } + } + + @Test + public void givenPythonInterpreter_whenErrorOccurs_thenExceptionIsThrown() { + thrown.expect(PyException.class); + thrown.expectMessage("ImportError: No module named syds"); + + try (PythonInterpreter pyInterp = new PythonInterpreter()) { + pyInterp.exec("import syds"); + } + } + + @Test + public void givenPythonScript_whenPythonProcessExecuted_thenSuccess() throws ExecuteException, IOException { + String line = "python " + resolvePythonScriptPath("hello.py"); + CommandLine cmdLine = CommandLine.parse(line); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); + + DefaultExecutor executor = new DefaultExecutor(); + executor.setStreamHandler(streamHandler); + + int exitCode = executor.execute(cmdLine); + assertEquals("No errors should be detected", 0, exitCode); + assertEquals("Should contain script output: ", "Hello Baeldung Readers!!", outputStream.toString() + .trim()); + } + + private List readProcessOutput(InputStream inputStream) throws IOException { + try (BufferedReader output = new BufferedReader(new InputStreamReader(inputStream))) { + return output.lines() + .collect(Collectors.toList()); + } + } + + private String resolvePythonScriptPath(String filename) { + File file = new File("src/test/resources/" + filename); + return file.getAbsolutePath(); + } + +} diff --git a/language-interop/src/test/resources/hello.py b/language-interop/src/test/resources/hello.py new file mode 100644 index 0000000000..13275d9257 --- /dev/null +++ b/language-interop/src/test/resources/hello.py @@ -0,0 +1 @@ +print("Hello Baeldung Readers!!") \ No newline at end of file diff --git a/libraries-2/README.md b/libraries-2/README.md index edf513c6ee..8dae12a1cf 100644 --- a/libraries-2/README.md +++ b/libraries-2/README.md @@ -18,5 +18,5 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m - [Guide to MapDB](https://www.baeldung.com/mapdb) - [A Guide to Apache Mesos](https://www.baeldung.com/apache-mesos) - [JasperReports with Spring](https://www.baeldung.com/spring-jasper) -- More articles [[<-- prev]](/libraries) +- More articles [[<-- prev]](/libraries) [[next -->]](/libraries-3) diff --git a/libraries-3/README.md b/libraries-3/README.md index ec433960ef..6279dcf5ad 100644 --- a/libraries-3/README.md +++ b/libraries-3/README.md @@ -17,3 +17,4 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m - [Using NullAway to Avoid NullPointerExceptions](https://www.baeldung.com/java-nullaway) - [Introduction to Alibaba Arthas](https://www.baeldung.com/java-alibaba-arthas-intro) - [Quick Guide to Spring Cloud Circuit Breaker](https://www.baeldung.com/spring-cloud-circuit-breaker) +- More articles [[<-- prev]](/libraries-2) [[next -->]](/libraries-4) diff --git a/libraries-4/README.md b/libraries-4/README.md new file mode 100644 index 0000000000..0dee9f1c1e --- /dev/null +++ b/libraries-4/README.md @@ -0,0 +1,21 @@ +## Libraries-4 + +This module contains articles about various Java libraries. +These are small libraries that are relatively easy to use and do not require any separate module of their own. + +The code examples related to different libraries are each in their own module. + +Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-modules) we already have separate modules. Please make sure to have a look at the existing modules in such cases. + +### Relevant articles +- [Quick Guide to RSS with Rome](https://www.baeldung.com/rome-rss) +- [Introduction to PCollections](https://www.baeldung.com/java-pcollections) +- [Introduction to Eclipse Collections](https://www.baeldung.com/eclipse-collections) +- [DistinctBy in the Java Stream API](https://www.baeldung.com/java-streams-distinct-by) +- [Introduction to NoException](https://www.baeldung.com/no-exception) +- [Spring Yarg Integration](https://www.baeldung.com/spring-yarg) +- [Delete a Directory Recursively in Java](https://www.baeldung.com/java-delete-directory) +- [Guide to JDeferred](https://www.baeldung.com/jdeferred) +- [Introduction to MBassador](https://www.baeldung.com/mbassador) +- [Using Pairs in Java](https://www.baeldung.com/java-pairs) +- More articles [[<-- prev]](/libraries-3) [[next -->]](/libraries-5) diff --git a/libraries-4/pom.xml b/libraries-4/pom.xml new file mode 100644 index 0000000000..f26e7fc055 --- /dev/null +++ b/libraries-4/pom.xml @@ -0,0 +1,116 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + libraries-4 + + + + org.jdeferred + jdeferred-core + ${jdeferred.version} + + + org.eclipse.collections + eclipse-collections + ${eclipse-collections.version} + + + com.haulmont.yarg + yarg + ${yarg.version} + + + net.engio + mbassador + ${mbassador.version} + + + com.machinezoo.noexception + noexception + ${noexception.version} + + + rome + rome + ${rome.version} + + + org.springframework + spring-web + ${spring.version} + + + org.datanucleus + javax.jdo + ${javax.jdo.version} + + + javax.servlet + servlet-api + ${javax.servlet.version} + + + io.vavr + vavr + ${vavr.version} + + + org.assertj + assertj-core + ${assertj.version} + + + org.pcollections + pcollections + ${pcollections.version} + + + org.awaitility + awaitility + ${awaitility.version} + test + + + one.util + streamex + ${streamex.version} + + + javax.el + javax.el-api + ${javax.el.version} + + + org.glassfish.web + javax.el + 2.2.4 + + + + + 1.2.6 + 8.2.0 + 1.1.0 + 2.0.12 + 1.3.1 + 1.0 + 4.3.8.RELEASE + 2.5 + 3.2.0-m7 + 0.9.0 + 3.6.2 + 2.1.2 + 3.0.0 + 0.6.5 + 3.0.0 + + + \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/distinct/DistinctWithJavaFunction.java b/libraries-4/src/main/java/com/baeldung/distinct/DistinctWithJavaFunction.java similarity index 100% rename from libraries/src/main/java/com/baeldung/distinct/DistinctWithJavaFunction.java rename to libraries-4/src/main/java/com/baeldung/distinct/DistinctWithJavaFunction.java diff --git a/libraries/src/main/java/com/baeldung/distinct/Person.java b/libraries-4/src/main/java/com/baeldung/distinct/Person.java similarity index 100% rename from libraries/src/main/java/com/baeldung/distinct/Person.java rename to libraries-4/src/main/java/com/baeldung/distinct/Person.java diff --git a/libraries/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java b/libraries-4/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java similarity index 100% rename from libraries/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java rename to libraries-4/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java diff --git a/libraries/src/main/java/com/baeldung/eclipsecollections/Student.java b/libraries-4/src/main/java/com/baeldung/eclipsecollections/Student.java similarity index 100% rename from libraries/src/main/java/com/baeldung/eclipsecollections/Student.java rename to libraries-4/src/main/java/com/baeldung/eclipsecollections/Student.java diff --git a/libraries/src/main/java/com/baeldung/jdeffered/FilterDemo.java b/libraries-4/src/main/java/com/baeldung/jdeffered/FilterDemo.java similarity index 100% rename from libraries/src/main/java/com/baeldung/jdeffered/FilterDemo.java rename to libraries-4/src/main/java/com/baeldung/jdeffered/FilterDemo.java diff --git a/libraries/src/main/java/com/baeldung/jdeffered/PipeDemo.java b/libraries-4/src/main/java/com/baeldung/jdeffered/PipeDemo.java similarity index 100% rename from libraries/src/main/java/com/baeldung/jdeffered/PipeDemo.java rename to libraries-4/src/main/java/com/baeldung/jdeffered/PipeDemo.java diff --git a/libraries/src/main/java/com/baeldung/jdeffered/PromiseDemo.java b/libraries-4/src/main/java/com/baeldung/jdeffered/PromiseDemo.java similarity index 100% rename from libraries/src/main/java/com/baeldung/jdeffered/PromiseDemo.java rename to libraries-4/src/main/java/com/baeldung/jdeffered/PromiseDemo.java diff --git a/libraries/src/main/java/com/baeldung/jdeffered/ThreadSafeDemo.java b/libraries-4/src/main/java/com/baeldung/jdeffered/ThreadSafeDemo.java similarity index 100% rename from libraries/src/main/java/com/baeldung/jdeffered/ThreadSafeDemo.java rename to libraries-4/src/main/java/com/baeldung/jdeffered/ThreadSafeDemo.java diff --git a/libraries/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerDemo.java b/libraries-4/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerDemo.java similarity index 100% rename from libraries/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerDemo.java rename to libraries-4/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerDemo.java diff --git a/libraries/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerWithExecutorDemo.java b/libraries-4/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerWithExecutorDemo.java similarity index 100% rename from libraries/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerWithExecutorDemo.java rename to libraries-4/src/main/java/com/baeldung/jdeffered/manager/DeferredManagerWithExecutorDemo.java diff --git a/libraries/src/main/java/com/baeldung/jdeffered/manager/SimpleDeferredManagerDemo.java b/libraries-4/src/main/java/com/baeldung/jdeffered/manager/SimpleDeferredManagerDemo.java similarity index 100% rename from libraries/src/main/java/com/baeldung/jdeffered/manager/SimpleDeferredManagerDemo.java rename to libraries-4/src/main/java/com/baeldung/jdeffered/manager/SimpleDeferredManagerDemo.java diff --git a/libraries/src/main/java/com/baeldung/mbassador/AckMessage.java b/libraries-4/src/main/java/com/baeldung/mbassador/AckMessage.java similarity index 100% rename from libraries/src/main/java/com/baeldung/mbassador/AckMessage.java rename to libraries-4/src/main/java/com/baeldung/mbassador/AckMessage.java diff --git a/libraries/src/main/java/com/baeldung/mbassador/Message.java b/libraries-4/src/main/java/com/baeldung/mbassador/Message.java similarity index 100% rename from libraries/src/main/java/com/baeldung/mbassador/Message.java rename to libraries-4/src/main/java/com/baeldung/mbassador/Message.java diff --git a/libraries/src/main/java/com/baeldung/mbassador/RejectMessage.java b/libraries-4/src/main/java/com/baeldung/mbassador/RejectMessage.java similarity index 100% rename from libraries/src/main/java/com/baeldung/mbassador/RejectMessage.java rename to libraries-4/src/main/java/com/baeldung/mbassador/RejectMessage.java diff --git a/libraries/src/main/java/com/baeldung/noexception/CustomExceptionHandler.java b/libraries-4/src/main/java/com/baeldung/noexception/CustomExceptionHandler.java similarity index 100% rename from libraries/src/main/java/com/baeldung/noexception/CustomExceptionHandler.java rename to libraries-4/src/main/java/com/baeldung/noexception/CustomExceptionHandler.java diff --git a/libraries/src/main/java/com/baeldung/pairs/CustomPair.java b/libraries-4/src/main/java/com/baeldung/pairs/CustomPair.java similarity index 100% rename from libraries/src/main/java/com/baeldung/pairs/CustomPair.java rename to libraries-4/src/main/java/com/baeldung/pairs/CustomPair.java diff --git a/libraries/src/main/java/com/baeldung/rome/RSSRomeExample.java b/libraries-4/src/main/java/com/baeldung/rome/RSSRomeExample.java similarity index 100% rename from libraries/src/main/java/com/baeldung/rome/RSSRomeExample.java rename to libraries-4/src/main/java/com/baeldung/rome/RSSRomeExample.java diff --git a/libraries/src/main/java/com/baeldung/yarg/DocumentController.java b/libraries-4/src/main/java/com/baeldung/yarg/DocumentController.java similarity index 100% rename from libraries/src/main/java/com/baeldung/yarg/DocumentController.java rename to libraries-4/src/main/java/com/baeldung/yarg/DocumentController.java diff --git a/libraries/src/test/java/com/baeldung/distinct/DistinctWithEclipseCollectionsUnitTest.java b/libraries-4/src/test/java/com/baeldung/distinct/DistinctWithEclipseCollectionsUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/distinct/DistinctWithEclipseCollectionsUnitTest.java rename to libraries-4/src/test/java/com/baeldung/distinct/DistinctWithEclipseCollectionsUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/distinct/DistinctWithJavaFunctionUnitTest.java b/libraries-4/src/test/java/com/baeldung/distinct/DistinctWithJavaFunctionUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/distinct/DistinctWithJavaFunctionUnitTest.java rename to libraries-4/src/test/java/com/baeldung/distinct/DistinctWithJavaFunctionUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/distinct/DistinctWithStreamexUnitTest.java b/libraries-4/src/test/java/com/baeldung/distinct/DistinctWithStreamexUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/distinct/DistinctWithStreamexUnitTest.java rename to libraries-4/src/test/java/com/baeldung/distinct/DistinctWithStreamexUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/distinct/DistinctWithVavrUnitTest.java b/libraries-4/src/test/java/com/baeldung/distinct/DistinctWithVavrUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/distinct/DistinctWithVavrUnitTest.java rename to libraries-4/src/test/java/com/baeldung/distinct/DistinctWithVavrUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/distinct/PersonDataGenerator.java b/libraries-4/src/test/java/com/baeldung/distinct/PersonDataGenerator.java similarity index 100% rename from libraries/src/test/java/com/baeldung/distinct/PersonDataGenerator.java rename to libraries-4/src/test/java/com/baeldung/distinct/PersonDataGenerator.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/AllSatisfyPatternUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/AllSatisfyPatternUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/AllSatisfyPatternUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/AllSatisfyPatternUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/AnySatisfyPatternUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/AnySatisfyPatternUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/AnySatisfyPatternUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/AnySatisfyPatternUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/CollectPatternUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/CollectPatternUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/DetectPatternUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/DetectPatternUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/DetectPatternUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/DetectPatternUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/FlatCollectUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/FlatCollectUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/FlatCollectUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/FlatCollectUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/ForEachPatternUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/ForEachPatternUnitTest.java similarity index 90% rename from libraries/src/test/java/com/baeldung/eclipsecollections/ForEachPatternUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/ForEachPatternUnitTest.java index 38d95047ed..a1bd280658 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/ForEachPatternUnitTest.java +++ b/libraries-4/src/test/java/com/baeldung/eclipsecollections/ForEachPatternUnitTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals; import org.eclipse.collections.api.tuple.Pair; import org.eclipse.collections.impl.map.mutable.UnifiedMap; import org.eclipse.collections.impl.tuple.Tuples; +import org.junit.Assert; import org.junit.Test; public class ForEachPatternUnitTest { @@ -23,7 +24,7 @@ public class ForEachPatternUnitTest { } for (int i = 0; i < map.size(); i++) { - assertEquals("New Value", map.get(i + 1)); + Assert.assertEquals("New Value", map.get(i + 1)); } } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/LazyIterationUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/LazyIterationUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/LazyIterationUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/LazyIterationUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/PartitionPatternUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/PartitionPatternUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/PartitionPatternUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/PartitionPatternUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/RejectPatternUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/RejectPatternUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/RejectPatternUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/RejectPatternUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/SelectPatternUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/SelectPatternUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/SelectPatternUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/SelectPatternUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/ZipUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/ZipUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/ZipUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/ZipUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexUnitTest.java b/libraries-4/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexUnitTest.java rename to libraries-4/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/java/io/JavaDirectoryDeleteUnitTest.java b/libraries-4/src/test/java/com/baeldung/io/JavaDirectoryDeleteUnitTest.java similarity index 96% rename from libraries/src/test/java/com/baeldung/java/io/JavaDirectoryDeleteUnitTest.java rename to libraries-4/src/test/java/com/baeldung/io/JavaDirectoryDeleteUnitTest.java index 53d9d11bbb..c9c8242cd5 100644 --- a/libraries/src/test/java/com/baeldung/java/io/JavaDirectoryDeleteUnitTest.java +++ b/libraries-4/src/test/java/com/baeldung/io/JavaDirectoryDeleteUnitTest.java @@ -1,138 +1,138 @@ -package com.baeldung.java.io; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.IOException; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; - -import org.apache.commons.io.FileUtils; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.springframework.util.FileSystemUtils; - -public class JavaDirectoryDeleteUnitTest { - private static Path TEMP_DIRECTORY; - private static final String DIRECTORY_NAME = "toBeDeleted"; - - private static final List ALL_LINES = Arrays.asList("This is line 1", "This is line 2", "This is line 3", "This is line 4", "This is line 5", "This is line 6"); - - @BeforeClass - public static void initializeTempDirectory() throws IOException { - TEMP_DIRECTORY = Files.createTempDirectory("tmpForJUnit"); - } - - @AfterClass - public static void cleanTempDirectory() throws IOException { - FileUtils.deleteDirectory(TEMP_DIRECTORY.toFile()); - } - - @Before - public void setupDirectory() throws IOException { - Path tempPathForEachTest = Files.createDirectory(TEMP_DIRECTORY.resolve(DIRECTORY_NAME)); - - // Create a directory structure - Files.write(tempPathForEachTest.resolve("file1.txt"), ALL_LINES.subList(0, 2)); - Files.write(tempPathForEachTest.resolve("file2.txt"), ALL_LINES.subList(2, 4)); - - Files.createDirectories(tempPathForEachTest.resolve("Empty")); - - Path aSubDir = Files.createDirectories(tempPathForEachTest.resolve("notEmpty")); - Files.write(aSubDir.resolve("file3.txt"), ALL_LINES.subList(3, 5)); - Files.write(aSubDir.resolve("file4.txt"), ALL_LINES.subList(0, 3)); - - aSubDir = Files.createDirectories(aSubDir.resolve("anotherSubDirectory")); - Files.write(aSubDir.resolve("file5.txt"), ALL_LINES.subList(4, 5)); - Files.write(aSubDir.resolve("file6.txt"), ALL_LINES.subList(0, 2)); - } - - @After - public void checkAndCleanupIfRequired() throws IOException { - Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); - if (Files.exists(pathToBeDeleted)) { - FileUtils.deleteDirectory(pathToBeDeleted.toFile()); - } - } - - private boolean deleteDirectory(File directoryToBeDeleted) { - File[] allContents = directoryToBeDeleted.listFiles(); - - if (allContents != null) { - for (File file : allContents) { - deleteDirectory(file); - } - } - - return directoryToBeDeleted.delete(); - } - - @Test - public void givenDirectory_whenDeletedWithRecursion_thenIsGone() throws IOException { - Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); - - boolean result = deleteDirectory(pathToBeDeleted.toFile()); - - assertTrue("Could not delete directory", result); - assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); - } - - @Test - public void givenDirectory_whenDeletedWithCommonsIOFileUtils_thenIsGone() throws IOException { - Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); - - FileUtils.deleteDirectory(pathToBeDeleted.toFile()); - - assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); - } - - @Test - public void givenDirectory_whenDeletedWithSpringFileSystemUtils_thenIsGone() throws IOException { - Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); - - boolean result = FileSystemUtils.deleteRecursively(pathToBeDeleted.toFile()); - - assertTrue("Could not delete directory", result); - assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); - } - - @Test - public void givenDirectory_whenDeletedWithFilesWalk_thenIsGone() throws IOException { - Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); - - Files.walk(pathToBeDeleted).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); - - assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); - } - - @Test - public void givenDirectory_whenDeletedWithNIO2WalkFileTree_thenIsGone() throws IOException { - Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); - - Files.walkFileTree(pathToBeDeleted, new SimpleFileVisitor() { - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - }); - - assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); - } -} +package com.baeldung.io; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.util.FileSystemUtils; + +public class JavaDirectoryDeleteUnitTest { + private static Path TEMP_DIRECTORY; + private static final String DIRECTORY_NAME = "toBeDeleted"; + + private static final List ALL_LINES = Arrays.asList("This is line 1", "This is line 2", "This is line 3", "This is line 4", "This is line 5", "This is line 6"); + + @BeforeClass + public static void initializeTempDirectory() throws IOException { + TEMP_DIRECTORY = Files.createTempDirectory("tmpForJUnit"); + } + + @AfterClass + public static void cleanTempDirectory() throws IOException { + FileUtils.deleteDirectory(TEMP_DIRECTORY.toFile()); + } + + @Before + public void setupDirectory() throws IOException { + Path tempPathForEachTest = Files.createDirectory(TEMP_DIRECTORY.resolve(DIRECTORY_NAME)); + + // Create a directory structure + Files.write(tempPathForEachTest.resolve("file1.txt"), ALL_LINES.subList(0, 2)); + Files.write(tempPathForEachTest.resolve("file2.txt"), ALL_LINES.subList(2, 4)); + + Files.createDirectories(tempPathForEachTest.resolve("Empty")); + + Path aSubDir = Files.createDirectories(tempPathForEachTest.resolve("notEmpty")); + Files.write(aSubDir.resolve("file3.txt"), ALL_LINES.subList(3, 5)); + Files.write(aSubDir.resolve("file4.txt"), ALL_LINES.subList(0, 3)); + + aSubDir = Files.createDirectories(aSubDir.resolve("anotherSubDirectory")); + Files.write(aSubDir.resolve("file5.txt"), ALL_LINES.subList(4, 5)); + Files.write(aSubDir.resolve("file6.txt"), ALL_LINES.subList(0, 2)); + } + + @After + public void checkAndCleanupIfRequired() throws IOException { + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); + if (Files.exists(pathToBeDeleted)) { + FileUtils.deleteDirectory(pathToBeDeleted.toFile()); + } + } + + private boolean deleteDirectory(File directoryToBeDeleted) { + File[] allContents = directoryToBeDeleted.listFiles(); + + if (allContents != null) { + for (File file : allContents) { + deleteDirectory(file); + } + } + + return directoryToBeDeleted.delete(); + } + + @Test + public void givenDirectory_whenDeletedWithRecursion_thenIsGone() throws IOException { + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); + + boolean result = deleteDirectory(pathToBeDeleted.toFile()); + + assertTrue("Could not delete directory", result); + assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); + } + + @Test + public void givenDirectory_whenDeletedWithCommonsIOFileUtils_thenIsGone() throws IOException { + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); + + FileUtils.deleteDirectory(pathToBeDeleted.toFile()); + + assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); + } + + @Test + public void givenDirectory_whenDeletedWithSpringFileSystemUtils_thenIsGone() throws IOException { + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); + + boolean result = FileSystemUtils.deleteRecursively(pathToBeDeleted.toFile()); + + assertTrue("Could not delete directory", result); + assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); + } + + @Test + public void givenDirectory_whenDeletedWithFilesWalk_thenIsGone() throws IOException { + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); + + Files.walk(pathToBeDeleted).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); + + assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); + } + + @Test + public void givenDirectory_whenDeletedWithNIO2WalkFileTree_thenIsGone() throws IOException { + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); + + Files.walkFileTree(pathToBeDeleted, new SimpleFileVisitor() { + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + }); + + assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); + } +} diff --git a/libraries/src/test/java/com/baeldung/jdeffered/JDeferredUnitTest.java b/libraries-4/src/test/java/com/baeldung/jdeffered/JDeferredUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/jdeffered/JDeferredUnitTest.java rename to libraries-4/src/test/java/com/baeldung/jdeffered/JDeferredUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/mbassador/MBassadorAsyncDispatchUnitTest.java b/libraries-4/src/test/java/com/baeldung/mbassador/MBassadorAsyncDispatchUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/mbassador/MBassadorAsyncDispatchUnitTest.java rename to libraries-4/src/test/java/com/baeldung/mbassador/MBassadorAsyncDispatchUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/mbassador/MBassadorAsyncInvocationUnitTest.java b/libraries-4/src/test/java/com/baeldung/mbassador/MBassadorAsyncInvocationUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/mbassador/MBassadorAsyncInvocationUnitTest.java rename to libraries-4/src/test/java/com/baeldung/mbassador/MBassadorAsyncInvocationUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/mbassador/MBassadorBasicUnitTest.java b/libraries-4/src/test/java/com/baeldung/mbassador/MBassadorBasicUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/mbassador/MBassadorBasicUnitTest.java rename to libraries-4/src/test/java/com/baeldung/mbassador/MBassadorBasicUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/mbassador/MBassadorConfigurationUnitTest.java b/libraries-4/src/test/java/com/baeldung/mbassador/MBassadorConfigurationUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/mbassador/MBassadorConfigurationUnitTest.java rename to libraries-4/src/test/java/com/baeldung/mbassador/MBassadorConfigurationUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/mbassador/MBassadorFilterUnitTest.java b/libraries-4/src/test/java/com/baeldung/mbassador/MBassadorFilterUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/mbassador/MBassadorFilterUnitTest.java rename to libraries-4/src/test/java/com/baeldung/mbassador/MBassadorFilterUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/mbassador/MBassadorHierarchyUnitTest.java b/libraries-4/src/test/java/com/baeldung/mbassador/MBassadorHierarchyUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/mbassador/MBassadorHierarchyUnitTest.java rename to libraries-4/src/test/java/com/baeldung/mbassador/MBassadorHierarchyUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/noexception/NoExceptionUnitTest.java b/libraries-4/src/test/java/com/baeldung/noexception/NoExceptionUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/noexception/NoExceptionUnitTest.java rename to libraries-4/src/test/java/com/baeldung/noexception/NoExceptionUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java b/libraries-4/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java rename to libraries-4/src/test/java/com/baeldung/pairs/ApacheCommonsPairUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/pairs/CoreJavaPairUnitTest.java b/libraries-4/src/test/java/com/baeldung/pairs/CoreJavaPairUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/pairs/CoreJavaPairUnitTest.java rename to libraries-4/src/test/java/com/baeldung/pairs/CoreJavaPairUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/pairs/CoreJavaSimpleEntryUnitTest.java b/libraries-4/src/test/java/com/baeldung/pairs/CoreJavaSimpleEntryUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/pairs/CoreJavaSimpleEntryUnitTest.java rename to libraries-4/src/test/java/com/baeldung/pairs/CoreJavaSimpleEntryUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/pairs/VavrPairsUnitTest.java b/libraries-4/src/test/java/com/baeldung/pairs/VavrPairsUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/pairs/VavrPairsUnitTest.java rename to libraries-4/src/test/java/com/baeldung/pairs/VavrPairsUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java b/libraries-4/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java rename to libraries-4/src/test/java/com/baeldung/pcollections/PCollectionsUnitTest.java diff --git a/libraries-5/README.md b/libraries-5/README.md new file mode 100644 index 0000000000..f1e749b293 --- /dev/null +++ b/libraries-5/README.md @@ -0,0 +1,21 @@ +## Libraries-5 + +This module contains articles about various Java libraries. +These are small libraries that are relatively easy to use and do not require any separate module of their own. + +The code examples related to different libraries are each in their own module. + +Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-modules) we already have separate modules. Please make sure to have a look at the existing modules in such cases. + +### Relevant articles +- [Introduction to Caffeine](https://www.baeldung.com/java-caching-caffeine) +- [Introduction to StreamEx](https://www.baeldung.com/streamex) +- [A Docker Guide for Java](https://www.baeldung.com/docker-java-api) +- [Introduction to Akka Actors in Java](https://www.baeldung.com/akka-actors-java) +- [A Guide to Byte Buddy](https://www.baeldung.com/byte-buddy) +- [Introduction to jOOL](https://www.baeldung.com/jool) +- [Consumer Driven Contracts with Pact](https://www.baeldung.com/pact-junit-consumer-driven-contracts) +- [Introduction to Atlassian Fugue](https://www.baeldung.com/java-fugue) +- [Publish and Receive Messages with Nats Java Client](https://www.baeldung.com/nats-java-client) +- [Java Concurrency Utility with JCTools](https://www.baeldung.com/java-concurrency-jc-tools) +- More articles [[<-- prev]](/libraries-4) [[next -->]](/libraries-6) diff --git a/libraries-5/pom.xml b/libraries-5/pom.xml new file mode 100644 index 0000000000..63296d4a89 --- /dev/null +++ b/libraries-5/pom.xml @@ -0,0 +1,146 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + + libraries-5 + 4.0.0 + + + + org.springframework + spring-web + ${spring.version} + + + org.assertj + assertj-core + ${assertj.version} + + + org.jooq + jool + ${jool.version} + + + au.com.dius + pact-jvm-consumer-junit_2.11 + ${pact.version} + test + + + org.codehaus.groovy + groovy-all + + + + + + + com.typesafe.akka + akka-actor_${scala.version} + ${typesafe-akka.version} + + + com.typesafe.akka + akka-testkit_${scala.version} + ${typesafe-akka.version} + test + + + + one.util + streamex + ${streamex.version} + + + net.bytebuddy + byte-buddy + ${bytebuddy.version} + + + net.bytebuddy + byte-buddy-agent + ${bytebuddy.version} + + + + + com.github.docker-java + docker-java + ${docker.version} + + + org.slf4j + slf4j-log4j12 + + + org.slf4j + jcl-over-slf4j + + + ch.qos.logback + logback-classic + + + + + + + com.github.ben-manes.caffeine + caffeine + ${caffeine.version} + + + com.google.code.findbugs + jsr305 + ${findbugs.version} + test + + + + io.atlassian.fugue + fugue + ${fugue.version} + + + io.nats + jnats + ${jnats.version} + + + org.jctools + jctools-core + ${jctools.version} + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + + + 3.5.0 + 0.9.12 + 4.3.8.RELEASE + 3.6.2 + 2.11 + 2.5.11 + 0.6.5 + 1.7.1 + 3.0.14 + 2.5.5 + 3.0.2 + 4.5.1 + 1.0 + 2.1.2 + 1.19 + + + \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/akka/FirstActor.java b/libraries-5/src/main/java/com/baeldung/akka/FirstActor.java similarity index 100% rename from libraries/src/main/java/com/baeldung/akka/FirstActor.java rename to libraries-5/src/main/java/com/baeldung/akka/FirstActor.java diff --git a/libraries/src/main/java/com/baeldung/akka/MyActor.java b/libraries-5/src/main/java/com/baeldung/akka/MyActor.java similarity index 100% rename from libraries/src/main/java/com/baeldung/akka/MyActor.java rename to libraries-5/src/main/java/com/baeldung/akka/MyActor.java diff --git a/libraries/src/main/java/com/baeldung/akka/PrinterActor.java b/libraries-5/src/main/java/com/baeldung/akka/PrinterActor.java similarity index 100% rename from libraries/src/main/java/com/baeldung/akka/PrinterActor.java rename to libraries-5/src/main/java/com/baeldung/akka/PrinterActor.java diff --git a/libraries/src/main/java/com/baeldung/akka/ReadingActor.java b/libraries-5/src/main/java/com/baeldung/akka/ReadingActor.java similarity index 100% rename from libraries/src/main/java/com/baeldung/akka/ReadingActor.java rename to libraries-5/src/main/java/com/baeldung/akka/ReadingActor.java diff --git a/libraries/src/main/java/com/baeldung/akka/WordCounterActor.java b/libraries-5/src/main/java/com/baeldung/akka/WordCounterActor.java similarity index 100% rename from libraries/src/main/java/com/baeldung/akka/WordCounterActor.java rename to libraries-5/src/main/java/com/baeldung/akka/WordCounterActor.java diff --git a/libraries/src/main/java/com/baeldung/bytebuddy/Bar.java b/libraries-5/src/main/java/com/baeldung/bytebuddy/Bar.java similarity index 100% rename from libraries/src/main/java/com/baeldung/bytebuddy/Bar.java rename to libraries-5/src/main/java/com/baeldung/bytebuddy/Bar.java diff --git a/libraries/src/main/java/com/baeldung/bytebuddy/Foo.java b/libraries-5/src/main/java/com/baeldung/bytebuddy/Foo.java similarity index 100% rename from libraries/src/main/java/com/baeldung/bytebuddy/Foo.java rename to libraries-5/src/main/java/com/baeldung/bytebuddy/Foo.java diff --git a/libraries/src/main/java/com/baeldung/caffeine/DataObject.java b/libraries-5/src/main/java/com/baeldung/caffeine/DataObject.java similarity index 100% rename from libraries/src/main/java/com/baeldung/caffeine/DataObject.java rename to libraries-5/src/main/java/com/baeldung/caffeine/DataObject.java diff --git a/libraries/src/main/java/com/baeldung/jctools/MpmcBenchmark.java b/libraries-5/src/main/java/com/baeldung/jctools/MpmcBenchmark.java similarity index 100% rename from libraries/src/main/java/com/baeldung/jctools/MpmcBenchmark.java rename to libraries-5/src/main/java/com/baeldung/jctools/MpmcBenchmark.java diff --git a/libraries/src/main/java/com/baeldung/jctools/README.md b/libraries-5/src/main/java/com/baeldung/jctools/README.md similarity index 100% rename from libraries/src/main/java/com/baeldung/jctools/README.md rename to libraries-5/src/main/java/com/baeldung/jctools/README.md diff --git a/libraries/src/main/java/com/baeldung/jnats/NatsClient.java b/libraries-5/src/main/java/com/baeldung/jnats/NatsClient.java similarity index 100% rename from libraries/src/main/java/com/baeldung/jnats/NatsClient.java rename to libraries-5/src/main/java/com/baeldung/jnats/NatsClient.java diff --git a/libraries/src/main/java/com/baeldung/streamex/Role.java b/libraries-5/src/main/java/com/baeldung/streamex/Role.java similarity index 100% rename from libraries/src/main/java/com/baeldung/streamex/Role.java rename to libraries-5/src/main/java/com/baeldung/streamex/Role.java diff --git a/libraries/src/main/java/com/baeldung/streamex/StreamEX.java b/libraries-5/src/main/java/com/baeldung/streamex/StreamEX.java similarity index 100% rename from libraries/src/main/java/com/baeldung/streamex/StreamEX.java rename to libraries-5/src/main/java/com/baeldung/streamex/StreamEX.java diff --git a/libraries/src/main/java/com/baeldung/streamex/User.java b/libraries-5/src/main/java/com/baeldung/streamex/User.java similarity index 100% rename from libraries/src/main/java/com/baeldung/streamex/User.java rename to libraries-5/src/main/java/com/baeldung/streamex/User.java diff --git a/libraries/src/test/java/com/baeldung/akka/AkkaActorsUnitTest.java b/libraries-5/src/test/java/com/baeldung/akka/AkkaActorsUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/akka/AkkaActorsUnitTest.java rename to libraries-5/src/test/java/com/baeldung/akka/AkkaActorsUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java b/libraries-5/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java rename to libraries-5/src/test/java/com/baeldung/bytebuddy/ByteBuddyUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java b/libraries-5/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java similarity index 88% rename from libraries/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java rename to libraries-5/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java index d523d0ff8b..65c441c50d 100644 --- a/libraries/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java +++ b/libraries-5/src/test/java/com/baeldung/caffeine/CaffeineUnitTest.java @@ -8,6 +8,7 @@ import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; +import org.junit.Assert; import org.junit.Test; import com.github.benmanes.caffeine.cache.*; @@ -65,43 +66,43 @@ public class CaffeineUnitTest { assertEquals("Data for " + key, dataObject.getData()); }); - cache.getAll(Arrays.asList("A", "B", "C")).thenAccept(dataObjectMap -> assertEquals(3, dataObjectMap.size())); + cache.getAll(Arrays.asList("A", "B", "C")).thenAccept(dataObjectMap -> Assert.assertEquals(3, dataObjectMap.size())); } @Test public void givenLoadingCacheWithSmallSize_whenPut_thenSizeIsConstant() { LoadingCache cache = Caffeine.newBuilder().maximumSize(1).refreshAfterWrite(10, TimeUnit.MINUTES).build(k -> DataObject.get("Data for " + k)); - assertEquals(0, cache.estimatedSize()); + Assert.assertEquals(0, cache.estimatedSize()); cache.get("A"); - assertEquals(1, cache.estimatedSize()); + Assert.assertEquals(1, cache.estimatedSize()); cache.get("B"); cache.cleanUp(); - assertEquals(1, cache.estimatedSize()); + Assert.assertEquals(1, cache.estimatedSize()); } @Test public void givenLoadingCacheWithWeigher_whenPut_thenSizeIsConstant() { LoadingCache cache = Caffeine.newBuilder().maximumWeight(10).weigher((k, v) -> 5).build(k -> DataObject.get("Data for " + k)); - assertEquals(0, cache.estimatedSize()); + Assert.assertEquals(0, cache.estimatedSize()); cache.get("A"); - assertEquals(1, cache.estimatedSize()); + Assert.assertEquals(1, cache.estimatedSize()); cache.get("B"); - assertEquals(2, cache.estimatedSize()); + Assert.assertEquals(2, cache.estimatedSize()); cache.get("C"); cache.cleanUp(); - assertEquals(2, cache.estimatedSize()); + Assert.assertEquals(2, cache.estimatedSize()); } @Test @@ -138,7 +139,7 @@ public class CaffeineUnitTest { cache.get("A"); cache.get("A"); - assertEquals(1, cache.stats().hitCount()); - assertEquals(1, cache.stats().missCount()); + Assert.assertEquals(1, cache.stats().hitCount()); + Assert.assertEquals(1, cache.stats().missCount()); } } \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/dockerapi/ContainerLiveTest.java b/libraries-5/src/test/java/com/baeldung/dockerapi/ContainerLiveTest.java similarity index 94% rename from libraries/src/test/java/com/baeldung/dockerapi/ContainerLiveTest.java rename to libraries-5/src/test/java/com/baeldung/dockerapi/ContainerLiveTest.java index e6f0fd1c31..007c70355a 100644 --- a/libraries/src/test/java/com/baeldung/dockerapi/ContainerLiveTest.java +++ b/libraries-5/src/test/java/com/baeldung/dockerapi/ContainerLiveTest.java @@ -6,6 +6,8 @@ import com.github.dockerjava.api.command.InspectContainerResponse; import com.github.dockerjava.api.model.Container; import com.github.dockerjava.api.model.PortBinding; import com.github.dockerjava.core.DockerClientBuilder; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.Is; import org.junit.BeforeClass; import org.junit.Test; @@ -51,7 +53,7 @@ public class ContainerLiveTest { CreateContainerResponse container = dockerClient.createContainerCmd("mongo:3.6").withCmd("--bind_ip_all").withName("mongo").withHostName("baeldung").withEnv("MONGO_LATEST_VERSION=3.6").withPortBindings(PortBinding.parse("9999:27017")).exec(); // then - assertThat(container.getId(), is(not(null))); + MatcherAssert.assertThat(container.getId(), is(not(null))); } @Test @@ -104,7 +106,7 @@ public class ContainerLiveTest { // then InspectContainerResponse containerResponse = dockerClient.inspectContainerCmd(container.getId()).exec(); - assertThat(containerResponse.getId(), is(container.getId())); + MatcherAssert.assertThat(containerResponse.getId(), Is.is(container.getId())); } @Test diff --git a/libraries/src/test/java/com/baeldung/dockerapi/DockerClientLiveTest.java b/libraries-5/src/test/java/com/baeldung/dockerapi/DockerClientLiveTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/dockerapi/DockerClientLiveTest.java rename to libraries-5/src/test/java/com/baeldung/dockerapi/DockerClientLiveTest.java diff --git a/libraries/src/test/java/com/baeldung/dockerapi/ImageLiveTest.java b/libraries-5/src/test/java/com/baeldung/dockerapi/ImageLiveTest.java similarity index 96% rename from libraries/src/test/java/com/baeldung/dockerapi/ImageLiveTest.java rename to libraries-5/src/test/java/com/baeldung/dockerapi/ImageLiveTest.java index 7e8cd6a354..96e7922f2a 100644 --- a/libraries/src/test/java/com/baeldung/dockerapi/ImageLiveTest.java +++ b/libraries-5/src/test/java/com/baeldung/dockerapi/ImageLiveTest.java @@ -8,6 +8,8 @@ import com.github.dockerjava.core.DockerClientBuilder; import com.github.dockerjava.core.command.BuildImageResultCallback; import com.github.dockerjava.core.command.PullImageResultCallback; import com.github.dockerjava.core.command.PushImageResultCallback; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.Is; import org.junit.BeforeClass; import org.junit.Test; @@ -81,7 +83,7 @@ public class ImageLiveTest { InspectImageResponse imageResponse = dockerClient.inspectImageCmd(image.getId()).exec(); // then - assertThat(imageResponse.getId(), is(image.getId())); + MatcherAssert.assertThat(imageResponse.getId(), Is.is(image.getId())); } @Test diff --git a/libraries/src/test/java/com/baeldung/dockerapi/NetworkLiveTest.java b/libraries-5/src/test/java/com/baeldung/dockerapi/NetworkLiveTest.java similarity index 95% rename from libraries/src/test/java/com/baeldung/dockerapi/NetworkLiveTest.java rename to libraries-5/src/test/java/com/baeldung/dockerapi/NetworkLiveTest.java index d3abbe2e7e..31ad32c7b5 100644 --- a/libraries/src/test/java/com/baeldung/dockerapi/NetworkLiveTest.java +++ b/libraries-5/src/test/java/com/baeldung/dockerapi/NetworkLiveTest.java @@ -5,6 +5,7 @@ import com.github.dockerjava.api.command.CreateNetworkResponse; import com.github.dockerjava.api.model.Network; import com.github.dockerjava.api.model.Network.Ipam; import com.github.dockerjava.core.DockerClientBuilder; +import org.hamcrest.MatcherAssert; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; @@ -64,7 +65,7 @@ public class NetworkLiveTest { Network network = dockerClient.inspectNetworkCmd().withNetworkId(networkName).exec(); // then - assertThat(network.getName(), is(networkName)); + MatcherAssert.assertThat(network.getName(), is(networkName)); } @Test diff --git a/libraries/src/test/java/com/baeldung/dockerapi/VolumeLiveTest.java b/libraries-5/src/test/java/com/baeldung/dockerapi/VolumeLiveTest.java similarity index 92% rename from libraries/src/test/java/com/baeldung/dockerapi/VolumeLiveTest.java rename to libraries-5/src/test/java/com/baeldung/dockerapi/VolumeLiveTest.java index 9e60a76b33..f2a078b2c6 100644 --- a/libraries/src/test/java/com/baeldung/dockerapi/VolumeLiveTest.java +++ b/libraries-5/src/test/java/com/baeldung/dockerapi/VolumeLiveTest.java @@ -5,6 +5,7 @@ import com.github.dockerjava.api.command.CreateVolumeResponse; import com.github.dockerjava.api.command.InspectVolumeResponse; import com.github.dockerjava.api.command.ListVolumesResponse; import com.github.dockerjava.core.DockerClientBuilder; +import org.hamcrest.MatcherAssert; import org.junit.BeforeClass; import org.junit.Test; @@ -57,7 +58,7 @@ public class VolumeLiveTest { CreateVolumeResponse unnamedVolume = dockerClient.createVolumeCmd().exec(); // then - assertThat(unnamedVolume.getName(), is(not(null))); + MatcherAssert.assertThat(unnamedVolume.getName(), is(not(null))); } @Test @@ -67,7 +68,7 @@ public class VolumeLiveTest { CreateVolumeResponse namedVolume = dockerClient.createVolumeCmd().withName("myNamedVolume").exec(); // then - assertThat(namedVolume.getName(), is(not(null))); + MatcherAssert.assertThat(namedVolume.getName(), is(not(null))); } @Test diff --git a/libraries/src/test/java/com/baeldung/atlassian/fugue/FugueUnitTest.java b/libraries-5/src/test/java/com/baeldung/fugue/FugueUnitTest.java similarity index 99% rename from libraries/src/test/java/com/baeldung/atlassian/fugue/FugueUnitTest.java rename to libraries-5/src/test/java/com/baeldung/fugue/FugueUnitTest.java index 773e39b76a..c3a89a1355 100644 --- a/libraries/src/test/java/com/baeldung/atlassian/fugue/FugueUnitTest.java +++ b/libraries-5/src/test/java/com/baeldung/fugue/FugueUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.atlassian.fugue; +package com.baeldung.fugue; import io.atlassian.fugue.*; import org.junit.Assert; diff --git a/libraries/src/test/java/com/baeldung/jctools/JCToolsUnitTest.java b/libraries-5/src/test/java/com/baeldung/jctools/JCToolsUnitTest.java similarity index 88% rename from libraries/src/test/java/com/baeldung/jctools/JCToolsUnitTest.java rename to libraries-5/src/test/java/com/baeldung/jctools/JCToolsUnitTest.java index 4a9d0fadb2..a5dacdbdac 100644 --- a/libraries/src/test/java/com/baeldung/jctools/JCToolsUnitTest.java +++ b/libraries-5/src/test/java/com/baeldung/jctools/JCToolsUnitTest.java @@ -1,5 +1,6 @@ package com.baeldung.jctools; +import org.assertj.core.api.Assertions; import org.jctools.queues.SpscArrayQueue; import org.jctools.queues.SpscChunkedArrayQueue; import org.junit.Test; @@ -44,16 +45,16 @@ public class JCToolsUnitTest { @Test public void whenQueueIsFull_thenNoMoreElementsCanBeAdded() throws InterruptedException { SpscChunkedArrayQueue queue = new SpscChunkedArrayQueue<>(8, 16); - assertThat(queue.capacity()).isEqualTo(16); + Assertions.assertThat(queue.capacity()).isEqualTo(16); CountDownLatch startConsuming = new CountDownLatch(1); CountDownLatch awakeProducer = new CountDownLatch(1); AtomicReference error = new AtomicReference<>(); Thread producer = new Thread(() -> { IntStream.range(0, queue.capacity()).forEach(i -> { - assertThat(queue.offer(i)).isTrue(); + Assertions.assertThat(queue.offer(i)).isTrue(); }); - assertThat(queue.offer(queue.capacity())).isFalse(); + Assertions.assertThat(queue.offer(queue.capacity())).isFalse(); startConsuming.countDown(); try { awakeProducer.await(); @@ -61,7 +62,7 @@ public class JCToolsUnitTest { throw new RuntimeException(e); } - assertThat(queue.offer(queue.capacity())).isTrue(); + Assertions.assertThat(queue.offer(queue.capacity())).isTrue(); }); producer.setUncaughtExceptionHandler((t, e) -> { error.set(e); diff --git a/libraries/src/test/java/com/baeldung/jnats/NatsClientLiveTest.java b/libraries-5/src/test/java/com/baeldung/jnats/NatsClientLiveTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/jnats/NatsClientLiveTest.java rename to libraries-5/src/test/java/com/baeldung/jnats/NatsClientLiveTest.java diff --git a/libraries/src/test/java/com/baeldung/jool/JOOLUnitTest.java b/libraries-5/src/test/java/com/baeldung/jool/JOOLUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/jool/JOOLUnitTest.java rename to libraries-5/src/test/java/com/baeldung/jool/JOOLUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java b/libraries-5/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java rename to libraries-5/src/test/java/com/baeldung/pact/PactConsumerDrivenContractUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/stream/StreamExMergeStreamsUnitTest.java b/libraries-5/src/test/java/com/baeldung/streamex/StreamExMergeStreamsUnitTest.java similarity index 98% rename from libraries/src/test/java/com/baeldung/stream/StreamExMergeStreamsUnitTest.java rename to libraries-5/src/test/java/com/baeldung/streamex/StreamExMergeStreamsUnitTest.java index 220348bf36..b267eaea9b 100644 --- a/libraries/src/test/java/com/baeldung/stream/StreamExMergeStreamsUnitTest.java +++ b/libraries-5/src/test/java/com/baeldung/streamex/StreamExMergeStreamsUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.stream; +package com.baeldung.streamex; import one.util.streamex.StreamEx; import org.junit.Test; diff --git a/libraries/src/test/resources/dockerapi/Dockerfile b/libraries-5/src/test/resources/dockerapi/Dockerfile similarity index 100% rename from libraries/src/test/resources/dockerapi/Dockerfile rename to libraries-5/src/test/resources/dockerapi/Dockerfile diff --git a/libraries-6/README.md b/libraries-6/README.md new file mode 100644 index 0000000000..79bb83113e --- /dev/null +++ b/libraries-6/README.md @@ -0,0 +1,17 @@ +## Libraries-6 + +This module contains articles about various Java libraries. +These are small libraries that are relatively easy to use and do not require any separate module of their own. + +The code examples related to different libraries are each in their own module. + +Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-modules) we already have separate modules. Please make sure to have a look at the existing modules in such cases. + +### Relevant articles +- [Introduction to JavaPoet](https://www.baeldung.com/java-poet) +- [Guide to Resilience4j](https://www.baeldung.com/resilience4j) +- [Implementing a FTP-Client in Java](https://www.baeldung.com/java-ftp-client) +- [Introduction to Functional Java](https://www.baeldung.com/java-functional-library) +- [A Guide to the Reflections Library](https://www.baeldung.com/reflections-library) +- [Exactly Once Processing in Kafka](https://www.baeldung.com/kafka-exactly-once) +- More articles [[<-- prev]](/libraries-5) diff --git a/libraries-6/pom.xml b/libraries-6/pom.xml new file mode 100644 index 0000000000..030e5aa77b --- /dev/null +++ b/libraries-6/pom.xml @@ -0,0 +1,111 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + libraries-6 + + + + org.functionaljava + functionaljava-java8 + ${functionaljava.version} + + + com.codepoetics + protonpack + ${protonpack.version} + + + org.apache.kafka + kafka-streams + ${kafka.version} + + + org.apache.kafka + kafka-clients + ${kafka.version} + test + test + + + io.github.resilience4j + resilience4j-circuitbreaker + ${resilience4j.version} + + + io.github.resilience4j + resilience4j-bulkhead + ${resilience4j.version} + + + io.github.resilience4j + resilience4j-retry + ${resilience4j.version} + + + io.github.resilience4j + resilience4j-timelimiter + ${resilience4j.version} + + + com.squareup + javapoet + ${javapoet.version} + + + org.mockftpserver + MockFtpServer + ${mockftpserver.version} + test + + + + org.reflections + reflections + ${reflections.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + commons-net + commons-net + ${commons-net.version} + + + org.assertj + assertj-core + ${assertj.version} + + + commons-io + commons-io + ${commonsio.version} + test + + + + + 2.0.0 + 1.10.0 + 0.9.11 + 2.7.1 + 4.8.1 + 0.12.1 + 1.15 + 3.6 + 3.6.2 + 2.6 + + + + \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/fj/FunctionalJavaIOMain.java b/libraries-6/src/main/java/com/baeldung/fj/FunctionalJavaIOMain.java similarity index 96% rename from libraries/src/main/java/com/baeldung/fj/FunctionalJavaIOMain.java rename to libraries-6/src/main/java/com/baeldung/fj/FunctionalJavaIOMain.java index eaa201d1ba..e97f128b30 100644 --- a/libraries/src/main/java/com/baeldung/fj/FunctionalJavaIOMain.java +++ b/libraries-6/src/main/java/com/baeldung/fj/FunctionalJavaIOMain.java @@ -1,43 +1,43 @@ -package com.baeldung.fj; - -import fj.F; -import fj.F1Functions; -import fj.Unit; -import fj.data.IO; -import fj.data.IOFunctions; - -public class FunctionalJavaIOMain { - - public static IO printLetters(final String s) { - return () -> { - for (int i = 0; i < s.length(); i++) { - System.out.println(s.charAt(i)); - } - return Unit.unit(); - }; - } - - public static void main(String[] args) { - - F> printLetters = i -> printLetters(i); - - IO lowerCase = IOFunctions.stdoutPrintln("What's your first Name ?"); - - IO input = IOFunctions.stdoutPrint("First Name: "); - - IO userInput = IOFunctions.append(lowerCase, input); - - IO readInput = IOFunctions.stdinReadLine(); - - F toUpperCase = i -> i.toUpperCase(); - - F> transformInput = F1Functions., String> o(printLetters).f(toUpperCase); - - IO readAndPrintResult = IOFunctions.bind(readInput, transformInput); - - IO program = IOFunctions.bind(userInput, nothing -> readAndPrintResult); - - IOFunctions.toSafe(program).run(); - - } -} +package com.baeldung.fj; + +import fj.F; +import fj.F1Functions; +import fj.Unit; +import fj.data.IO; +import fj.data.IOFunctions; + +public class FunctionalJavaIOMain { + + public static IO printLetters(final String s) { + return () -> { + for (int i = 0; i < s.length(); i++) { + System.out.println(s.charAt(i)); + } + return Unit.unit(); + }; + } + + public static void main(String[] args) { + + F> printLetters = i -> printLetters(i); + + IO lowerCase = IOFunctions.stdoutPrintln("What's your first Name ?"); + + IO input = IOFunctions.stdoutPrint("First Name: "); + + IO userInput = IOFunctions.append(lowerCase, input); + + IO readInput = IOFunctions.stdinReadLine(); + + F toUpperCase = i -> i.toUpperCase(); + + F> transformInput = F1Functions., String> o(printLetters).f(toUpperCase); + + IO readAndPrintResult = IOFunctions.bind(readInput, transformInput); + + IO program = IOFunctions.bind(userInput, nothing -> readAndPrintResult); + + IOFunctions.toSafe(program).run(); + + } +} diff --git a/libraries/src/main/java/com/baeldung/fj/FunctionalJavaMain.java b/libraries-6/src/main/java/com/baeldung/fj/FunctionalJavaMain.java similarity index 96% rename from libraries/src/main/java/com/baeldung/fj/FunctionalJavaMain.java rename to libraries-6/src/main/java/com/baeldung/fj/FunctionalJavaMain.java index c6412f2923..1a59e6c22a 100644 --- a/libraries/src/main/java/com/baeldung/fj/FunctionalJavaMain.java +++ b/libraries-6/src/main/java/com/baeldung/fj/FunctionalJavaMain.java @@ -1,48 +1,48 @@ -package com.baeldung.fj; - -import fj.F; -import fj.Show; -import fj.data.Array; -import fj.data.List; -import fj.data.Option; -import fj.function.Characters; -import fj.function.Integers; - -public class FunctionalJavaMain { - - public static final F isEven = i -> i % 2 == 0; - - public static void main(String[] args) { - - List fList = List.list(3, 4, 5, 6); - List evenList = fList.map(isEven); - Show.listShow(Show.booleanShow).println(evenList); - - fList = fList.map(i -> i + 1); - Show.listShow(Show.intShow).println(fList); - - Array a = Array.array(17, 44, 67, 2, 22, 80, 1, 27); - Array b = a.filter(Integers.even); - Show.arrayShow(Show.intShow).println(b); - - Array array = Array.array("Welcome", "To", "baeldung"); - Boolean isExist = array.exists(s -> List.fromString(s).forall(Characters.isLowerCase)); - System.out.println(isExist); - - Array intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27); - int sum = intArray.foldLeft(Integers.add, 0); - System.out.println(sum); - - Option n1 = Option.some(1); - Option n2 = Option.some(2); - - F> f1 = i -> i % 2 == 0 ? Option.some(i + 100) : Option.none(); - - Option result1 = n1.bind(f1); - Option result2 = n2.bind(f1); - - Show.optionShow(Show.intShow).println(result1); - Show.optionShow(Show.intShow).println(result2); - } - -} +package com.baeldung.fj; + +import fj.F; +import fj.Show; +import fj.data.Array; +import fj.data.List; +import fj.data.Option; +import fj.function.Characters; +import fj.function.Integers; + +public class FunctionalJavaMain { + + public static final F isEven = i -> i % 2 == 0; + + public static void main(String[] args) { + + List fList = List.list(3, 4, 5, 6); + List evenList = fList.map(isEven); + Show.listShow(Show.booleanShow).println(evenList); + + fList = fList.map(i -> i + 1); + Show.listShow(Show.intShow).println(fList); + + Array a = Array.array(17, 44, 67, 2, 22, 80, 1, 27); + Array b = a.filter(Integers.even); + Show.arrayShow(Show.intShow).println(b); + + Array array = Array.array("Welcome", "To", "baeldung"); + Boolean isExist = array.exists(s -> List.fromString(s).forall(Characters.isLowerCase)); + System.out.println(isExist); + + Array intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27); + int sum = intArray.foldLeft(Integers.add, 0); + System.out.println(sum); + + Option n1 = Option.some(1); + Option n2 = Option.some(2); + + F> f1 = i -> i % 2 == 0 ? Option.some(i + 100) : Option.none(); + + Option result1 = n1.bind(f1); + Option result2 = n2.bind(f1); + + Show.optionShow(Show.intShow).println(result1); + Show.optionShow(Show.intShow).println(result2); + } + +} diff --git a/libraries/src/main/java/com/baeldung/ftp/FtpClient.java b/libraries-6/src/main/java/com/baeldung/ftp/FtpClient.java similarity index 100% rename from libraries/src/main/java/com/baeldung/ftp/FtpClient.java rename to libraries-6/src/main/java/com/baeldung/ftp/FtpClient.java diff --git a/libraries/src/main/java/com/baeldung/javapoet/PersonGenerator.java b/libraries-6/src/main/java/com/baeldung/javapoet/PersonGenerator.java similarity index 100% rename from libraries/src/main/java/com/baeldung/javapoet/PersonGenerator.java rename to libraries-6/src/main/java/com/baeldung/javapoet/PersonGenerator.java diff --git a/libraries/src/main/java/com/baeldung/kafka/TransactionalMessageProducer.java b/libraries-6/src/main/java/com/baeldung/kafka/TransactionalMessageProducer.java similarity index 100% rename from libraries/src/main/java/com/baeldung/kafka/TransactionalMessageProducer.java rename to libraries-6/src/main/java/com/baeldung/kafka/TransactionalMessageProducer.java diff --git a/libraries/src/main/java/com/baeldung/kafka/TransactionalWordCount.java b/libraries-6/src/main/java/com/baeldung/kafka/TransactionalWordCount.java similarity index 100% rename from libraries/src/main/java/com/baeldung/kafka/TransactionalWordCount.java rename to libraries-6/src/main/java/com/baeldung/kafka/TransactionalWordCount.java diff --git a/libraries/src/main/java/com/baeldung/kafka/Tuple.java b/libraries-6/src/main/java/com/baeldung/kafka/Tuple.java similarity index 100% rename from libraries/src/main/java/com/baeldung/kafka/Tuple.java rename to libraries-6/src/main/java/com/baeldung/kafka/Tuple.java diff --git a/libraries/src/main/java/com/baeldung/reflections/ReflectionsApp.java b/libraries-6/src/main/java/com/baeldung/reflections/ReflectionsApp.java similarity index 97% rename from libraries/src/main/java/com/baeldung/reflections/ReflectionsApp.java rename to libraries-6/src/main/java/com/baeldung/reflections/ReflectionsApp.java index 30da8ea837..4f5b6dd183 100644 --- a/libraries/src/main/java/com/baeldung/reflections/ReflectionsApp.java +++ b/libraries-6/src/main/java/com/baeldung/reflections/ReflectionsApp.java @@ -1,71 +1,71 @@ -package com.baeldung.reflections; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.util.Date; -import java.util.Set; -import java.util.regex.Pattern; - -import org.reflections.Reflections; -import org.reflections.scanners.MethodAnnotationsScanner; -import org.reflections.scanners.MethodParameterScanner; -import org.reflections.scanners.ResourcesScanner; -import org.reflections.scanners.Scanner; -import org.reflections.scanners.SubTypesScanner; -import org.reflections.util.ClasspathHelper; -import org.reflections.util.ConfigurationBuilder; - -public class ReflectionsApp { - - public Set> getReflectionsSubTypes() { - Reflections reflections = new Reflections("org.reflections"); - Set> scannersSet = reflections.getSubTypesOf(Scanner.class); - return scannersSet; - } - - public Set> getJDKFunctinalInterfaces() { - Reflections reflections = new Reflections("java.util.function"); - Set> typesSet = reflections.getTypesAnnotatedWith(FunctionalInterface.class); - return typesSet; - } - - public Set getDateDeprecatedMethods() { - Reflections reflections = new Reflections(java.util.Date.class, new MethodAnnotationsScanner()); - Set deprecatedMethodsSet = reflections.getMethodsAnnotatedWith(Deprecated.class); - return deprecatedMethodsSet; - } - - @SuppressWarnings("rawtypes") - public Set getDateDeprecatedConstructors() { - Reflections reflections = new Reflections(java.util.Date.class, new MethodAnnotationsScanner()); - Set constructorsSet = reflections.getConstructorsAnnotatedWith(Deprecated.class); - return constructorsSet; - } - - public Set getMethodsWithDateParam() { - Reflections reflections = new Reflections(java.text.SimpleDateFormat.class, new MethodParameterScanner()); - Set methodsSet = reflections.getMethodsMatchParams(Date.class); - return methodsSet; - } - - public Set getMethodsWithVoidReturn() { - Reflections reflections = new Reflections(java.text.SimpleDateFormat.class, new MethodParameterScanner()); - Set methodsSet = reflections.getMethodsReturn(void.class); - return methodsSet; - } - - public Set getPomXmlPaths() { - Reflections reflections = new Reflections(new ResourcesScanner()); - Set resourcesSet = reflections.getResources(Pattern.compile(".*pom\\.xml")); - return resourcesSet; - } - - public Set> getReflectionsSubTypesUsingBuilder() { - Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("org.reflections")) - .setScanners(new SubTypesScanner())); - - Set> scannersSet = reflections.getSubTypesOf(Scanner.class); - return scannersSet; - } - -} +package com.baeldung.reflections; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.Date; +import java.util.Set; +import java.util.regex.Pattern; + +import org.reflections.Reflections; +import org.reflections.scanners.MethodAnnotationsScanner; +import org.reflections.scanners.MethodParameterScanner; +import org.reflections.scanners.ResourcesScanner; +import org.reflections.scanners.Scanner; +import org.reflections.scanners.SubTypesScanner; +import org.reflections.util.ClasspathHelper; +import org.reflections.util.ConfigurationBuilder; + +public class ReflectionsApp { + + public Set> getReflectionsSubTypes() { + Reflections reflections = new Reflections("org.reflections"); + Set> scannersSet = reflections.getSubTypesOf(Scanner.class); + return scannersSet; + } + + public Set> getJDKFunctinalInterfaces() { + Reflections reflections = new Reflections("java.util.function"); + Set> typesSet = reflections.getTypesAnnotatedWith(FunctionalInterface.class); + return typesSet; + } + + public Set getDateDeprecatedMethods() { + Reflections reflections = new Reflections(java.util.Date.class, new MethodAnnotationsScanner()); + Set deprecatedMethodsSet = reflections.getMethodsAnnotatedWith(Deprecated.class); + return deprecatedMethodsSet; + } + + @SuppressWarnings("rawtypes") + public Set getDateDeprecatedConstructors() { + Reflections reflections = new Reflections(java.util.Date.class, new MethodAnnotationsScanner()); + Set constructorsSet = reflections.getConstructorsAnnotatedWith(Deprecated.class); + return constructorsSet; + } + + public Set getMethodsWithDateParam() { + Reflections reflections = new Reflections(java.text.SimpleDateFormat.class, new MethodParameterScanner()); + Set methodsSet = reflections.getMethodsMatchParams(Date.class); + return methodsSet; + } + + public Set getMethodsWithVoidReturn() { + Reflections reflections = new Reflections(java.text.SimpleDateFormat.class, new MethodParameterScanner()); + Set methodsSet = reflections.getMethodsReturn(void.class); + return methodsSet; + } + + public Set getPomXmlPaths() { + Reflections reflections = new Reflections(new ResourcesScanner()); + Set resourcesSet = reflections.getResources(Pattern.compile(".*pom\\.xml")); + return resourcesSet; + } + + public Set> getReflectionsSubTypesUsingBuilder() { + Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("org.reflections")) + .setScanners(new SubTypesScanner())); + + Set> scannersSet = reflections.getSubTypesOf(Scanner.class); + return scannersSet; + } + +} diff --git a/libraries/src/test/java/com/baeldung/fj/FunctionalJavaUnitTest.java b/libraries-6/src/test/java/com/baeldung/fj/FunctionalJavaUnitTest.java similarity index 95% rename from libraries/src/test/java/com/baeldung/fj/FunctionalJavaUnitTest.java rename to libraries-6/src/test/java/com/baeldung/fj/FunctionalJavaUnitTest.java index 97ead07470..f79d334b23 100644 --- a/libraries/src/test/java/com/baeldung/fj/FunctionalJavaUnitTest.java +++ b/libraries-6/src/test/java/com/baeldung/fj/FunctionalJavaUnitTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import org.junit.Assert; import org.junit.Test; import fj.F; @@ -96,9 +97,9 @@ public class FunctionalJavaUnitTest { Option result2 = n2.bind(function); Option result3 = n3.bind(function); - assertEquals(Option.none(), result1); - assertEquals(Option.some(102), result2); - assertEquals(Option.none(), result3); + Assert.assertEquals(Option.none(), result1); + Assert.assertEquals(Option.some(102), result2); + Assert.assertEquals(Option.none(), result3); } @Test diff --git a/libraries/src/test/java/com/baeldung/ftp/FtpClientIntegrationTest.java b/libraries-6/src/test/java/com/baeldung/ftp/FtpClientIntegrationTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/ftp/FtpClientIntegrationTest.java rename to libraries-6/src/test/java/com/baeldung/ftp/FtpClientIntegrationTest.java diff --git a/libraries/src/test/java/com/baeldung/ftp/JdkFtpClientIntegrationTest.java b/libraries-6/src/test/java/com/baeldung/ftp/JdkFtpClientIntegrationTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/ftp/JdkFtpClientIntegrationTest.java rename to libraries-6/src/test/java/com/baeldung/ftp/JdkFtpClientIntegrationTest.java diff --git a/libraries/src/test/java/com/baeldung/javapoet/test/PersonGeneratorUnitTest.java b/libraries-6/src/test/java/com/baeldung/javapoet/test/PersonGeneratorUnitTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/javapoet/test/PersonGeneratorUnitTest.java rename to libraries-6/src/test/java/com/baeldung/javapoet/test/PersonGeneratorUnitTest.java diff --git a/libraries/src/test/java/com/baeldung/javapoet/test/person/Gender.java b/libraries-6/src/test/java/com/baeldung/javapoet/test/person/Gender.java similarity index 100% rename from libraries/src/test/java/com/baeldung/javapoet/test/person/Gender.java rename to libraries-6/src/test/java/com/baeldung/javapoet/test/person/Gender.java diff --git a/libraries/src/test/java/com/baeldung/javapoet/test/person/Person.java b/libraries-6/src/test/java/com/baeldung/javapoet/test/person/Person.java similarity index 100% rename from libraries/src/test/java/com/baeldung/javapoet/test/person/Person.java rename to libraries-6/src/test/java/com/baeldung/javapoet/test/person/Person.java diff --git a/libraries/src/test/java/com/baeldung/javapoet/test/person/Student.java b/libraries-6/src/test/java/com/baeldung/javapoet/test/person/Student.java similarity index 100% rename from libraries/src/test/java/com/baeldung/javapoet/test/person/Student.java rename to libraries-6/src/test/java/com/baeldung/javapoet/test/person/Student.java diff --git a/libraries/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java b/libraries-6/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java similarity index 100% rename from libraries/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java rename to libraries-6/src/test/java/com/baeldung/kafkastreams/KafkaStreamsLiveTest.java diff --git a/libraries/src/test/java/com/baeldung/reflections/ReflectionsUnitTest.java b/libraries-6/src/test/java/com/baeldung/reflections/ReflectionsUnitTest.java similarity index 96% rename from libraries/src/test/java/com/baeldung/reflections/ReflectionsUnitTest.java rename to libraries-6/src/test/java/com/baeldung/reflections/ReflectionsUnitTest.java index 9a3ef0747b..b86094b6f4 100644 --- a/libraries/src/test/java/com/baeldung/reflections/ReflectionsUnitTest.java +++ b/libraries-6/src/test/java/com/baeldung/reflections/ReflectionsUnitTest.java @@ -1,50 +1,50 @@ -package com.baeldung.reflections; - -import static org.junit.jupiter.api.Assertions.assertFalse; - -import org.junit.jupiter.api.Test; - -public class ReflectionsUnitTest { - - @Test - public void givenTypeThenGetAllSubTypes() { - ReflectionsApp reflectionsApp = new ReflectionsApp(); - assertFalse(reflectionsApp.getReflectionsSubTypes() - .isEmpty()); - } - - @Test - public void givenTypeAndUsingBuilderThenGetAllSubTypes() { - ReflectionsApp reflectionsApp = new ReflectionsApp(); - assertFalse(reflectionsApp.getReflectionsSubTypesUsingBuilder() - .isEmpty()); - } - - @Test - public void givenAnnotationThenGetAllAnnotatedMethods() { - ReflectionsApp reflectionsApp = new ReflectionsApp(); - assertFalse(reflectionsApp.getDateDeprecatedMethods() - .isEmpty()); - } - - @Test - public void givenAnnotationThenGetAllAnnotatedConstructors() { - ReflectionsApp reflectionsApp = new ReflectionsApp(); - assertFalse(reflectionsApp.getDateDeprecatedConstructors() - .isEmpty()); - } - - @Test - public void givenParamTypeThenGetAllMethods() { - ReflectionsApp reflectionsApp = new ReflectionsApp(); - assertFalse(reflectionsApp.getMethodsWithDateParam() - .isEmpty()); - } - - @Test - public void givenReturnTypeThenGetAllMethods() { - ReflectionsApp reflectionsApp = new ReflectionsApp(); - assertFalse(reflectionsApp.getMethodsWithVoidReturn() - .isEmpty()); - } -} +package com.baeldung.reflections; + +import static org.junit.jupiter.api.Assertions.assertFalse; + +import org.junit.jupiter.api.Test; + +public class ReflectionsUnitTest { + + @Test + public void givenTypeThenGetAllSubTypes() { + ReflectionsApp reflectionsApp = new ReflectionsApp(); + assertFalse(reflectionsApp.getReflectionsSubTypes() + .isEmpty()); + } + + @Test + public void givenTypeAndUsingBuilderThenGetAllSubTypes() { + ReflectionsApp reflectionsApp = new ReflectionsApp(); + assertFalse(reflectionsApp.getReflectionsSubTypesUsingBuilder() + .isEmpty()); + } + + @Test + public void givenAnnotationThenGetAllAnnotatedMethods() { + ReflectionsApp reflectionsApp = new ReflectionsApp(); + assertFalse(reflectionsApp.getDateDeprecatedMethods() + .isEmpty()); + } + + @Test + public void givenAnnotationThenGetAllAnnotatedConstructors() { + ReflectionsApp reflectionsApp = new ReflectionsApp(); + assertFalse(reflectionsApp.getDateDeprecatedConstructors() + .isEmpty()); + } + + @Test + public void givenParamTypeThenGetAllMethods() { + ReflectionsApp reflectionsApp = new ReflectionsApp(); + assertFalse(reflectionsApp.getMethodsWithDateParam() + .isEmpty()); + } + + @Test + public void givenReturnTypeThenGetAllMethods() { + ReflectionsApp reflectionsApp = new ReflectionsApp(); + assertFalse(reflectionsApp.getMethodsWithVoidReturn() + .isEmpty()); + } +} diff --git a/libraries/src/test/java/com/baeldung/resilience4j/Resilience4jUnitTest.java b/libraries-6/src/test/java/com/baeldung/resilence4j/Resilience4jUnitTest.java similarity index 99% rename from libraries/src/test/java/com/baeldung/resilience4j/Resilience4jUnitTest.java rename to libraries-6/src/test/java/com/baeldung/resilence4j/Resilience4jUnitTest.java index ced95c99cb..1d69d20bc2 100644 --- a/libraries/src/test/java/com/baeldung/resilience4j/Resilience4jUnitTest.java +++ b/libraries-6/src/test/java/com/baeldung/resilence4j/Resilience4jUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.resilience4j; +package com.baeldung.resilence4j; import io.github.resilience4j.bulkhead.Bulkhead; import io.github.resilience4j.bulkhead.BulkheadConfig; diff --git a/libraries/src/test/resources/ftp/baz.txt b/libraries-6/src/test/resources/ftp/baz.txt similarity index 100% rename from libraries/src/test/resources/ftp/baz.txt rename to libraries-6/src/test/resources/ftp/baz.txt diff --git a/libraries-concurrency/README.md b/libraries-concurrency/coroutines-with-quasar/README.md similarity index 100% rename from libraries-concurrency/README.md rename to libraries-concurrency/coroutines-with-quasar/README.md diff --git a/libraries-data-2/README.md b/libraries-data-2/README.md index f992186bd9..ce7a1680d1 100644 --- a/libraries-data-2/README.md +++ b/libraries-data-2/README.md @@ -11,7 +11,8 @@ This module contains articles about libraries for data processing in Java. - [Guide to JMapper](https://www.baeldung.com/jmapper) - [An Introduction to SuanShu](https://www.baeldung.com/suanshu) - [Intro to Derive4J](https://www.baeldung.com/derive4j) -More articles: [[<-- prev]](/../libraries-data) +- [Java-R Integration](https://www.baeldung.com/java-r-integration) +- More articles: [[<-- prev]](/../libraries-data) ##### Building the project You can build the project from the command line using: *mvn clean install*, or in an IDE. If you have issues with the derive4j imports in your IDE, you have to add the folder: *target/generated-sources/annotations* to the project build path in your IDE. diff --git a/libraries-data-2/pom.xml b/libraries-data-2/pom.xml index 73c5452f77..cbb24edd3f 100644 --- a/libraries-data-2/pom.xml +++ b/libraries-data-2/pom.xml @@ -116,6 +116,11 @@ slf4j-log4j12 ${slf4j.version} + + com.univocity + univocity-parsers + ${univocity.version} + org.awaitility awaitility @@ -175,6 +180,7 @@ 3.6.2 1.7.25 3.0.0 + 2.8.4 RELEASE 3.0 1.8.1 diff --git a/libraries-data-2/src/main/java/com/baeldung/univocity/OutputService.java b/libraries-data-2/src/main/java/com/baeldung/univocity/OutputService.java new file mode 100644 index 0000000000..b316a602ad --- /dev/null +++ b/libraries-data-2/src/main/java/com/baeldung/univocity/OutputService.java @@ -0,0 +1,80 @@ +package com.baeldung.univocity; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.baeldung.univocity.model.Product; +import com.univocity.parsers.common.processor.BeanWriterProcessor; +import com.univocity.parsers.csv.CsvWriter; +import com.univocity.parsers.csv.CsvWriterSettings; +import com.univocity.parsers.fixed.FixedWidthFields; +import com.univocity.parsers.fixed.FixedWidthWriter; +import com.univocity.parsers.fixed.FixedWidthWriterSettings; +import com.univocity.parsers.tsv.TsvWriter; +import com.univocity.parsers.tsv.TsvWriterSettings; + +public class OutputService { + private Logger logger = LoggerFactory.getLogger(ParsingService.class); + + public enum OutputType { + CSV, TSV, FIXED_WIDTH + }; + + public boolean writeData(List products, OutputType outputType, String outputPath) { + try (Writer outputWriter = new OutputStreamWriter(new FileOutputStream(new File(outputPath)), "UTF-8")) { + switch (outputType) { + case CSV: { + CsvWriter writer = new CsvWriter(outputWriter, new CsvWriterSettings()); + writer.writeRowsAndClose(products); + } + break; + case TSV: { + TsvWriter writer = new TsvWriter(outputWriter, new TsvWriterSettings()); + writer.writeRowsAndClose(products); + } + break; + case FIXED_WIDTH: { + FixedWidthFields fieldLengths = new FixedWidthFields(8, 30, 10); + FixedWidthWriterSettings settings = new FixedWidthWriterSettings(fieldLengths); + FixedWidthWriter writer = new FixedWidthWriter(outputWriter, settings); + writer.writeRowsAndClose(products); + } + break; + default: + logger.warn("Invalid OutputType: " + outputType); + return false; + } + return true; + } catch (IOException e) { + logger.error(e.getMessage()); + return false; + } + } + + public boolean writeBeanToFixedWidthFile(List products, String outputPath) { + try (Writer outputWriter = new OutputStreamWriter(new FileOutputStream(new File(outputPath)), "UTF-8")) { + BeanWriterProcessor rowProcessor = new BeanWriterProcessor(Product.class); + FixedWidthFields fieldLengths = new FixedWidthFields(8, 30, 10); + FixedWidthWriterSettings settings = new FixedWidthWriterSettings(fieldLengths); + settings.setHeaders("product_no", "description", "unit_price"); + settings.setRowWriterProcessor(rowProcessor); + FixedWidthWriter writer = new FixedWidthWriter(outputWriter, settings); + writer.writeHeaders(); + for (Product product : products) { + writer.processRecord(product); + } + writer.close(); + return true; + } catch (IOException e) { + logger.error(e.getMessage()); + return false; + } + } +} diff --git a/libraries-data-2/src/main/java/com/baeldung/univocity/ParsingService.java b/libraries-data-2/src/main/java/com/baeldung/univocity/ParsingService.java new file mode 100644 index 0000000000..a88023b5d3 --- /dev/null +++ b/libraries-data-2/src/main/java/com/baeldung/univocity/ParsingService.java @@ -0,0 +1,85 @@ +package com.baeldung.univocity; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.baeldung.univocity.model.Product; +import com.univocity.parsers.common.processor.BatchedColumnProcessor; +import com.univocity.parsers.common.processor.BeanListProcessor; +import com.univocity.parsers.csv.CsvParser; +import com.univocity.parsers.csv.CsvParserSettings; +import com.univocity.parsers.fixed.FixedWidthFields; +import com.univocity.parsers.fixed.FixedWidthParser; +import com.univocity.parsers.fixed.FixedWidthParserSettings; + +public class ParsingService { + private Logger logger = LoggerFactory.getLogger(ParsingService.class); + + public List parseCsvFile(String relativePath) { + try (Reader inputReader = new InputStreamReader(new FileInputStream(new File(relativePath)), "UTF-8")) { + CsvParserSettings settings = new CsvParserSettings(); + settings.setMaxCharsPerColumn(100); + settings.setMaxColumns(50); + CsvParser parser = new CsvParser(settings); + List parsedRows = parser.parseAll(inputReader); + return parsedRows; + } catch (IOException e) { + logger.error("IOException opening file: " + relativePath + " " + e.getMessage()); + return new ArrayList(); + } + } + + public List parseFixedWidthFile(String relativePath) { + try (Reader inputReader = new InputStreamReader(new FileInputStream(new File(relativePath)), "UTF-8")) { + FixedWidthFields fieldLengths = new FixedWidthFields(8, 30, 10); + FixedWidthParserSettings settings = new FixedWidthParserSettings(fieldLengths); + + FixedWidthParser parser = new FixedWidthParser(settings); + List parsedRows = parser.parseAll(inputReader); + return parsedRows; + } catch (IOException e) { + logger.error("IOException opening file: " + relativePath + " " + e.getMessage()); + return new ArrayList(); + } + } + + public List parseCsvFileIntoBeans(String relativePath) { + try (Reader inputReader = new InputStreamReader(new FileInputStream(new File(relativePath)), "UTF-8")) { + BeanListProcessor rowProcessor = new BeanListProcessor(Product.class); + CsvParserSettings settings = new CsvParserSettings(); + settings.setHeaderExtractionEnabled(true); + settings.setProcessor(rowProcessor); + CsvParser parser = new CsvParser(settings); + parser.parse(inputReader); + return rowProcessor.getBeans(); + } catch (IOException e) { + logger.error("IOException opening file: " + relativePath + " " + e.getMessage()); + return new ArrayList(); + } + } + + public List parseCsvFileInBatches(String relativePath) { + try (Reader inputReader = new InputStreamReader(new FileInputStream(new File(relativePath)), "UTF-8")) { + CsvParserSettings settings = new CsvParserSettings(); + settings.setProcessor(new BatchedColumnProcessor(5) { + @Override + public void batchProcessed(int rowsInThisBatch) { + } + }); + CsvParser parser = new CsvParser(settings); + List parsedRows = parser.parseAll(inputReader); + return parsedRows; + } catch (IOException e) { + logger.error("IOException opening file: " + relativePath + " " + e.getMessage()); + return new ArrayList(); + } + } +} diff --git a/libraries-data-2/src/main/java/com/baeldung/univocity/model/Product.java b/libraries-data-2/src/main/java/com/baeldung/univocity/model/Product.java new file mode 100644 index 0000000000..dc4a28f62a --- /dev/null +++ b/libraries-data-2/src/main/java/com/baeldung/univocity/model/Product.java @@ -0,0 +1,44 @@ +package com.baeldung.univocity.model; + +import com.univocity.parsers.annotations.Parsed; + +public class Product { + + @Parsed(field = "product_no") + private String productNumber; + + @Parsed + private String description; + + @Parsed(field = "unit_price") + private float unitPrice; + + public String getProductNumber() { + return productNumber; + } + + public void setProductNumber(String productNumber) { + this.productNumber = productNumber; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public float getUnitPrice() { + return unitPrice; + } + + public void setUnitPrice(float unitPrice) { + this.unitPrice = unitPrice; + } + + @Override + public String toString() { + return "Product [Product Number: " + productNumber + ", Description: " + description + ", Unit Price: " + unitPrice + "]"; + } +} diff --git a/libraries-data-2/src/test/java/com/baeldung/univocity/ParsingServiceUnitTest.java b/libraries-data-2/src/test/java/com/baeldung/univocity/ParsingServiceUnitTest.java new file mode 100644 index 0000000000..8cac0bc4b8 --- /dev/null +++ b/libraries-data-2/src/test/java/com/baeldung/univocity/ParsingServiceUnitTest.java @@ -0,0 +1,112 @@ +package com.baeldung.univocity; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import com.baeldung.univocity.model.Product; + +public class ParsingServiceUnitTest { + + @Test + public void givenCsvFile_thenParsedResultsShouldBeReturned() { + ParsingService parsingService = new ParsingService(); + List productData = parsingService.parseCsvFile("src/test/resources/productList.csv"); + assertEquals(3, productData.size()); + assertEquals(3, productData.get(0).length); + assertEquals("A8993-10", productData.get(0)[0]); + assertEquals("Extra large widget", productData.get(0)[1]); + assertEquals("35.42", productData.get(0)[2]); + assertEquals("D-2938-1", productData.get(1)[0]); + assertEquals("Winding widget \"Deluxe Model\"", productData.get(1)[1]); + assertEquals("245.99", productData.get(1)[2]); + assertEquals("R3212-32", productData.get(2)[0]); + assertEquals("Standard widget", productData.get(2)[1]); + assertEquals("2.34", productData.get(2)[2]); + } + + @Test + public void givenFixedWidthFile_thenParsedResultsShouldBeReturned() { + ParsingService parsingService = new ParsingService(); + List productData = parsingService.parseFixedWidthFile("src/test/resources/productList.txt"); + // Note: any extra spaces on the end will cause a null line to be added + assertEquals(3, productData.size()); + assertEquals(3, productData.get(0).length); + assertEquals("A8993-10", productData.get(0)[0]); + assertEquals("Extra large widget", productData.get(0)[1]); + assertEquals("35.42", productData.get(0)[2]); + assertEquals("D-2938-1", productData.get(1)[0]); + assertEquals("Winding widget \"Deluxe Model\"", productData.get(1)[1]); + assertEquals("245.99", productData.get(1)[2]); + assertEquals("R3212-32", productData.get(2)[0]); + assertEquals("Standard widget", productData.get(2)[1]); + assertEquals("2.34", productData.get(2)[2]); + } + + @Test + public void givenDataAndCsvOutputType_thenCsvFileProduced() { + OutputService outputService = new OutputService(); + List productData = new ArrayList<>(); + productData.add(new Object[] { "1000-3-0", "Widget No. 96", "5.67" }); + productData.add(new Object[] { "G930-M-P", "1/4\" Wocket", ".67" }); + productData.add(new Object[] { "8080-0-M", "No. 54 Jumbo Widget", "35.74" }); + outputService.writeData(productData, OutputService.OutputType.CSV, "src/test/resources/outputProductList.csv"); + + ParsingService parsingService = new ParsingService(); + List writtenData = parsingService.parseCsvFile("src/test/resources/outputProductList.csv"); + assertEquals(3, writtenData.size()); + assertEquals(3, writtenData.get(0).length); + } + + @Test + public void givenDataAndFixedWidthOutputType_thenFixedWidthFileProduced() { + OutputService outputService = new OutputService(); + List productData = new ArrayList<>(); + productData.add(new Object[] { "1000-3-0", "Widget No. 96", "5.67" }); + productData.add(new Object[] { "G930-M-P", "1/4\" Wocket", ".67" }); + productData.add(new Object[] { "8080-0-M", "No. 54 Jumbo Widget", "35.74" }); + outputService.writeData(productData, OutputService.OutputType.FIXED_WIDTH, "src/test/resources/outputProductList.txt"); + + ParsingService parsingService = new ParsingService(); + List writtenData = parsingService.parseFixedWidthFile("src/test/resources/outputProductList.txt"); + assertEquals(3, writtenData.size()); + assertEquals(3, writtenData.get(0).length); + } + + @Test + public void givenCsvFile_thenCsvFileParsedIntoBeans() { + ParsingService parsingService = new ParsingService(); + List products = parsingService.parseCsvFileIntoBeans("src/test/resources/productListWithHeaders.csv"); + assertEquals(2, products.size()); + assertEquals("Product [Product Number: 99-378AG, Description: Wocket Widget #42, Unit Price: 3.56]", products.get(0) + .toString()); + assertEquals("Product [Product Number: TB-333-0, Description: Turbine Widget replacement kit, Unit Price: 800.99]", products.get(1) + .toString()); + } + + @Test + public void givenLisOfProduct_thenWriteFixedWidthFile() { + OutputService outputService = new OutputService(); + Product product = new Product(); + product.setProductNumber("007-PPG0"); + product.setDescription("3/8\" x 1\" Wocket"); + product.setUnitPrice(45.99f); + List products = new ArrayList<>(); + products.add(product); + outputService.writeBeanToFixedWidthFile(products, "src/test/resources/productListWithHeaders.txt"); + ParsingService parsingService = new ParsingService(); + List writtenData = parsingService.parseFixedWidthFile("src/test/resources/productListWithHeaders.txt"); + assertEquals(2, writtenData.size()); + assertEquals(3, writtenData.get(0).length); + } + + @Test + public void givenLargeCsvFile_thenParsedDataShouldBeReturned() { + ParsingService parsingService = new ParsingService(); + List productData = parsingService.parseCsvFileInBatches("src/test/resources/largeProductList.csv"); + assertEquals(36, productData.size()); + } +} diff --git a/libraries-data-2/src/test/resources/largeProductList.csv b/libraries-data-2/src/test/resources/largeProductList.csv new file mode 100644 index 0000000000..2b1813d974 --- /dev/null +++ b/libraries-data-2/src/test/resources/largeProductList.csv @@ -0,0 +1,36 @@ +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 \ No newline at end of file diff --git a/libraries-data-2/src/test/resources/productList.csv b/libraries-data-2/src/test/resources/productList.csv new file mode 100644 index 0000000000..f3a03b6c98 --- /dev/null +++ b/libraries-data-2/src/test/resources/productList.csv @@ -0,0 +1,3 @@ +A8993-10,"Extra large widget",35.42 +D-2938-1,"Winding widget ""Deluxe Model""",245.99 +R3212-32,"Standard widget",2.34 \ No newline at end of file diff --git a/libraries-data-2/src/test/resources/productList.txt b/libraries-data-2/src/test/resources/productList.txt new file mode 100644 index 0000000000..eca4caf55a --- /dev/null +++ b/libraries-data-2/src/test/resources/productList.txt @@ -0,0 +1,3 @@ +A8993-10Extra large widget 35.42 +D-2938-1Winding widget "Deluxe Model" 245.99 +R3212-32Standard widget 2.34 \ No newline at end of file diff --git a/libraries-data-2/src/test/resources/productListWithHeaders.csv b/libraries-data-2/src/test/resources/productListWithHeaders.csv new file mode 100644 index 0000000000..6514a7f0f7 --- /dev/null +++ b/libraries-data-2/src/test/resources/productListWithHeaders.csv @@ -0,0 +1,3 @@ +product_no, description, unit_price +99-378AG,Wocket Widget #42,3.56 +TB-333-0,Turbine Widget replacement kit,800.99 \ No newline at end of file diff --git a/libraries-data-2/src/test/resources/productListWithHeaders.txt b/libraries-data-2/src/test/resources/productListWithHeaders.txt new file mode 100644 index 0000000000..0fced57aed --- /dev/null +++ b/libraries-data-2/src/test/resources/productListWithHeaders.txt @@ -0,0 +1,2 @@ +product_description unit_price +007-PPG03/8" x 1" Wocket 45.99 diff --git a/libraries-http-2/pom.xml b/libraries-http-2/pom.xml index c0a4f6455d..73fe6c66bd 100644 --- a/libraries-http-2/pom.xml +++ b/libraries-http-2/pom.xml @@ -35,6 +35,37 @@ ${mockwebserver.version} test + + + org.eclipse.jetty + jetty-reactive-httpclient + ${jetty.httpclient.version} + + + org.eclipse.jetty + jetty-server + ${jetty.server.version} + + + io.reactivex.rxjava2 + rxjava + ${rxjava2.version} + + + org.springframework + spring-webflux + ${spring.webflux.version} + + + io.projectreactor + reactor-core + ${reactor.version} + + + org.reactivestreams + reactive-streams + ${reactive.stream.version} + @@ -42,6 +73,12 @@ 2.8.5 3.14.2 2.9.8 + 1.0.3 + 9.4.19.v20190610 + 2.2.11 + 5.1.9.RELEASE + 1.0.3 + 3.2.12.RELEASE diff --git a/libraries-http-2/src/main/java/com/baeldung/jetty/httpclient/BlockingSubscriber.java b/libraries-http-2/src/main/java/com/baeldung/jetty/httpclient/BlockingSubscriber.java new file mode 100644 index 0000000000..6986172cd3 --- /dev/null +++ b/libraries-http-2/src/main/java/com/baeldung/jetty/httpclient/BlockingSubscriber.java @@ -0,0 +1,35 @@ +package com.baeldung.jetty.httpclient; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +import org.eclipse.jetty.reactive.client.ReactiveResponse; +import org.reactivestreams.Subscriber; +import org.reactivestreams.Subscription; + +public class BlockingSubscriber implements Subscriber { + BlockingQueue sink = new LinkedBlockingQueue<>(1); + + @Override + public void onSubscribe(Subscription subscription) { + subscription.request(1); + } + + @Override + public void onNext(ReactiveResponse response) { + sink.offer(response); + } + + @Override + public void onError(Throwable failure) { + } + + @Override + public void onComplete() { + } + + public ReactiveResponse block() throws InterruptedException { + return sink.poll(5, TimeUnit.SECONDS); + } +} \ No newline at end of file diff --git a/libraries-http-2/src/main/java/com/baeldung/jetty/httpclient/RequestHandler.java b/libraries-http-2/src/main/java/com/baeldung/jetty/httpclient/RequestHandler.java new file mode 100644 index 0000000000..c3dbff9b11 --- /dev/null +++ b/libraries-http-2/src/main/java/com/baeldung/jetty/httpclient/RequestHandler.java @@ -0,0 +1,21 @@ +package com.baeldung.jetty.httpclient; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.util.IO; + +public class RequestHandler extends AbstractHandler { + + @Override + public void handle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + jettyRequest.setHandled(true); + response.setContentType(request.getContentType()); + IO.copy(request.getInputStream(), response.getOutputStream()); + } +} \ No newline at end of file diff --git a/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/AbstractUnitTest.java b/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/AbstractUnitTest.java new file mode 100644 index 0000000000..4a3e67a7c5 --- /dev/null +++ b/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/AbstractUnitTest.java @@ -0,0 +1,54 @@ +package com.baeldung.jetty.httpclient; + +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; +import org.junit.After; +import org.junit.Before; + +public abstract class AbstractUnitTest { + + protected HttpClient httpClient; + protected Server server; + protected static final String CONTENT = "Hello World!"; + protected final int port = 9080; + + @Before + public void init() { + startServer(new RequestHandler()); + startClient(); + } + + private void startClient() { + httpClient = new HttpClient(); + try { + httpClient.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void startServer(Handler handler) { + server = new Server(port); + server.setHandler(handler); + try { + server.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @After + public void dispose() throws Exception { + if (httpClient != null) { + httpClient.stop(); + } + if (server != null) { + server.stop(); + } + } + + protected String uri() { + return "http://localhost:" + port; + } +} \ No newline at end of file diff --git a/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/ProjectReactorUnitTest.java b/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/ProjectReactorUnitTest.java new file mode 100644 index 0000000000..6d79773609 --- /dev/null +++ b/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/ProjectReactorUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.jetty.httpclient; + +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.reactive.client.ReactiveRequest; +import org.eclipse.jetty.reactive.client.ReactiveResponse; +import org.junit.Assert; +import org.junit.Test; +import org.reactivestreams.Publisher; + +import reactor.core.publisher.Mono; + +public class ProjectReactorUnitTest extends AbstractUnitTest { + + @Test + public void givenReactiveClient_whenRequested_shouldReturn200() throws Exception { + + Request request = httpClient.newRequest(uri()); + ReactiveRequest reactiveRequest = ReactiveRequest.newBuilder(request) + .build(); + Publisher publisher = reactiveRequest.response(); + + ReactiveResponse response = Mono.from(publisher) + .block(); + + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), HttpStatus.OK_200); + + } +} diff --git a/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/ReactiveStreamsUnitTest.java b/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/ReactiveStreamsUnitTest.java new file mode 100644 index 0000000000..3db4553c86 --- /dev/null +++ b/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/ReactiveStreamsUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.jetty.httpclient; + +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.reactive.client.ReactiveRequest; +import org.eclipse.jetty.reactive.client.ReactiveResponse; +import org.junit.Assert; +import org.junit.Test; +import org.reactivestreams.Publisher; + +public class ReactiveStreamsUnitTest extends AbstractUnitTest { + + @Test + public void givenReactiveClient_whenRequested_shouldReturn200() throws Exception { + + Request request = httpClient.newRequest(uri()); + ReactiveRequest reactiveRequest = ReactiveRequest.newBuilder(request) + .build(); + Publisher publisher = reactiveRequest.response(); + + BlockingSubscriber subscriber = new BlockingSubscriber(); + publisher.subscribe(subscriber); + ReactiveResponse response = subscriber.block(); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), HttpStatus.OK_200); + } + +} diff --git a/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/RxJava2UnitTest.java b/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/RxJava2UnitTest.java new file mode 100644 index 0000000000..dabd768702 --- /dev/null +++ b/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/RxJava2UnitTest.java @@ -0,0 +1,67 @@ +package com.baeldung.jetty.httpclient; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.reactive.client.ReactiveRequest; +import org.eclipse.jetty.reactive.client.ReactiveRequest.Event.Type; +import org.eclipse.jetty.reactive.client.ReactiveResponse; +import org.junit.Assert; +import org.junit.Test; +import org.reactivestreams.Publisher; +import org.springframework.http.MediaType; + +import io.reactivex.Flowable; +import io.reactivex.Single; + +public class RxJava2UnitTest extends AbstractUnitTest { + + @Test + public void givenReactiveClient_whenRequestedWithBody_ShouldReturnBody() throws Exception { + + Request request = httpClient.newRequest(uri()); + ReactiveRequest reactiveRequest = ReactiveRequest.newBuilder(request) + .content(ReactiveRequest.Content.fromString(CONTENT, MediaType.TEXT_PLAIN_VALUE, UTF_8)) + .build(); + Publisher publisher = reactiveRequest.response(ReactiveResponse.Content.asString()); + + String responseContent = Single.fromPublisher(publisher) + .blockingGet(); + + Assert.assertEquals(CONTENT, responseContent); + } + + @Test + public void givenReactiveClient_whenRequested_ShouldPrintEvents() throws Exception { + ReactiveRequest request = ReactiveRequest.newBuilder(httpClient, uri()) + .content(ReactiveRequest.Content.fromString(CONTENT, MediaType.TEXT_PLAIN_VALUE, UTF_8)) + .build(); + Publisher requestEvents = request.requestEvents(); + Publisher responseEvents = request.responseEvents(); + + List requestEventTypes = new ArrayList<>(); + List responseEventTypes = new ArrayList<>(); + + Flowable.fromPublisher(requestEvents) + .map(ReactiveRequest.Event::getType) + .subscribe(requestEventTypes::add); + + Flowable.fromPublisher(responseEvents) + .map(ReactiveResponse.Event::getType) + .subscribe(responseEventTypes::add); + + Single response = Single.fromPublisher(request.response()); + int actualStatus = response.blockingGet() + .getStatus(); + + Assert.assertEquals(6, requestEventTypes.size()); + Assert.assertEquals(5, responseEventTypes.size()); + + Assert.assertEquals(actualStatus, HttpStatus.OK_200); + } + +} \ No newline at end of file diff --git a/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/SpringWebFluxUnitTest.java b/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/SpringWebFluxUnitTest.java new file mode 100644 index 0000000000..4a1a9bb2b5 --- /dev/null +++ b/libraries-http-2/src/test/java/com/baeldung/jetty/httpclient/SpringWebFluxUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.jetty.httpclient; + +import org.eclipse.jetty.client.HttpClient; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.http.MediaType; +import org.springframework.http.client.reactive.ClientHttpConnector; +import org.springframework.http.client.reactive.JettyClientHttpConnector; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; + +import reactor.core.publisher.Mono; + +public class SpringWebFluxUnitTest extends AbstractUnitTest { + + @Test + public void givenReactiveClient_whenRequested_shouldReturnResponse() throws Exception { + + HttpClient httpClient = new HttpClient(); + httpClient.start(); + + ClientHttpConnector clientConnector = new JettyClientHttpConnector(httpClient); + WebClient client = WebClient.builder() + .clientConnector(clientConnector) + .build(); + String responseContent = client.post() + .uri(uri()) + .contentType(MediaType.TEXT_PLAIN) + .body(BodyInserters.fromPublisher(Mono.just(CONTENT), String.class)) + .retrieve() + .bodyToMono(String.class) + .block(); + Assert.assertNotNull(responseContent); + Assert.assertEquals(CONTENT, responseContent); + } +} \ No newline at end of file diff --git a/libraries-rpc/pom.xml b/libraries-rpc/pom.xml new file mode 100644 index 0000000000..8741a41062 --- /dev/null +++ b/libraries-rpc/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + libraries-rpc + libraries-rpc + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + com.twitter + finagle-core_2.13 + ${finagle.core.version} + + + com.twitter + finagle-http_2.13 + ${finagle.http.version} + + + + + + 20.4.0 + 20.4.0 + + + + diff --git a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/GreetingService.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/GreetingService.java new file mode 100644 index 0000000000..7f3d741f94 --- /dev/null +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/GreetingService.java @@ -0,0 +1,18 @@ +package com.baeldung.rpc.finagle; + +import com.twitter.finagle.Service; +import com.twitter.finagle.http.Request; +import com.twitter.finagle.http.Response; +import com.twitter.finagle.http.Status; +import com.twitter.io.Buf; +import com.twitter.io.Reader; +import com.twitter.util.Future; + +public class GreetingService extends Service { + @Override + public Future apply(Request request) { + String greeting = "Hello " + request.getParam("name"); + Reader reader = Reader.fromBuf(new Buf.ByteArray(greeting.getBytes(), 0, greeting.length())); + return Future.value(Response.apply(request.version(), Status.Ok(), reader)); + } +} diff --git a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java new file mode 100644 index 0000000000..7fc5440016 --- /dev/null +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java @@ -0,0 +1,22 @@ +package com.baeldung.rpc.finagle; + +import com.twitter.finagle.Service; +import com.twitter.finagle.SimpleFilter; +import com.twitter.finagle.http.Request; +import com.twitter.finagle.http.Response; +import com.twitter.util.Future; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LogFilter extends SimpleFilter { + + private static final Logger logger = LoggerFactory.getLogger(LogFilter.class); + + @Override + public Future apply(Request request, Service service) { + logger.info("Request host:" + request.host().getOrElse(() -> "")); + logger.info("Request params:"); + request.getParams().forEach(entry -> logger.info("\t" + entry.getKey() + " : " + entry.getValue())); + return service.apply(request); + } +} diff --git a/libraries-rpc/src/main/resources/logback.xml b/libraries-rpc/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/libraries-rpc/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/FinagleIntegrationTest.java b/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/FinagleIntegrationTest.java new file mode 100644 index 0000000000..8dcdb19e7e --- /dev/null +++ b/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/FinagleIntegrationTest.java @@ -0,0 +1,40 @@ +package com.baeldung.rpc.finagle; + +import com.twitter.finagle.Http; +import com.twitter.finagle.Service; +import com.twitter.finagle.http.Method; +import com.twitter.finagle.http.Request; +import com.twitter.finagle.http.Response; +import com.twitter.util.Await; +import com.twitter.util.Future; +import org.junit.Test; +import scala.runtime.BoxedUnit; + +import static org.junit.Assert.assertEquals; + +public class FinagleIntegrationTest { + @Test + public void givenServerAndClient_whenRequestSent_thenClientShouldReceiveResponseFromServer() throws Exception { + // given + Service serverService = new LogFilter().andThen(new GreetingService()); + Http.serve(":8080", serverService); + + Service clientService = new LogFilter().andThen(Http.newService(":8080")); + + // when + Request request = Request.apply(Method.Get(), "/?name=John"); + request.host("localhost"); + Future response = clientService.apply(request); + + // then + Await.result(response + .onSuccess(r -> { + assertEquals("Hello John", r.getContentString()); + return BoxedUnit.UNIT; + }) + .onFailure(r -> { + throw new RuntimeException(r); + }) + ); + } +} diff --git a/libraries-testing/README.md b/libraries-testing/README.md index 7098c10d28..ffdefe4b19 100644 --- a/libraries-testing/README.md +++ b/libraries-testing/README.md @@ -11,4 +11,4 @@ This module contains articles about test libraries. - [Introduction to Awaitlity](https://www.baeldung.com/awaitlity-testing) - [Introduction to Hoverfly in Java](https://www.baeldung.com/hoverfly) - [Testing with Hamcrest](https://www.baeldung.com/java-junit-hamcrest-guide) -- [Introduction To DBUnit](https://www.baeldung.com/dbunit) +- [Introduction To DBUnit](https://www.baeldung.com/java-dbunit) diff --git a/libraries-testing/src/test/java/com/baeldung/dbunit/DataSourceDBUnitTest.java b/libraries-testing/src/test/java/com/baeldung/dbunit/DataSourceDBUnitTest.java index 93c7e9a456..ab3e9f3dbd 100644 --- a/libraries-testing/src/test/java/com/baeldung/dbunit/DataSourceDBUnitTest.java +++ b/libraries-testing/src/test/java/com/baeldung/dbunit/DataSourceDBUnitTest.java @@ -24,6 +24,8 @@ import java.sql.ResultSet; import java.sql.SQLException; import static com.baeldung.dbunit.ConnectionSettings.JDBC_URL; +import static com.baeldung.dbunit.ConnectionSettings.PASSWORD; +import static com.baeldung.dbunit.ConnectionSettings.USER; import static java.util.stream.Collectors.joining; import static org.assertj.core.api.Assertions.assertThat; import static org.dbunit.Assertion.assertEqualsIgnoreCols; @@ -33,12 +35,14 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase { private static final Logger logger = LoggerFactory.getLogger(DataSourceDBUnitTest.class); + private Connection connection; + @Override protected DataSource getDataSource() { JdbcDataSource dataSource = new JdbcDataSource(); dataSource.setURL(JDBC_URL); - dataSource.setUser("sa"); - dataSource.setPassword(""); + dataSource.setUser(USER); + dataSource.setPassword(PASSWORD); return dataSource; } @@ -62,6 +66,7 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase { @Before public void setUp() throws Exception { super.setUp(); + connection = getConnection().getConnection(); } @After @@ -71,9 +76,7 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase { @Test public void givenDataSet_whenSelect_thenFirstTitleIsGreyTShirt() throws SQLException { - final Connection connection = getDataSource().getConnection(); - - final ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1"); + ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1"); assertThat(rs.next()).isTrue(); assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt"); @@ -81,58 +84,59 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase { @Test public void givenDataSetEmptySchema_whenDataSetCreated_thenTablesAreEqual() throws Exception { - final IDataSet expectedDataSet = getDataSet(); - final ITable expectedTable = expectedDataSet.getTable("CLIENTS"); - final IDataSet databaseDataSet = getConnection().createDataSet(); - final ITable actualTable = databaseDataSet.getTable("CLIENTS"); + IDataSet expectedDataSet = getDataSet(); + ITable expectedTable = expectedDataSet.getTable("CLIENTS"); + IDataSet databaseDataSet = getConnection().createDataSet(); + ITable actualTable = databaseDataSet.getTable("CLIENTS"); Assertion.assertEquals(expectedTable, actualTable); } @Test public void givenDataSet_whenInsert_thenTableHasNewClient() throws Exception { - try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-user.xml")) { - final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is); - final ITable expectedTable = expectedDataSet.getTable("CLIENTS"); - final Connection conn = getDataSource().getConnection(); + try (InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-user.xml")) { + // given + IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is); + ITable expectedTable = expectedDataSet.getTable("CLIENTS"); + Connection conn = getDataSource().getConnection(); + // when conn.createStatement() - .executeUpdate( - "INSERT INTO CLIENTS (first_name, last_name) VALUES ('John', 'Jansen')"); - final ITable actualData = getConnection() - .createQueryTable( - "result_name", - "SELECT * FROM CLIENTS WHERE last_name='Jansen'"); + .executeUpdate("INSERT INTO CLIENTS (first_name, last_name) VALUES ('John', 'Jansen')"); + ITable actualData = getConnection() + .createQueryTable("result_name", "SELECT * FROM CLIENTS WHERE last_name='Jansen'"); + // then assertEqualsIgnoreCols(expectedTable, actualData, new String[] { "id" }); } } @Test public void givenDataSet_whenDelete_thenItemIsDeleted() throws Exception { - final Connection connection = getConnection().getConnection(); - - try (final InputStream is = DataSourceDBUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) { + try (InputStream is = DataSourceDBUnitTest.class.getClassLoader() + .getResourceAsStream("dbunit/items_exp_delete.xml")) { + // given ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS"); + // when connection.createStatement().executeUpdate("delete from ITEMS where id = 2"); - final IDataSet databaseDataSet = getConnection().createDataSet(); + // then + IDataSet databaseDataSet = getConnection().createDataSet(); ITable actualTable = databaseDataSet.getTable("ITEMS"); - Assertion.assertEquals(expectedTable, actualTable); } } @Test public void givenDataSet_whenUpdate_thenItemHasNewName() throws Exception { - final Connection connection = getConnection().getConnection(); - - try (final InputStream is = DataSourceDBUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename.xml")) { + try (InputStream is = DataSourceDBUnitTest.class.getClassLoader() + .getResourceAsStream("dbunit/items_exp_rename.xml")) { + // given ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS"); connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); - final IDataSet databaseDataSet = getConnection().createDataSet(); + IDataSet databaseDataSet = getConnection().createDataSet(); ITable actualTable = databaseDataSet.getTable("ITEMS"); Assertion.assertEquals(expectedTable, actualTable); @@ -140,22 +144,24 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase { } @Test - public void givenDataSet_whenInsertUnexpectedData_thenFailOnAllUnexpectedValues() throws Exception { - try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-multiple-failures.xml")) { - final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is); - final ITable expectedTable = expectedDataSet.getTable("ITEMS"); - final Connection conn = getDataSource().getConnection(); - final DiffCollectingFailureHandler collectingHandler = new DiffCollectingFailureHandler(); + public void givenDataSet_whenInsertUnexpectedData_thenFail() throws Exception { + try (InputStream is = getClass().getClassLoader() + .getResourceAsStream("dbunit/expected-multiple-failures.xml")) { - conn.createStatement().executeUpdate( - "INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')"); - final ITable actualData = getConnection().createDataSet().getTable("ITEMS"); + // given + IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is); + ITable expectedTable = expectedDataSet.getTable("ITEMS"); + Connection conn = getDataSource().getConnection(); + DiffCollectingFailureHandler collectingHandler = new DiffCollectingFailureHandler(); + // when + conn.createStatement().executeUpdate("INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')"); + ITable actualData = getConnection().createDataSet().getTable("ITEMS"); + + // then Assertion.assertEquals(expectedTable, actualData, collectingHandler); if (!collectingHandler.getDiffList().isEmpty()) { - String message = (String) collectingHandler - .getDiffList() - .stream() + String message = (String) collectingHandler.getDiffList().stream() .map(d -> formatDifference((Difference) d)).collect(joining("\n")); logger.error(() -> message); } @@ -163,6 +169,8 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase { } private static String formatDifference(Difference diff) { - return "expected value in " + diff.getExpectedTable().getTableMetaData().getTableName() + "." + diff.getColumnName() + " row " + diff.getRowIndex() + ":" + diff.getExpectedValue() + ", but was: " + diff.getActualValue(); + return "expected value in " + diff.getExpectedTable().getTableMetaData().getTableName() + "." + diff + .getColumnName() + " row " + diff.getRowIndex() + ":" + diff.getExpectedValue() + ", but was: " + diff + .getActualValue(); } } diff --git a/libraries-testing/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java b/libraries-testing/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java index 6243af9676..e2db31283f 100644 --- a/libraries-testing/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java +++ b/libraries-testing/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java @@ -31,13 +31,15 @@ public class OldSchoolDbUnitTest { private static IDatabaseTester tester = null; + private Connection connection; + @BeforeClass public static void setUp() throws Exception { tester = initDatabaseTester(); } private static IDatabaseTester initDatabaseTester() throws Exception { - final JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD); + JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD); tester.setDataSet(initDataSet()); tester.setSetUpOperation(DatabaseOperation.REFRESH); tester.setTearDownOperation(DatabaseOperation.DELETE_ALL); @@ -45,7 +47,7 @@ public class OldSchoolDbUnitTest { } private static IDataSet initDataSet() throws Exception { - try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/data.xml")) { + try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/data.xml")) { return new FlatXmlDataSetBuilder().build(is); } } @@ -53,6 +55,7 @@ public class OldSchoolDbUnitTest { @Before public void setup() throws Exception { tester.onSetup(); + connection = tester.getConnection().getConnection(); } @After @@ -62,94 +65,100 @@ public class OldSchoolDbUnitTest { @Test public void givenDataSet_whenSelect_thenFirstTitleIsGreyTShirt() throws Exception { - final Connection connection = tester.getConnection().getConnection(); - - final ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1"); + ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1"); assertThat(rs.next()).isTrue(); assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt"); } @Test - public void givenDataSet_whenInsert_thenGetResultsAreStillEqualIfIgnoringColumnsWithDifferentProduced() throws Exception { - final Connection connection = tester.getConnection().getConnection(); - final String[] excludedColumns = { "id", "produced" }; - try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-ignoring-registered_at.xml")) { - final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is); - final ITable expectedTable = DefaultColumnFilter.excludedColumnsTable( - expectedDataSet.getTable("ITEMS"), excludedColumns); + public void givenDataSet_whenInsert_thenGetResultsAreStillEqualIfIgnoringColumnsWithDifferentProduced() + throws Exception { + String[] excludedColumns = { "id", "produced" }; + try (InputStream is = getClass().getClassLoader() + .getResourceAsStream("dbunit/expected-ignoring-registered_at.xml")) { + // given + IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is); + ITable expectedTable = DefaultColumnFilter + .excludedColumnsTable(expectedDataSet.getTable("ITEMS"), excludedColumns); - connection.createStatement().executeUpdate( - "INSERT INTO ITEMS (title, price, produced) VALUES('Necklace', 199.99, now())"); - - final IDataSet databaseDataSet = tester.getConnection().createDataSet(); - final ITable actualTable = DefaultColumnFilter.excludedColumnsTable( - databaseDataSet.getTable("ITEMS"), excludedColumns); + // when + connection.createStatement() + .executeUpdate("INSERT INTO ITEMS (title, price, produced) VALUES('Necklace', 199.99, now())"); + // then + IDataSet databaseDataSet = tester.getConnection().createDataSet(); + ITable actualTable = DefaultColumnFilter + .excludedColumnsTable(databaseDataSet.getTable("ITEMS"), excludedColumns); Assertion.assertEquals(expectedTable, actualTable); } } @Test public void givenDataSet_whenDelete_thenItemIsRemoved() throws Exception { - final Connection connection = tester.getConnection().getConnection(); - - try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) { + try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader() + .getResourceAsStream("dbunit/items_exp_delete.xml")) { + // given ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS"); + // when connection.createStatement().executeUpdate("delete from ITEMS where id = 2"); - final IDataSet databaseDataSet = tester.getConnection().createDataSet(); + // then + IDataSet databaseDataSet = tester.getConnection().createDataSet(); ITable actualTable = databaseDataSet.getTable("ITEMS"); - assertEquals(expectedTable, actualTable); } } @Test - public void givenDataSet_whenDelete_thenItemIsRemovedAndResultsEqualIfProducedIsIgnored() throws Exception { - final Connection connection = tester.getConnection().getConnection(); - - try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete_no_produced.xml")) { - final ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS"); + public void givenDataSet_whenProductIgnoredAndDelete_thenItemIsRemoved() throws Exception { + try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader() + .getResourceAsStream("dbunit/items_exp_delete_no_produced.xml")) { + // given + ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS"); + // when connection.createStatement().executeUpdate("delete from ITEMS where id = 2"); - final IDataSet databaseDataSet = tester.getConnection().createDataSet(); + // then + IDataSet databaseDataSet = tester.getConnection().createDataSet(); ITable actualTable = databaseDataSet.getTable("ITEMS"); actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" }); - assertEquals(expectedTable, actualTable); } } @Test public void givenDataSet_whenUpdate_thenItemHasNewName() throws Exception { - final Connection connection = tester.getConnection().getConnection(); - - try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename.xml")) { - final ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS"); + try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader() + .getResourceAsStream("dbunit/items_exp_rename.xml")) { + // given + ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS"); + // when connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); - final IDataSet databaseDataSet = tester.getConnection().createDataSet(); + // then + IDataSet databaseDataSet = tester.getConnection().createDataSet(); ITable actualTable = databaseDataSet.getTable("ITEMS"); - assertEquals(expectedTable, actualTable); } } @Test public void givenDataSet_whenUpdateWithNoProduced_thenItemHasNewName() throws Exception { - final Connection connection = tester.getConnection().getConnection(); - - try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename_no_produced.xml")) { + try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader() + .getResourceAsStream("dbunit/items_exp_rename_no_produced.xml")) { + // given ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS"); expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[] { "produced" }); + // when connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); - final IDataSet databaseDataSet = tester.getConnection().createDataSet(); + // then + IDataSet databaseDataSet = tester.getConnection().createDataSet(); ITable actualTable = databaseDataSet.getTable("ITEMS"); actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" }); assertEquals(expectedTable, actualTable); diff --git a/libraries/README.md b/libraries/README.md index 79ba8fe55d..b61289504c 100644 --- a/libraries/README.md +++ b/libraries/README.md @@ -19,30 +19,4 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m - [Software Transactional Memory in Java Using Multiverse](https://www.baeldung.com/java-multiverse-stm) - [Locality-Sensitive Hashing in Java Using Java-LSH](https://www.baeldung.com/locality-sensitive-hashing) - [Introduction to Neuroph](https://www.baeldung.com/neuroph) -- [Quick Guide to RSS with Rome](https://www.baeldung.com/rome-rss) -- [Introduction to PCollections](https://www.baeldung.com/java-pcollections) -- [Introduction to Eclipse Collections](https://www.baeldung.com/eclipse-collections) -- [DistinctBy in the Java Stream API](https://www.baeldung.com/java-streams-distinct-by) -- [Introduction to NoException](https://www.baeldung.com/no-exception) -- [Spring Yarg Integration](https://www.baeldung.com/spring-yarg) -- [Delete a Directory Recursively in Java](https://www.baeldung.com/java-delete-directory) -- [Guide to JDeferred](https://www.baeldung.com/jdeferred) -- [Introduction to MBassador](https://www.baeldung.com/mbassador) -- [Using Pairs in Java](https://www.baeldung.com/java-pairs) -- [Introduction to Caffeine](https://www.baeldung.com/java-caching-caffeine) -- [Introduction to StreamEx](https://www.baeldung.com/streamex) -- [A Docker Guide for Java](https://www.baeldung.com/docker-java-api) -- [Introduction to Akka Actors in Java](https://www.baeldung.com/akka-actors-java) -- [A Guide to Byte Buddy](https://www.baeldung.com/byte-buddy) -- [Introduction to jOOL](https://www.baeldung.com/jool) -- [Consumer Driven Contracts with Pact](https://www.baeldung.com/pact-junit-consumer-driven-contracts) -- [Introduction to Atlassian Fugue](https://www.baeldung.com/java-fugue) -- [Publish and Receive Messages with Nats Java Client](https://www.baeldung.com/nats-java-client) -- [Java Concurrency Utility with JCTools](https://www.baeldung.com/java-concurrency-jc-tools) -- [Introduction to JavaPoet](https://www.baeldung.com/java-poet) -- [Guide to Resilience4j](https://www.baeldung.com/resilience4j) -- [Exactly Once Processing in Kafka](https://www.baeldung.com/kafka-exactly-once) -- [Implementing a FTP-Client in Java](https://www.baeldung.com/java-ftp-client) -- [Introduction to Functional Java](https://www.baeldung.com/java-functional-library) -- [A Guide to the Reflections Library](https://www.baeldung.com/reflections-library) - More articles [[next -->]](/libraries-2) diff --git a/libraries/pom.xml b/libraries/pom.xml index 41bc2b9311..fee66f928d 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -12,18 +12,6 @@ - - - com.typesafe.akka - akka-actor_2.12 - ${typesafe-akka.version} - - - com.typesafe.akka - akka-testkit_2.12 - ${typesafe-akka.version} - test - org.beykery @@ -63,18 +51,6 @@ javers-core ${javers.version} - - - io.nats - jnats - ${jnats.version} - - - - rome - rome - ${rome.version} - net.serenity-bdd serenity-core @@ -218,11 +194,6 @@ quartz ${quartz.version} - - one.util - streamex - ${streamex.version} - org.jooq jool @@ -245,28 +216,9 @@ ${java-lsh.version} - au.com.dius - pact-jvm-consumer-junit_2.11 - ${pact.version} - test - - - org.codehaus.groovy - groovy-all - - - - - org.awaitility - awaitility - ${awaitility.version} - test - - - org.awaitility - awaitility-proxy - ${awaitility.version} - test + commons-io + commons-io + ${commonsio.version} org.hamcrest @@ -274,89 +226,12 @@ ${org.hamcrest.java-hamcrest.version} test - - net.bytebuddy - byte-buddy - ${bytebuddy.version} - - - net.bytebuddy - byte-buddy-agent - ${bytebuddy.version} - - - org.pcollections - pcollections - ${pcollections.version} - - - com.machinezoo.noexception - noexception - ${noexception.version} - - - org.eclipse.collections - eclipse-collections - ${eclipse-collections.version} - - - io.vavr - vavr - ${vavr.version} - - - com.haulmont.yarg - yarg - ${yarg.version} - - - net.engio - mbassador - ${mbassador.version} - - - org.jdeferred - jdeferred-core - ${jdeferred.version} - com.codepoetics protonpack ${protonpack.version} - - org.functionaljava - functionaljava-java8 - ${functionaljava.version} - - - com.github.ben-manes.caffeine - caffeine - ${caffeine.version} - - - - - com.github.docker-java - docker-java - ${docker.version} - - - org.slf4j - slf4j-log4j12 - - - org.slf4j - jcl-over-slf4j - - - ch.qos.logback - logback-classic - - - - @@ -364,77 +239,12 @@ google-oauth-client-jetty ${google-api.version} - - org.apache.kafka - kafka-streams - ${kafka.version} - - - org.apache.kafka - kafka-clients - ${kafka.version} - test - test - - - - - io.atlassian.fugue - fugue - ${fugue.version} - - - - org.jctools - jctools-core - ${jctools.version} - - - - - io.github.resilience4j - resilience4j-circuitbreaker - ${resilience4j.version} - - - io.github.resilience4j - resilience4j-bulkhead - ${resilience4j.version} - - - io.github.resilience4j - resilience4j-retry - ${resilience4j.version} - - - io.github.resilience4j - resilience4j-timelimiter - ${resilience4j.version} - - - com.squareup - javapoet - ${javapoet.version} - org.hamcrest hamcrest-all ${hamcrest-all.version} test - - - org.mockftpserver - MockFtpServer - ${mockftpserver.version} - test - - - - org.reflections - reflections - ${reflections.version} - @@ -562,8 +372,6 @@ 1.2 3.6.2 3.1.0 - 1.0 - 2.92 1.9.26 1.41.0 @@ -572,26 +380,10 @@ 1.1.0 0.10 3.5.0 - 3.0.0 2.0.0.0 - 1.7.1 - 2.1.2 - 1.0 - 8.2.0 - 0.6.5 - 0.9.0 - 1.15 - 2.5.5 1.23.0 - 2.0.0 - 3.0.14 - 0.9.4.0006L - 2.1.2 - 2.5.11 - 0.12.1 - 1.10.0 1.3 3.2.0-m7 5.1.1 @@ -604,16 +396,9 @@ 2.3.0 0.9.12 1.19 - 1.1.0 - 2.0.4 - 1.3.1 - 1.2.6 - 4.8.1 - 4.5.1 3.0.2 - 2.7.1 3.6 - 0.9.11 + 2.6 diff --git a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/DockerPatternConverter.java b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/DockerPatternConverter.java new file mode 100644 index 0000000000..c2e40cd565 --- /dev/null +++ b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/DockerPatternConverter.java @@ -0,0 +1,30 @@ +package com.baeldung.logging.log4j2.plugins; + +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.pattern.ConverterKeys; +import org.apache.logging.log4j.core.pattern.LogEventPatternConverter; +import org.apache.logging.log4j.core.pattern.PatternConverter; + +@Plugin(name = "DockerPatternConverter", category = PatternConverter.CATEGORY) +@ConverterKeys({"docker", "container"}) +public class DockerPatternConverter extends LogEventPatternConverter { + + private DockerPatternConverter(String[] options) { + super("Docker", "docker"); + } + + public static DockerPatternConverter newInstance(String[] options) { + return new DockerPatternConverter(options); + } + + @Override + public void format(LogEvent event, StringBuilder toAppendTo) { + toAppendTo.append(dockerContainer()); + } + + private String dockerContainer() { + //get docker container ID inside which application is running here + return "container-1"; + } +} diff --git a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/KafkaAppender.java b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/KafkaAppender.java new file mode 100644 index 0000000000..3e91aa5e94 --- /dev/null +++ b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/KafkaAppender.java @@ -0,0 +1,133 @@ +package com.baeldung.logging.log4j2.plugins; + +import org.apache.logging.log4j.core.Core; +import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.Layout; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; +import org.apache.logging.log4j.core.config.plugins.PluginElement; +import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required; + +import java.io.Serializable; + +@Plugin(name = "Kafka2", category = Core.CATEGORY_NAME) +public class KafkaAppender extends AbstractAppender { + + @PluginBuilderFactory + public static Builder newBuilder() { + return new Builder(); + } + + public static class Builder implements org.apache.logging.log4j.core.util.Builder { + + @PluginBuilderAttribute("name") + @Required + private String name; + + @PluginBuilderAttribute("ip") + private String ipAddress; + + @PluginBuilderAttribute("port") + private int port; + + @PluginBuilderAttribute("topic") + private String topic; + + @PluginBuilderAttribute("partition") + private String partition; + + @PluginElement("Layout") + private Layout layout; + + @PluginElement("Filter") + private Filter filter; + + public Layout getLayout() { + return layout; + } + + public Builder setLayout(Layout layout) { + this.layout = layout; + return this; + } + + public Filter getFilter() { + return filter; + } + + public String getName() { + return name; + } + + public Builder setName(String name) { + this.name = name; + return this; + } + + public Builder setFilter(Filter filter) { + this.filter = filter; + return this; + } + + public String getIpAddress() { + return ipAddress; + } + + public Builder setIpAddress(String ipAddress) { + this.ipAddress = ipAddress; + return this; + } + + public int getPort() { + return port; + } + + public Builder setPort(int port) { + this.port = port; + return this; + } + + public String getTopic() { + return topic; + } + + public Builder setTopic(String topic) { + this.topic = topic; + return this; + } + + public String getPartition() { + return partition; + } + + public Builder setPartition(String partition) { + this.partition = partition; + return this; + } + + @Override + public KafkaAppender build() { + return new KafkaAppender(getName(), getFilter(), getLayout(), true, new KafkaBroker(ipAddress, port, topic, partition)); + } + } + + private KafkaBroker broker; + + private KafkaAppender(String name, Filter filter, Layout layout, boolean ignoreExceptions, KafkaBroker broker) { + super(name, filter, layout, ignoreExceptions); + this.broker = broker; + } + + @Override + public void append(LogEvent event) { + + connectAndSendToKafka(broker, event); + } + + private void connectAndSendToKafka(KafkaBroker broker, LogEvent event) { + //send to Kafka + } +} diff --git a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/KafkaBroker.java b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/KafkaBroker.java new file mode 100644 index 0000000000..0479e71c41 --- /dev/null +++ b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/KafkaBroker.java @@ -0,0 +1,38 @@ +package com.baeldung.logging.log4j2.plugins; + +import java.io.Serializable; + +public class KafkaBroker implements Serializable { + + private final String ipAddress; + private final int port; + + public KafkaBroker(String ipAddress, int port, String topic, String partition) { + this.ipAddress = ipAddress; + this.port = port; + this.topic = topic; + this.partition = partition; + } + + public String getTopic() { + return topic; + } + + public String getPartition() { + return partition; + } + + private final String topic; + private final String partition; + + + public String getIpAddress() { + return ipAddress; + } + + public int getPort() { + return port; + } + + +} diff --git a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/KafkaLookup.java b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/KafkaLookup.java new file mode 100644 index 0000000000..c2679861c4 --- /dev/null +++ b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/KafkaLookup.java @@ -0,0 +1,24 @@ +package com.baeldung.logging.log4j2.plugins; + +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.lookup.StrLookup; + +@Plugin(name = "kafka", category = StrLookup.CATEGORY) +public class KafkaLookup implements StrLookup { + + @Override + public String lookup(String key) { + return getFromKafka(key); + } + + @Override + public String lookup(LogEvent event, String key) { + return getFromKafka(key); + } + + private String getFromKafka(String topicName) { + //kafka search logic should go here + return "topic1-p1"; + } +} diff --git a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/ListAppender.java b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/ListAppender.java new file mode 100644 index 0000000000..e3819028db --- /dev/null +++ b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/plugins/ListAppender.java @@ -0,0 +1,48 @@ +/** + * + */ +package com.baeldung.logging.log4j2.plugins; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.Core; +import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginElement; +import org.apache.logging.log4j.core.config.plugins.PluginFactory; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static java.util.Collections.synchronizedList; + +@Plugin(name = "ListAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE) +public class ListAppender extends AbstractAppender { + + private List logList; + + protected ListAppender(String name, Filter filter) { + super(name, filter, null); + logList = synchronizedList(new ArrayList<>()); + } + + @PluginFactory + public static ListAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Filter") final Filter filter) { + return new ListAppender(name, filter); + } + + @Override + public void append(LogEvent event) { + if (event.getLevel() + .isLessSpecificThan(Level.WARN)) { + error("Unable to log less than WARN level."); + return; + } + logList.add(event); + } + +} diff --git a/logging-modules/log4j2/src/test/resources/log4j2.xml b/logging-modules/log4j2/src/test/resources/log4j2.xml index ee26bcecf2..050e0aa705 100644 --- a/logging-modules/log4j2/src/test/resources/log4j2.xml +++ b/logging-modules/log4j2/src/test/resources/log4j2.xml @@ -50,6 +50,12 @@ + + + + + + + + + diff --git a/maven-all/README.md b/maven-all/README.md index b20d944b14..b448be2cd0 100644 --- a/maven-all/README.md +++ b/maven-all/README.md @@ -5,3 +5,4 @@ This module contains articles about Apache Maven. Please refer to its submodules ### Relevant Articles - [Apache Maven Tutorial](https://www.baeldung.com/maven) +- [Find Unused Maven Dependencies](https://www.baeldung.com/maven-unused-dependencies) diff --git a/maven-all/maven-unused-dependencies/pom.xml b/maven-all/maven-unused-dependencies/pom.xml new file mode 100644 index 0000000000..825858e481 --- /dev/null +++ b/maven-all/maven-unused-dependencies/pom.xml @@ -0,0 +1,47 @@ + + 4.0.0 + com.baeldung + maven-unused-dependencies + 0.0.1-SNAPSHOT + + + 3.2.2 + 1.7.25 + 3.1.2 + 3.1 + + + + + commons-collections + commons-collections + ${commons-collections.version} + + + org.slf4j + slf4j-api + ${slf4j-api.version} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + maven-dependency-plugin + ${maven-dependency-plugin.version} + + + + + \ No newline at end of file diff --git a/maven-all/maven-unused-dependencies/src/main/java/com/baeldung/mavendependencyplugin/UnusedDependenciesExample.java b/maven-all/maven-unused-dependencies/src/main/java/com/baeldung/mavendependencyplugin/UnusedDependenciesExample.java new file mode 100644 index 0000000000..c9390880ed --- /dev/null +++ b/maven-all/maven-unused-dependencies/src/main/java/com/baeldung/mavendependencyplugin/UnusedDependenciesExample.java @@ -0,0 +1,17 @@ +package com.baeldung.mavendependencyplugin; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UnusedDependenciesExample { + + /** + * When the Maven dependency analyzer analyzes the code, it will see that the slf4j dependency is being used in this method. + * + * @return the slf4j {@link Logger}. + */ + public Logger getLogger() { + return LoggerFactory.getLogger(UnusedDependenciesExample.class); + } + +} diff --git a/netty/README.md b/netty/README.md new file mode 100644 index 0000000000..30c63cd5a8 --- /dev/null +++ b/netty/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [HTTP/2 in Netty](https://www.baeldung.com/netty-http2) diff --git a/oauth2-framework-impl/oauth2-authorization-server/README.md b/oauth2-framework-impl/oauth2-authorization-server/README.md deleted file mode 100644 index 4bcb9790b1..0000000000 --- a/oauth2-framework-impl/oauth2-authorization-server/README.md +++ /dev/null @@ -1,3 +0,0 @@ -### Relevant Articles: - -- [Implementing The OAuth 2.0 Authorization Framework Using Jakarta EE](https://www.baeldung.com/java-ee-oauth2-implementation) diff --git a/oauth2-framework-impl/oauth2-resource-server/README.md b/oauth2-framework-impl/oauth2-resource-server/README.md deleted file mode 100644 index 4bcb9790b1..0000000000 --- a/oauth2-framework-impl/oauth2-resource-server/README.md +++ /dev/null @@ -1,3 +0,0 @@ -### Relevant Articles: - -- [Implementing The OAuth 2.0 Authorization Framework Using Jakarta EE](https://www.baeldung.com/java-ee-oauth2-implementation) diff --git a/parent-spring-4/pom.xml b/parent-spring-4/pom.xml index 3f9a22fb03..931cad374b 100644 --- a/parent-spring-4/pom.xml +++ b/parent-spring-4/pom.xml @@ -53,7 +53,7 @@ - 4.3.26.RELEASE + 4.3.27.RELEASE 1.6.1 diff --git a/patterns/cqrs-es/README.md b/patterns/cqrs-es/README.md new file mode 100644 index 0000000000..92570280ab --- /dev/null +++ b/patterns/cqrs-es/README.md @@ -0,0 +1,5 @@ +This module contains articles about composing together CQRS and Event Sourcing + +## Relevant Articles + +- [CQRS and Event Sourcing in Java](https://www.baeldung.com/cqrs-event-sourcing-java) diff --git a/patterns/cqrs-es/pom.xml b/patterns/cqrs-es/pom.xml new file mode 100644 index 0000000000..3c54038837 --- /dev/null +++ b/patterns/cqrs-es/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + cqrs-es + 1.0-SNAPSHOT + cqrs-es + + com.baeldung + patterns + 1.0.0-SNAPSHOT + + + 1.8 + 1.8 + + + + org.projectlombok + lombok + 1.18.12 + + + junit + junit + 4.13 + test + + + \ No newline at end of file diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/aggregates/UserAggregate.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/aggregates/UserAggregate.java new file mode 100644 index 0000000000..96476dc15a --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/aggregates/UserAggregate.java @@ -0,0 +1,30 @@ +package com.baeldung.patterns.cqrs.aggregates; + +import com.baeldung.patterns.cqrs.commands.CreateUserCommand; +import com.baeldung.patterns.cqrs.commands.UpdateUserCommand; +import com.baeldung.patterns.cqrs.repository.UserWriteRepository; +import com.baeldung.patterns.domain.User; + +public class UserAggregate { + + private UserWriteRepository writeRepository; + + public UserAggregate(UserWriteRepository repository) { + this.writeRepository = repository; + } + + public User handleCreateUserCommand(CreateUserCommand command) { + User user = new User(command.getUserId(), command.getFirstName(), command.getLastName()); + writeRepository.addUser(user.getUserid(), user); + return user; + } + + public User handleUpdateUserCommand(UpdateUserCommand command) { + User user = writeRepository.getUser(command.getUserId()); + user.setAddresses(command.getAddresses()); + user.setContacts(command.getContacts()); + writeRepository.addUser(user.getUserid(), user); + return user; + } + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/commands/CreateUserCommand.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/commands/CreateUserCommand.java new file mode 100644 index 0000000000..362804c610 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/commands/CreateUserCommand.java @@ -0,0 +1,14 @@ +package com.baeldung.patterns.cqrs.commands; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class CreateUserCommand { + + private String userId; + private String firstName; + private String lastName; + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/commands/UpdateUserCommand.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/commands/UpdateUserCommand.java new file mode 100644 index 0000000000..58a4889458 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/commands/UpdateUserCommand.java @@ -0,0 +1,20 @@ +package com.baeldung.patterns.cqrs.commands; + +import java.util.HashSet; +import java.util.Set; + +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class UpdateUserCommand { + + private String userId; + private Set
addresses = new HashSet<>(); + private Set contacts = new HashSet<>(); + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/projections/UserProjection.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/projections/UserProjection.java new file mode 100644 index 0000000000..40a56e727f --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/projections/UserProjection.java @@ -0,0 +1,37 @@ +package com.baeldung.patterns.cqrs.projections; + +import java.util.Set; + +import com.baeldung.patterns.cqrs.queries.AddressByRegionQuery; +import com.baeldung.patterns.cqrs.queries.ContactByTypeQuery; +import com.baeldung.patterns.cqrs.repository.UserReadRepository; +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; +import com.baeldung.patterns.domain.UserAddress; +import com.baeldung.patterns.domain.UserContact; + +public class UserProjection { + + private UserReadRepository repository; + + public UserProjection(UserReadRepository repository) { + this.repository = repository; + } + + public Set handle(ContactByTypeQuery query) throws Exception { + UserContact userContact = repository.getUserContact(query.getUserId()); + if (userContact == null) + throw new Exception("User does not exist."); + return userContact.getContactByType() + .get(query.getContactType()); + } + + public Set
handle(AddressByRegionQuery query) throws Exception { + UserAddress userAddress = repository.getUserAddress(query.getUserId()); + if (userAddress == null) + throw new Exception("User does not exist."); + return userAddress.getAddressByRegion() + .get(query.getState()); + } + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/projectors/UserProjector.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/projectors/UserProjector.java new file mode 100644 index 0000000000..0344c352d9 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/projectors/UserProjector.java @@ -0,0 +1,49 @@ +package com.baeldung.patterns.cqrs.projectors; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +import com.baeldung.patterns.cqrs.repository.UserReadRepository; +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; +import com.baeldung.patterns.domain.User; +import com.baeldung.patterns.domain.UserAddress; +import com.baeldung.patterns.domain.UserContact; + +public class UserProjector { + + UserReadRepository readRepository = new UserReadRepository(); + + public UserProjector(UserReadRepository readRepository) { + this.readRepository = readRepository; + } + + public void project(User user) { + UserContact userContact = Optional.ofNullable(readRepository.getUserContact(user.getUserid())) + .orElse(new UserContact()); + Map> contactByType = new HashMap<>(); + for (Contact contact : user.getContacts()) { + Set contacts = Optional.ofNullable(contactByType.get(contact.getType())) + .orElse(new HashSet<>()); + contacts.add(contact); + contactByType.put(contact.getType(), contacts); + } + userContact.setContactByType(contactByType); + readRepository.addUserContact(user.getUserid(), userContact); + + UserAddress userAddress = Optional.ofNullable(readRepository.getUserAddress(user.getUserid())) + .orElse(new UserAddress()); + Map> addressByRegion = new HashMap<>(); + for (Address address : user.getAddresses()) { + Set
addresses = Optional.ofNullable(addressByRegion.get(address.getState())) + .orElse(new HashSet<>()); + addresses.add(address); + addressByRegion.put(address.getState(), addresses); + } + userAddress.setAddressByRegion(addressByRegion); + readRepository.addUserAddress(user.getUserid(), userAddress); + } +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/queries/AddressByRegionQuery.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/queries/AddressByRegionQuery.java new file mode 100644 index 0000000000..4a0f0769f2 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/queries/AddressByRegionQuery.java @@ -0,0 +1,12 @@ +package com.baeldung.patterns.cqrs.queries; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class AddressByRegionQuery { + + private String userId; + private String state; +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/queries/ContactByTypeQuery.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/queries/ContactByTypeQuery.java new file mode 100644 index 0000000000..b6c271b472 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/queries/ContactByTypeQuery.java @@ -0,0 +1,12 @@ +package com.baeldung.patterns.cqrs.queries; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class ContactByTypeQuery { + + private String userId; + private String contactType; +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/repository/UserReadRepository.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/repository/UserReadRepository.java new file mode 100644 index 0000000000..a7ef2f6f96 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/repository/UserReadRepository.java @@ -0,0 +1,31 @@ +package com.baeldung.patterns.cqrs.repository; + +import java.util.HashMap; +import java.util.Map; + +import com.baeldung.patterns.domain.UserAddress; +import com.baeldung.patterns.domain.UserContact; + +public class UserReadRepository { + + private Map userAddress = new HashMap<>(); + + private Map userContact = new HashMap<>(); + + public void addUserAddress(String id, UserAddress user) { + userAddress.put(id, user); + } + + public UserAddress getUserAddress(String id) { + return userAddress.get(id); + } + + public void addUserContact(String id, UserContact user) { + userContact.put(id, user); + } + + public UserContact getUserContact(String id) { + return userContact.get(id); + } + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/repository/UserWriteRepository.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/repository/UserWriteRepository.java new file mode 100644 index 0000000000..8636e36225 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/cqrs/repository/UserWriteRepository.java @@ -0,0 +1,20 @@ +package com.baeldung.patterns.cqrs.repository; + +import java.util.HashMap; +import java.util.Map; + +import com.baeldung.patterns.domain.User; + +public class UserWriteRepository { + + private Map store = new HashMap<>(); + + public void addUser(String id, User user) { + store.put(id, user); + } + + public User getUser(String id) { + return store.get(id); + } + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/crud/repository/UserRepository.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/crud/repository/UserRepository.java new file mode 100644 index 0000000000..b22d40e6e0 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/crud/repository/UserRepository.java @@ -0,0 +1,20 @@ +package com.baeldung.patterns.crud.repository; + +import java.util.HashMap; +import java.util.Map; + +import com.baeldung.patterns.domain.User; + +public class UserRepository { + + private Map store = new HashMap<>(); + + public void addUser(String id, User user) { + store.put(id, user); + } + + public User getUser(String id) { + return store.get(id); + } + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/crud/service/UserService.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/crud/service/UserService.java new file mode 100644 index 0000000000..d21a796304 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/crud/service/UserService.java @@ -0,0 +1,55 @@ +package com.baeldung.patterns.crud.service; + +import java.util.Set; +import java.util.stream.Collectors; + +import com.baeldung.patterns.crud.repository.UserRepository; +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; +import com.baeldung.patterns.domain.User; + +public class UserService { + + private UserRepository repository; + + public UserService(UserRepository repository) { + this.repository = repository; + } + + public void createUser(String userId, String firstName, String lastName) { + User user = new User(userId, firstName, lastName); + repository.addUser(userId, user); + } + + public void updateUser(String userId, Set contacts, Set
addresses) throws Exception { + User user = repository.getUser(userId); + if (user == null) + throw new Exception("User does not exist."); + user.setContacts(contacts); + user.setAddresses(addresses); + repository.addUser(userId, user); + } + + public Set getContactByType(String userId, String contactType) throws Exception { + User user = repository.getUser(userId); + if (user == null) + throw new Exception("User does not exit."); + Set contacts = user.getContacts(); + return contacts.stream() + .filter(c -> c.getType() + .equals(contactType)) + .collect(Collectors.toSet()); + } + + public Set
getAddressByRegion(String userId, String state) throws Exception { + User user = repository.getUser(userId); + if (user == null) + throw new Exception("User does not exist."); + Set
addresses = user.getAddresses(); + return addresses.stream() + .filter(a -> a.getState() + .equals(state)) + .collect(Collectors.toSet()); + } + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/Address.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/Address.java new file mode 100644 index 0000000000..dfaa724434 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/Address.java @@ -0,0 +1,14 @@ +package com.baeldung.patterns.domain; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class Address { + + private String city; + private String state; + private String postcode; + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/Contact.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/Contact.java new file mode 100644 index 0000000000..a151cdb4ff --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/Contact.java @@ -0,0 +1,13 @@ +package com.baeldung.patterns.domain; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class Contact { + + private String type; + private String detail; + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/User.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/User.java new file mode 100644 index 0000000000..8f59723894 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/User.java @@ -0,0 +1,22 @@ +package com.baeldung.patterns.domain; + +import java.util.HashSet; +import java.util.Set; + +import lombok.Data; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; + +@Data +@RequiredArgsConstructor +public class User { + @NonNull + private String userid; + @NonNull + private String firstname; + @NonNull + private String lastname; + private Set contacts = new HashSet<>(); + private Set
addresses = new HashSet<>(); + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/UserAddress.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/UserAddress.java new file mode 100644 index 0000000000..8105a53265 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/UserAddress.java @@ -0,0 +1,14 @@ +package com.baeldung.patterns.domain; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import lombok.Data; + +@Data +public class UserAddress { + + private Map> addressByRegion = new HashMap<>(); + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/UserContact.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/UserContact.java new file mode 100644 index 0000000000..1768dadc39 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/domain/UserContact.java @@ -0,0 +1,14 @@ +package com.baeldung.patterns.domain; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import lombok.Data; + +@Data +public class UserContact { + + private Map> contactByType = new HashMap<>(); + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/Event.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/Event.java new file mode 100644 index 0000000000..4718450cac --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/Event.java @@ -0,0 +1,15 @@ +package com.baeldung.patterns.es.events; + +import java.util.Date; +import java.util.UUID; + +import lombok.ToString; + +@ToString +public abstract class Event { + + public final UUID id = UUID.randomUUID(); + + public final Date created = new Date(); + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserAddressAddedEvent.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserAddressAddedEvent.java new file mode 100644 index 0000000000..caa640798b --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserAddressAddedEvent.java @@ -0,0 +1,16 @@ +package com.baeldung.patterns.es.events; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@AllArgsConstructor +@EqualsAndHashCode(callSuper = false) +public class UserAddressAddedEvent extends Event { + + private String city; + private String state; + private String postCode; + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserAddressRemovedEvent.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserAddressRemovedEvent.java new file mode 100644 index 0000000000..f0702d8810 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserAddressRemovedEvent.java @@ -0,0 +1,16 @@ +package com.baeldung.patterns.es.events; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@AllArgsConstructor +@EqualsAndHashCode(callSuper = false) +public class UserAddressRemovedEvent extends Event { + + private String city; + private String state; + private String postCode; + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserContactAddedEvent.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserContactAddedEvent.java new file mode 100644 index 0000000000..0bce2f997e --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserContactAddedEvent.java @@ -0,0 +1,15 @@ +package com.baeldung.patterns.es.events; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@AllArgsConstructor +@EqualsAndHashCode(callSuper = false) +public class UserContactAddedEvent extends Event { + + private String contactType; + private String contactDetails; + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserContactRemovedEvent.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserContactRemovedEvent.java new file mode 100644 index 0000000000..9c043971e7 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserContactRemovedEvent.java @@ -0,0 +1,15 @@ +package com.baeldung.patterns.es.events; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@AllArgsConstructor +@EqualsAndHashCode(callSuper = false) +public class UserContactRemovedEvent extends Event { + + private String contactType; + private String contactDetails; + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserCreatedEvent.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserCreatedEvent.java new file mode 100644 index 0000000000..8e725cd667 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/events/UserCreatedEvent.java @@ -0,0 +1,16 @@ +package com.baeldung.patterns.es.events; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@AllArgsConstructor +@EqualsAndHashCode(callSuper = false) +public class UserCreatedEvent extends Event { + + private String userId; + private String firstName; + private String lastName; + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/repository/EventStore.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/repository/EventStore.java new file mode 100644 index 0000000000..87914d8c12 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/repository/EventStore.java @@ -0,0 +1,29 @@ +package com.baeldung.patterns.es.repository; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.baeldung.patterns.es.events.Event; + +public class EventStore { + + private Map> store = new HashMap<>(); + + public void addEvent(String id, Event event) { + List events = store.get(id); + if (events == null) { + events = new ArrayList(); + events.add(event); + store.put(id, events); + } else { + events.add(event); + } + } + + public List getEvents(String id) { + return store.get(id); + } + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/service/UserService.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/service/UserService.java new file mode 100644 index 0000000000..9650c4294f --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/service/UserService.java @@ -0,0 +1,72 @@ +package com.baeldung.patterns.es.service; + +import java.util.Set; +import java.util.stream.Collectors; + +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; +import com.baeldung.patterns.domain.User; +import com.baeldung.patterns.es.events.UserAddressAddedEvent; +import com.baeldung.patterns.es.events.UserAddressRemovedEvent; +import com.baeldung.patterns.es.events.UserContactAddedEvent; +import com.baeldung.patterns.es.events.UserContactRemovedEvent; +import com.baeldung.patterns.es.events.UserCreatedEvent; +import com.baeldung.patterns.es.repository.EventStore; + +public class UserService { + + private EventStore repository; + + public UserService(EventStore repository) { + this.repository = repository; + } + + public void createUser(String userId, String firstName, String lastName) { + repository.addEvent(userId, new UserCreatedEvent(userId, firstName, lastName)); + } + + public void updateUser(String userId, Set contacts, Set
addresses) throws Exception { + User user = UserUtility.recreateUserState(repository, userId); + if (user == null) + throw new Exception("User does not exist."); + + user.getContacts() + .stream() + .filter(c -> !contacts.contains(c)) + .forEach(c -> repository.addEvent(userId, new UserContactRemovedEvent(c.getType(), c.getDetail()))); + contacts.stream() + .filter(c -> !user.getContacts() + .contains(c)) + .forEach(c -> repository.addEvent(userId, new UserContactAddedEvent(c.getType(), c.getDetail()))); + user.getAddresses() + .stream() + .filter(a -> !addresses.contains(a)) + .forEach(a -> repository.addEvent(userId, new UserAddressRemovedEvent(a.getCity(), a.getState(), a.getPostcode()))); + addresses.stream() + .filter(a -> !user.getAddresses() + .contains(a)) + .forEach(a -> repository.addEvent(userId, new UserAddressAddedEvent(a.getCity(), a.getState(), a.getPostcode()))); + } + + public Set getContactByType(String userId, String contactType) throws Exception { + User user = UserUtility.recreateUserState(repository, userId); + if (user == null) + throw new Exception("User does not exist."); + return user.getContacts() + .stream() + .filter(c -> c.getType() + .equals(contactType)) + .collect(Collectors.toSet()); + } + + public Set
getAddressByRegion(String userId, String state) throws Exception { + User user = UserUtility.recreateUserState(repository, userId); + if (user == null) + throw new Exception("User does not exist."); + return user.getAddresses() + .stream() + .filter(a -> a.getState() + .equals(state)) + .collect(Collectors.toSet()); + } +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/service/UserUtility.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/service/UserUtility.java new file mode 100644 index 0000000000..e44e404588 --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/es/service/UserUtility.java @@ -0,0 +1,62 @@ +package com.baeldung.patterns.es.service; + +import java.util.List; +import java.util.UUID; + +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; +import com.baeldung.patterns.domain.User; +import com.baeldung.patterns.es.events.Event; +import com.baeldung.patterns.es.events.UserAddressAddedEvent; +import com.baeldung.patterns.es.events.UserAddressRemovedEvent; +import com.baeldung.patterns.es.events.UserContactAddedEvent; +import com.baeldung.patterns.es.events.UserContactRemovedEvent; +import com.baeldung.patterns.es.events.UserCreatedEvent; +import com.baeldung.patterns.es.repository.EventStore; + +public class UserUtility { + + public static User recreateUserState(EventStore store, String userId) { + User user = null; + + List events = store.getEvents(userId); + for (Event event : events) { + if (event instanceof UserCreatedEvent) { + UserCreatedEvent e = (UserCreatedEvent) event; + user = new User(UUID.randomUUID() + .toString(), e.getFirstName(), e.getLastName()); + } + if (event instanceof UserAddressAddedEvent) { + UserAddressAddedEvent e = (UserAddressAddedEvent) event; + Address address = new Address(e.getCity(), e.getState(), e.getPostCode()); + if (user != null) + user.getAddresses() + .add(address); + } + if (event instanceof UserAddressRemovedEvent) { + UserAddressRemovedEvent e = (UserAddressRemovedEvent) event; + Address address = new Address(e.getCity(), e.getState(), e.getPostCode()); + if (user != null) + user.getAddresses() + .remove(address); + } + if (event instanceof UserContactAddedEvent) { + UserContactAddedEvent e = (UserContactAddedEvent) event; + Contact contact = new Contact(e.getContactType(), e.getContactDetails()); + if (user != null) + user.getContacts() + .add(contact); + } + if (event instanceof UserContactRemovedEvent) { + UserContactRemovedEvent e = (UserContactRemovedEvent) event; + Contact contact = new Contact(e.getContactType(), e.getContactDetails()); + if (user != null) + user.getContacts() + .remove(contact); + } + } + + return user; + } + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/escqrs/aggregates/UserAggregate.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/escqrs/aggregates/UserAggregate.java new file mode 100644 index 0000000000..04bfeb634d --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/escqrs/aggregates/UserAggregate.java @@ -0,0 +1,87 @@ +package com.baeldung.patterns.escqrs.aggregates; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import com.baeldung.patterns.cqrs.commands.CreateUserCommand; +import com.baeldung.patterns.cqrs.commands.UpdateUserCommand; +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; +import com.baeldung.patterns.domain.User; +import com.baeldung.patterns.es.events.Event; +import com.baeldung.patterns.es.events.UserAddressAddedEvent; +import com.baeldung.patterns.es.events.UserAddressRemovedEvent; +import com.baeldung.patterns.es.events.UserContactAddedEvent; +import com.baeldung.patterns.es.events.UserContactRemovedEvent; +import com.baeldung.patterns.es.events.UserCreatedEvent; +import com.baeldung.patterns.es.repository.EventStore; +import com.baeldung.patterns.es.service.UserUtility; + +public class UserAggregate { + + private EventStore writeRepository; + + public UserAggregate(EventStore repository) { + this.writeRepository = repository; + } + + public List handleCreateUserCommand(CreateUserCommand command) { + UserCreatedEvent event = new UserCreatedEvent(command.getUserId(), command.getFirstName(), command.getLastName()); + writeRepository.addEvent(command.getUserId(), event); + return Arrays.asList(event); + } + + public List handleUpdateUserCommand(UpdateUserCommand command) { + User user = UserUtility.recreateUserState(writeRepository, command.getUserId()); + List events = new ArrayList<>(); + + List contactsToRemove = user.getContacts() + .stream() + .filter(c -> !command.getContacts() + .contains(c)) + .collect(Collectors.toList()); + for (Contact contact : contactsToRemove) { + UserContactRemovedEvent contactRemovedEvent = new UserContactRemovedEvent(contact.getType(), contact.getDetail()); + events.add(contactRemovedEvent); + writeRepository.addEvent(command.getUserId(), contactRemovedEvent); + } + + List contactsToAdd = command.getContacts() + .stream() + .filter(c -> !user.getContacts() + .contains(c)) + .collect(Collectors.toList()); + for (Contact contact : contactsToAdd) { + UserContactAddedEvent contactAddedEvent = new UserContactAddedEvent(contact.getType(), contact.getDetail()); + events.add(contactAddedEvent); + writeRepository.addEvent(command.getUserId(), contactAddedEvent); + } + + List
addressesToRemove = user.getAddresses() + .stream() + .filter(a -> !command.getAddresses() + .contains(a)) + .collect(Collectors.toList()); + for (Address address : addressesToRemove) { + UserAddressRemovedEvent addressRemovedEvent = new UserAddressRemovedEvent(address.getCity(), address.getState(), address.getPostcode()); + events.add(addressRemovedEvent); + writeRepository.addEvent(command.getUserId(), addressRemovedEvent); + } + + List
addressesToAdd = command.getAddresses() + .stream() + .filter(a -> !user.getAddresses() + .contains(a)) + .collect(Collectors.toList()); + for (Address address : addressesToAdd) { + UserAddressAddedEvent addressAddedEvent = new UserAddressAddedEvent(address.getCity(), address.getState(), address.getPostcode()); + events.add(addressAddedEvent); + writeRepository.addEvent(command.getUserId(), addressAddedEvent); + } + + return events; + } + +} diff --git a/patterns/cqrs-es/src/main/java/com/baeldung/patterns/escqrs/projectors/UserProjector.java b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/escqrs/projectors/UserProjector.java new file mode 100644 index 0000000000..d388abe7cb --- /dev/null +++ b/patterns/cqrs-es/src/main/java/com/baeldung/patterns/escqrs/projectors/UserProjector.java @@ -0,0 +1,91 @@ +package com.baeldung.patterns.escqrs.projectors; + +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import com.baeldung.patterns.cqrs.repository.UserReadRepository; +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; +import com.baeldung.patterns.domain.UserAddress; +import com.baeldung.patterns.domain.UserContact; +import com.baeldung.patterns.es.events.Event; +import com.baeldung.patterns.es.events.UserAddressAddedEvent; +import com.baeldung.patterns.es.events.UserAddressRemovedEvent; +import com.baeldung.patterns.es.events.UserContactAddedEvent; +import com.baeldung.patterns.es.events.UserContactRemovedEvent; + +public class UserProjector { + + UserReadRepository readRepository = new UserReadRepository(); + + public UserProjector(UserReadRepository readRepository) { + this.readRepository = readRepository; + } + + public void project(String userId, List events) { + + for (Event event : events) { + if (event instanceof UserAddressAddedEvent) + apply(userId, (UserAddressAddedEvent) event); + if (event instanceof UserAddressRemovedEvent) + apply(userId, (UserAddressRemovedEvent) event); + if (event instanceof UserContactAddedEvent) + apply(userId, (UserContactAddedEvent) event); + if (event instanceof UserContactRemovedEvent) + apply(userId, (UserContactRemovedEvent) event); + } + + } + + public void apply(String userId, UserAddressAddedEvent event) { + Address address = new Address(event.getCity(), event.getState(), event.getPostCode()); + UserAddress userAddress = Optional.ofNullable(readRepository.getUserAddress(userId)) + .orElse(new UserAddress()); + Set
addresses = Optional.ofNullable(userAddress.getAddressByRegion() + .get(address.getState())) + .orElse(new HashSet<>()); + addresses.add(address); + userAddress.getAddressByRegion() + .put(address.getState(), addresses); + readRepository.addUserAddress(userId, userAddress); + } + + public void apply(String userId, UserAddressRemovedEvent event) { + Address address = new Address(event.getCity(), event.getState(), event.getPostCode()); + UserAddress userAddress = readRepository.getUserAddress(userId); + if (userAddress != null) { + Set
addresses = userAddress.getAddressByRegion() + .get(address.getState()); + if (addresses != null) + addresses.remove(address); + readRepository.addUserAddress(userId, userAddress); + } + } + + public void apply(String userId, UserContactAddedEvent event) { + Contact contact = new Contact(event.getContactType(), event.getContactDetails()); + UserContact userContact = Optional.ofNullable(readRepository.getUserContact(userId)) + .orElse(new UserContact()); + Set contacts = Optional.ofNullable(userContact.getContactByType() + .get(contact.getType())) + .orElse(new HashSet<>()); + contacts.add(contact); + userContact.getContactByType() + .put(contact.getType(), contacts); + readRepository.addUserContact(userId, userContact); + } + + public void apply(String userId, UserContactRemovedEvent event) { + Contact contact = new Contact(event.getContactType(), event.getContactDetails()); + UserContact userContact = readRepository.getUserContact(userId); + if (userContact != null) { + Set contacts = userContact.getContactByType() + .get(contact.getType()); + if (contacts != null) + contacts.remove(contact); + readRepository.addUserContact(userId, userContact); + } + } +} diff --git a/patterns/cqrs-es/src/test/java/com/baeldung/patterns/cqrs/ApplicationUnitTest.java b/patterns/cqrs-es/src/test/java/com/baeldung/patterns/cqrs/ApplicationUnitTest.java new file mode 100644 index 0000000000..7f68a64274 --- /dev/null +++ b/patterns/cqrs-es/src/test/java/com/baeldung/patterns/cqrs/ApplicationUnitTest.java @@ -0,0 +1,74 @@ +package com.baeldung.patterns.cqrs; + +import static org.junit.Assert.assertEquals; + +import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.patterns.cqrs.aggregates.UserAggregate; +import com.baeldung.patterns.cqrs.commands.CreateUserCommand; +import com.baeldung.patterns.cqrs.commands.UpdateUserCommand; +import com.baeldung.patterns.cqrs.projections.UserProjection; +import com.baeldung.patterns.cqrs.projectors.UserProjector; +import com.baeldung.patterns.cqrs.queries.AddressByRegionQuery; +import com.baeldung.patterns.cqrs.queries.ContactByTypeQuery; +import com.baeldung.patterns.cqrs.repository.UserReadRepository; +import com.baeldung.patterns.cqrs.repository.UserWriteRepository; +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; +import com.baeldung.patterns.domain.User; + +public class ApplicationUnitTest { + + private UserWriteRepository writeRepository; + private UserReadRepository readRepository; + private UserProjector projector; + private UserAggregate userAggregate; + private UserProjection userProjection; + + @Before + public void setUp() { + writeRepository = new UserWriteRepository(); + readRepository = new UserReadRepository(); + projector = new UserProjector(readRepository); + userAggregate = new UserAggregate(writeRepository); + userProjection = new UserProjection(readRepository); + } + + @Test + public void givenCQRSApplication_whenCommandRun_thenQueryShouldReturnResult() throws Exception { + String userId = UUID.randomUUID() + .toString(); + User user = null; + CreateUserCommand createUserCommand = new CreateUserCommand(userId, "Tom", "Sawyer"); + user = userAggregate.handleCreateUserCommand(createUserCommand); + projector.project(user); + + UpdateUserCommand updateUserCommand = new UpdateUserCommand(user.getUserid(), Stream.of(new Address("New York", "NY", "10001"), new Address("Los Angeles", "CA", "90001")) + .collect(Collectors.toSet()), + Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("EMAIL", "tom.sawyer@rediff.com")) + .collect(Collectors.toSet())); + user = userAggregate.handleUpdateUserCommand(updateUserCommand); + projector.project(user); + + updateUserCommand = new UpdateUserCommand(userId, Stream.of(new Address("New York", "NY", "10001"), new Address("Housten", "TX", "77001")) + .collect(Collectors.toSet()), + Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("PHONE", "700-000-0001")) + .collect(Collectors.toSet())); + user = userAggregate.handleUpdateUserCommand(updateUserCommand); + projector.project(user); + + ContactByTypeQuery contactByTypeQuery = new ContactByTypeQuery(userId, "EMAIL"); + assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com")) + .collect(Collectors.toSet()), userProjection.handle(contactByTypeQuery)); + AddressByRegionQuery addressByRegionQuery = new AddressByRegionQuery(userId, "NY"); + assertEquals(Stream.of(new Address("New York", "NY", "10001")) + .collect(Collectors.toSet()), userProjection.handle(addressByRegionQuery)); + + } + +} diff --git a/patterns/cqrs-es/src/test/java/com/baeldung/patterns/crud/ApplicationUnitTest.java b/patterns/cqrs-es/src/test/java/com/baeldung/patterns/crud/ApplicationUnitTest.java new file mode 100644 index 0000000000..3fabfe405d --- /dev/null +++ b/patterns/cqrs-es/src/test/java/com/baeldung/patterns/crud/ApplicationUnitTest.java @@ -0,0 +1,48 @@ +package com.baeldung.patterns.crud; + +import static org.junit.Assert.assertEquals; + +import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.patterns.crud.repository.UserRepository; +import com.baeldung.patterns.crud.service.UserService; +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; + +public class ApplicationUnitTest { + + private UserRepository repository; + + @Before + public void setUp() { + repository = new UserRepository(); + } + + @Test + public void givenCRUDApplication_whenDataCreated_thenDataCanBeFetched() throws Exception { + UserService service = new UserService(repository); + String userId = UUID.randomUUID() + .toString(); + + service.createUser(userId, "Tom", "Sawyer"); + service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("EMAIL", "tom.sawyer@rediff.com"), new Contact("PHONE", "700-000-0001")) + .collect(Collectors.toSet()), + Stream.of(new Address("New York", "NY", "10001"), new Address("Los Angeles", "CA", "90001"), new Address("Housten", "TX", "77001")) + .collect(Collectors.toSet())); + service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("PHONE", "700-000-0001")) + .collect(Collectors.toSet()), + Stream.of(new Address("New York", "NY", "10001"), new Address("Housten", "TX", "77001")) + .collect(Collectors.toSet())); + + assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com")) + .collect(Collectors.toSet()), service.getContactByType(userId, "EMAIL")); + assertEquals(Stream.of(new Address("New York", "NY", "10001")) + .collect(Collectors.toSet()), service.getAddressByRegion(userId, "NY")); + } + +} diff --git a/patterns/cqrs-es/src/test/java/com/baeldung/patterns/es/ApplicationUnitTest.java b/patterns/cqrs-es/src/test/java/com/baeldung/patterns/es/ApplicationUnitTest.java new file mode 100644 index 0000000000..61e7b4c05a --- /dev/null +++ b/patterns/cqrs-es/src/test/java/com/baeldung/patterns/es/ApplicationUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.patterns.es; + +import static org.junit.Assert.assertEquals; + +import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; +import com.baeldung.patterns.es.repository.EventStore; +import com.baeldung.patterns.es.service.UserService; + +public class ApplicationUnitTest { + + private EventStore repository; + private UserService service; + + @Before + public void setUp() { + repository = new EventStore(); + service = new UserService(repository); + } + + @Test + public void givenCRUDApplication_whenDataCreated_thenDataCanBeFetched() throws Exception { + String userId = UUID.randomUUID() + .toString(); + + service.createUser(userId, "Tom", "Sawyer"); + service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("EMAIL", "tom.sawyer@rediff.com"), new Contact("PHONE", "700-000-0001")) + .collect(Collectors.toSet()), + Stream.of(new Address("New York", "NY", "10001"), new Address("Los Angeles", "CA", "90001"), new Address("Housten", "TX", "77001")) + .collect(Collectors.toSet())); + service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("PHONE", "700-000-0001")) + .collect(Collectors.toSet()), + Stream.of(new Address("New York", "NY", "10001"), new Address("Housten", "TX", "77001")) + .collect(Collectors.toSet())); + + assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com")) + .collect(Collectors.toSet()), service.getContactByType(userId, "EMAIL")); + assertEquals(Stream.of(new Address("New York", "NY", "10001")) + .collect(Collectors.toSet()), service.getAddressByRegion(userId, "NY")); + + } + +} diff --git a/patterns/cqrs-es/src/test/java/com/baeldung/patterns/escqrs/ApplicationUnitTest.java b/patterns/cqrs-es/src/test/java/com/baeldung/patterns/escqrs/ApplicationUnitTest.java new file mode 100644 index 0000000000..e0460b2f12 --- /dev/null +++ b/patterns/cqrs-es/src/test/java/com/baeldung/patterns/escqrs/ApplicationUnitTest.java @@ -0,0 +1,76 @@ +package com.baeldung.patterns.escqrs; + +import static org.junit.Assert.assertEquals; + +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.patterns.cqrs.commands.CreateUserCommand; +import com.baeldung.patterns.cqrs.commands.UpdateUserCommand; +import com.baeldung.patterns.cqrs.projections.UserProjection; +import com.baeldung.patterns.cqrs.queries.AddressByRegionQuery; +import com.baeldung.patterns.cqrs.queries.ContactByTypeQuery; +import com.baeldung.patterns.cqrs.repository.UserReadRepository; +import com.baeldung.patterns.domain.Address; +import com.baeldung.patterns.domain.Contact; +import com.baeldung.patterns.es.events.Event; +import com.baeldung.patterns.es.repository.EventStore; +import com.baeldung.patterns.escqrs.aggregates.UserAggregate; +import com.baeldung.patterns.escqrs.projectors.UserProjector; + +public class ApplicationUnitTest { + + private EventStore writeRepository; + private UserReadRepository readRepository; + private UserProjector projector; + private UserAggregate userAggregate; + private UserProjection userProjection; + + @Before + public void setUp() { + writeRepository = new EventStore(); + readRepository = new UserReadRepository(); + projector = new UserProjector(readRepository); + userAggregate = new UserAggregate(writeRepository); + userProjection = new UserProjection(readRepository); + } + + @Test + public void givenCQRSApplication_whenCommandRun_thenQueryShouldReturnResult() throws Exception { + String userId = UUID.randomUUID() + .toString(); + List events = null; + CreateUserCommand createUserCommand = new CreateUserCommand(userId, "Kumar", "Chandrakant"); + events = userAggregate.handleCreateUserCommand(createUserCommand); + + projector.project(userId, events); + + UpdateUserCommand updateUserCommand = new UpdateUserCommand(userId, Stream.of(new Address("New York", "NY", "10001"), new Address("Los Angeles", "CA", "90001")) + .collect(Collectors.toSet()), + Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("EMAIL", "tom.sawyer@rediff.com")) + .collect(Collectors.toSet())); + events = userAggregate.handleUpdateUserCommand(updateUserCommand); + projector.project(userId, events); + + updateUserCommand = new UpdateUserCommand(userId, Stream.of(new Address("New York", "NY", "10001"), new Address("Housten", "TX", "77001")) + .collect(Collectors.toSet()), + Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("PHONE", "700-000-0001")) + .collect(Collectors.toSet())); + events = userAggregate.handleUpdateUserCommand(updateUserCommand); + projector.project(userId, events); + + ContactByTypeQuery contactByTypeQuery = new ContactByTypeQuery(userId, "EMAIL"); + assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com")) + .collect(Collectors.toSet()), userProjection.handle(contactByTypeQuery)); + AddressByRegionQuery addressByRegionQuery = new AddressByRegionQuery(userId, "NY"); + assertEquals(Stream.of(new Address("New York", "NY", "10001")) + .collect(Collectors.toSet()), userProjection.handle(addressByRegionQuery)); + + } + +} diff --git a/patterns/pom.xml b/patterns/pom.xml index 4c17055231..e1753aba56 100644 --- a/patterns/pom.xml +++ b/patterns/pom.xml @@ -5,13 +5,11 @@ patterns patterns pom - com.baeldung parent-modules 1.0.0-SNAPSHOT - design-patterns-architectural design-patterns-behavioral @@ -21,11 +19,11 @@ design-patterns-functional design-patterns-structural dip + cqrs-es front-controller intercepting-filter solid - @@ -36,7 +34,6 @@ - @@ -53,9 +50,7 @@ - 9.4.0.v20161208 - - + \ No newline at end of file diff --git a/patterns/solid/README.md b/patterns/solid/README.md index ddd2f78b7e..cae346e3c9 100644 --- a/patterns/solid/README.md +++ b/patterns/solid/README.md @@ -1,5 +1,5 @@ ### Relevant Articles: - [A Solid Guide to Solid Principles](https://www.baeldung.com/solid-principles) - +- [Single Responsibility Principle in Java](https://www.baeldung.com/java-single-responsibility-principle) diff --git a/patterns/solid/src/main/java/com/baeldung/o/AbstractCalculatorOperation.java b/patterns/solid/src/main/java/com/baeldung/o/AbstractCalculatorOperation.java new file mode 100644 index 0000000000..798d5e2d95 --- /dev/null +++ b/patterns/solid/src/main/java/com/baeldung/o/AbstractCalculatorOperation.java @@ -0,0 +1,7 @@ +package com.baeldung.o; + +public abstract class AbstractCalculatorOperation { + + abstract void perform(); + +} diff --git a/patterns/solid/src/main/java/com/baeldung/o/Addition.java b/patterns/solid/src/main/java/com/baeldung/o/Addition.java new file mode 100644 index 0000000000..1ad99aad4b --- /dev/null +++ b/patterns/solid/src/main/java/com/baeldung/o/Addition.java @@ -0,0 +1,41 @@ +package com.baeldung.o; + +public class Addition implements CalculatorOperation { + private double left; + private double right; + private double result = 0.0; + + public Addition(double left, double right) { + this.left = left; + this.right = right; + } + + public double getLeft() { + return left; + } + + public void setLeft(double left) { + this.left = left; + } + + public double getRight() { + return right; + } + + public void setRight(double right) { + this.right = right; + } + + public double getResult() { + return result; + } + + public void setResult(double result) { + this.result = result; + } + + @Override + public void perform() { + result = left + right; + } +} diff --git a/patterns/solid/src/main/java/com/baeldung/o/Calculator.java b/patterns/solid/src/main/java/com/baeldung/o/Calculator.java new file mode 100644 index 0000000000..11ace8eb20 --- /dev/null +++ b/patterns/solid/src/main/java/com/baeldung/o/Calculator.java @@ -0,0 +1,14 @@ +package com.baeldung.o; + +import java.security.InvalidParameterException; + +public class Calculator { + + public void calculate(CalculatorOperation operation) { + if (operation == null) { + throw new InvalidParameterException("Can not perform operation"); + } + + operation.perform(); + } +} diff --git a/patterns/solid/src/main/java/com/baeldung/o/CalculatorOperation.java b/patterns/solid/src/main/java/com/baeldung/o/CalculatorOperation.java new file mode 100644 index 0000000000..46648e25d8 --- /dev/null +++ b/patterns/solid/src/main/java/com/baeldung/o/CalculatorOperation.java @@ -0,0 +1,7 @@ +package com.baeldung.o; + +public interface CalculatorOperation { + + void perform(); + +} diff --git a/patterns/solid/src/main/java/com/baeldung/o/Division.java b/patterns/solid/src/main/java/com/baeldung/o/Division.java new file mode 100644 index 0000000000..fbb8a1f523 --- /dev/null +++ b/patterns/solid/src/main/java/com/baeldung/o/Division.java @@ -0,0 +1,43 @@ +package com.baeldung.o; + +public class Division implements CalculatorOperation { + private double left; + private double right; + private double result = 0.0; + + public Division(double left, double right) { + this.left = left; + this.right = right; + } + + public double getLeft() { + return left; + } + + public void setLeft(double left) { + this.left = left; + } + + public double getRight() { + return right; + } + + public void setRight(double right) { + this.right = right; + } + + public double getResult() { + return result; + } + + public void setResult(double result) { + this.result = result; + } + + @Override + public void perform() { + if (right != 0) { + result = left / right; + } + } +} diff --git a/patterns/solid/src/main/java/com/baeldung/o/Subtraction.java b/patterns/solid/src/main/java/com/baeldung/o/Subtraction.java new file mode 100644 index 0000000000..5827d33568 --- /dev/null +++ b/patterns/solid/src/main/java/com/baeldung/o/Subtraction.java @@ -0,0 +1,41 @@ +package com.baeldung.o; + +public class Subtraction implements CalculatorOperation { + private double left; + private double right; + private double result = 0.0; + + public Subtraction(double left, double right) { + this.left = left; + this.right = right; + } + + public double getLeft() { + return left; + } + + public void setLeft(double left) { + this.left = left; + } + + public double getRight() { + return right; + } + + public void setRight(double right) { + this.right = right; + } + + public double getResult() { + return result; + } + + public void setResult(double result) { + this.result = result; + } + + @Override + public void perform() { + result = left - right; + } +} diff --git a/patterns/solid/src/main/java/com/baeldung/s/TextManipulator.java b/patterns/solid/src/main/java/com/baeldung/s/TextManipulator.java index a6b32a0ff9..d084a78815 100644 --- a/patterns/solid/src/main/java/com/baeldung/s/TextManipulator.java +++ b/patterns/solid/src/main/java/com/baeldung/s/TextManipulator.java @@ -15,16 +15,18 @@ public class TextManipulator { text = text.concat(newText); } - public void findWordAndReplace(String word, String replacementWord) { + public String findWordAndReplace(String word, String replacementWord) { if (text.contains(word)) { text = text.replace(word, replacementWord); - } else System.out.println("Word you want to replace is not found in the text"); + } + return text; } - public void findWordAndDelete(String word) { + public String findWordAndDelete(String word) { if (text.contains(word)) { text = text.replace(word, ""); - } else System.out.println("Word you want to delete is not found in the text"); + } + return text; } /* diff --git a/patterns/solid/src/main/test/com/baeldung/o/CalculatorUnitTest.java b/patterns/solid/src/main/test/com/baeldung/o/CalculatorUnitTest.java new file mode 100644 index 0000000000..3ae894e963 --- /dev/null +++ b/patterns/solid/src/main/test/com/baeldung/o/CalculatorUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.o; + +import static org.junit.Assert.assertEquals; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class CalculatorUnitTest { + + private static final double RIGHT = 10.0; + private static final double LEFT = 20.0; + private static final double SUM = 30.0; + private static final double SUBTRACTION_RESULT = 10.0; + private static final double DIVISION_RESULT = 2.0; + + private Calculator calculator; + + @BeforeEach + public void setUp() { + calculator = new Calculator(); + } + + @Test + public void whenAddTwoNumber_returnSum() { + Addition addition = new Addition(RIGHT, LEFT); + calculator.calculate(addition); + assertEquals(SUM, addition.getResult(), 0.0); + } + + @Test + public void whenSutractTwoNumber_returnCorrectResult() { + Subtraction subtraction = new Subtraction(LEFT, RIGHT); + calculator.calculate(subtraction); + assertEquals(SUBTRACTION_RESULT, subtraction.getResult(), 0.0); + } + + @Test + public void whenDivideTwoNumber_returnCorrectResult() { + Division division = new Division(LEFT, RIGHT); + calculator.calculate(division); + assertEquals(DIVISION_RESULT, division.getResult(), 0.0); + } + +} diff --git a/pdf/pom.xml b/pdf/pom.xml index d148aa1670..463c88948d 100644 --- a/pdf/pom.xml +++ b/pdf/pom.xml @@ -60,6 +60,16 @@ poi-ooxml ${poi-ooxml.version} + + org.thymeleaf + thymeleaf + 3.0.11.RELEASE + + + org.xhtmlrenderer + flying-saucer-pdf + 9.1.20 + diff --git a/pdf/src/main/java/com/baeldung/pdf/PDFThymeleafExample.java b/pdf/src/main/java/com/baeldung/pdf/PDFThymeleafExample.java new file mode 100644 index 0000000000..2e1df1d320 --- /dev/null +++ b/pdf/src/main/java/com/baeldung/pdf/PDFThymeleafExample.java @@ -0,0 +1,48 @@ +package com.baeldung.pdf; + +import com.lowagie.text.DocumentException; +import org.thymeleaf.TemplateEngine; +import org.thymeleaf.context.Context; +import org.thymeleaf.templatemode.TemplateMode; +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; +import org.xhtmlrenderer.pdf.ITextRenderer; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class PDFThymeleafExample { + + public static void main(String[] args) throws IOException, DocumentException { + PDFThymeleafExample thymeleaf2Pdf = new PDFThymeleafExample(); + String html = thymeleaf2Pdf.parseThymeleafTemplate(); + thymeleaf2Pdf.generatePdfFromHtml(html); + } + + public void generatePdfFromHtml(String html) throws IOException, DocumentException { + String outputFolder = System.getProperty("user.home") + File.separator + "thymeleaf.pdf"; + OutputStream outputStream = new FileOutputStream(outputFolder); + + ITextRenderer renderer = new ITextRenderer(); + renderer.setDocumentFromString(html); + renderer.layout(); + renderer.createPDF(outputStream); + + outputStream.close(); + } + + private String parseThymeleafTemplate() { + ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode(TemplateMode.HTML); + + TemplateEngine templateEngine = new TemplateEngine(); + templateEngine.setTemplateResolver(templateResolver); + + Context context = new Context(); + context.setVariable("to", "Baeldung.com"); + + return templateEngine.process("thymeleaf_template", context); + } +} diff --git a/pdf/src/main/resources/thymeleaf_template.html b/pdf/src/main/resources/thymeleaf_template.html new file mode 100644 index 0000000000..3e856367cc --- /dev/null +++ b/pdf/src/main/resources/thymeleaf_template.html @@ -0,0 +1,7 @@ + + +

+ +

+ + \ No newline at end of file diff --git a/pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java b/pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java new file mode 100644 index 0000000000..e253dce06c --- /dev/null +++ b/pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.pdf; + +import com.lowagie.text.DocumentException; +import org.junit.Test; +import org.thymeleaf.TemplateEngine; +import org.thymeleaf.context.Context; +import org.thymeleaf.templatemode.TemplateMode; +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; +import org.xhtmlrenderer.pdf.ITextRenderer; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import static org.junit.Assert.assertTrue; + +public class PDFThymeleafUnitTest { + + @Test + public void givenThymeleafTemplate_whenParsedAndRenderedToPDF_thenItShouldNotBeEmpty() throws DocumentException, IOException { + String html = parseThymeleafTemplate(); + + ByteArrayOutputStream outputStream = generatePdfOutputStreamFromHtml(html); + + assertTrue(outputStream.size() > 0); + } + + private ByteArrayOutputStream generatePdfOutputStreamFromHtml(String html) throws IOException, DocumentException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + ITextRenderer renderer = new ITextRenderer(); + renderer.setDocumentFromString(html); + renderer.layout(); + renderer.createPDF(outputStream); + + outputStream.close(); + return outputStream; + } + + private String parseThymeleafTemplate() { + ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode(TemplateMode.HTML); + + TemplateEngine templateEngine = new TemplateEngine(); + templateEngine.setTemplateResolver(templateResolver); + + Context context = new Context(); + context.setVariable("to", "Baeldung.com"); + + return templateEngine.process("thymeleaf_template", context); + } +} diff --git a/persistence-modules/core-java-persistence/src/main/java/com/baeldung/connectionpool/BasicConnectionPool.java b/persistence-modules/core-java-persistence/src/main/java/com/baeldung/connectionpool/BasicConnectionPool.java index 289db18c53..22ef0e5411 100644 --- a/persistence-modules/core-java-persistence/src/main/java/com/baeldung/connectionpool/BasicConnectionPool.java +++ b/persistence-modules/core-java-persistence/src/main/java/com/baeldung/connectionpool/BasicConnectionPool.java @@ -14,7 +14,8 @@ public class BasicConnectionPool implements ConnectionPool { private final List connectionPool; private final List usedConnections = new ArrayList<>(); private static final int INITIAL_POOL_SIZE = 10; - private final int MAX_POOL_SIZE = 20; + private static final int MAX_POOL_SIZE = 20; + private static final int MAX_TIMEOUT = 5; public static BasicConnectionPool create(String url, String user, String password) throws SQLException { List pool = new ArrayList<>(INITIAL_POOL_SIZE); @@ -42,6 +43,11 @@ public class BasicConnectionPool implements ConnectionPool { } Connection connection = connectionPool.remove(connectionPool.size() - 1); + + if(!connection.isValid(MAX_TIMEOUT)){ + connection = createConnection(url, user, password); + } + usedConnections.add(connection); return connection; } diff --git a/persistence-modules/hibernate-jpa/README.md b/persistence-modules/hibernate-jpa/README.md index fb48f975bc..514fcedb8a 100644 --- a/persistence-modules/hibernate-jpa/README.md +++ b/persistence-modules/hibernate-jpa/README.md @@ -14,3 +14,4 @@ This module contains articles specific to use of Hibernate as a JPA implementati - [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception) - [JPA/Hibernate Persistence Context](https://www.baeldung.com/jpa-hibernate-persistence-context) - [Quick Guide to EntityManager#getReference()](https://www.baeldung.com/jpa-entity-manager-get-reference) +- [Hibernate Error “No Persistence Provider for EntityManager”](https://www.baeldung.com/hibernate-no-persistence-provider) diff --git a/persistence-modules/hibernate-libraries/create-database.sh b/persistence-modules/hibernate-libraries/create-database.sh new file mode 100755 index 0000000000..da55dc917d --- /dev/null +++ b/persistence-modules/hibernate-libraries/create-database.sh @@ -0,0 +1,7 @@ +#!/bin/bash +docker run \ + -p 53306:3306 \ + --name=mysql57-hibernate-types \ + -e MYSQL_ALLOW_EMPTY_PASSWORD=true \ + -v "${PWD}/docker/docker-entrypoint-initdb.d":/docker-entrypoint-initdb.d \ + -d mysql:5.7 diff --git a/persistence-modules/hibernate-libraries/docker-compose.yml b/persistence-modules/hibernate-libraries/docker-compose.yml new file mode 100644 index 0000000000..3ea9fef2e7 --- /dev/null +++ b/persistence-modules/hibernate-libraries/docker-compose.yml @@ -0,0 +1,19 @@ +version: '3.2' + +services: + mysql: + image: mysql:5.7 + container_name: mysql57 + restart: unless-stopped + ports: + - 53306:3306 + environment: + - MYSQL_ALLOW_EMPTY_PASSWORD=true + volumes : + - ./docker/etc/mysql/conf.d:/etc/mysql/conf.d + - ./docker/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d + logging: + driver: "json-file" + options: + max-size: "50m" + max-file: "1" diff --git a/persistence-modules/hibernate-libraries/docker/docker-entrypoint-initdb.d/init-db.sql b/persistence-modules/hibernate-libraries/docker/docker-entrypoint-initdb.d/init-db.sql new file mode 100644 index 0000000000..1df234f5a1 --- /dev/null +++ b/persistence-modules/hibernate-libraries/docker/docker-entrypoint-initdb.d/init-db.sql @@ -0,0 +1,3 @@ +CREATE DATABASE hibernate_types; +use hibernate_types; +GRANT ALL PRIVILEGES ON hibernate_types.* TO 'mysql'@'%' IDENTIFIED BY 'admin'; diff --git a/persistence-modules/hibernate-libraries/pom.xml b/persistence-modules/hibernate-libraries/pom.xml new file mode 100644 index 0000000000..ea2dda7e88 --- /dev/null +++ b/persistence-modules/hibernate-libraries/pom.xml @@ -0,0 +1,188 @@ + + + 4.0.0 + hibernate-libraries + 0.0.1-SNAPSHOT + hibernate-libraries + Introduction into hibernate types library + + + com.baeldung + persistence-modules + 1.0.0-SNAPSHOT + + + + + com.vladmihalcea + hibernate-types-52 + ${hibernate-types.version} + + + org.springframework.boot + spring-boot-starter-data-jpa + ${spring-boot.version} + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot.version} + test + + + org.junit.vintage + junit-vintage-engine + + + + + org.springframework.boot + spring-boot-devtools + ${spring-boot.version} + runtime + true + + + org.hibernate + hibernate-core + ${hibernate.version} + provided + + + org.hibernate + hibernate-ehcache + ${hibernate.version} + test + + + org.hibernate + hibernate-testing + ${hibernate.version} + test + + + org.assertj + assertj-core + ${assertj-core.version} + test + + + mysql + mysql-connector-java + ${mysql.version} + runtime + + + org.slf4j + slf4j-api + ${slf4j.version} + provided + true + + + ch.qos.logback + logback-classic + ${logback.version} + provided + true + + + javax.xml.bind + jaxb-api + ${jaxb.version} + + + org.javassist + javassist + ${javassist.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + provided + true + + + com.google.guava + guava + ${guava.version} + provided + true + + + net.ttddyy + datasource-proxy + ${datasource-proxy.version} + test + + + com.integralblue + log4jdbc-spring-boot-starter + ${log4jdbc.version} + + + + + hibernate-types + + + src/main/resources + true + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + none + + + **/*IntegrationTest.java + + + + + + + + + + + + 3.15.0 + 1.6 + 29.0-jre + 2.9.7 + 5.4.14.Final + 2.10.3 + 1.8 + 3.27.0-GA + 2.3.1 + 2.0.0 + 1.2.3 + 3.0.2 + 2.22.2 + 3.8.1 + 3.8.1 + 8.0.19 + 1.7.30 + 2.1.3.RELEASE + + + diff --git a/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/Album.java b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/Album.java new file mode 100644 index 0000000000..f18818c3a9 --- /dev/null +++ b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/Album.java @@ -0,0 +1,34 @@ +package com.baeldung.hibernate.types; + +import org.hibernate.annotations.Type; + +import javax.persistence.*; +import java.util.List; + +@Entity(name = "Album") +@Table(name = "album") +public class Album extends BaseEntity { + @Type(type = "json") + @Column(columnDefinition = "json") + private CoverArt coverArt; + + @OneToMany(fetch = FetchType.EAGER) + private List songs; + + public CoverArt getCoverArt() { + return coverArt; + } + + public void setCoverArt(CoverArt coverArt) { + this.coverArt = coverArt; + } + + + public List getSongs() { + return songs; + } + + public void setSong(List songs) { + this.songs = songs; + } +} diff --git a/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/AlbumRepository.java b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/AlbumRepository.java new file mode 100644 index 0000000000..d89542de46 --- /dev/null +++ b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/AlbumRepository.java @@ -0,0 +1,8 @@ +package com.baeldung.hibernate.types; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AlbumRepository extends CrudRepository { +} diff --git a/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/Artist.java b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/Artist.java new file mode 100644 index 0000000000..8f3ccb44c5 --- /dev/null +++ b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/Artist.java @@ -0,0 +1,72 @@ +package com.baeldung.hibernate.types; + +import java.io.Serializable; + +public class Artist implements Serializable { + + private String name; + private String country; + private String genre; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getGenre() { + return genre; + } + + public void setGenre(String genre) { + this.genre = genre; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((country == null) ? 0 : country.hashCode()); + result = prime * result + ((genre == null) ? 0 : genre.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Artist other = (Artist) obj; + if (country == null) { + if (other.country != null) + return false; + } else if (!country.equals(other.country)) + return false; + if (genre == null) { + if (other.genre != null) + return false; + } else if (!genre.equals(other.genre)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + } diff --git a/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/BaseEntity.java b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/BaseEntity.java new file mode 100644 index 0000000000..3e0fbc7595 --- /dev/null +++ b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/BaseEntity.java @@ -0,0 +1,37 @@ +package com.baeldung.hibernate.types; + +import com.vladmihalcea.hibernate.type.json.JsonBinaryType; +import com.vladmihalcea.hibernate.type.json.JsonStringType; +import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.TypeDefs; + +import javax.persistence.*; + +@TypeDefs({ + @TypeDef(name = "json", typeClass = JsonStringType.class), + @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class) +}) +@MappedSuperclass +public class BaseEntity { + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + @Column(name = "id", unique = true, nullable = false) + long id; + String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/CoverArt.java b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/CoverArt.java new file mode 100644 index 0000000000..bd71edc53c --- /dev/null +++ b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/CoverArt.java @@ -0,0 +1,71 @@ +package com.baeldung.hibernate.types; + +import java.io.Serializable; + +public class CoverArt implements Serializable { + + private String frontCoverArtUrl; + private String backCoverArtUrl; + private String upcCode; + + public String getFrontCoverArtUrl() { + return frontCoverArtUrl; + } + + public void setFrontCoverArtUrl(String frontCoverArtUrl) { + this.frontCoverArtUrl = frontCoverArtUrl; + } + + public String getBackCoverArtUrl() { + return backCoverArtUrl; + } + + public void setBackCoverArtUrl(String backCoverArtUrl) { + this.backCoverArtUrl = backCoverArtUrl; + } + + public String getUpcCode() { + return upcCode; + } + + public void setUpcCode(String upcCode) { + this.upcCode = upcCode; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((backCoverArtUrl == null) ? 0 : backCoverArtUrl.hashCode()); + result = prime * result + ((frontCoverArtUrl == null) ? 0 : frontCoverArtUrl.hashCode()); + result = prime * result + ((upcCode == null) ? 0 : upcCode.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CoverArt other = (CoverArt) obj; + if (backCoverArtUrl == null) { + if (other.backCoverArtUrl != null) + return false; + } else if (!backCoverArtUrl.equals(other.backCoverArtUrl)) + return false; + if (frontCoverArtUrl == null) { + if (other.frontCoverArtUrl != null) + return false; + } else if (!frontCoverArtUrl.equals(other.frontCoverArtUrl)) + return false; + if (upcCode == null) { + if (other.upcCode != null) + return false; + } else if (!upcCode.equals(other.upcCode)) + return false; + return true; + } +} diff --git a/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/HibernateTypesApplication.java b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/HibernateTypesApplication.java new file mode 100644 index 0000000000..ac379f9ee2 --- /dev/null +++ b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/HibernateTypesApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.hibernate.types; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class HibernateTypesApplication { + + public static void main(String[] args) { + SpringApplication.run(HibernateTypesApplication.class, args); + } + +} diff --git a/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/Song.java b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/Song.java new file mode 100644 index 0000000000..2d22296024 --- /dev/null +++ b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/Song.java @@ -0,0 +1,57 @@ +package com.baeldung.hibernate.types; + +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; + +import java.time.YearMonth; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +import com.vladmihalcea.hibernate.type.basic.YearMonthIntegerType; + +@Entity(name = "Song") +@Table(name = "song") +@TypeDef( + typeClass = YearMonthIntegerType.class, + defaultForType = YearMonth.class +) +public class Song extends BaseEntity { + + private Long length = 0L; + + @Type(type = "json") + @Column(columnDefinition = "json") + private Artist artist; + + @Column( + name = "recorded_on", + columnDefinition = "mediumint" + ) + private YearMonth recordedOn = YearMonth.now(); + + public Long getLength() { + return length; + } + + public void setLength(Long length) { + this.length = length; + } + + public Artist getArtist() { + return artist; + } + + public void setArtist(Artist artist) { + this.artist = artist; + } + + public YearMonth getRecordedOn() { + return recordedOn; + } + + public void setRecordedOn(YearMonth recordedOn) { + this.recordedOn = recordedOn; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/SongRepository.java b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/SongRepository.java new file mode 100644 index 0000000000..e79e88108b --- /dev/null +++ b/persistence-modules/hibernate-libraries/src/main/java/com/baeldung/hibernate/types/SongRepository.java @@ -0,0 +1,8 @@ +package com.baeldung.hibernate.types; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface SongRepository extends CrudRepository { +} diff --git a/persistence-modules/hibernate-libraries/src/main/resources/application.properties b/persistence-modules/hibernate-libraries/src/main/resources/application.properties new file mode 100644 index 0000000000..9588139eb0 --- /dev/null +++ b/persistence-modules/hibernate-libraries/src/main/resources/application.properties @@ -0,0 +1,14 @@ +log4jdbc.dump.sql.addsemicolon=true +log4jdbc.dump.sql.maxlinelength=0 +log4jdbc.trim.sql.extrablanklines=false +logging.level.jdbc.audit=fatal +logging.level.jdbc.connection=fatal +logging.level.jdbc.resultset=fatal +logging.level.jdbc.resultsettable=info +logging.level.jdbc.sqlonly=fatal +logging.level.jdbc.sqltiming=info +spring.datasource.url=jdbc:mysql://localhost:53306/hibernate_types?serverTimezone=UTC&useSSL=false +spring.datasource.username=root +spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect +spring.jpa.hibernate.ddl-auto=create +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect diff --git a/persistence-modules/hibernate-libraries/src/main/resources/hibernate-types.properties b/persistence-modules/hibernate-libraries/src/main/resources/hibernate-types.properties new file mode 100644 index 0000000000..226b50fafd --- /dev/null +++ b/persistence-modules/hibernate-libraries/src/main/resources/hibernate-types.properties @@ -0,0 +1 @@ +hibernate.types.print.banner=false diff --git a/persistence-modules/hibernate-libraries/src/test/java/com/baeldung/hibernate/types/HibernateTypesIntegrationTest.java b/persistence-modules/hibernate-libraries/src/test/java/com/baeldung/hibernate/types/HibernateTypesIntegrationTest.java new file mode 100644 index 0000000000..9d7479e77d --- /dev/null +++ b/persistence-modules/hibernate-libraries/src/test/java/com/baeldung/hibernate/types/HibernateTypesIntegrationTest.java @@ -0,0 +1,181 @@ +package com.baeldung.hibernate.types; + +import com.google.common.collect.Lists; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.time.Duration; +import java.time.YearMonth; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +public class HibernateTypesIntegrationTest { + + @Autowired + AlbumRepository albumRepository; + + @Autowired + SongRepository songRepository; + + private void deleteAll() { + albumRepository.deleteAll(); + songRepository.deleteAll(); + } + + @BeforeEach + public void setUp() { + deleteAll(); + } + + @BeforeEach + public void tearDown() { + setUp(); + } + + @Test + void whenSavingHibernateTypes_thenTheCorrectJsonIsStoredInTheDatabase() { + Album emptyAlbum = new Album(); + emptyAlbum = albumRepository.save(emptyAlbum); + + Song emptySong = new Song(); + emptySong = songRepository.save(emptySong); + + Artist superstarArtist = new Artist(); + superstarArtist.setCountry("England"); + superstarArtist.setGenre("Pop"); + superstarArtist.setName("Superstar"); + + Song aHappySong = new Song(); + aHappySong.setArtist(superstarArtist); + aHappySong.setName("A Happy Song"); + aHappySong.setLength(Duration.ofMinutes(4).getSeconds()); + aHappySong = songRepository.save(aHappySong); + + Song aSadSong = new Song(); + aSadSong.setArtist(superstarArtist); + aSadSong.setName("A Sad Song"); + aSadSong.setLength(Duration.ofMinutes(2).getSeconds()); + aSadSong = songRepository.save(aSadSong); + + Song anotherHappySong = new Song(); + anotherHappySong.setArtist(superstarArtist); + anotherHappySong.setName("Another Happy Song"); + anotherHappySong.setLength(Duration.ofMinutes(3).getSeconds()); + anotherHappySong = songRepository.save(anotherHappySong); + + Artist newcomer = new Artist(); + newcomer.setCountry("Jamaica"); + newcomer.setGenre("Reggae"); + newcomer.setName("Newcomer"); + + Song aNewSong = new Song(); + aNewSong.setArtist(newcomer); + aNewSong.setName("A New Song"); + aNewSong.setLength(Duration.ofMinutes(5).getSeconds()); + aNewSong = songRepository.save(aNewSong); + + CoverArt superstarAlbumCoverArt = new CoverArt(); + superstarAlbumCoverArt.setUpcCode(UUID.randomUUID().toString()); + superstarAlbumCoverArt.setFrontCoverArtUrl("http://fakeurl-0"); + superstarAlbumCoverArt.setBackCoverArtUrl("http://fakeurl-1"); + + Album superstarAlbum = new Album(); + superstarAlbum.setCoverArt(superstarAlbumCoverArt); + superstarAlbum.setName("The Superstar Album"); + superstarAlbum.setSong(Lists.newArrayList(aHappySong, aSadSong, anotherHappySong)); + superstarAlbum = albumRepository.save(superstarAlbum); + + CoverArt newcomerAlbumCoverArt = new CoverArt(); + newcomerAlbumCoverArt.setUpcCode(UUID.randomUUID().toString()); + newcomerAlbumCoverArt.setFrontCoverArtUrl("http://fakeurl-2"); + newcomerAlbumCoverArt.setBackCoverArtUrl("http://fakeurl-3"); + + Album newcomerAlbum = new Album(); + newcomerAlbum.setCoverArt(newcomerAlbumCoverArt); + newcomerAlbum.setName("The Newcomer Album"); + newcomerAlbum.setSong(Lists.newArrayList(aNewSong)); + albumRepository.save(newcomerAlbum); + + Iterable selectAlbumsQueryResult = albumRepository.findAll(); + assertThat(selectAlbumsQueryResult).hasSize(3); + + Iterable selectSongsQueryResult = songRepository.findAll(); + assertThat(selectSongsQueryResult).hasSize(5); + + Album selectAlbumQueryResult; + + selectAlbumQueryResult = albumRepository.findById(emptyAlbum.getId()).get(); + assertThat(selectAlbumQueryResult.getName()).isNull(); + assertThat(selectAlbumQueryResult.getCoverArt()).isNull(); + assertThat(selectAlbumQueryResult.getSongs()).isNullOrEmpty(); + + selectAlbumQueryResult = albumRepository.findById(superstarAlbum.getId()).get(); + assertThat(selectAlbumQueryResult.getName()).isEqualTo("The Superstar Album"); + assertThat(selectAlbumQueryResult.getCoverArt().getFrontCoverArtUrl()).isEqualTo("http://fakeurl-0"); + assertThat(selectAlbumQueryResult.getCoverArt().getBackCoverArtUrl()).isEqualTo("http://fakeurl-1"); + assertThat(selectAlbumQueryResult.getSongs()).hasSize(3); + assertThat(selectAlbumQueryResult.getSongs()).usingFieldByFieldElementComparator().containsExactlyInAnyOrder(aHappySong, aSadSong, anotherHappySong); + + selectAlbumQueryResult = albumRepository.findById(newcomerAlbum.getId()).get(); + assertThat(selectAlbumQueryResult.getName()).isEqualTo("The Newcomer Album"); + assertThat(selectAlbumQueryResult.getCoverArt().getFrontCoverArtUrl()).isEqualTo("http://fakeurl-2"); + assertThat(selectAlbumQueryResult.getCoverArt().getBackCoverArtUrl()).isEqualTo("http://fakeurl-3"); + assertThat(selectAlbumQueryResult.getSongs()).hasSize(1); + assertThat(selectAlbumQueryResult.getSongs()).usingFieldByFieldElementComparator().containsExactlyInAnyOrder(aNewSong); + + Song selectSongQueryResult; + + selectSongQueryResult = songRepository.findById(emptySong.getId()).get(); + assertThat(selectSongQueryResult.getName()).isNull(); + assertThat(selectSongQueryResult.getLength()).isZero(); + assertThat(selectSongQueryResult.getArtist()).isNull(); + + selectSongQueryResult = songRepository.findById(aHappySong.getId()).get(); + assertThat(selectSongQueryResult.getName()).isEqualTo("A Happy Song"); + assertThat(selectSongQueryResult.getLength()).isEqualTo(Duration.ofMinutes(4).getSeconds()); + assertThat(selectSongQueryResult.getArtist().getName()).isEqualTo("Superstar"); + assertThat(selectSongQueryResult.getArtist().getGenre()).isEqualTo("Pop"); + assertThat(selectSongQueryResult.getArtist().getCountry()).isEqualTo("England"); + + selectSongQueryResult = songRepository.findById(aSadSong.getId()).get(); + assertThat(selectSongQueryResult.getName()).isEqualTo("A Sad Song"); + assertThat(selectSongQueryResult.getLength()).isEqualTo(Duration.ofMinutes(2).getSeconds()); + assertThat(selectSongQueryResult.getArtist().getName()).isEqualTo("Superstar"); + assertThat(selectSongQueryResult.getArtist().getGenre()).isEqualTo("Pop"); + assertThat(selectSongQueryResult.getArtist().getCountry()).isEqualTo("England"); + + selectSongQueryResult = songRepository.findById(anotherHappySong.getId()).get(); + assertThat(selectSongQueryResult.getName()).isEqualTo("Another Happy Song"); + assertThat(selectSongQueryResult.getLength()).isEqualTo(Duration.ofMinutes(3).getSeconds()); + assertThat(selectSongQueryResult.getArtist().getName()).isEqualTo("Superstar"); + assertThat(selectSongQueryResult.getArtist().getGenre()).isEqualTo("Pop"); + assertThat(selectSongQueryResult.getArtist().getCountry()).isEqualTo("England"); + + selectSongQueryResult = songRepository.findById(aNewSong.getId()).get(); + assertThat(selectSongQueryResult.getName()).isEqualTo("A New Song"); + assertThat(selectSongQueryResult.getLength()).isEqualTo(Duration.ofMinutes(5).getSeconds()); + assertThat(selectSongQueryResult.getArtist().getName()).isEqualTo("Newcomer"); + assertThat(selectSongQueryResult.getArtist().getGenre()).isEqualTo("Reggae"); + assertThat(selectSongQueryResult.getArtist().getCountry()).isEqualTo("Jamaica"); + } + + @Test + void whenSavingAHibernateTypeYearMonth_thenTheCorrectValueIsStoredInTheDatabase() { + Song mySong = new Song(); + YearMonth now = YearMonth.of(2019, 12); + mySong.setArtist(new Artist()); + mySong.setName("My Song"); + mySong.setLength(Duration.ofMinutes(1).getSeconds()); + mySong.setRecordedOn(now); + mySong = songRepository.save(mySong); + + Song selectSongQueryResult; + selectSongQueryResult = songRepository.findById(mySong.getId()).get(); + assertThat(selectSongQueryResult.getRecordedOn().getYear()).isEqualTo(2019); + assertThat(selectSongQueryResult.getRecordedOn().getMonthValue()).isEqualTo(12); + } +} diff --git a/persistence-modules/java-jpa-2/README.md b/persistence-modules/java-jpa-2/README.md index 9d46c0d814..4b822c4782 100644 --- a/persistence-modules/java-jpa-2/README.md +++ b/persistence-modules/java-jpa-2/README.md @@ -12,4 +12,5 @@ This module contains articles about the Java Persistence API (JPA) in Java. - [Combining JPA And/Or Criteria Predicates](https://www.baeldung.com/jpa-and-or-criteria-predicates) - [JPA Annotation for the PostgreSQL TEXT Type](https://www.baeldung.com/jpa-annotation-postgresql-text-type) - [Mapping a Single Entity to Multiple Tables in JPA](https://www.baeldung.com/jpa-mapping-single-entity-to-multiple-tables) +- [Constructing a JPA Query Between Unrelated Entities](https://www.baeldung.com/jpa-query-unrelated-entities) - More articles: [[<-- prev]](/java-jpa) diff --git a/persistence-modules/java-mongodb/README.md b/persistence-modules/java-mongodb/README.md index 5c3c448b03..b79cea6781 100644 --- a/persistence-modules/java-mongodb/README.md +++ b/persistence-modules/java-mongodb/README.md @@ -11,3 +11,4 @@ This module contains articles about MongoDB in Java. - [Introduction to Morphia – Java ODM for MongoDB](https://www.baeldung.com/mongodb-morphia) - [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations) - [MongoDB BSON to JSON](https://www.baeldung.com/bson-to-json) +- [BSON to JSON Document Conversion in Java](https://www.baeldung.com/java-convert-bson-to-json) diff --git a/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Book.java b/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Book.java new file mode 100644 index 0000000000..44e4ecb539 --- /dev/null +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Book.java @@ -0,0 +1,168 @@ +package com.baeldung.bsontojson; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.HashSet; +import java.util.Set; + +import dev.morphia.annotations.Embedded; +import dev.morphia.annotations.Entity; +import dev.morphia.annotations.Field; +import dev.morphia.annotations.Id; +import dev.morphia.annotations.Index; +import dev.morphia.annotations.IndexOptions; +import dev.morphia.annotations.Indexes; +import dev.morphia.annotations.Property; +import dev.morphia.annotations.Reference; +import dev.morphia.annotations.Validation; + +@Entity("Books") +@Indexes({ @Index(fields = @Field("title"), options = @IndexOptions(name = "book_title")) }) +@Validation("{ price : { $gt : 0 } }") +public class Book { + @Id + private String isbn; + @Property + private String title; + private String author; + @Embedded + private Publisher publisher; + @Property("price") + private double cost; + @Reference + private Set companionBooks; + @Property + private LocalDateTime publishDate; + + public Book() { + + } + + public Book(String isbn, String title, String author, double cost, Publisher publisher) { + this.isbn = isbn; + this.title = title; + this.author = author; + this.cost = cost; + this.publisher = publisher; + this.companionBooks = new HashSet<>(); + } + + // Getters and setters ... + public String getIsbn() { + return isbn; + } + + public Book setIsbn(String isbn) { + this.isbn = isbn; + return this; + } + + public String getTitle() { + return title; + } + + public Book setTitle(String title) { + this.title = title; + return this; + } + + public String getAuthor() { + return author; + } + + public Book setAuthor(String author) { + this.author = author; + return this; + } + + public Publisher getPublisher() { + return publisher; + } + + public Book setPublisher(Publisher publisher) { + this.publisher = publisher; + return this; + } + + public double getCost() { + return cost; + } + + public Book setCost(double cost) { + this.cost = cost; + return this; + } + + public LocalDateTime getPublishDate() { + return publishDate; + } + + public Book setPublishDate(LocalDateTime publishDate) { + this.publishDate = publishDate; + return this; + } + + public Set getCompanionBooks() { + return companionBooks; + } + + public Book addCompanionBooks(Book book) { + if (companionBooks != null) + this.companionBooks.add(book); + return this; + } + + @Override + public String toString() { + return "Book [isbn=" + isbn + ", title=" + title + ", author=" + author + ", publisher=" + publisher + ", cost=" + cost + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((author == null) ? 0 : author.hashCode()); + long temp; + temp = Double.doubleToLongBits(cost); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + ((isbn == null) ? 0 : isbn.hashCode()); + result = prime * result + ((publisher == null) ? 0 : publisher.hashCode()); + result = prime * result + ((title == null) ? 0 : title.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Book other = (Book) obj; + if (author == null) { + if (other.author != null) + return false; + } else if (!author.equals(other.author)) + return false; + if (Double.doubleToLongBits(cost) != Double.doubleToLongBits(other.cost)) + return false; + if (isbn == null) { + if (other.isbn != null) + return false; + } else if (!isbn.equals(other.isbn)) + return false; + if (publisher == null) { + if (other.publisher != null) + return false; + } else if (!publisher.equals(other.publisher)) + return false; + if (title == null) { + if (other.title != null) + return false; + } else if (!title.equals(other.title)) + return false; + return true; + } + +} diff --git a/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Publisher.java b/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Publisher.java new file mode 100644 index 0000000000..1ab262c82b --- /dev/null +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Publisher.java @@ -0,0 +1,70 @@ +package com.baeldung.bsontojson; + +import org.bson.types.ObjectId; + +import dev.morphia.annotations.Entity; +import dev.morphia.annotations.Id; + +@Entity +public class Publisher { + + @Id + private ObjectId id; + private String name; + + public Publisher() { + + } + + public Publisher(ObjectId id, String name) { + this.id = id; + this.name = name; + } + + public ObjectId getId() { + return id; + } + + public void setId(ObjectId id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Catalog [id=" + id + ", name=" + name + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Publisher other = (Publisher) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + +} diff --git a/persistence-modules/java-mongodb/src/test/java/com/baeldung/bsontojson/BsonToJsonIntegrationTest.java b/persistence-modules/java-mongodb/src/test/java/com/baeldung/bsontojson/BsonToJsonLiveTest.java similarity index 84% rename from persistence-modules/java-mongodb/src/test/java/com/baeldung/bsontojson/BsonToJsonIntegrationTest.java rename to persistence-modules/java-mongodb/src/test/java/com/baeldung/bsontojson/BsonToJsonLiveTest.java index e382ea4ab2..12053523f8 100644 --- a/persistence-modules/java-mongodb/src/test/java/com/baeldung/bsontojson/BsonToJsonIntegrationTest.java +++ b/persistence-modules/java-mongodb/src/test/java/com/baeldung/bsontojson/BsonToJsonLiveTest.java @@ -1,142 +1,128 @@ -package com.baeldung.bsontojson; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.sql.Timestamp; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.Date; -import java.util.TimeZone; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.bson.Document; -import org.bson.json.Converter; -import org.bson.json.JsonMode; -import org.bson.json.JsonWriterSettings; -import org.bson.json.StrictJsonWriter; -import org.bson.types.ObjectId; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.jupiter.api.AfterEach; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.baeldung.morphia.domain.Book; -import com.baeldung.morphia.domain.Publisher; -import com.mongodb.MongoClient; -import com.mongodb.client.MongoDatabase; - -import dev.morphia.Datastore; -import dev.morphia.Morphia; - -public class BsonToJsonIntegrationTest { - - private static final String DB_NAME = "library"; - private static Datastore datastore; - - @BeforeClass - public static void setUp() { - Morphia morphia = new Morphia(); - morphia.mapPackage("com.baeldung.morphia"); - datastore = morphia.createDatastore(new MongoClient(), DB_NAME); - datastore.ensureIndexes(); - - datastore.save(new Book() - .setIsbn("isbn") - .setTitle("title") - .setAuthor("author") - .setCost(3.95) - .setPublisher(new Publisher(new ObjectId("fffffffffffffffffffffffa"),"publisher")) - .setPublishDate(LocalDateTime.parse("2020-01-01T18:13:32Z", DateTimeFormatter.ISO_DATE_TIME)) - .addCompanionBooks(new Book().setIsbn("isbn2"))); - } - - @AfterClass - public static void tearDown() { - datastore.delete(datastore.createQuery(Book.class)); - } - - @Test - public void givenBsonDocument_whenUsingStandardJsonTransformation_thenJsonDateIsObjectEpochTime() { - - String json = null; - try (MongoClient mongoClient = new MongoClient()) { - MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME); - Document bson = mongoDatabase.getCollection("Books").find().first(); - json = bson.toJson(); - } - - String expectedJson = "{\"_id\": \"isbn\", " + - "\"className\": \"com.baeldung.morphia.domain.Book\", " + - "\"title\": \"title\", " + - "\"author\": \"author\", " + - "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + - "\"name\": \"publisher\"}, " + - "\"price\": 3.95, " + - "\"publishDate\": {\"$date\": 1577898812000}}"; - - assertNotNull(json); - - assertEquals(expectedJson, json); - } - - - @Test - public void givenBsonDocument_whenUsingRelaxedJsonTransformation_thenJsonDateIsObjectIsoDate() { - - String json = null; - try (MongoClient mongoClient = new MongoClient()) { - MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME); - Document bson = mongoDatabase.getCollection("Books").find().first(); - json = bson.toJson(JsonWriterSettings - .builder() - .outputMode(JsonMode.RELAXED) - .build()); - } - - String expectedJson = "{\"_id\": \"isbn\", " + - "\"className\": \"com.baeldung.morphia.domain.Book\", " + - "\"title\": \"title\", " + - "\"author\": \"author\", " + - "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + - "\"name\": \"publisher\"}, " + - "\"price\": 3.95, " + - "\"publishDate\": {\"$date\": \"2020-01-01T17:13:32Z\"}}"; - - assertNotNull(json); - - assertEquals(expectedJson, json); - } - - @Test - public void givenBsonDocument_whenUsingCustomJsonTransformation_thenJsonDateIsStringField() { - - String json = null; - try (MongoClient mongoClient = new MongoClient()) { - MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME); - Document bson = mongoDatabase.getCollection("Books").find().first(); - json = bson.toJson(JsonWriterSettings - .builder() - .dateTimeConverter(new JsonDateTimeConverter()) - .build()); - } - - String expectedJson = "{\"_id\": \"isbn\", " + - "\"className\": \"com.baeldung.morphia.domain.Book\", " + - "\"title\": \"title\", " + - "\"author\": \"author\", " + - "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + - "\"name\": \"publisher\"}, " + - "\"price\": 3.95, " + - "\"publishDate\": \"2020-01-01T17:13:32Z\"}"; - - assertEquals(expectedJson, json); - - } - -} +package com.baeldung.bsontojson; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import org.bson.Document; +import org.bson.json.JsonMode; +import org.bson.json.JsonWriterSettings; +import org.bson.types.ObjectId; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.mongodb.MongoClient; +import com.mongodb.client.MongoDatabase; + +import dev.morphia.Datastore; +import dev.morphia.Morphia; + +public class BsonToJsonLiveTest { + + private static final String DB_NAME = "library"; + private static Datastore datastore; + + @BeforeClass + public static void setUp() { + Morphia morphia = new Morphia(); + morphia.mapPackage("com.baeldung.bsontojson"); + datastore = morphia.createDatastore(new MongoClient(), DB_NAME); + datastore.ensureIndexes(); + + datastore.save(new Book() + .setIsbn("isbn") + .setTitle("title") + .setAuthor("author") + .setCost(3.95) + .setPublisher(new Publisher(new ObjectId("fffffffffffffffffffffffa"),"publisher")) + .setPublishDate(LocalDateTime.parse("2020-01-01T18:13:32Z", DateTimeFormatter.ISO_DATE_TIME)) + .addCompanionBooks(new Book().setIsbn("isbn2"))); + } + + @AfterClass + public static void tearDown() { + datastore.delete(datastore.createQuery(Book.class)); + } + + @Test + public void givenBsonDocument_whenUsingStandardJsonTransformation_thenJsonDateIsObjectEpochTime() { + + String json = null; + try (MongoClient mongoClient = new MongoClient()) { + MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME); + Document bson = mongoDatabase.getCollection("Books").find().first(); + json = bson.toJson(); + } + + String expectedJson = "{\"_id\": \"isbn\", " + + "\"className\": \"com.baeldung.bsontojson.Book\", " + + "\"title\": \"title\", " + + "\"author\": \"author\", " + + "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + + "\"name\": \"publisher\"}, " + + "\"price\": 3.95, " + + "\"publishDate\": {\"$date\": 1577898812000}}"; + + assertNotNull(json); + + assertEquals(expectedJson, json); + } + + + @Test + public void givenBsonDocument_whenUsingRelaxedJsonTransformation_thenJsonDateIsObjectIsoDate() { + + String json = null; + try (MongoClient mongoClient = new MongoClient()) { + MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME); + Document bson = mongoDatabase.getCollection("Books").find().first(); + json = bson.toJson(JsonWriterSettings + .builder() + .outputMode(JsonMode.RELAXED) + .build()); + } + + String expectedJson = "{\"_id\": \"isbn\", " + + "\"className\": \"com.baeldung.bsontojson.Book\", " + + "\"title\": \"title\", " + + "\"author\": \"author\", " + + "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + + "\"name\": \"publisher\"}, " + + "\"price\": 3.95, " + + "\"publishDate\": {\"$date\": \"2020-01-01T17:13:32Z\"}}"; + + assertNotNull(json); + + assertEquals(expectedJson, json); + } + + @Test + public void givenBsonDocument_whenUsingCustomJsonTransformation_thenJsonDateIsStringField() { + + String json = null; + try (MongoClient mongoClient = new MongoClient()) { + MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME); + Document bson = mongoDatabase.getCollection("Books").find().first(); + json = bson.toJson(JsonWriterSettings + .builder() + .dateTimeConverter(new JsonDateTimeConverter()) + .build()); + } + + String expectedJson = "{\"_id\": \"isbn\", " + + "\"className\": \"com.baeldung.bsontojson.Book\", " + + "\"title\": \"title\", " + + "\"author\": \"author\", " + + "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + + "\"name\": \"publisher\"}, " + + "\"price\": 3.95, " + + "\"publishDate\": \"2020-01-01T17:13:32Z\"}"; + + assertEquals(expectedJson, json); + + } + +} diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index ec7f0bcec2..a03ba1ec5d 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -24,6 +24,7 @@ hibernate-mapping hibernate-ogm hibernate-annotations + hibernate-libraries hibernate-jpa hibernate-queries hibernate-enterprise @@ -61,6 +62,7 @@ spring-data-jpa-2 spring-data-jpa-3 spring-data-jpa-4 + spring-data-jpa-5 spring-data-keyvalue spring-data-mongodb spring-data-neo4j diff --git a/persistence-modules/redis/README.md b/persistence-modules/redis/README.md index 668b8d33f8..71d009241a 100644 --- a/persistence-modules/redis/README.md +++ b/persistence-modules/redis/README.md @@ -3,3 +3,4 @@ - [A Guide to Redis with Redisson](http://www.baeldung.com/redis-redisson) - [Introduction to Lettuce – the Java Redis Client](https://www.baeldung.com/java-redis-lettuce) - [List All Available Redis Keys](https://www.baeldung.com/redis-list-available-keys) +- [Spring Data Redis’s Property-Based Configuration](https://www.baeldung.com/spring-data-redis-properties) diff --git a/persistence-modules/redis/pom.xml b/persistence-modules/redis/pom.xml index fffcfbb96b..c9206e5f92 100644 --- a/persistence-modules/redis/pom.xml +++ b/persistence-modules/redis/pom.xml @@ -9,16 +9,31 @@ com.baeldung - persistence-modules - 1.0.0-SNAPSHOT + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-data-redis + + redis.clients jedis - ${jedis.version} - + com.github.kstyrc embedded-redis @@ -32,15 +47,12 @@ io.lettuce lettuce-core - ${lettuce-core.version} - + - - 3.2.0 + 0.6 - 3.3.0 - 5.0.1.RELEASE + 3.3.0 diff --git a/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/RedisConfig.java b/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/RedisConfig.java new file mode 100644 index 0000000000..ad43ad8159 --- /dev/null +++ b/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/RedisConfig.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.redis.configuration; + +import org.springframework.boot.autoconfigure.data.redis.RedisProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; + +import com.baeldung.spring.redis.configuration.entity.Book; + +@Configuration +@EnableConfigurationProperties(RedisProperties.class) +public class RedisConfig { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(connectionFactory); + // Add some specific configuration here. Key serializers, etc. + return template; + } + +} diff --git a/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/SpringRedisConfigurationApplication.java b/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/SpringRedisConfigurationApplication.java new file mode 100644 index 0000000000..932a6b7478 --- /dev/null +++ b/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/SpringRedisConfigurationApplication.java @@ -0,0 +1,19 @@ +package com.baeldung.spring.redis.configuration; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringRedisConfigurationApplication implements CommandLineRunner { + + public static void main(String[] args) { + SpringApplication.run(SpringRedisConfigurationApplication.class, args); + } + + @Override + public void run(String... args) throws Exception { + + } + +} diff --git a/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/controller/BooksController.java b/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/controller/BooksController.java new file mode 100644 index 0000000000..b3faf030c9 --- /dev/null +++ b/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/controller/BooksController.java @@ -0,0 +1,32 @@ +package com.baeldung.spring.redis.configuration.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.spring.redis.configuration.entity.Book; +import com.baeldung.spring.redis.configuration.repository.BooksRepository; + +@RestController +@RequestMapping("/books") +public class BooksController { + + @Autowired + private BooksRepository repository; + + @PostMapping("/create") + public String newBook(@RequestBody Book newBook) { + repository.save(newBook); + return "Added"; + } + + @GetMapping("/get/{id}") + public Book findOne(@PathVariable Long id) { + return repository.findById(id); + } + +} diff --git a/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/entity/Book.java b/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/entity/Book.java new file mode 100644 index 0000000000..52d579d4fb --- /dev/null +++ b/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/entity/Book.java @@ -0,0 +1,46 @@ +package com.baeldung.spring.redis.configuration.entity; + +import java.io.Serializable; + +public class Book implements Serializable { + + private static final long serialVersionUID = 1L; + + private Long id; + + private String name; + + public Book() { + } + + public Book(String name) { + this.name = name; + } + + public Book(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Book [id=" + id + ", name=" + name + "]"; + } + +} diff --git a/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/repository/BooksRepository.java b/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/repository/BooksRepository.java new file mode 100644 index 0000000000..a6eba0e742 --- /dev/null +++ b/persistence-modules/redis/src/main/java/com/baeldung/spring/redis/configuration/repository/BooksRepository.java @@ -0,0 +1,25 @@ +package com.baeldung.spring.redis.configuration.repository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import com.baeldung.spring.redis.configuration.entity.Book; + +@Component +public class BooksRepository { + + @Autowired + private RedisTemplate redisTemplate; + + public void save(Book book) { + redisTemplate.opsForValue() + .set(book.getId(), book); + } + + public Book findById(Long id) { + return redisTemplate.opsForValue() + .get(id); + } + +} diff --git a/persistence-modules/redis/src/main/resources/application.properties b/persistence-modules/redis/src/main/resources/application.properties new file mode 100644 index 0000000000..e3140c4e29 --- /dev/null +++ b/persistence-modules/redis/src/main/resources/application.properties @@ -0,0 +1,9 @@ +spring.redis.database=0 +spring.redis.host=localhost +spring.redis.port=16379 +spring.redis.password=mypass +spring.redis.timeout=60000 +spring.redis.lettuce.pool.max-active=8 +spring.redis.lettuce.pool.max-idle=8 +spring.redis.lettuce.pool.max-wait=-1 +spring.redis.lettuce.pool.min-idle=0 \ No newline at end of file diff --git a/persistence-modules/redis/src/test/java/com/baeldung/spring/redis/configuration/controller/BooksControllerUnitTest.java b/persistence-modules/redis/src/test/java/com/baeldung/spring/redis/configuration/controller/BooksControllerUnitTest.java new file mode 100644 index 0000000000..a5c3340065 --- /dev/null +++ b/persistence-modules/redis/src/test/java/com/baeldung/spring/redis/configuration/controller/BooksControllerUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.spring.redis.configuration.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.mockito.Mockito.when; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.spring.redis.configuration.entity.Book; +import com.baeldung.spring.redis.configuration.repository.BooksRepository; + +@RunWith(SpringJUnit4ClassRunner.class) +public class BooksControllerUnitTest { + + @Spy + @InjectMocks + private BooksController booksController; + + @Mock + private BooksRepository booksRepository; + + private static final Long BOOK_ID = 1l; + private static final Long OTHER_BOOK_ID = 2l; + private static final String BOOK_NAME = "Ulysses"; + private static final String ADDED = "Added"; + + @Test + public void whenNewBook_thenCorrect() { + assertEquals(ADDED, booksController.newBook(new Book(BOOK_ID, BOOK_NAME))); + } + + @Test + public void whenFindOneFinds_thenCorrect() { + Book book = new Book(BOOK_ID, BOOK_NAME); + when(booksRepository.findById(BOOK_ID)).thenReturn(book); + assertSame(book, booksController.findOne(BOOK_ID)); + } + + @Test + public void whenFindOneNotFound_thenReturnsNull() { + Book book = new Book(BOOK_ID, BOOK_NAME); + when(booksRepository.findById(BOOK_ID)).thenReturn(book); + assertNull(booksController.findOne(OTHER_BOOK_ID)); + } + +} diff --git a/persistence-modules/redis/src/test/java/com/baeldung/spring/redis/configuration/repository/BooksRepositoryUnitTest.java b/persistence-modules/redis/src/test/java/com/baeldung/spring/redis/configuration/repository/BooksRepositoryUnitTest.java new file mode 100644 index 0000000000..1edf9c7e89 --- /dev/null +++ b/persistence-modules/redis/src/test/java/com/baeldung/spring/redis/configuration/repository/BooksRepositoryUnitTest.java @@ -0,0 +1,66 @@ +package com.baeldung.spring.redis.configuration.repository; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.spring.redis.configuration.entity.Book; + +@RunWith(SpringJUnit4ClassRunner.class) +public class BooksRepositoryUnitTest { + + @Spy + @InjectMocks + private BooksRepository booksRepository; + + @Mock + private RedisTemplate redisTemplate; + + @Mock + private ValueOperations valueOperations; + + private static final Long BOOK_ID = 1l; + private static final Long OTHER_BOOK_ID = 2l; + private static final String BOOK_NAME = "Ulysses"; + + @Test + public void whenSave_thenCorrect() { + Book book = new Book(BOOK_ID, BOOK_NAME); + when(redisTemplate.opsForValue()).thenReturn(valueOperations); + booksRepository.save(book); + verify(redisTemplate, times(1)).opsForValue(); + verify(valueOperations, times(1)).set(BOOK_ID, book); + } + + @Test + public void whenFindByIdFound_thenReturnsBook() { + Book book = new Book(BOOK_ID, BOOK_NAME); + when(redisTemplate.opsForValue()).thenReturn(valueOperations); + when(valueOperations.get(BOOK_ID)).thenReturn(book); + assertSame(book, booksRepository.findById(BOOK_ID)); + verify(redisTemplate, times(1)).opsForValue(); + verify(valueOperations, times(1)).get(BOOK_ID); + } + + @Test + public void whenFindByIdNotFound_thenReturnsNull() { + Book book = new Book(BOOK_ID, BOOK_NAME); + when(redisTemplate.opsForValue()).thenReturn(valueOperations); + when(valueOperations.get(BOOK_ID)).thenReturn(book); + assertNull(booksRepository.findById(OTHER_BOOK_ID)); + verify(redisTemplate, times(1)).opsForValue(); + verify(valueOperations, times(1)).get(OTHER_BOOK_ID); + } + +} diff --git a/persistence-modules/spring-boot-persistence-2/README.md b/persistence-modules/spring-boot-persistence-2/README.md index 5d171fb2ca..e0479ffe10 100644 --- a/persistence-modules/spring-boot-persistence-2/README.md +++ b/persistence-modules/spring-boot-persistence-2/README.md @@ -1,3 +1,8 @@ ### Relevant Articles: - [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi) +- [Configuring a Tomcat Connection Pool in Spring Boot](https://www.baeldung.com/spring-boot-tomcat-connection-pool) +- [Integrating Spring Boot with HSQLDB](https://www.baeldung.com/spring-boot-hsqldb) +- [List of In-Memory Databases](http://www.baeldung.com/java-in-memory-databases) +- [Oracle Connection Pooling With Spring](https://www.baeldung.com/spring-oracle-connection-pooling) +- More articles: [[<-- prev]](../spring-boot-persistence) diff --git a/persistence-modules/spring-boot-persistence-2/pom.xml b/persistence-modules/spring-boot-persistence-2/pom.xml index 9f456fa8af..f36d8fc43f 100644 --- a/persistence-modules/spring-boot-persistence-2/pom.xml +++ b/persistence-modules/spring-boot-persistence-2/pom.xml @@ -104,6 +104,38 @@ tomcat-jdbc + + mysql + mysql-connector-java + + + org.xerial + sqlite-jdbc + + + org.apache.derby + derby + + + org.hsqldb + hsqldb + + + + com.oracle.database.jdbc + ojdbc8 + ${oracle-database.version} + + + com.oracle.database.ha + ons + ${oracle-database.version} + + + com.oracle.database.jdbc + ucp + ${oracle-database.version} + @@ -112,23 +144,6 @@ org.springframework.boot spring-boot-maven-plugin - - - org.apache.maven.plugins - maven-compiler-plugin - - - com/baeldung/spring/oracle/pooling/configuration/OracleConfiguration.java - com/baeldung/spring/oracle/pooling/configuration/OracleUCPConfiguration.java - - - com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleLiveTest.java - com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleUCPLiveTest.java - - - @@ -136,6 +151,7 @@ 3.9.1 2.1.8.RELEASE 0.9.5.2 + 19.6.0.0 diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/TransactionalCompareApplication.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/TransactionalCompareApplication.java new file mode 100644 index 0000000000..7fee55be8a --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/TransactionalCompareApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.spring.transactional; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +class TransactionalCompareApplication { + + public static void main(String[] args) { + SpringApplication.run(TransactionalCompareApplication.class, args); + } +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/entity/Car.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/entity/Car.java new file mode 100644 index 0000000000..1219111ffa --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/entity/Car.java @@ -0,0 +1,60 @@ +package com.baeldung.spring.transactional.entity; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Car { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String make; + + private String model; + + public Car() { + } + + public Car(Long id, String make, String model) { + this.id = id; + this.make = make; + this.model = model; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + @Override + public String toString() { + return "Car{" + + "id=" + id + + ", make='" + make + '\'' + + ", model='" + model + '\'' + + '}'; + } +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/repository/CarRepository.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/repository/CarRepository.java new file mode 100644 index 0000000000..f8ecc8f550 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/repository/CarRepository.java @@ -0,0 +1,7 @@ +package com.baeldung.spring.transactional.repository; + +import com.baeldung.spring.transactional.entity.Car; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CarRepository extends JpaRepository { +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/service/CarService.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/service/CarService.java new file mode 100644 index 0000000000..0821ddb02b --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/service/CarService.java @@ -0,0 +1,25 @@ +package com.baeldung.spring.transactional.service; + +import com.baeldung.spring.transactional.entity.Car; +import com.baeldung.spring.transactional.repository.CarRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import javax.persistence.EntityExistsException; + +@Service +@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.SUPPORTS, readOnly = false, timeout = 30) +public class CarService { + + @Autowired + private CarRepository carRepository; + + @Transactional(rollbackFor = IllegalArgumentException.class, noRollbackFor = EntityExistsException.class, + rollbackForClassName = "IllegalArgumentException", noRollbackForClassName = "EntityExistsException") + public Car save(Car car) { + return carRepository.save(car); + } +} diff --git a/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/service/RentalService.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/service/RentalService.java new file mode 100644 index 0000000000..0aa0815a98 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/spring/transactional/service/RentalService.java @@ -0,0 +1,22 @@ +package com.baeldung.spring.transactional.service; + +import com.baeldung.spring.transactional.entity.Car; +import com.baeldung.spring.transactional.repository.CarRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.persistence.EntityExistsException; +import javax.transaction.Transactional; + +@Service +@Transactional(Transactional.TxType.SUPPORTS) +public class RentalService { + + @Autowired + private CarRepository carRepository; + + @Transactional(rollbackOn = IllegalArgumentException.class, dontRollbackOn = EntityExistsException.class) + public Car rent(Car car) { + return carRepository.save(car); + } +} diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/Application.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/Application.java similarity index 96% rename from persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/Application.java rename to persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/Application.java index c5a5c291c6..bde8346482 100644 --- a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/Application.java +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/Application.java @@ -1,12 +1,12 @@ -package com.baeldung.springboothsqldb.application; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class Application { - - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } -} +package com.baeldung.springboothsqldb.application; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/controllers/CustomerController.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/controllers/CustomerController.java similarity index 92% rename from persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/controllers/CustomerController.java rename to persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/controllers/CustomerController.java index 8229a1d1c0..045612a86f 100644 --- a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/controllers/CustomerController.java +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/controllers/CustomerController.java @@ -1,33 +1,32 @@ -package com.baeldung.springboothsqldb.application.controllers; - -import com.baeldung.springboothsqldb.application.entities.Customer; -import com.baeldung.springboothsqldb.application.repositories.CustomerRepository; -import java.util.List; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class CustomerController { - - private final CustomerRepository customerRepository; - - @Autowired - public CustomerController(CustomerRepository customerRepository) { - this.customerRepository = customerRepository; - } - - @PostMapping("/customers") - public Customer addCustomer(@RequestBody Customer customer) { - customerRepository.save(customer); - return customer; - } - - @GetMapping("/customers") - public List getCustomers() { - return (List) customerRepository.findAll(); - } -} +package com.baeldung.springboothsqldb.application.controllers; + +import com.baeldung.springboothsqldb.application.entities.Customer; +import com.baeldung.springboothsqldb.application.repositories.CustomerRepository; +import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class CustomerController { + + private final CustomerRepository customerRepository; + + @Autowired + public CustomerController(CustomerRepository customerRepository) { + this.customerRepository = customerRepository; + } + + @PostMapping("/customers") + public Customer addCustomer(@RequestBody Customer customer) { + customerRepository.save(customer); + return customer; + } + + @GetMapping("/customers") + public List getCustomers() { + return (List) customerRepository.findAll(); + } +} diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/entities/Customer.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/entities/Customer.java similarity index 95% rename from persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/entities/Customer.java rename to persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/entities/Customer.java index 636a80e272..ee2735abb8 100644 --- a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/entities/Customer.java +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/entities/Customer.java @@ -1,55 +1,55 @@ -package com.baeldung.springboothsqldb.application.entities; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; - -@Entity -@Table(name = "customers") -public class Customer { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - - private String name; - private String email; - - public Customer() {} - - public Customer(String name, String email) { - this.name = name; - this.email = email; - } - - public void setId(long id) { - this.id = id; - } - - public long getId() { - return id; - } - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setEmail(String email) { - this.email = email; - } - - public String getEmail() { - return email; - } - - @Override - public String toString() { - return "Customer{" + "id=" + id + ", name=" + name + ", email=" + email + '}'; - } -} +package com.baeldung.springboothsqldb.application.entities; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "customers") +public class Customer { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + private String name; + private String email; + + public Customer() {} + + public Customer(String name, String email) { + this.name = name; + this.email = email; + } + + public void setId(long id) { + this.id = id; + } + + public long getId() { + return id; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getEmail() { + return email; + } + + @Override + public String toString() { + return "Customer{" + "id=" + id + ", name=" + name + ", email=" + email + '}'; + } +} diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/repositories/CustomerRepository.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/repositories/CustomerRepository.java similarity index 97% rename from persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/repositories/CustomerRepository.java rename to persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/repositories/CustomerRepository.java index a81aa62c43..cc8a045e83 100644 --- a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/springboothsqldb/application/repositories/CustomerRepository.java +++ b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/springboothsqldb/application/repositories/CustomerRepository.java @@ -1,8 +1,8 @@ -package com.baeldung.springboothsqldb.application.repositories; - -import com.baeldung.springboothsqldb.application.entities.Customer; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface CustomerRepository extends CrudRepository {} +package com.baeldung.springboothsqldb.application.repositories; + +import com.baeldung.springboothsqldb.application.entities.Customer; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CustomerRepository extends CrudRepository {} diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/SpringBootConsoleApplication.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/tomcatconnectionpool/application/SpringBootConsoleApplication.java similarity index 100% rename from persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/SpringBootConsoleApplication.java rename to persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/tomcatconnectionpool/application/SpringBootConsoleApplication.java diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/entities/Customer.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/tomcatconnectionpool/application/entities/Customer.java similarity index 100% rename from persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/entities/Customer.java rename to persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/tomcatconnectionpool/application/entities/Customer.java diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/repositories/CustomerRepository.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/tomcatconnectionpool/application/repositories/CustomerRepository.java similarity index 100% rename from persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/repositories/CustomerRepository.java rename to persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/tomcatconnectionpool/application/repositories/CustomerRepository.java diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/runners/CommandLineCrudRunner.java b/persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/tomcatconnectionpool/application/runners/CommandLineCrudRunner.java similarity index 100% rename from persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/runners/CommandLineCrudRunner.java rename to persistence-modules/spring-boot-persistence-2/src/main/java/com/baeldung/tomcatconnectionpool/application/runners/CommandLineCrudRunner.java diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-derby.properties b/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-derby.properties new file mode 100644 index 0000000000..5b5ff05236 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-derby.properties @@ -0,0 +1,8 @@ +jdbc.driverClassName=org.apache.derby.jdbc.EmbeddedDriver +jdbc.url=jdbc:derby:memory:myD;create=true +jdbc.user=sa +jdbc.pass= + +hibernate.dialect=org.hibernate.dialect.DerbyDialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-generic-entity.properties b/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-generic-entity.properties new file mode 100644 index 0000000000..b19304cb1f --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-generic-entity.properties @@ -0,0 +1,8 @@ +jdbc.driverClassName=org.h2.Driver +jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 +jdbc.user=sa +jdbc.pass=sa + +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-hsqldb.properties b/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-hsqldb.properties new file mode 100644 index 0000000000..d045a8b7e5 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-hsqldb.properties @@ -0,0 +1,8 @@ +jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver +jdbc.url=jdbc:hsqldb:mem:myDb +jdbc.user=sa +jdbc.pass= + +hibernate.dialect=org.hibernate.dialect.HSQLDialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-sqlite.properties b/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-sqlite.properties new file mode 100644 index 0000000000..ee16081603 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-2/src/main/resources/persistence-sqlite.properties @@ -0,0 +1,7 @@ +jdbc.driverClassName=org.sqlite.JDBC +jdbc.url=jdbc:sqlite:memory:myDb?cache=shared +jdbc.user=sa +jdbc.pass=sa +hibernate.dialect=com.baeldung.dialect.SQLiteDialect +hibernate.hbm2ddl.auto=create-drop +hibernate.show_sql=true diff --git a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springboothsqldb/application/tests/CustomerControllerUnitTest.java b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/springboothsqldb/application/tests/CustomerControllerUnitTest.java similarity index 97% rename from persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springboothsqldb/application/tests/CustomerControllerUnitTest.java rename to persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/springboothsqldb/application/tests/CustomerControllerUnitTest.java index 33891e3d43..be16f8f563 100644 --- a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/springboothsqldb/application/tests/CustomerControllerUnitTest.java +++ b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/springboothsqldb/application/tests/CustomerControllerUnitTest.java @@ -1,61 +1,61 @@ -package com.baeldung.springboothsqldb.application.tests; - -import com.baeldung.springboothsqldb.application.entities.Customer; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; -import com.fasterxml.jackson.databind.SerializationFeature; -import java.nio.charset.Charset; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.MediaType; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import org.springframework.test.web.servlet.result.MockMvcResultMatchers; - -@RunWith(SpringRunner.class) -@SpringBootTest -@AutoConfigureMockMvc -public class CustomerControllerUnitTest { - - private static MediaType MEDIA_TYPE_JSON; - - @Autowired - private MockMvc mockMvc; - - @Before - public void setUpJsonMediaType() { - MEDIA_TYPE_JSON = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); - - } - - @Test - public void whenPostHttpRequesttoCustomers_thenStatusOK() throws Exception { - Customer customer = new Customer("John", "john@domain.com"); - ObjectMapper mapper = new ObjectMapper(); - mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); - ObjectWriter objectWriter = mapper.writer().withDefaultPrettyPrinter(); - String requestJson = objectWriter.writeValueAsString(customer); - - this.mockMvc - .perform(MockMvcRequestBuilders.post("/customers") - .contentType(MEDIA_TYPE_JSON) - .content(requestJson) - ) - - .andExpect(MockMvcResultMatchers.status().isOk()); - } - - @Test - public void whenGetHttpRequesttoCustomers_thenStatusOK() throws Exception { - this.mockMvc - .perform(MockMvcRequestBuilders.get("/customers")) - - .andExpect(MockMvcResultMatchers.content().contentType(MEDIA_TYPE_JSON)) - .andExpect(MockMvcResultMatchers.status().isOk()); - } -} +package com.baeldung.springboothsqldb.application.tests; + +import com.baeldung.springboothsqldb.application.entities.Customer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.databind.SerializationFeature; +import java.nio.charset.Charset; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +public class CustomerControllerUnitTest { + + private static MediaType MEDIA_TYPE_JSON; + + @Autowired + private MockMvc mockMvc; + + @Before + public void setUpJsonMediaType() { + MEDIA_TYPE_JSON = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); + + } + + @Test + public void whenPostHttpRequesttoCustomers_thenStatusOK() throws Exception { + Customer customer = new Customer("John", "john@domain.com"); + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); + ObjectWriter objectWriter = mapper.writer().withDefaultPrettyPrinter(); + String requestJson = objectWriter.writeValueAsString(customer); + + this.mockMvc + .perform(MockMvcRequestBuilders.post("/customers") + .contentType(MEDIA_TYPE_JSON) + .content(requestJson) + ) + + .andExpect(MockMvcResultMatchers.status().isOk()); + } + + @Test + public void whenGetHttpRequesttoCustomers_thenStatusOK() throws Exception { + this.mockMvc + .perform(MockMvcRequestBuilders.get("/customers")) + + .andExpect(MockMvcResultMatchers.content().contentType(MEDIA_TYPE_JSON)) + .andExpect(MockMvcResultMatchers.status().isOk()); + } +} diff --git a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java similarity index 100% rename from persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java rename to persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java diff --git a/persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql b/persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql index a0d0eaf62e..8d7db6c9f3 100644 --- a/persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql +++ b/persistence-modules/spring-boot-persistence-2/src/test/resources/schema.sql @@ -1,3 +1,6 @@ +drop table if exists car_maker; +drop table if exists car_model; + -- -- Car makers table -- diff --git a/persistence-modules/spring-boot-persistence/README.MD b/persistence-modules/spring-boot-persistence/README.MD index 96eb326cbe..d6ef239448 100644 --- a/persistence-modules/spring-boot-persistence/README.MD +++ b/persistence-modules/spring-boot-persistence/README.MD @@ -3,10 +3,8 @@ - [Spring Boot with Multiple SQL Import Files](http://www.baeldung.com/spring-boot-sql-import-files) - [Configuring Separate Spring DataSource for Tests](http://www.baeldung.com/spring-testing-separate-data-source) - [Quick Guide on Loading Initial Data with Spring Boot](http://www.baeldung.com/spring-boot-data-sql-and-schema-sql) -- [Configuring a Tomcat Connection Pool in Spring Boot](https://www.baeldung.com/spring-boot-tomcat-connection-pool) -- [Hibernate Field Naming with Spring Boot](https://www.baeldung.com/hibernate-field-naming-spring-boot) -- [Integrating Spring Boot with HSQLDB](https://www.baeldung.com/spring-boot-hsqldb) - [Configuring a DataSource Programmatically in Spring Boot](https://www.baeldung.com/spring-boot-configure-data-source-programmatic) - [Resolving “Failed to Configure a DataSource” Error](https://www.baeldung.com/spring-boot-failed-to-configure-data-source) +- [Hibernate Field Naming with Spring Boot](https://www.baeldung.com/hibernate-field-naming-spring-boot) - [Spring Boot with Hibernate](https://www.baeldung.com/spring-boot-hibernate) -- [List of In-Memory Databases](http://www.baeldung.com/java-in-memory-databases) \ No newline at end of file +- More articles: [[more -->]](../spring-boot-persistence-2) \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-4/README.md b/persistence-modules/spring-data-jpa-4/README.md index 3884435f75..085dfcb366 100644 --- a/persistence-modules/spring-data-jpa-4/README.md +++ b/persistence-modules/spring-data-jpa-4/README.md @@ -6,6 +6,7 @@ - [JPA Entity Lifecycle Events](https://www.baeldung.com/jpa-entity-lifecycle-events) - [Working with Lazy Element Collections in JPA](https://www.baeldung.com/java-jpa-lazy-collections) - [Calling Stored Procedures from Spring Data JPA Repositories](https://www.baeldung.com/spring-data-jpa-stored-procedures) +- [Custom Naming Convention with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-custom-naming) ### Eclipse Config After importing the project into Eclipse, you may see the following error: diff --git a/persistence-modules/spring-data-jpa-5/README.md b/persistence-modules/spring-data-jpa-5/README.md new file mode 100644 index 0000000000..fa29c82bac --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/README.md @@ -0,0 +1,14 @@ +### Relevant Articles: + +- [Spring JPA @Embedded and @EmbeddedId](https://www.baeldung.com/spring-jpa-embedded-method-parameters) +- [Generate Database Schema with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-generate-db-schema) + +### Eclipse Config +After importing the project into Eclipse, you may see the following error: +"No persistence xml file found in project" + +This can be ignored: +- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project" +Or: +- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator + diff --git a/persistence-modules/spring-data-jpa-5/pom.xml b/persistence-modules/spring-data-jpa-5/pom.xml new file mode 100644 index 0000000000..3053384559 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + spring-data-jpa-5 + spring-data-jpa-5 + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + + com.h2database + h2 + + + + diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/BookApplication.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/BookApplication.java new file mode 100644 index 0000000000..52f06058aa --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/BookApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.composite; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class BookApplication { + + public static void main(String[] args) { + SpringApplication.run(BookApplication.class); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/Book.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/Book.java new file mode 100644 index 0000000000..e4f1727654 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/Book.java @@ -0,0 +1,47 @@ +package com.baeldung.composite.entity; + +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; + +@Entity +public class Book { + + @EmbeddedId + private BookId id; + private String genre; + private Integer price; + + public Book() { + } + + public Book(String author, String name, String genre, Integer price) { + BookId id = new BookId(author, name); + this.id = id; + this.genre = genre; + this.price = price; + } + + public BookId getId() { + return id; + } + + public void setId(BookId id) { + this.id = id; + } + + public String getGenre() { + return genre; + } + + public void setGenre(String genre) { + this.genre = genre; + } + + public Integer getPrice() { + return price; + } + + public void setPrice(Integer price) { + this.price = price; + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/BookId.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/BookId.java new file mode 100644 index 0000000000..1524452412 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/entity/BookId.java @@ -0,0 +1,51 @@ +package com.baeldung.composite.entity; + +import javax.persistence.Embeddable; +import java.io.Serializable; +import java.util.Objects; + +@Embeddable +public class BookId implements Serializable { + + private String author; + private String name; + + public BookId() { + } + + public BookId(String author, String name) { + this.author = author; + this.name = name; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + BookId bookId = (BookId) o; + return Objects.equals(author, bookId.author) && Objects.equals(name, bookId.name); + } + + @Override + public int hashCode() { + return Objects.hash(author, name); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/repository/BookRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/repository/BookRepository.java new file mode 100644 index 0000000000..d5993eaf79 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/composite/repository/BookRepository.java @@ -0,0 +1,18 @@ +package com.baeldung.composite.repository; + +import com.baeldung.composite.entity.Book; +import com.baeldung.composite.entity.BookId; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface BookRepository extends JpaRepository { + + List findByIdName(String name); + + List findByIdAuthor(String author); + + List findByGenre(String genre); +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/AccountApplication.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/AccountApplication.java new file mode 100644 index 0000000000..547992a6c1 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/AccountApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.schemageneration; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class AccountApplication { + + public static void main(String[] args) { + SpringApplication.run(AccountApplication.class, args); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/HibernateUtil.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/HibernateUtil.java new file mode 100644 index 0000000000..7d69d65705 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/HibernateUtil.java @@ -0,0 +1,39 @@ +package com.baeldung.schemageneration; + +import com.baeldung.schemageneration.model.Account; +import com.baeldung.schemageneration.model.AccountSetting; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.Environment; +import org.hibernate.tool.hbm2ddl.SchemaExport; +import org.hibernate.tool.schema.TargetType; + +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; + +public class HibernateUtil { + + /** + * Generates database create commands for the specified entities using Hibernate native API, SchemaExport. + * Creation commands are exported into the create.sql file. + */ + public static void generateSchema() { + Map settings = new HashMap<>(); + settings.put(Environment.URL, "jdbc:h2:mem:schema"); + + StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(settings).build(); + + MetadataSources metadataSources = new MetadataSources(serviceRegistry); + metadataSources.addAnnotatedClass(Account.class); + metadataSources.addAnnotatedClass(AccountSetting.class); + Metadata metadata = metadataSources.buildMetadata(); + + SchemaExport schemaExport = new SchemaExport(); + schemaExport.setFormat(true); + schemaExport.setOutputFile("create.sql"); + schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), metadata); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/model/Account.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/model/Account.java new file mode 100644 index 0000000000..785e275e26 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/model/Account.java @@ -0,0 +1,74 @@ +package com.baeldung.schemageneration.model; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import java.util.ArrayList; +import java.util.List; + +@Entity +@Table(name = "accounts") +public class Account { + + @Id + @GeneratedValue + private Long id; + + @Column(nullable = false, length = 100) + private String name; + + @Column(name = "email_address") + private String emailAddress; + + @OneToMany(mappedBy = "account", cascade = CascadeType.ALL) + private List accountSettings = new ArrayList<>(); + + public Account() { + } + + public Account(String name, String emailAddress) { + this.name = name; + this.emailAddress = emailAddress; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmailAddress() { + return emailAddress; + } + + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + + public List getAccountSettings() { + return accountSettings; + } + + public void setAccountSettings(List accountSettings) { + this.accountSettings = accountSettings; + } + + public void addAccountSetting(AccountSetting setting) { + this.accountSettings.add(setting); + setting.setAccount(this); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/model/AccountSetting.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/model/AccountSetting.java new file mode 100644 index 0000000000..61e43894a8 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/model/AccountSetting.java @@ -0,0 +1,68 @@ +package com.baeldung.schemageneration.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "account_settings") +public class AccountSetting { + + @Id + @GeneratedValue + private Long id; + + @Column(name = "name", nullable = false) + private String settingName; + + @Column(name = "value", nullable = false) + private String settingValue; + + @ManyToOne() + @JoinColumn(name ="account_id", nullable = false) + private Account account; + + public AccountSetting() { + } + + public AccountSetting(String settingName, String settingValue) { + this.settingName = settingName; + this.settingValue = settingValue; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getSettingName() { + return settingName; + } + + public void setSettingName(String settingName) { + this.settingName = settingName; + } + + public String getSettingValue() { + return settingValue; + } + + public void setSettingValue(String settingValue) { + this.settingValue = settingValue; + } + + public Account getAccount() { + return account; + } + + public void setAccount(Account account) { + this.account = account; + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/repository/AccountRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/repository/AccountRepository.java new file mode 100644 index 0000000000..dc57ffe6d3 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/repository/AccountRepository.java @@ -0,0 +1,8 @@ +package com.baeldung.schemageneration.repository; + +import com.baeldung.schemageneration.model.Account; +import org.springframework.data.repository.CrudRepository; + +public interface AccountRepository extends CrudRepository { + Account findByName(String name); +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/repository/AccountSettingRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/repository/AccountSettingRepository.java new file mode 100644 index 0000000000..c2b8ff7398 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/schemageneration/repository/AccountSettingRepository.java @@ -0,0 +1,8 @@ +package com.baeldung.schemageneration.repository; + +import com.baeldung.schemageneration.model.AccountSetting; +import org.springframework.data.repository.CrudRepository; + +public interface AccountSettingRepository extends CrudRepository { + AccountSetting findByAccountId(Long accountId); +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/resources/application.properties b/persistence-modules/spring-data-jpa-5/src/main/resources/application.properties new file mode 100644 index 0000000000..f55ad438ff --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/resources/application.properties @@ -0,0 +1,13 @@ + +spring.datasource.url=jdbc:h2:mem:baeldung + +# JPA-Schema-Generation +# Use below configuration to generate database schema create commands based on the entity models +# and export them into the create.sql file +#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create +#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=create.sql +#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-source=metadata +#spring.jpa.properties.hibernate.format_sql=true + +spring.jpa.show-sql=true + diff --git a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java new file mode 100644 index 0000000000..9d25acbd96 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/composite/repository/BookRepositoryIntegrationTest.java @@ -0,0 +1,64 @@ +package com.baeldung.composite.repository; + +import com.baeldung.composite.BookApplication; +import com.baeldung.composite.entity.Book; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = BookApplication.class) +public class BookRepositoryIntegrationTest { + + public static final String JAVA_101 = "Java101"; + public static final String JANE = "Jane"; + public static final String TECH = "Tech"; + @Autowired + BookRepository repository; + + @Before + public void setUp() { + Book book1 = new Book("John", JAVA_101, TECH, 20); + Book book2 = new Book(JANE, JAVA_101, "Arch", 25); + Book book3 = new Book(JANE, "Scala101", TECH, 23); + + repository.saveAll(Arrays.asList(book1, book2, book3)); + } + + @After + public void tearDown() { + repository.deleteAll(); + } + + @Test + public void testFindByName() { + List books = repository.findByIdName(JAVA_101); + + assertEquals(2, books.size()); + } + + @Test + public void testFindByAuthor() { + List books = repository.findByIdAuthor(JANE); + + assertEquals(2, books.size()); + } + + @Test + public void testFindByGenre() { + List books = repository.findByGenre(TECH); + + assertEquals(2, books.size()); + } +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/schemageneration/AccountRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/schemageneration/AccountRepositoryIntegrationTest.java new file mode 100644 index 0000000000..86a7671fe4 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/schemageneration/AccountRepositoryIntegrationTest.java @@ -0,0 +1,72 @@ +package com.baeldung.schemageneration; + +import com.baeldung.schemageneration.model.Account; +import com.baeldung.schemageneration.model.AccountSetting; +import com.baeldung.schemageneration.repository.AccountRepository; +import com.baeldung.schemageneration.repository.AccountSettingRepository; +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = AccountApplication.class) +public class AccountRepositoryIntegrationTest { + + private static final String USER_NAME = "Eduard"; + private static final String USER_EMAIL_ADDRESS = "eduard@gmx.com"; + private static final String ACCOUNT_SETTING_NAME = "Timezone"; + private static final String ACCOUNT_SETTING_VALUE = "UTC+02"; + + @Autowired + private AccountRepository accountRepository; + + @Autowired + private AccountSettingRepository accountSettingRepository; + + @After + public void tearDown() { + accountRepository.deleteAll(); + } + + @Test + public void givenNewAccount_whenSave_thenSuccess() { + Account account = new Account(USER_NAME, USER_EMAIL_ADDRESS); + accountRepository.save(account); + + assertEquals(1, accountRepository.count()); + } + + @Test + public void givenSavedAccount_whenFindByName_thenFound() { + Account account = new Account(USER_NAME, USER_EMAIL_ADDRESS); + accountRepository.save(account); + + Account accountFound = accountRepository.findByName(USER_NAME); + + assertNotNull(accountFound); + assertEquals(USER_NAME, accountFound.getName()); + assertEquals(USER_EMAIL_ADDRESS, accountFound.getEmailAddress()); + } + + @Test + public void givenSavedAccount_whenAccountSettingIsAdded_thenPersisted() { + Account account = new Account(USER_NAME, USER_EMAIL_ADDRESS); + account.addAccountSetting(new AccountSetting(ACCOUNT_SETTING_NAME, ACCOUNT_SETTING_VALUE)); + accountRepository.save(account); + + Account accountFound = accountRepository.findByName(USER_NAME); + assertNotNull(accountFound); + AccountSetting accountSetting = accountSettingRepository.findByAccountId(accountFound.getId()); + + assertNotNull(accountSetting); + assertEquals(ACCOUNT_SETTING_NAME, accountSetting.getSettingName()); + assertEquals(ACCOUNT_SETTING_VALUE, accountSetting.getSettingValue()); + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/test/resources/application-test.properties b/persistence-modules/spring-data-jpa-5/src/test/resources/application-test.properties new file mode 100644 index 0000000000..e3d39fe1e2 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/test/resources/application-test.properties @@ -0,0 +1,3 @@ +spring.jpa.hibernate.ddl-auto=update +spring.datasource.url=jdbc:h2:mem:baeldung + diff --git a/persistence-modules/spring-persistence-simple-2/README.md b/persistence-modules/spring-persistence-simple-2/README.md index a6408df8f2..70eab26d45 100644 --- a/persistence-modules/spring-persistence-simple-2/README.md +++ b/persistence-modules/spring-persistence-simple-2/README.md @@ -1,3 +1,4 @@ ### Relevant Articles: - [Spring JdbcTemplate Unit Testing](https://www.baeldung.com/spring-jdbctemplate-testing) +- [Using a List of Values in a JdbcTemplate IN Clause](https://www.baeldung.com/spring-jdbctemplate-in-list) diff --git a/persistence-modules/spring-persistence-simple/pom.xml b/persistence-modules/spring-persistence-simple/pom.xml index 878c4592f9..7318ec55bd 100644 --- a/persistence-modules/spring-persistence-simple/pom.xml +++ b/persistence-modules/spring-persistence-simple/pom.xml @@ -132,13 +132,13 @@ - 5.2.5.RELEASE + 5.2.6.RELEASE 5.4.13.Final 8.0.19 1.4.200 - 2.2.6.RELEASE + 2.2.7.RELEASE 9.0.0.M26 1.1 4.2.1 diff --git a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/config/PersistenceConfig.java b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/config/PersistenceConfig.java index 80f3ff14c5..cdddbaa787 100644 --- a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/config/PersistenceConfig.java +++ b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/config/PersistenceConfig.java @@ -11,7 +11,6 @@ import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.orm.jpa.JpaTransactionManager; @@ -26,7 +25,6 @@ import java.util.Properties; @Configuration @EnableTransactionManagement -@EnableJpaRepositories(basePackages = { "com.baeldung.hibernate.dao" }, transactionManagerRef = "jpaTransactionManager") @EnableJpaAuditing @PropertySource({ "classpath:persistence-mysql.properties" }) @ComponentScan(basePackages = { "com.baeldung.persistence.dao", "com.baeldung.jpa.dao" }) @@ -97,7 +95,7 @@ public class PersistenceConfig { return new FooService(); } - private final Properties hibernateProperties() { + private Properties hibernateProperties() { final Properties hibernateProperties = new Properties(); hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); diff --git a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/config/PersistenceJPAConfig.java b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/config/PersistenceJPAConfig.java index 06cae493c9..e8a2aefd6b 100644 --- a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/config/PersistenceJPAConfig.java +++ b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/config/PersistenceJPAConfig.java @@ -8,7 +8,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; @@ -24,7 +23,6 @@ import java.util.Properties; @EnableTransactionManagement @PropertySource({ "classpath:persistence-h2.properties" }) @ComponentScan({ "com.baeldung.persistence","com.baeldung.jpa.dao" }) -@EnableJpaRepositories(basePackages = "com.baeldung.jpa.dao") public class PersistenceJPAConfig { @Autowired diff --git a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/config/PersistenceConfig.java b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/config/PersistenceConfig.java index 66b540a692..604923d615 100644 --- a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/config/PersistenceConfig.java +++ b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/config/PersistenceConfig.java @@ -1,9 +1,6 @@ package com.baeldung.spring.data.persistence.config; -import java.util.Properties; - -import javax.sql.DataSource; - +import com.google.common.base.Preconditions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -19,14 +16,15 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; -import com.google.common.base.Preconditions; +import javax.sql.DataSource; +import java.util.Properties; @Configuration @EnableTransactionManagement @PropertySource({ "classpath:persistence-${envTarget:h2}.properties" }) @ComponentScan({ "com.baeldung.spring.data.persistence" }) -// @ImportResource("classpath*:springDataPersistenceConfig.xml") -@EnableJpaRepositories(basePackages = { "com.baeldung.spring.data.persistence.dao", "com.baeldung.spring.data.persistence.jpaquery" }) +//@ImportResource("classpath*:*springDataJpaRepositoriesConfig.xml") +@EnableJpaRepositories("com.baeldung.spring.data.persistence.repository") public class PersistenceConfig { @Autowired @@ -40,10 +38,9 @@ public class PersistenceConfig { public LocalContainerEntityManagerFactoryBean entityManagerFactory() { final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "com.baeldung.spring.data.persistence.model" }); + em.setPackagesToScan("com.baeldung.spring.data.persistence.model"); final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); - // vendorAdapter.set em.setJpaVendorAdapter(vendorAdapter); em.setJpaProperties(additionalProperties()); @@ -78,7 +75,7 @@ public class PersistenceConfig { final Properties hibernateProperties = new Properties(); hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); - // hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); + return hibernateProperties; } diff --git a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/model/User.java b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/model/User.java index 09f1092644..1475eccbf0 100644 --- a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/model/User.java +++ b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/model/User.java @@ -1,7 +1,6 @@ package com.baeldung.spring.data.persistence.model; import javax.persistence.*; - import java.time.LocalDate; import java.util.List; import java.util.Objects; @@ -13,14 +12,22 @@ public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; + private String name; + private LocalDate creationDate; + private LocalDate lastLoginDate; + private boolean active; + private int age; + @Column(unique = true, nullable = false) private String email; + private Integer status; + @OneToMany List possessionList; @@ -28,7 +35,7 @@ public class User { super(); } - public User(String name, LocalDate creationDate,String email, Integer status) { + public User(String name, LocalDate creationDate, String email, Integer status) { this.name = name; this.creationDate = creationDate; this.email = email; @@ -75,7 +82,7 @@ public class User { public void setAge(final int age) { this.age = age; } - + public LocalDate getCreationDate() { return creationDate; } @@ -94,18 +101,18 @@ public class User { builder.append("User [name=").append(name).append(", id=").append(id).append("]"); return builder.toString(); } - + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return id == user.id && - age == user.age && - Objects.equals(name, user.name) && - Objects.equals(creationDate, user.creationDate) && - Objects.equals(email, user.email) && - Objects.equals(status, user.status); + age == user.age && + Objects.equals(name, user.name) && + Objects.equals(creationDate, user.creationDate) && + Objects.equals(email, user.email) && + Objects.equals(status, user.status); } @Override diff --git a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/dao/IFooDao.java b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/IFooDao.java similarity index 87% rename from persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/dao/IFooDao.java rename to persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/IFooDao.java index d2b746dc8b..0b750e37e1 100644 --- a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/dao/IFooDao.java +++ b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/IFooDao.java @@ -1,11 +1,13 @@ -package com.baeldung.spring.data.persistence.dao; +package com.baeldung.spring.data.persistence.repository; import com.baeldung.spring.data.persistence.model.Foo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; + public interface IFooDao extends JpaRepository { @Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)") Foo retrieveByName(@Param("name") String name); + } diff --git a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/jpaquery/UserRepository.java b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/UserRepository.java similarity index 96% rename from persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/jpaquery/UserRepository.java rename to persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/UserRepository.java index f22970c401..a8e3a536c3 100644 --- a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/jpaquery/UserRepository.java +++ b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/UserRepository.java @@ -1,9 +1,4 @@ -package com.baeldung.spring.data.persistence.jpaquery; - -import java.time.LocalDate; -import java.util.Collection; -import java.util.List; -import java.util.stream.Stream; +package com.baeldung.spring.data.persistence.repository; import com.baeldung.spring.data.persistence.model.User; import org.springframework.data.domain.Page; @@ -14,13 +9,18 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import java.time.LocalDate; +import java.util.Collection; +import java.util.List; +import java.util.stream.Stream; + public interface UserRepository extends JpaRepository, UserRepositoryCustom { Stream findAllByName(String name); @Query("SELECT u FROM User u WHERE u.status = 1") Collection findAllActiveUsers(); - + @Query("select u from User u where u.email like '%@gmail.com'") List findUsersWithGmailAddress(); @@ -74,14 +74,14 @@ public interface UserRepository extends JpaRepository, UserReposi @Query(value = "INSERT INTO Users (name, age, email, status, active) VALUES (:name, :age, :email, :status, :active)", nativeQuery = true) @Modifying void insertUser(@Param("name") String name, @Param("age") Integer age, @Param("email") String email, @Param("status") Integer status, @Param("active") boolean active); - + @Modifying @Query(value = "UPDATE Users u SET status = ? WHERE u.name = ?", nativeQuery = true) int updateUserSetStatusForNameNativePostgres(Integer status, String name); - + @Query(value = "SELECT u FROM User u WHERE u.name IN :names") - List findUserByNameList(@Param("names") Collection names); - + List findUserByNameList(@Param("names") Collection names); + void deleteAllByCreationDateAfter(LocalDate date); @Modifying(clearAutomatically = true, flushAutomatically = true) diff --git a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryCustom.java b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/UserRepositoryCustom.java similarity index 85% rename from persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryCustom.java rename to persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/UserRepositoryCustom.java index 8bfcb93158..77e661bbbe 100644 --- a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryCustom.java +++ b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/UserRepositoryCustom.java @@ -1,14 +1,16 @@ -package com.baeldung.spring.data.persistence.jpaquery; +package com.baeldung.spring.data.persistence.repository; + +import com.baeldung.spring.data.persistence.model.User; import java.util.Collection; import java.util.List; import java.util.Set; import java.util.function.Predicate; -import com.baeldung.spring.data.persistence.model.User; - public interface UserRepositoryCustom { + List findUserByEmails(Set emails); List findAllUsersByPredicates(Collection> predicates); + } diff --git a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryCustomImpl.java b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/UserRepositoryCustomImpl.java similarity index 85% rename from persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryCustomImpl.java rename to persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/UserRepositoryCustomImpl.java index f264ca0b44..366b2c54d0 100644 --- a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryCustomImpl.java +++ b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/repository/UserRepositoryCustomImpl.java @@ -1,5 +1,10 @@ -package com.baeldung.spring.data.persistence.jpaquery; +package com.baeldung.spring.data.persistence.repository; +import com.baeldung.spring.data.persistence.model.User; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.criteria.*; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -7,16 +12,6 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Path; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; - -import com.baeldung.spring.data.persistence.model.User; - public class UserRepositoryCustomImpl implements UserRepositoryCustom { @PersistenceContext diff --git a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/service/impl/FooService.java b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/service/impl/FooService.java index cd566ba9f6..c1406b8602 100644 --- a/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/service/impl/FooService.java +++ b/persistence-modules/spring-persistence-simple/src/main/java/com/baeldung/spring/data/persistence/service/impl/FooService.java @@ -2,7 +2,7 @@ package com.baeldung.spring.data.persistence.service.impl; import com.baeldung.spring.data.persistence.model.Foo; -import com.baeldung.spring.data.persistence.dao.IFooDao; +import com.baeldung.spring.data.persistence.repository.IFooDao; import com.baeldung.spring.data.persistence.service.IFooService; import com.baeldung.spring.data.persistence.service.common.AbstractService; import org.springframework.beans.factory.annotation.Autowired; diff --git a/persistence-modules/spring-persistence-simple/src/main/resources/springDataPersistenceConfig.xml b/persistence-modules/spring-persistence-simple/src/main/resources/springDataJpaRepositoriesConfig.xml similarity index 60% rename from persistence-modules/spring-persistence-simple/src/main/resources/springDataPersistenceConfig.xml rename to persistence-modules/spring-persistence-simple/src/main/resources/springDataJpaRepositoriesConfig.xml index 5ea2d9c05b..91778a17af 100644 --- a/persistence-modules/spring-persistence-simple/src/main/resources/springDataPersistenceConfig.xml +++ b/persistence-modules/spring-persistence-simple/src/main/resources/springDataJpaRepositoriesConfig.xml @@ -1,12 +1,13 @@ - - + \ No newline at end of file diff --git a/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryCommon.java b/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/repository/UserRepositoryCommon.java similarity index 99% rename from persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryCommon.java rename to persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/repository/UserRepositoryCommon.java index 5874b3c643..13b5b4357d 100644 --- a/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryCommon.java +++ b/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/repository/UserRepositoryCommon.java @@ -1,4 +1,4 @@ -package com.baeldung.spring.data.persistence.jpaquery; +package com.baeldung.spring.data.persistence.repository; import com.baeldung.spring.data.persistence.config.PersistenceConfig; import com.baeldung.spring.data.persistence.model.User; diff --git a/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryIntegrationTest.java b/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/repository/UserRepositoryIntegrationTest.java similarity index 94% rename from persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryIntegrationTest.java rename to persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/repository/UserRepositoryIntegrationTest.java index 3bffb51917..c76e345fdd 100644 --- a/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/jpaquery/UserRepositoryIntegrationTest.java +++ b/persistence-modules/spring-persistence-simple/src/test/java/com/baeldung/spring/data/persistence/repository/UserRepositoryIntegrationTest.java @@ -1,8 +1,4 @@ -package com.baeldung.spring.data.persistence.jpaquery; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.time.LocalDate; +package com.baeldung.spring.data.persistence.repository; import com.baeldung.spring.data.persistence.config.PersistenceConfig; import com.baeldung.spring.data.persistence.model.User; @@ -14,9 +10,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.transaction.annotation.Transactional; -/** - * Created by adam. - */ +import java.time.LocalDate; + +import static org.assertj.core.api.Assertions.assertThat; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) @DirtiesContext diff --git a/pom.xml b/pom.xml index 2f4579c999..2efea40c91 100644 --- a/pom.xml +++ b/pom.xml @@ -377,6 +377,7 @@ atomix aws + aws-app-sync aws-lambda aws-reactive @@ -461,7 +462,7 @@ java-lite java-numbers java-numbers-2 - java-numbers-3 + java-numbers-3 java-rmi java-spi java-vavr-stream @@ -495,7 +496,8 @@ kotlin-quasar - libraries-2 + language-interop + libraries-2 libraries-3 libraries-apache-commons libraries-apache-commons-collections @@ -508,6 +510,7 @@ libraries-http-2 libraries-io libraries-primitive + libraries-rpc libraries-security libraries-server libraries-testing @@ -811,6 +814,9 @@ jws libraries + libraries-4 + libraries-5 + libraries-6 vaadin vavr @@ -1012,6 +1018,7 @@ libraries-2 libraries-3 + libraries-apache-commons libraries-apache-commons-collections libraries-apache-commons-io @@ -1311,6 +1318,9 @@ jws libraries + libraries-4 + libraries-5 + libraries-6 vaadin vavr diff --git a/reactor-core/README.md b/reactor-core/README.md index e3cca35f86..0214aa26fd 100644 --- a/reactor-core/README.md +++ b/reactor-core/README.md @@ -7,3 +7,4 @@ This module contains articles about Reactor Core. - [Intro To Reactor Core](https://www.baeldung.com/reactor-core) - [Combining Publishers in Project Reactor](https://www.baeldung.com/reactor-combine-streams) - [Programmatically Creating Sequences with Project Reactor](https://www.baeldung.com/flux-sequences-reactor) +- [How to Extract a Mono’s Content in Java](https://www.baeldung.com/java-string-from-mono) diff --git a/spring-5-security/README.md b/spring-5-security/README.md index 07f2d48b7f..764d726ff8 100644 --- a/spring-5-security/README.md +++ b/spring-5-security/README.md @@ -9,3 +9,5 @@ This module contains articles about Spring Security 5 - [New Password Storage In Spring Security 5](https://www.baeldung.com/spring-security-5-password-storage) - [Default Password Encoder in Spring Security 5](https://www.baeldung.com/spring-security-5-default-password-encoder) - [Guide to the AuthenticationManagerResolver in Spring Security](https://www.baeldung.com/spring-security-authenticationmanagerresolver) +- [Disable Security for a Profile in Spring Boot](https://www.baeldung.com/spring-security-disable-profile) +- [Manual Logout With Spring Security](https://www.baeldung.com/spring-security-manual-logout) diff --git a/spring-5-security/src/main/java/com/baeldung/manuallogout/SimpleSecurityConfiguration.java b/spring-5-security/src/main/java/com/baeldung/manuallogout/SimpleSecurityConfiguration.java index 63394b64f2..08c2d83b99 100644 --- a/spring-5-security/src/main/java/com/baeldung/manuallogout/SimpleSecurityConfiguration.java +++ b/spring-5-security/src/main/java/com/baeldung/manuallogout/SimpleSecurityConfiguration.java @@ -2,14 +2,12 @@ package com.baeldung.manuallogout; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; + import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import org.springframework.security.web.authentication.logout.CookieClearingLogoutHandler; import org.springframework.security.web.authentication.logout.HeaderWriterLogoutHandler; import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; -import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices; import org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter; import javax.servlet.http.Cookie; @@ -26,13 +24,11 @@ public class SimpleSecurityConfiguration { @Override protected void configure(HttpSecurity http) throws Exception { http - .antMatcher("/basic/**") - .authorizeRequests(authz -> authz.anyRequest().permitAll()) - .logout(logout -> logout - .logoutUrl("/basic/basiclogout") - .addLogoutHandler(new SecurityContextLogoutHandler()) - .addLogoutHandler(new CookieClearingLogoutHandler(AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY)) - ); + .antMatcher("/basic/**") + .authorizeRequests(authz -> authz.anyRequest().permitAll()) + .logout(logout -> logout + .logoutUrl("/basic/basiclogout") + ); } } @@ -42,20 +38,20 @@ public class SimpleSecurityConfiguration { @Override protected void configure(HttpSecurity http) throws Exception { http - .antMatcher("/cookies/**") - .authorizeRequests(authz -> authz.anyRequest().permitAll()) - .logout(logout -> logout - .logoutUrl("/cookies/cookielogout") - .addLogoutHandler(new SecurityContextLogoutHandler()) - .addLogoutHandler((request, response, auth) -> { - for (Cookie cookie : request.getCookies()) { - String cookieName = cookie.getName(); - Cookie cookieToDelete = new Cookie(cookieName, null); - cookieToDelete.setMaxAge(0); - response.addCookie(cookieToDelete); - } - } - )); + .antMatcher("/cookies/**") + .authorizeRequests(authz -> authz.anyRequest().permitAll()) + .logout(logout -> logout + .logoutUrl("/cookies/cookielogout") + .addLogoutHandler(new SecurityContextLogoutHandler()) + .addLogoutHandler((request, response, auth) -> { + for (Cookie cookie : request.getCookies()) { + String cookieName = cookie.getName(); + Cookie cookieToDelete = new Cookie(cookieName, null); + cookieToDelete.setMaxAge(0); + response.addCookie(cookieToDelete); + } + }) + ); } } @@ -64,17 +60,17 @@ public class SimpleSecurityConfiguration { public static class ClearSiteDataHeaderLogoutConfiguration extends WebSecurityConfigurerAdapter { private static final ClearSiteDataHeaderWriter.Directive[] SOURCE = - { CACHE, COOKIES, STORAGE, EXECUTION_CONTEXTS }; + {CACHE, COOKIES, STORAGE, EXECUTION_CONTEXTS}; @Override protected void configure(HttpSecurity http) throws Exception { http - .antMatcher("/csd/**") - .authorizeRequests(authz -> authz.anyRequest().permitAll()) - .logout(logout -> logout - .logoutUrl("/csd/csdlogout") - .addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(SOURCE))) - ); + .antMatcher("/csd/**") + .authorizeRequests(authz -> authz.anyRequest().permitAll()) + .logout(logout -> logout + .logoutUrl("/csd/csdlogout") + .addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(SOURCE))) + ); } } } diff --git a/spring-5-security/src/test/java/com/baeldung/manuallogout/ManualLogoutIntegrationTest.java b/spring-5-security/src/test/java/com/baeldung/manuallogout/ManualLogoutIntegrationTest.java index 09e7daf877..8909dc2620 100644 --- a/spring-5-security/src/test/java/com/baeldung/manuallogout/ManualLogoutIntegrationTest.java +++ b/spring-5-security/src/test/java/com/baeldung/manuallogout/ManualLogoutIntegrationTest.java @@ -39,15 +39,10 @@ public class ManualLogoutIntegrationTest { @Test public void givenLoggedUserWhenUserLogoutThenSessionClearedAndNecessaryCookieCleared() throws Exception { - MockHttpServletRequest requestStateAfterLogout = this.mockMvc.perform(post("/basic/basiclogout").secure(true).with(csrf())) + this.mockMvc.perform(post("/basic/basiclogout").secure(true).with(csrf())) .andExpect(status().is3xxRedirection()) .andExpect(unauthenticated()) - .andExpect(cookie().maxAge(AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY, 0)) - .andReturn() - .getRequest(); - - HttpSession sessionStateAfterLogout = requestStateAfterLogout.getSession(); - assertNull(sessionStateAfterLogout.getAttribute(ATTRIBUTE_NAME)); + .andReturn(); } @WithMockUser(value = "spring") diff --git a/spring-boot-groovy/README.md b/spring-boot-groovy/README.md new file mode 100644 index 0000000000..d2472a11d0 --- /dev/null +++ b/spring-boot-groovy/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Building a Simple Web Application with Spring Boot and Groovy](https://www.baeldung.com/spring-boot-groovy-web-app) diff --git a/spring-boot-groovy/src/main/groovy/com/baeldung/app/SpringBootGroovyApplication.groovy b/spring-boot-groovy/src/main/groovy/com/baeldung/app/SpringBootGroovyApplication.groovy deleted file mode 100644 index 226a2ff53d..0000000000 --- a/spring-boot-groovy/src/main/groovy/com/baeldung/app/SpringBootGroovyApplication.groovy +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.app - -import org.springframework.boot.SpringApplication -import org.springframework.boot.autoconfigure.SpringBootApplication - -import com.baeldung.app.SpringBootGroovyApplication - -@SpringBootApplication -class SpringBootGroovyApplication { - static void main(String[] args) { - SpringApplication.run SpringBootGroovyApplication, args - } -} diff --git a/spring-boot-groovy/src/main/groovy/com/baeldung/app/controller/TodoController.groovy b/spring-boot-groovy/src/main/groovy/com/baeldung/app/controller/TodoController.groovy deleted file mode 100644 index 02f6d0223b..0000000000 --- a/spring-boot-groovy/src/main/groovy/com/baeldung/app/controller/TodoController.groovy +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.app.controller - -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.web.bind.annotation.DeleteMapping -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PathVariable -import org.springframework.web.bind.annotation.PostMapping -import org.springframework.web.bind.annotation.PutMapping -import org.springframework.web.bind.annotation.RequestBody -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RequestMethod -import org.springframework.web.bind.annotation.RestController - -import com.baeldung.app.entity.Todo -import com.baeldung.app.service.TodoService - -@RestController -@RequestMapping('todo') -public class TodoController { - - @Autowired - TodoService todoService - - @GetMapping - List getAllTodoList(){ - todoService.findAll() - } - - @PostMapping - Todo saveTodo(@RequestBody Todo todo){ - todoService.saveTodo todo - } - - @PutMapping - Todo updateTodo(@RequestBody Todo todo){ - todoService.updateTodo todo - } - - @DeleteMapping('/{todoId}') - deleteTodo(@PathVariable Integer todoId){ - todoService.deleteTodo todoId - } - - @GetMapping('/{todoId}') - Todo getTodoById(@PathVariable Integer todoId){ - todoService.findById todoId - } -} \ No newline at end of file diff --git a/spring-boot-groovy/src/main/groovy/com/baeldung/app/entity/Todo.groovy b/spring-boot-groovy/src/main/groovy/com/baeldung/app/entity/Todo.groovy deleted file mode 100644 index 9f1253c5b3..0000000000 --- a/spring-boot-groovy/src/main/groovy/com/baeldung/app/entity/Todo.groovy +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.app.entity - -import javax.persistence.Column -import javax.persistence.Entity -import javax.persistence.GeneratedValue -import javax.persistence.GenerationType -import javax.persistence.Id -import javax.persistence.Table - -@Entity -@Table(name = 'todo') -class Todo { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - Integer id - - @Column - String task - - @Column - Boolean isCompleted - -} diff --git a/spring-boot-groovy/src/main/groovy/com/baeldung/app/repository/TodoRepository.groovy b/spring-boot-groovy/src/main/groovy/com/baeldung/app/repository/TodoRepository.groovy deleted file mode 100644 index c0b35cc37d..0000000000 --- a/spring-boot-groovy/src/main/groovy/com/baeldung/app/repository/TodoRepository.groovy +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.app.repository - -import org.springframework.data.jpa.repository.JpaRepository -import org.springframework.stereotype.Repository - -import com.baeldung.app.entity.Todo - -@Repository -interface TodoRepository extends JpaRepository {} \ No newline at end of file diff --git a/spring-boot-groovy/src/main/groovy/com/baeldung/app/service/TodoService.groovy b/spring-boot-groovy/src/main/groovy/com/baeldung/app/service/TodoService.groovy deleted file mode 100644 index 0a59d93330..0000000000 --- a/spring-boot-groovy/src/main/groovy/com/baeldung/app/service/TodoService.groovy +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.app.service - -import com.baeldung.app.entity.Todo - -interface TodoService { - - List findAll() - - Todo findById(Integer todoId) - - Todo saveTodo(Todo todo) - - Todo updateTodo(Todo todo) - - Todo deleteTodo(Integer todoId) -} diff --git a/spring-boot-groovy/src/main/groovy/com/baeldung/app/service/impl/TodoServiceImpl.groovy b/spring-boot-groovy/src/main/groovy/com/baeldung/app/service/impl/TodoServiceImpl.groovy deleted file mode 100644 index 6d0ee03a9f..0000000000 --- a/spring-boot-groovy/src/main/groovy/com/baeldung/app/service/impl/TodoServiceImpl.groovy +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.app.service.impl - -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.stereotype.Service - -import com.baeldung.app.entity.Todo -import com.baeldung.app.repository.TodoRepository -import com.baeldung.app.service.TodoService - -@Service -class TodoServiceImpl implements TodoService { - - @Autowired - TodoRepository todoRepository - - @Override - List findAll() { - todoRepository.findAll() - } - - @Override - Todo findById(Integer todoId) { - todoRepository.findById todoId get() - } - - @Override - Todo saveTodo(Todo todo){ - todoRepository.save todo - } - - @Override - Todo updateTodo(Todo todo){ - todoRepository.save todo - } - - @Override - Todo deleteTodo(Integer todoId){ - todoRepository.deleteById todoId - } -} diff --git a/spring-boot-groovy/src/test/groovy/com/baeldung/app/TodoAppUnitTest.groovy b/spring-boot-groovy/src/test/groovy/com/baeldung/app/TodoAppUnitTest.groovy deleted file mode 100644 index faf2d64ba7..0000000000 --- a/spring-boot-groovy/src/test/groovy/com/baeldung/app/TodoAppUnitTest.groovy +++ /dev/null @@ -1,97 +0,0 @@ -package com.baeldung.app - -import static org.junit.jupiter.api.Assertions.assertEquals -import static org.junit.jupiter.api.Assertions.assertTrue - -import org.junit.BeforeClass -import org.junit.Test -import org.junit.runner.RunWith -import org.springframework.boot.test.context.SpringBootTest -import org.springframework.http.HttpStatus -import org.springframework.http.MediaType -import org.springframework.test.context.event.annotation.BeforeTestClass -import org.springframework.test.context.junit4.SpringRunner - -import com.baeldung.app.entity.Todo - -import io.restassured.RestAssured -import io.restassured.response.Response - -class TodoAppUnitTest { - static API_ROOT = 'http://localhost:8081/todo' - static readingTodoId - static writingTodoId - - @BeforeClass - static void populateDummyData() { - Todo readingTodo = new Todo(task: 'Reading', isCompleted: false) - Todo writingTodo = new Todo(task: 'Writing', isCompleted: false) - - final Response readingResponse = - RestAssured.given() - .contentType(MediaType.APPLICATION_JSON_VALUE) - .body(readingTodo).post(API_ROOT) - - Todo cookingTodoResponse = readingResponse.as Todo.class - readingTodoId = cookingTodoResponse.getId() - - final Response writingResponse = - RestAssured.given() - .contentType(MediaType.APPLICATION_JSON_VALUE) - .body(writingTodo).post(API_ROOT) - - Todo writingTodoResponse = writingResponse.as Todo.class - writingTodoId = writingTodoResponse.getId() - } - - @Test - void whenGetAllTodoList_thenOk(){ - final Response response = RestAssured.get(API_ROOT) - - assertEquals HttpStatus.OK.value(),response.getStatusCode() - assertTrue response.as(List.class).size() > 0 - } - - @Test - void whenGetTodoById_thenOk(){ - final Response response = - RestAssured.get("$API_ROOT/$readingTodoId") - - assertEquals HttpStatus.OK.value(),response.getStatusCode() - Todo todoResponse = response.as Todo.class - assertEquals readingTodoId,todoResponse.getId() - } - - @Test - void whenUpdateTodoById_thenOk(){ - Todo todo = new Todo(id:readingTodoId, isCompleted: true) - final Response response = - RestAssured.given() - .contentType(MediaType.APPLICATION_JSON_VALUE) - .body(todo).put(API_ROOT) - - assertEquals HttpStatus.OK.value(),response.getStatusCode() - Todo todoResponse = response.as Todo.class - assertTrue todoResponse.getIsCompleted() - } - - @Test - void whenDeleteTodoById_thenOk(){ - final Response response = - RestAssured.given() - .delete("$API_ROOT/$writingTodoId") - - assertEquals HttpStatus.OK.value(),response.getStatusCode() - } - - @Test - void whenSaveTodo_thenOk(){ - Todo todo = new Todo(task: 'Blogging', isCompleted: false) - final Response response = - RestAssured.given() - .contentType(MediaType.APPLICATION_JSON_VALUE) - .body(todo).post(API_ROOT) - - assertEquals HttpStatus.OK.value(),response.getStatusCode() - } -} \ No newline at end of file diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 228e7ce481..6caa93158a 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -28,6 +28,7 @@ spring-boot-deployment spring-boot-di spring-boot-camel + spring-boot-ci-cd spring-boot-custom-starter spring-boot-crud @@ -48,6 +49,7 @@ spring-boot-parent spring-boot-performance spring-boot-properties + spring-boot-properties-2 spring-boot-property-exp spring-boot-runtime spring-boot-security diff --git a/spring-boot-modules/spring-boot-ci-cd/.mvn/wrapper/MavenWrapperDownloader.java b/spring-boot-modules/spring-boot-ci-cd/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 0000000000..b901097f2d --- /dev/null +++ b/spring-boot-modules/spring-boot-ci-cd/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,117 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if(mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if(mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if(!outputFile.getParentFile().exists()) { + if(!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/spring-boot-modules/spring-boot-ci-cd/.mvn/wrapper/maven-wrapper.properties b/spring-boot-modules/spring-boot-ci-cd/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000000..642d572ce9 --- /dev/null +++ b/spring-boot-modules/spring-boot-ci-cd/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/spring-boot-modules/spring-boot-ci-cd/.travis.yml b/spring-boot-modules/spring-boot-ci-cd/.travis.yml new file mode 100644 index 0000000000..8467e11408 --- /dev/null +++ b/spring-boot-modules/spring-boot-ci-cd/.travis.yml @@ -0,0 +1,16 @@ +language: java +jdk: + - openjdk11 +services: + - docker + +before_install: + - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin + - docker pull openjdk:11-jre-slim-sid + +script: + - ./mvnw clean org.jacoco:jacoco-maven-plugin:prepare-agent install + - ./mvnw heroku:deploy jib:build -P deploy-heroku,deploy-docker + +after_success: + - bash <(curl -s https://codecov.io/bash) \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-ci-cd/README.md b/spring-boot-modules/spring-boot-ci-cd/README.md new file mode 100644 index 0000000000..ee8f106b53 --- /dev/null +++ b/spring-boot-modules/spring-boot-ci-cd/README.md @@ -0,0 +1,7 @@ +# Spring Boot CI/CD + +This module contains articles about CI/CD with Spring Boot + +## Relevant Articles + +- [CI/CD for a Spring Boot Project](https://www.baeldung.com/spring-boot-ci-cd) diff --git a/spring-boot-modules/spring-boot-ci-cd/mvnw b/spring-boot-modules/spring-boot-ci-cd/mvnw new file mode 100755 index 0000000000..41c0f0c23d --- /dev/null +++ b/spring-boot-modules/spring-boot-ci-cd/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/spring-boot-modules/spring-boot-ci-cd/mvnw.cmd b/spring-boot-modules/spring-boot-ci-cd/mvnw.cmd new file mode 100644 index 0000000000..86115719e5 --- /dev/null +++ b/spring-boot-modules/spring-boot-ci-cd/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/spring-boot-modules/spring-boot-ci-cd/pom.xml b/spring-boot-modules/spring-boot-ci-cd/pom.xml new file mode 100644 index 0000000000..25e45d56f3 --- /dev/null +++ b/spring-boot-modules/spring-boot-ci-cd/pom.xml @@ -0,0 +1,107 @@ + + + 4.0.0 + spring-boot-ci-cd + 0.0.1-SNAPSHOT + spring-boot-ci-cd + jar + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.jacoco + jacoco-maven-plugin + 0.8.5 + + + default-prepare-agent + + prepare-agent + + + + report + test + + report + + + + + + + + + + deploy-heroku + + true + + + + + com.heroku.sdk + heroku-maven-plugin + 3.0.2 + + spring-boot-ci-cd + + java $JAVA_OPTS -jar -Dserver.port=$PORT target/${project.build.finalName}.jar + + + + + + + + deploy-docker + + true + + + + + com.google.cloud.tools + jib-maven-plugin + 2.2.0 + + + registry.hub.docker.com/baeldungjib/baeldung-ci-cd-process + + ${project.version} + latest + + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-ci-cd/src/main/java/com/baeldung/cicd/CiCdApplication.java b/spring-boot-modules/spring-boot-ci-cd/src/main/java/com/baeldung/cicd/CiCdApplication.java new file mode 100644 index 0000000000..8451084148 --- /dev/null +++ b/spring-boot-modules/spring-boot-ci-cd/src/main/java/com/baeldung/cicd/CiCdApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.cicd; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class CiCdApplication { + + public static void main(String[] args) { + SpringApplication.run(CiCdApplication.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-ci-cd/src/main/resources/introduction-config.yml b/spring-boot-modules/spring-boot-ci-cd/src/main/resources/introduction-config.yml new file mode 100644 index 0000000000..02ff36de05 --- /dev/null +++ b/spring-boot-modules/spring-boot-ci-cd/src/main/resources/introduction-config.yml @@ -0,0 +1 @@ +defaultSize: 5 \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-ci-cd/src/test/java/com/baeldung/cicd/CiCdApplicationIntegrationTest.java b/spring-boot-modules/spring-boot-ci-cd/src/test/java/com/baeldung/cicd/CiCdApplicationIntegrationTest.java new file mode 100644 index 0000000000..7ab246faf8 --- /dev/null +++ b/spring-boot-modules/spring-boot-ci-cd/src/test/java/com/baeldung/cicd/CiCdApplicationIntegrationTest.java @@ -0,0 +1,15 @@ +package com.baeldung.cicd; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class CiCdApplicationIntegrationTest { + + @Test + public void contextLoads() { + + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-logging-log4j2/README.md b/spring-boot-modules/spring-boot-logging-log4j2/README.md index 76029caef8..aa6bb9b6e1 100644 --- a/spring-boot-modules/spring-boot-logging-log4j2/README.md +++ b/spring-boot-modules/spring-boot-logging-log4j2/README.md @@ -5,4 +5,4 @@ This module contains articles about logging in Spring Boot projects with Log4j 2 ### Relevant Articles: - [Logging in Spring Boot](https://www.baeldung.com/spring-boot-logging) - [Logging to Graylog with Spring Boot](https://www.baeldung.com/graylog-with-spring-boot) - +- [Log Groups in Spring Boot 2.1](https://www.baeldung.com/spring-boot-log-groups) diff --git a/spring-boot-modules/spring-boot-mvc-2/pom.xml b/spring-boot-modules/spring-boot-mvc-2/pom.xml index 3c503eb23d..45202d1e4c 100644 --- a/spring-boot-modules/spring-boot-mvc-2/pom.xml +++ b/spring-boot-modules/spring-boot-mvc-2/pom.xml @@ -87,19 +87,6 @@
- - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - jcenter-snapshots jcenter @@ -107,27 +94,12 @@ - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - 3.0.0-SNAPSHOT com.baeldung.swagger2boot.SpringBootSwaggerApplication - 2.2.0.BUILD-SNAPSHOT + 2.2.6.RELEASE 1.4.11.1 diff --git a/spring-boot-modules/spring-boot-parent/spring-boot-with-starter-parent/pom.xml b/spring-boot-modules/spring-boot-parent/spring-boot-with-starter-parent/pom.xml index baba410b39..331a85ec5b 100644 --- a/spring-boot-modules/spring-boot-parent/spring-boot-with-starter-parent/pom.xml +++ b/spring-boot-modules/spring-boot-parent/spring-boot-with-starter-parent/pom.xml @@ -20,7 +20,7 @@ org.springframework.boot spring-boot-starter-data-jpa - ${spring-boot.version} + ${spring-boot-starter-data-jpa.version} @@ -38,7 +38,7 @@ 1.8 - 2.1.5.RELEASE + 2.2.5.RELEASE 4.11 diff --git a/spring-boot-modules/spring-boot-properties-2/README.md b/spring-boot-modules/spring-boot-properties-2/README.md new file mode 100644 index 0000000000..cd0fd5e5ee --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/README.md @@ -0,0 +1,10 @@ +## Spring Boot Properties + +This module contains articles about Properties in Spring Boot. + +### Relevant Articles: +- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties) +- [A Quick Guide to Spring @Value](https://www.baeldung.com/spring-value-annotation) +- [Using Spring @Value with Defaults](https://www.baeldung.com/spring-value-defaults) +- [How to Inject a Property Value Into a Class Not Managed by Spring?](https://www.baeldung.com/inject-properties-value-non-spring-class) +- More articles: [[<-- prev]](../spring-boot-properties) \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-2/pom.xml b/spring-boot-modules/spring-boot-properties-2/pom.xml new file mode 100644 index 0000000000..bd2a35b19d --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + spring-boot-properties-2 + spring-boot-properties-2 + jar + Spring Boot Properties Module + 0.0.1-SNAPSHOT + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + + diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/ConfigPropertiesDemoApplication.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/ConfigPropertiesDemoApplication.java new file mode 100644 index 0000000000..a1e2584b2c --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/ConfigPropertiesDemoApplication.java @@ -0,0 +1,16 @@ +package com.baeldung.properties.json; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan(basePackageClasses = {JsonProperties.class, CustomJsonProperties.class}) +public class ConfigPropertiesDemoApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer()) + .run(); + } + +} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/CustomJsonProperties.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/CustomJsonProperties.java similarity index 97% rename from spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/CustomJsonProperties.java rename to spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/CustomJsonProperties.java index 084138ec6f..555711c49b 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/CustomJsonProperties.java +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/CustomJsonProperties.java @@ -1,4 +1,4 @@ -package com.baeldung.properties; +package com.baeldung.properties.json; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonProperties.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonProperties.java similarity index 92% rename from spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonProperties.java rename to spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonProperties.java index 31b3be14b4..6ada770e3b 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonProperties.java +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonProperties.java @@ -1,12 +1,13 @@ -package com.baeldung.properties; - -import java.util.LinkedHashMap; -import java.util.List; +package com.baeldung.properties.json; +import com.baeldung.properties.json.factory.JsonPropertySourceFactory; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; +import java.util.LinkedHashMap; +import java.util.List; + @Component @PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class) @ConfigurationProperties diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonPropertyContextInitializer.java similarity index 98% rename from spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java rename to spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonPropertyContextInitializer.java index 0aee149123..e3b713f29b 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonPropertyContextInitializer.java @@ -1,4 +1,11 @@ -package com.baeldung.properties; +package com.baeldung.properties.json; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.Resource; import java.io.IOException; import java.util.Collection; @@ -10,14 +17,6 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.core.env.MapPropertySource; -import org.springframework.core.env.PropertySource; -import org.springframework.core.io.Resource; - -import com.fasterxml.jackson.databind.ObjectMapper; - public class JsonPropertyContextInitializer implements ApplicationContextInitializer { private final static String CUSTOM_PREFIX = "custom."; diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/factory/JsonPropertySourceFactory.java similarity index 93% rename from spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java rename to spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/factory/JsonPropertySourceFactory.java index c14d3faea5..dccaae4ad2 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/factory/JsonPropertySourceFactory.java @@ -1,14 +1,13 @@ -package com.baeldung.properties; - -import java.io.IOException; -import java.util.Map; +package com.baeldung.properties.json.factory; +import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.PropertySourceFactory; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import java.util.Map; public class JsonPropertySourceFactory implements PropertySourceFactory { diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ClassNotManagedBySpring.java similarity index 95% rename from spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java rename to spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ClassNotManagedBySpring.java index 0329769d3c..59ee73a07b 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ClassNotManagedBySpring.java @@ -1,4 +1,4 @@ -package com.baeldung.value; +package com.baeldung.properties.value; public class ClassNotManagedBySpring { diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/CollectionProvider.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/CollectionProvider.java new file mode 100644 index 0000000000..6327e688e8 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/CollectionProvider.java @@ -0,0 +1,27 @@ +package com.baeldung.properties.value; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Component +@PropertySource("classpath:values.properties") +public class CollectionProvider { + + private final List values = new ArrayList<>(); + + public Collection getValues() { + return Collections.unmodifiableCollection(values); + } + + @Autowired + public void setValues(@Value("#{'${listOfValues}'.split(',')}") List values) { + this.values.addAll(values); + } +} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/InitializerBean.java similarity index 94% rename from spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java rename to spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/InitializerBean.java index 8c8634c767..b06cfa12ea 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/InitializerBean.java @@ -1,4 +1,4 @@ -package com.baeldung.value; +package com.baeldung.properties.value; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/PriorityProvider.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/PriorityProvider.java new file mode 100644 index 0000000000..892118eb06 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/PriorityProvider.java @@ -0,0 +1,22 @@ +package com.baeldung.properties.value; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +@Component +@PropertySource("classpath:values.properties") +public class PriorityProvider { + + private final String priority; + + @Autowired + public PriorityProvider(@Value("${priority:normal}") String priority) { + this.priority = priority; + } + + public String getPriority() { + return priority; + } +} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/SomeBean.java similarity index 88% rename from spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java rename to spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/SomeBean.java index 39d5245049..b3346a96dd 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/SomeBean.java @@ -1,4 +1,4 @@ -package com.baeldung.value; +package com.baeldung.properties.value; public class SomeBean { private int someValue; diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ValuesApp.java similarity index 98% rename from spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java rename to spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ValuesApp.java index 80893c1adf..67547199a6 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ValuesApp.java @@ -1,10 +1,4 @@ -package com.baeldung.value; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import javax.annotation.PostConstruct; +package com.baeldung.properties.value; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; @@ -13,6 +7,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; +import javax.annotation.PostConstruct; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + @Configuration @PropertySource(name = "myProperties", value = "values.properties") public class ValuesApp { diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/defaults/ValuesWithDefaultsApp.java similarity index 85% rename from spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java rename to spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/defaults/ValuesWithDefaultsApp.java index 589f891e6b..72fa0e03c0 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/defaults/ValuesWithDefaultsApp.java @@ -1,10 +1,6 @@ -package com.baeldung.valuewithdefaults; - -import java.util.Arrays; -import java.util.List; - -import javax.annotation.PostConstruct; +package com.baeldung.properties.value.defaults; +import org.apache.commons.lang3.ArrayUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -12,8 +8,9 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.util.Assert; -import com.google.common.collect.Lists; -import com.google.common.primitives.Ints; +import javax.annotation.PostConstruct; +import java.util.Arrays; +import java.util.List; /** * Demonstrates setting defaults for @Value annotation. Note that there are no properties @@ -62,14 +59,13 @@ public class ValuesWithDefaultsApp { Assert.isTrue(intWithDefaultValue == 42); // arrays - List stringListValues = Lists.newArrayList("one", "two", "three"); + List stringListValues = Arrays.asList("one", "two", "three"); Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues)); - List intListValues = Lists.newArrayList(1, 2, 3); - Assert.isTrue(Ints.asList(intArrayWithDefaults).containsAll(intListValues)); + List intListValues = Arrays.asList(1, 2, 3); + Assert.isTrue(Arrays.asList(ArrayUtils.toObject(intArrayWithDefaults)).containsAll(intListValues)); // SpEL Assert.isTrue(spelWithDefaultValue.equals("my default system property value")); - } } diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlApplication.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlApplication.java new file mode 100644 index 0000000000..f1244925f1 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlApplication.java @@ -0,0 +1,22 @@ +package com.baeldung.properties.yaml; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class YamlApplication implements CommandLineRunner { + + @Autowired + private YamlFooProperties yamlFooProperties; + + public static void main(String[] args) { + SpringApplication.run(YamlApplication.class, args); + } + + @Override + public void run(String... args) throws Exception { + System.out.println("YAML Properties " + yamlFooProperties); + } +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlFooProperties.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlFooProperties.java new file mode 100644 index 0000000000..264fc865ad --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/YamlFooProperties.java @@ -0,0 +1,42 @@ +package com.baeldung.properties.yaml; + +import com.baeldung.properties.yaml.factory.YamlPropertySourceFactory; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +import java.util.List; + +@Configuration +@ConfigurationProperties(prefix = "yaml") +@PropertySource(value = "classpath:foo.yml", factory = YamlPropertySourceFactory.class) +public class YamlFooProperties { + + private String name; + + private List aliases; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getAliases() { + return aliases; + } + + public void setAliases(List aliases) { + this.aliases = aliases; + } + + @Override + public String toString() { + return "YamlFooProperties{" + + "name='" + name + '\'' + + ", aliases=" + aliases + + '}'; + } +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/factory/YamlPropertySourceFactory.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/factory/YamlPropertySourceFactory.java new file mode 100644 index 0000000000..50201f9377 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/yaml/factory/YamlPropertySourceFactory.java @@ -0,0 +1,24 @@ +package com.baeldung.properties.yaml.factory; + +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.env.PropertiesPropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.io.support.EncodedResource; +import org.springframework.core.io.support.PropertySourceFactory; + +import java.io.IOException; +import java.util.Properties; + +public class YamlPropertySourceFactory implements PropertySourceFactory { + + @Override + public PropertySource createPropertySource(String name, EncodedResource encodedResource) throws IOException { + YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); + factory.setResources(encodedResource.getResource()); + + Properties properties = factory.getObject(); + + return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties); + } + +} diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/configprops.json b/spring-boot-modules/spring-boot-properties-2/src/main/resources/configprops.json similarity index 100% rename from spring-boot-modules/spring-boot-properties/src/main/resources/configprops.json rename to spring-boot-modules/spring-boot-properties-2/src/main/resources/configprops.json diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/resources/foo.yml b/spring-boot-modules/spring-boot-properties-2/src/main/resources/foo.yml new file mode 100644 index 0000000000..6320510a94 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/resources/foo.yml @@ -0,0 +1,5 @@ +yaml: + name: foo + aliases: + - abc + - xyz \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/values.properties b/spring-boot-modules/spring-boot-properties-2/src/main/resources/values.properties similarity index 80% rename from spring-boot-modules/spring-boot-properties/src/main/resources/values.properties rename to spring-boot-modules/spring-boot-properties-2/src/main/resources/values.properties index c6db5873fb..9c85893d5f 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/resources/values.properties +++ b/spring-boot-modules/spring-boot-properties-2/src/main/resources/values.properties @@ -1,4 +1,4 @@ value.from.file=Value got from the file -priority=Properties file +priority=high listOfValues=A,B,C valuesMap={key1:'1', key2 : '2', key3 : '3'} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/valueswithdefaults.properties b/spring-boot-modules/spring-boot-properties-2/src/main/resources/valueswithdefaults.properties similarity index 100% rename from spring-boot-modules/spring-boot-properties/src/main/resources/valueswithdefaults.properties rename to spring-boot-modules/spring-boot-properties-2/src/main/resources/valueswithdefaults.properties diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/json/JsonPropertiesIntegrationTest.java similarity index 98% rename from spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java rename to spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/json/JsonPropertiesIntegrationTest.java index 2f0e5ae408..6b00489b5c 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/json/JsonPropertiesIntegrationTest.java @@ -1,6 +1,4 @@ -package com.baeldung.properties; - -import java.util.Arrays; +package com.baeldung.properties.json; import org.hamcrest.Matchers; import org.junit.Assert; @@ -10,6 +8,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import java.util.Arrays; + @RunWith(SpringRunner.class) @ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class) public class JsonPropertiesIntegrationTest { diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/ClassNotManagedBySpringIntegrationTest.java similarity index 96% rename from spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java rename to spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/ClassNotManagedBySpringIntegrationTest.java index 689801bece..271242d4e8 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/ClassNotManagedBySpringIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.value; +package com.baeldung.properties.value; import org.junit.Before; import org.junit.Test; diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/CollectionProviderIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/CollectionProviderIntegrationTest.java new file mode 100644 index 0000000000..b045786a29 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/CollectionProviderIntegrationTest.java @@ -0,0 +1,22 @@ +package com.baeldung.properties.value; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = CollectionProvider.class) +public class CollectionProviderIntegrationTest { + + @Autowired + private CollectionProvider collectionProvider; + + @Test + public void givenPropertyFileWhenSetterInjectionUsedThenValueInjected() { + assertThat(collectionProvider.getValues()).contains("A", "B", "C"); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/PriorityProviderIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/PriorityProviderIntegrationTest.java new file mode 100644 index 0000000000..d7d1e7d78b --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/PriorityProviderIntegrationTest.java @@ -0,0 +1,22 @@ +package com.baeldung.properties.value; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = PriorityProvider.class) +public class PriorityProviderIntegrationTest { + + @Autowired + private PriorityProvider priorityProvider; + + @Test + public void givenPropertyFileWhenConstructorInjectionUsedThenValueInjected() { + assertThat(priorityProvider.getPriority()).isEqualTo("high"); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yaml/YamlFooPropertiesIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yaml/YamlFooPropertiesIntegrationTest.java new file mode 100644 index 0000000000..dfe1c830b4 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/yaml/YamlFooPropertiesIntegrationTest.java @@ -0,0 +1,23 @@ +package com.baeldung.properties.yaml; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class YamlFooPropertiesIntegrationTest { + + @Autowired + private YamlFooProperties yamlFooProperties; + + @Test + public void whenFactoryProvidedThenYamlPropertiesInjected() { + assertThat(yamlFooProperties.getName()).isEqualTo("foo"); + assertThat(yamlFooProperties.getAliases()).containsExactly("abc", "xyz"); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/README.md b/spring-boot-modules/spring-boot-properties/README.md index f861a01d10..b6685c7587 100644 --- a/spring-boot-modules/spring-boot-properties/README.md +++ b/spring-boot-modules/spring-boot-properties/README.md @@ -5,12 +5,9 @@ This module contains articles about Properties in Spring Boot. ### Relevant Articles: - [Reloading Properties Files in Spring](https://www.baeldung.com/spring-reloading-properties) - [Guide to @ConfigurationProperties in Spring Boot](https://www.baeldung.com/configuration-properties-in-spring-boot) -- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties) - [Guide to @EnableConfigurationProperties](https://www.baeldung.com/spring-enable-config-properties) - [Properties with Spring and Spring Boot](https://www.baeldung.com/properties-with-spring) - checkout the `com.baeldung.properties` package for all scenarios of properties injection and usage -- [A Quick Guide to Spring @Value](https://www.baeldung.com/spring-value-annotation) - [Spring YAML Configuration](https://www.baeldung.com/spring-yaml) -- [Using Spring @Value with Defaults](https://www.baeldung.com/spring-value-defaults) -- [How to Inject a Property Value Into a Class Not Managed by Spring?](https://www.baeldung.com/inject-properties-value-non-spring-class) - [Add Build Properties to a Spring Boot Application](https://www.baeldung.com/spring-boot-build-properties) - [IntelliJ – Cannot Resolve Spring Boot Configuration Properties Error](https://www.baeldung.com/intellij-resolve-spring-boot-configuration-properties) +- More articles: [[more -->]](../spring-boot-properties-2) \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/pom.xml b/spring-boot-modules/spring-boot-properties/pom.xml index ec05ec1bdc..ef9c084f4c 100644 --- a/spring-boot-modules/spring-boot-properties/pom.xml +++ b/spring-boot-modules/spring-boot-properties/pom.xml @@ -128,6 +128,7 @@ 4.4.11 @ 2.2.4.RELEASE + com.baeldung.buildproperties.Application diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/JdbcProperties.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/JdbcProperties.java new file mode 100644 index 0000000000..bbd193acba --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/JdbcProperties.java @@ -0,0 +1,21 @@ +package com.baeldung.configuration.processor; + +import org.springframework.boot.context.properties.*; +import org.springframework.context.annotation.*; +import org.springframework.beans.factory.annotation.*; + +@Configuration +@ConfigurationProperties(prefix = "com.baeldung") +public class JdbcProperties { + + @Value("${jdbc.url:jdbc:postgresql:/localhost:5432}") + private String jdbcUrl; + + public String getJdbcUrl() { + return jdbcUrl; + } + + public void setJdbcUrl(String jdbcUrl) { + this.jdbcUrl = jdbcUrl; + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/PropertyBeanInjection.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/PropertyBeanInjection.java index 3bcbf41f54..89a8f9458c 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/PropertyBeanInjection.java +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configuration/processor/PropertyBeanInjection.java @@ -6,16 +6,23 @@ import org.springframework.stereotype.*; @Component public class PropertyBeanInjection { - private final CustomProperties customProperties; + private CustomProperties customProperties; - PropertyBeanInjection(@Autowired CustomProperties customProperties) { + private JdbcProperties jdbcProperties; + + PropertyBeanInjection(@Autowired CustomProperties customProperties, @Autowired JdbcProperties jdbcProperties) { this.customProperties = customProperties; + this.jdbcProperties = jdbcProperties; } String getUrl() { return customProperties.getUrl(); } + String getJdbcUrl() { + return jdbcProperties.getJdbcUrl(); + } + int getTimeoutInMilliseconds() { return customProperties.getTimeoutInMilliSeconds(); } diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/Database.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/Database.java similarity index 73% rename from spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/Database.java rename to spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/Database.java index 6e798672e7..990ede35cd 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/Database.java +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/Database.java @@ -1,33 +1,36 @@ -package com.baeldung.properties; +package com.baeldung.configurationproperties; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -@Configuration + @ConfigurationProperties(prefix = "database") public class Database { - private String url; - private String username; - private String password; - + String url; + String username; + String password; + public String getUrl() { return url; } + public void setUrl(String url) { this.url = url; } + public String getUsername() { return username; } + public void setUsername(String username) { this.username = username; } + public String getPassword() { return password; } + public void setPassword(String password) { this.password = password; } - -} +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/DatabaseConfig.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/DatabaseConfig.java new file mode 100644 index 0000000000..8f17c98f03 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/DatabaseConfig.java @@ -0,0 +1,23 @@ +package com.baeldung.configurationproperties; + +import org.springframework.core.env.Environment; +import org.springframework.beans.factory.annotation.*; +import org.springframework.context.annotation.*; + +@Configuration +public class DatabaseConfig { + + @Autowired private Environment env; + + @Bean(name="dataSource") + public Database dataSource() { + + Database dataSource = new Database(); + dataSource.setUrl(env.getProperty("jdbc.url")); + dataSource.setUsername(env.getProperty("database.username")); + dataSource.setPassword(env.getProperty("database.password")); + + return dataSource; + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/DatabaseConfigPropertiesApp.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/DatabaseConfigPropertiesApp.java new file mode 100644 index 0000000000..bb1c937f60 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/DatabaseConfigPropertiesApp.java @@ -0,0 +1,13 @@ +package com.baeldung.configurationproperties; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan(basePackageClasses = {Database.class,DatabaseConfig.class}) +public class DatabaseConfigPropertiesApp{ + + public static void main(String[]args) {SpringApplication.run(DatabaseConfigPropertiesApp.class,args);} + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java index e54f28837d..1e5e88921a 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java @@ -1,5 +1,7 @@ package com.baeldung.properties; +import com.baeldung.buildproperties.Application; +import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.ComponentScan; @@ -7,11 +9,10 @@ import org.springframework.context.annotation.ComponentScan; import com.baeldung.configurationproperties.ConfigProperties; @SpringBootApplication -@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class, Database.class }) +@ComponentScan(basePackageClasses = {ConfigProperties.class, AdditionalProperties.class}) public class ConfigPropertiesDemoApplication { public static void main(String[] args) { - new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer()) - .run(); + SpringApplication.run(ConfigPropertiesDemoApplication.class, args); } } diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/spring/PropertyPlaceholderConfig.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/spring/PropertyPlaceholderConfig.java new file mode 100644 index 0000000000..0d1eb4ccf7 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/spring/PropertyPlaceholderConfig.java @@ -0,0 +1,23 @@ +package com.baeldung.properties.spring; + +import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.*; + +@Configuration +public class PropertyPlaceholderConfig { + + public PropertyPlaceholderConfig(){ + super(); + } + + @Bean + public static PropertyPlaceholderConfigurer properties() { + PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); + Resource[] resources = new ClassPathResource[]{ new ClassPathResource("foo.properties") }; + ppc.setLocations( resources ); + ppc.setIgnoreUnresolvablePlaceholders( true ); + return ppc; + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/spring/PropertySourcesPlaceholderConfig.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/spring/PropertySourcesPlaceholderConfig.java new file mode 100644 index 0000000000..8ff464e4cf --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/spring/PropertySourcesPlaceholderConfig.java @@ -0,0 +1,24 @@ +package com.baeldung.properties.spring; + +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.*; + +@Configuration +public class PropertySourcesPlaceholderConfig{ + + public PropertySourcesPlaceholderConfig(){ + super(); + } + + @Bean + public static PropertySourcesPlaceholderConfigurer properties(){ + PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); + Resource[] resources = new ClassPathResource[]{ new ClassPathResource("foo.properties") }; + pspc.setLocations(resources); + pspc.setIgnoreUnresolvablePlaceholders(true); + return pspc; + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/application.properties b/spring-boot-modules/spring-boot-properties/src/main/resources/application.properties index d4d1df7abc..af38556f81 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/application.properties @@ -3,6 +3,3 @@ spring.properties.refreshDelay=1000 spring.config.location=file:extra.properties spring.main.allow-bean-definition-overriding=true -database.url=jdbc:postgresql:/localhost:5432/instance -database.username=foo -database.password=bar \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/configForDbProperties.xml b/spring-boot-modules/spring-boot-properties/src/main/resources/configForDbProperties.xml new file mode 100644 index 0000000000..00fd5f0508 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/configForDbProperties.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/configForProperties.xml b/spring-boot-modules/spring-boot-properties/src/main/resources/configForProperties.xml index d796f791cb..16db00ace3 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/resources/configForProperties.xml +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/configForProperties.xml @@ -7,7 +7,8 @@ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd" > - + + diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/configForPropertyPlaceholderBeans.xml b/spring-boot-modules/spring-boot-properties/src/main/resources/configForPropertyPlaceholderBeans.xml new file mode 100644 index 0000000000..a296cf5169 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/configForPropertyPlaceholderBeans.xml @@ -0,0 +1,23 @@ + + + + + + + classpath:foo.properties + + + + + + + + + classpath:foo.properties + + + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/configuration-processor.properties b/spring-boot-modules/spring-boot-properties/src/main/resources/configuration-processor.properties new file mode 100644 index 0000000000..b68a4fbda3 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/configuration-processor.properties @@ -0,0 +1,3 @@ +com.baeldung.url=www.abc.test.com +com.baeldung.jdbc.url= +com.baeldung.timeout-in-milli-seconds=2000 diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/database.properties b/spring-boot-modules/spring-boot-properties/src/main/resources/database.properties new file mode 100644 index 0000000000..24bfdf2096 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/database.properties @@ -0,0 +1,4 @@ +jdbc.url=jdbc:postgresql:/localhost:5432 +database.username=foo +database.password=bar + diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/database.yml b/spring-boot-modules/spring-boot-properties/src/main/resources/database.yml new file mode 100644 index 0000000000..83432f65bc --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/database.yml @@ -0,0 +1,6 @@ +database: + jdbc: + url: jdbc:postresql:/localhost:5432 + username: foo + password: bar +secret: foo \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configuration/processor/PropertyBeanInjectionUnitTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configuration/processor/PropertyBeanInjectionUnitTest.java index bdeb6547c3..eb6add2b94 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configuration/processor/PropertyBeanInjectionUnitTest.java +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configuration/processor/PropertyBeanInjectionUnitTest.java @@ -15,6 +15,11 @@ class PropertyBeanInjectionUnitTest { @Autowired private PropertyBeanInjection propertyBeanInjection; + @Test + void checkThatJdbcPropertiesHaveTheCorrectValueFromPropertiesFile() { + Assertions.assertEquals("jdbc:postgresql:/localhost:5432", propertyBeanInjection.getJdbcUrl()); + } + @Test void checkThatCustomPropertiesHaveTheCorrectValueFromPropertiesFile() { Assertions.assertEquals("www.abc.test.com", propertyBeanInjection.getUrl()); diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/ConfigPropertiesIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/ConfigPropertiesIntegrationTest.java index 141400b1fe..3b80fa66fe 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/ConfigPropertiesIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/ConfigPropertiesIntegrationTest.java @@ -4,6 +4,7 @@ import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; @@ -13,8 +14,8 @@ import com.baeldung.properties.AdditionalProperties; import com.baeldung.properties.ConfigPropertiesDemoApplication; @RunWith(SpringRunner.class) -@SpringBootTest(classes = ConfigPropertiesDemoApplication.class) -@TestPropertySource("classpath:configprops-test.properties") +@SpringBootTest(classes = {ConfigPropertiesDemoApplication.class}) +@TestPropertySource(locations = {"classpath:configprops-test.properties"}) public class ConfigPropertiesIntegrationTest { @Autowired @@ -53,4 +54,5 @@ public class ConfigPropertiesIntegrationTest { Assert.assertTrue(additionalProperties.getUnit().equals("km")); Assert.assertTrue(additionalProperties.getMax() == 100); } + } diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/DatabasePropertiesIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/DatabasePropertiesIntegrationTest.java index 0b9bd797ae..50a129916f 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/DatabasePropertiesIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/DatabasePropertiesIntegrationTest.java @@ -4,26 +4,28 @@ import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import com.baeldung.properties.ConfigPropertiesDemoApplication; -import com.baeldung.properties.Database; +import com.baeldung.configurationproperties.Database; @RunWith(SpringRunner.class) -@SpringBootTest(classes = ConfigPropertiesDemoApplication.class) -@TestPropertySource("classpath:application.properties") +@SpringBootTest(classes = DatabaseConfigPropertiesApp.class) +@TestPropertySource("classpath:database-test.properties") public class DatabasePropertiesIntegrationTest { @Autowired + @Qualifier("dataSource") private Database database; @Test public void testDatabaseProperties() { Assert.assertNotNull(database); - Assert.assertEquals("jdbc:postgresql:/localhost:5432/instance", database.getUrl()); + Assert.assertEquals("jdbc:postgresql:/localhost:5432", database.getUrl()); Assert.assertEquals("foo", database.getUsername()); Assert.assertEquals("bar", database.getPassword()); } diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePlaceholdersJavaConfigIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePlaceholdersJavaConfigIntegrationTest.java new file mode 100644 index 0000000000..8ebda90321 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePlaceholdersJavaConfigIntegrationTest.java @@ -0,0 +1,23 @@ +package com.baeldung.properties.multiple; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import com.baeldung.properties.spring.PropertyPlaceholderConfig; +import com.baeldung.properties.spring.PropertySourcesPlaceholderConfig; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringJUnitConfig({PropertyPlaceholderConfig.class, PropertySourcesPlaceholderConfig.class}) +public class MultiplePlaceholdersJavaConfigIntegrationTest { + + @Value("${key.something}") + private String something; + + + @Test + public void whenReadInjectedValues_thenGetCorrectValues() { + assertThat(something).isEqualTo("val"); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePlaceholdersXmlConfigIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePlaceholdersXmlConfigIntegrationTest.java new file mode 100644 index 0000000000..b863e2e080 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePlaceholdersXmlConfigIntegrationTest.java @@ -0,0 +1,25 @@ +package com.baeldung.properties.multiple; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; + + +@SpringJUnitConfig(locations = "classpath:configForPropertyPlaceholderBeans.xml") +public class MultiplePlaceholdersXmlConfigIntegrationTest { + + @Value("${foo}") + private String something; + + @Value("${key.something}") + private String something2; + + + @Test + public void whenReadInjectedValues_thenGetCorrectValues() { + assertThat(something).isEqualTo("bar"); + assertThat(something2).isEqualTo("val"); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java index db71e816dd..2150d4b3ec 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java @@ -6,16 +6,19 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import static org.assertj.core.api.Assertions.assertThat; -@SpringJUnitConfig(locations = "classpath:configForProperties.xml") +@SpringJUnitConfig(locations = {"classpath:configForProperties.xml", "classpath:configForDbProperties.xml"}) public class MultiplePropertiesXmlConfigIntegrationTest { @Value("${key.something}") private String something; @Value("${key.something2}") private String something2; + @Value("${jdbc.url}") private String jdbcUrl; + @Test public void whenReadInjectedValues_thenGetCorrectValues() { assertThat(something).isEqualTo("val"); assertThat(something2).isEqualTo("val2"); + assertThat(jdbcUrl).isEqualTo("jdbc:postgresql:/localhost:5432"); } } diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/test/IntegrationTestSuite.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/test/IntegrationTestSuite.java index d41d328867..0e0f8f6230 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/test/IntegrationTestSuite.java +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/test/IntegrationTestSuite.java @@ -10,6 +10,8 @@ import com.baeldung.properties.basic.PropertiesWithXmlIntegrationTest; import com.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest; import com.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest; import com.baeldung.properties.external.ExternalPropertiesWithXmlManualTest; +import com.baeldung.properties.multiple.MultiplePropertiesXmlConfigIntegrationTest; +import com.baeldung.properties.multiple.MultiplePlaceholdersXmlConfigIntegrationTest; @RunWith(Suite.class) @SuiteClasses({ //@formatter:off @@ -17,8 +19,8 @@ import com.baeldung.properties.external.ExternalPropertiesWithXmlManualTest; ExternalPropertiesWithJavaIntegrationTest.class, ExternalPropertiesWithMultipleXmlsIntegrationTest.class, ExternalPropertiesWithXmlManualTest.class, - ExtendedPropertiesWithJavaIntegrationTest.class, - PropertiesWithMultipleXmlsIntegrationTest.class, + ExtendedPropertiesWithJavaIntegrationTest.class, MultiplePropertiesXmlConfigIntegrationTest.class, + PropertiesWithMultipleXmlsIntegrationTest.class, MultiplePlaceholdersXmlConfigIntegrationTest.class })// @formatter:on public final class IntegrationTestSuite { // diff --git a/spring-boot-modules/spring-boot-properties/src/test/resources/application.properties b/spring-boot-modules/spring-boot-properties/src/test/resources/application.properties index d4d1df7abc..af38556f81 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/resources/application.properties +++ b/spring-boot-modules/spring-boot-properties/src/test/resources/application.properties @@ -3,6 +3,3 @@ spring.properties.refreshDelay=1000 spring.config.location=file:extra.properties spring.main.allow-bean-definition-overriding=true -database.url=jdbc:postgresql:/localhost:5432/instance -database.username=foo -database.password=bar \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/test/resources/configuration-processor.properties b/spring-boot-modules/spring-boot-properties/src/test/resources/configuration-processor.properties index 00369f2eff..b68a4fbda3 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/resources/configuration-processor.properties +++ b/spring-boot-modules/spring-boot-properties/src/test/resources/configuration-processor.properties @@ -1,2 +1,3 @@ com.baeldung.url=www.abc.test.com +com.baeldung.jdbc.url= com.baeldung.timeout-in-milli-seconds=2000 diff --git a/spring-boot-modules/spring-boot-properties/src/test/resources/database-test.properties b/spring-boot-modules/spring-boot-properties/src/test/resources/database-test.properties new file mode 100644 index 0000000000..384d0850ab --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/test/resources/database-test.properties @@ -0,0 +1,3 @@ +jdbc.url=jdbc:postgresql:/localhost:5432 +database.username=foo +database.password=bar diff --git a/spring-boot-modules/spring-boot-springdoc/pom.xml b/spring-boot-modules/spring-boot-springdoc/pom.xml index 375cf06c2c..1c1f27b5a5 100644 --- a/spring-boot-modules/spring-boot-springdoc/pom.xml +++ b/spring-boot-modules/spring-boot-springdoc/pom.xml @@ -37,7 +37,7 @@ test - + org.hibernate hibernate-core ${hibernate.version} @@ -53,7 +53,19 @@ org.springdoc springdoc-openapi-data-rest ${springdoc.version} + + + + + org.springframework.restdocs + spring-restdocs-mockmvc + test + + org.springframework.restdocs + spring-restdocs-restassured + test + @@ -62,13 +74,49 @@ org.springframework.boot spring-boot-maven-plugin + + org.asciidoctor + asciidoctor-maven-plugin + ${asciidoctor-plugin.version} + + + generate-docs + package + + process-asciidoc + + + html + book + + ${snippetsDirectory} + + src/main/resources/asciidocs + target/generated-docs + + + + + + + true + src/main/resources + + application.properties + data.sql + schema.sql + + + 1.8 5.2.10.Final 1.2.32 + 1.5.6 + ${project.build.directory}/generated-snippets diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/Application.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/Application.java new file mode 100644 index 0000000000..93a28a2b49 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/Application.java @@ -0,0 +1,28 @@ +package com.baeldung.restdocopenapi; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; + +@SpringBootApplication() +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @Bean + public OpenAPI customOpenAPI(@Value("${springdoc.version}") String appVersion) { + return new OpenAPI().info(new Info().title("Foobar API") + .version(appVersion) + .description("This is a sample Foobar server created using springdocs - a library for OpenAPI 3 with spring boot.") + .termsOfService("http://swagger.io/terms/") + .license(new License().name("Apache 2.0") + .url("http://springdoc.org"))); + } +} diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/Foo.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/Foo.java new file mode 100644 index 0000000000..99d63581be --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/Foo.java @@ -0,0 +1,93 @@ +package com.baeldung.restdocopenapi; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Foo { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private String title; + + @Column() + private String body; + + + protected Foo() { + } + + public Foo(long id, String title, String body) { + this.id = id; + this.title = title; + this.body = body; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + ((title == null) ? 0 : title.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Foo other = (Foo) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (title == null) { + if (other.title != null) + return false; + } else if (!title.equals(other.title)) + return false; + return true; + } + + @Override + public String toString() { + return "Foo [id=" + id + ", title=" + title + "]"; + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/FooController.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/FooController.java new file mode 100644 index 0000000000..55c2cccb3c --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/FooController.java @@ -0,0 +1,83 @@ +package com.baeldung.restdocopenapi; + +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; + +import java.util.List; +import java.util.Optional; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/foo") +public class FooController { + + @Autowired + FooRepository repository; + + @GetMapping + public ResponseEntity> getAllFoos() { + List fooList = (List) repository.findAll(); + if (fooList.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + return new ResponseEntity<>(fooList, HttpStatus.OK); + } + + @GetMapping(value = "{id}") + public ResponseEntity getFooById(@PathVariable("id") Long id) { + + Optional foo = repository.findById(id); + return foo.isPresent() ? new ResponseEntity<>(foo.get(), HttpStatus.OK) : new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @PostMapping + public ResponseEntity addFoo(@RequestBody @Valid Foo foo) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setLocation(linkTo(FooController.class).slash(foo.getId()) + .toUri()); + Foo savedFoo; + try { + savedFoo = repository.save(foo); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + return new ResponseEntity<>(savedFoo, httpHeaders, HttpStatus.CREATED); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteFoo(@PathVariable("id") long id) { + try { + repository.deleteById(id); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + @PutMapping("/{id}") + public ResponseEntity updateFoo(@PathVariable("id") long id, @RequestBody Foo foo) { + boolean isFooPresent = repository.existsById(Long.valueOf(id)); + + if (!isFooPresent) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + Foo updatedFoo = repository.save(foo); + + return new ResponseEntity<>(updatedFoo, HttpStatus.OK); + } +} diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/FooRepository.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/FooRepository.java new file mode 100644 index 0000000000..105b57b2ef --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/FooRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.restdocopenapi; + +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface FooRepository extends PagingAndSortingRepository{ + +} diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/springdoc/FooBarController.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/springdoc/FooBarController.java new file mode 100644 index 0000000000..8af414c8fd --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/restdocopenapi/springdoc/FooBarController.java @@ -0,0 +1,121 @@ +package com.baeldung.restdocopenapi.springdoc; + +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; + +import java.util.List; +import java.util.Optional; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.restdocopenapi.Foo; +import com.baeldung.restdocopenapi.FooRepository; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; + +@RestController +@RequestMapping("/foobar") +@Tag(name = "foobar", description = "the foobar API with documentation annotations") +public class FooBarController { + + @Autowired + FooRepository repository; + + @Operation(summary = "Get all foos") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "found foos", content = { + @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = Foo.class)))}), + @ApiResponse(responseCode = "404", description = "No Foos found", content = @Content) }) + @GetMapping + public ResponseEntity> getAllFoos() { + List fooList = (List) repository.findAll(); + if (fooList.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + return new ResponseEntity<>(fooList, HttpStatus.OK); + } + + @Operation(summary = "Get a foo by foo id") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "found the foo", content = { + @Content(mediaType = "application/json", schema = @Schema(implementation = Foo.class))}), + @ApiResponse(responseCode = "400", description = "Invalid id supplied", content = @Content), + @ApiResponse(responseCode = "404", description = "Foo not found", content = @Content) }) + @GetMapping(value = "{id}") + public ResponseEntity getFooById(@Parameter(description = "id of foo to be searched") @PathVariable("id") String id) { + + Optional foo = repository.findById(Long.valueOf(id)); + return foo.isPresent() ? new ResponseEntity<>(foo.get(), HttpStatus.OK) : new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @Operation(summary = "Create a foo") + @ApiResponses(value = { + @ApiResponse(responseCode = "201", description = "foo created", content = { @ + Content(mediaType = "application/json", schema = @Schema(implementation = Foo.class))}), + @ApiResponse(responseCode = "404", description = "Bad request", content = @Content) }) + @PostMapping + public ResponseEntity addFoo(@Parameter(description = "foo object to be created") @RequestBody @Valid Foo foo) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setLocation(linkTo(FooBarController.class).slash(foo.getId()).toUri()); + Foo savedFoo; + try { + savedFoo = repository.save(foo); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + return new ResponseEntity<>(savedFoo, httpHeaders, HttpStatus.CREATED); + } + + @Operation(summary = "Delete a foo") + @ApiResponses(value = { + @ApiResponse(responseCode = "204", description = "foo deleted"), + @ApiResponse(responseCode = "404", description = "Bad request", content = @Content) }) + @DeleteMapping("/{id}") + public ResponseEntity deleteFoo(@Parameter(description = "id of foo to be deleted") @PathVariable("id") long id) { + try { + repository.deleteById(id); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + @Operation(summary = "Update a foo") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "foo updated successfully", content = { + @Content(mediaType = "application/json", schema = @Schema(implementation = Foo.class))}), + @ApiResponse(responseCode = "404", description = "No Foo exists with given id", content = @Content) }) + @PutMapping("/{id}") + public ResponseEntity updateFoo(@Parameter(description = "id of foo to be updated") @PathVariable("id") long id, @RequestBody Foo foo) { + + boolean isFooPresent = repository.existsById(Long.valueOf(id)); + + if (!isFooPresent) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + Foo updatedFoo = repository.save(foo); + + return new ResponseEntity<>(updatedFoo, HttpStatus.OK); + } +} diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/resources/application.properties b/spring-boot-modules/spring-boot-springdoc/src/main/resources/application.properties index 45378e610b..7debdc6503 100644 --- a/spring-boot-modules/spring-boot-springdoc/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-springdoc/src/main/resources/application.properties @@ -5,4 +5,9 @@ springdoc.swagger-ui.path=/swagger-ui-custom.html springdoc.api-docs.path=/api-docs # H2 Related Configurations -spring.datasource.url=jdbc:h2:mem:springdoc \ No newline at end of file +spring.datasource.url=jdbc:h2:mem:springdoc + +## for com.baeldung.restdocopenapi ## +springdoc.version=@springdoc.version@ +spring.jpa.hibernate.ddl-auto=none +###################################### \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/resources/asciidocs/fooapi.adoc b/spring-boot-modules/spring-boot-springdoc/src/main/resources/asciidocs/fooapi.adoc new file mode 100644 index 0000000000..90791cbbcf --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/resources/asciidocs/fooapi.adoc @@ -0,0 +1,153 @@ += RESTful Notes API Guide +Baeldung; +:doctype: book +:icons: font +:source-highlighter: highlightjs +:toc: left +:toclevels: 4 +:sectlinks: + +[[overview]] += Overview + +[[overview-http-verbs]] +== HTTP verbs + +RESTful notes tries to adhere as closely as possible to standard HTTP and REST conventions in its +use of HTTP verbs. + +|=== +| Verb | Usage + +| `GET` +| Used to retrieve a resource + +| `POST` +| Used to create a new resource + +| `PUT` +| Used to update an existing resource + +| `DELETE` +| Used to delete an existing resource +|=== + +RESTful notes tries to adhere as closely as possible to standard HTTP and REST conventions in its +use of HTTP status codes. + +|=== +| Status code | Usage + +| `200 OK` +| The request completed successfully + +| `201 Created` +| A new resource has been created successfully. The resource's URI is available from the response's +`Location` header + +| `204 No Content` +| An update to an existing resource has been applied successfully + +| `400 Bad Request` +| The request was malformed. The response body will include an error providing further information + +| `404 Not Found` +| The requested resource did not exist +|=== + +[[overview-hypermedia]] +== Hypermedia + +RESTful Notes uses hypermedia and resources include links to other resources in their +responses. Responses are in http://stateless.co/hal_specification.html[Hypertext Application +from resource to resource. +Language (HAL)] format. Links can be found beneath the `_links` key. Users of the API should +not create URIs themselves, instead they should use the above-described links to navigate + +[[resources]] += Resources + +[[resources-FOO]] +== FOO REST Service + +The FOO provides the entry point into the service. + +[[resources-foo-get]] +=== Accessing the foo GET + +A `GET` request is used to access the foo read. + +==== Request structure + +include::{snippets}/getAFoo/http-request.adoc[] + +==== Path Parameters +include::{snippets}/getAFoo/path-parameters.adoc[] + +==== Example response + +include::{snippets}/getAFoo/http-response.adoc[] + +==== CURL request + +include::{snippets}/getAFoo/curl-request.adoc[] + +[[resources-foo-post]] +=== Accessing the foo POST + +A `POST` request is used to access the foo create. + +==== Request structure + +include::{snippets}/createFoo/http-request.adoc[] + +==== Example response + +include::{snippets}/createFoo/http-response.adoc[] + +==== CURL request + +include::{snippets}/createFoo/curl-request.adoc[] + +[[resources-foo-delete]] +=== Accessing the foo DELETE + +A `DELETE` request is used to access the foo delete. + +==== Request structure + +include::{snippets}/deleteFoo/http-request.adoc[] + +==== Path Parameters +include::{snippets}/deleteFoo/path-parameters.adoc[] + +==== Example response + +include::{snippets}/deleteFoo/http-response.adoc[] + +==== CURL request + +include::{snippets}/deleteFoo/curl-request.adoc[] + +[[resources-foo-put]] +=== Accessing the foo PUT + +A `PUT` request is used to access the foo update. + +==== Request structure + +include::{snippets}/updateFoo/http-request.adoc[] + +==== Path Parameters +include::{snippets}/updateFoo/path-parameters.adoc[] + +==== Example response + +include::{snippets}/updateFoo/http-response.adoc[] + +==== CURL request + +include::{snippets}/updateFoo/curl-request.adoc[] + + + diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/resources/data.sql b/spring-boot-modules/spring-boot-springdoc/src/main/resources/data.sql new file mode 100644 index 0000000000..f80e53b717 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/resources/data.sql @@ -0,0 +1,4 @@ +INSERT INTO Foo(id, title, body) VALUES (1, 'Foo 1', 'Foo body 1'); +INSERT INTO Foo(id, title, body) VALUES (2, 'Foo 2', 'Foo body 2'); +INSERT INTO Foo(id, title, body) VALUES (3, 'Foo 3', 'Foo body 3'); + diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/resources/schema.sql b/spring-boot-modules/spring-boot-springdoc/src/main/resources/schema.sql new file mode 100644 index 0000000000..e5d33da019 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/resources/schema.sql @@ -0,0 +1,8 @@ +DROP TABLE IF EXISTS foo; + +CREATE TABLE foo ( + id INTEGER NOT NULL AUTO_INCREMENT, + title VARCHAR(250) NOT NULL, + body VARCHAR(250), + PRIMARY KEY (id) +); \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/restdocopenapi/restdoc/SpringRestDocsUnitTest.java b/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/restdocopenapi/restdoc/SpringRestDocsUnitTest.java new file mode 100644 index 0000000000..4d37abf78a --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/test/java/com/baeldung/restdocopenapi/restdoc/SpringRestDocsUnitTest.java @@ -0,0 +1,120 @@ +package com.baeldung.restdocopenapi.restdoc; + +import static org.hamcrest.Matchers.containsString; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.put; +import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; +import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse; +import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; +import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; +import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields; +import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; +import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName; +import static org.springframework.restdocs.request.RequestDocumentation.pathParameters; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.util.StringUtils.collectionToDelimitedString; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.hateoas.MediaTypes; +import org.springframework.restdocs.RestDocumentationContextProvider; +import org.springframework.restdocs.RestDocumentationExtension; +import org.springframework.restdocs.constraints.ConstraintDescriptions; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.restdocopenapi.Application; +import com.baeldung.restdocopenapi.Foo; +import com.fasterxml.jackson.databind.ObjectMapper; + +@ExtendWith({ RestDocumentationExtension.class, SpringExtension.class }) +@SpringBootTest(classes = Application.class) +public class SpringRestDocsUnitTest { + + private MockMvc mockMvc; + + @Autowired + private ObjectMapper objectMapper; + + @BeforeEach + public void setup(WebApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentation) { + this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) + .apply(documentationConfiguration(restDocumentation)) + .build(); + } + + @Test + public void whenGetFoo_thenSuccessful() throws Exception { + this.mockMvc.perform(get("/foo")) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("Foo 1"))) + .andDo(document("getAllFoos")); + } + + @Test + public void whenGetFooById_thenSuccessful() throws Exception { + ConstraintDescriptions desc = new ConstraintDescriptions(Foo.class); + + this.mockMvc.perform(get("/foo/{id}", 1)) + .andExpect(status().isOk()) + .andDo(document("getAFoo", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), + pathParameters(parameterWithName("id").description("id of foo to be searched")), + responseFields(fieldWithPath("id").description("The id of the foo" + collectionToDelimitedString(desc.descriptionsForProperty("id"), ". ")), + fieldWithPath("title").description("The title of the foo"), fieldWithPath("body").description("The body of the foo")))); + } + + @Test + public void whenPostFoo_thenSuccessful() throws Exception { + Map foo = new HashMap<>(); + foo.put("id", 4L); + foo.put("title", "New Foo"); + foo.put("body", "Body of New Foo"); + + this.mockMvc.perform(post("/foo").contentType(MediaTypes.HAL_JSON) + .content(this.objectMapper.writeValueAsString(foo))) + .andExpect(status().isCreated()) + .andDo(document("createFoo", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), requestFields(fieldWithPath("id").description("The id of the foo"), fieldWithPath("title").description("The title of the foo"), + fieldWithPath("body").description("The body of the foo")))); + } + + @Test + public void whenDeleteFoo_thenSuccessful() throws Exception { + this.mockMvc.perform(delete("/foo/{id}", 2)) + .andExpect(status().isNoContent()) + .andDo(document("deleteFoo", pathParameters(parameterWithName("id").description("The id of the foo to delete")))); + } + + @Test + public void whenUpdateFoo_thenSuccessful() throws Exception { + + ConstraintDescriptions desc = new ConstraintDescriptions(Foo.class); + + Map foo = new HashMap<>(); + foo.put("title", "Updated Foo"); + foo.put("body", "Body of Updated Foo"); + + this.mockMvc.perform(put("/foo/{id}", 3).contentType(MediaTypes.HAL_JSON) + .content(this.objectMapper.writeValueAsString(foo))) + .andExpect(status().isOk()) + .andDo(document("updateFoo", pathParameters(parameterWithName("id").description("The id of the foo to update")), + responseFields(fieldWithPath("id").description("The id of the updated foo" + collectionToDelimitedString(desc.descriptionsForProperty("id"), ". ")), + fieldWithPath("title").description("The title of the updated foo"), fieldWithPath("body").description("The body of the updated foo")))); + } + + +} diff --git a/spring-boot-modules/spring-boot/src/test/resources/README.md b/spring-boot-modules/spring-boot/src/test/resources/README.md new file mode 100644 index 0000000000..51c95afd9d --- /dev/null +++ b/spring-boot-modules/spring-boot/src/test/resources/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [How to Test GraphQL Using Postman](https://www.baeldung.com/graphql-postman) diff --git a/spring-caching/README.md b/spring-caching/README.md index 3efbfe3eaa..52ddc4f3eb 100644 --- a/spring-caching/README.md +++ b/spring-caching/README.md @@ -3,3 +3,4 @@ - [A Guide To Caching in Spring](http://www.baeldung.com/spring-cache-tutorial) - [Spring Cache – Creating a Custom KeyGenerator](http://www.baeldung.com/spring-cache-custom-keygenerator) - [Cache Eviction in Spring Boot](https://www.baeldung.com/spring-boot-evict-cache) +- [Using Multiple Cache Managers in Spring](https://www.baeldung.com/spring-multiple-cache-managers) diff --git a/spring-caching/src/test/java/com/baeldung/caching/boot/SimpleCacheCustomizerIntegrationTest.java b/spring-caching/src/test/java/com/baeldung/caching/boot/SimpleCacheCustomizerIntegrationTest.java index 56a4bd4745..3c83065c5c 100644 --- a/spring-caching/src/test/java/com/baeldung/caching/boot/SimpleCacheCustomizerIntegrationTest.java +++ b/spring-caching/src/test/java/com/baeldung/caching/boot/SimpleCacheCustomizerIntegrationTest.java @@ -10,7 +10,7 @@ import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) -@SpringBootTest +@SpringBootTest("spring.cache.type=simple") public class SimpleCacheCustomizerIntegrationTest { @Autowired diff --git a/spring-cloud/spring-cloud-aws/README.md b/spring-cloud/spring-cloud-aws/README.md index bf33728c74..ddcc97420f 100644 --- a/spring-cloud/spring-cloud-aws/README.md +++ b/spring-cloud/spring-cloud-aws/README.md @@ -23,7 +23,6 @@ Let's say that the RDS instance is called `spring-cloud-test-db` having the mast to write the following in `application.properties`: ``` -cloud.aws.rds.spring-cloud-test-db cloud.aws.rds.spring-cloud-test-db.password=se3retpass ``` Multiple application classes are available under this project. To launch InstanceProfileAwsApplication application, replace `start-class` under `pom.xml`: diff --git a/spring-cloud/spring-cloud-aws/pom.xml b/spring-cloud/spring-cloud-aws/pom.xml index abf9363288..2b05020888 100644 --- a/spring-cloud/spring-cloud-aws/pom.xml +++ b/spring-cloud/spring-cloud-aws/pom.xml @@ -10,9 +10,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../../parent-boot-1 + ../../parent-boot-2 @@ -43,6 +43,12 @@ org.postgresql postgresql + + + mysql + mysql-connector-java + + @@ -60,7 +66,7 @@ com.baeldung.spring.cloud.aws.SpringCloudAwsApplication Dalston.SR4 - 2.0.1.RELEASE + 2.2.1.RELEASE diff --git a/spring-cloud/spring-cloud-aws/src/main/java/com/baeldung/spring/cloud/aws/s3/SpringCloudS3.java b/spring-cloud/spring-cloud-aws/src/main/java/com/baeldung/spring/cloud/aws/s3/SpringCloudS3.java index cfad6e904f..acfd6d339e 100644 --- a/spring-cloud/spring-cloud-aws/src/main/java/com/baeldung/spring/cloud/aws/s3/SpringCloudS3.java +++ b/spring-cloud/spring-cloud-aws/src/main/java/com/baeldung/spring/cloud/aws/s3/SpringCloudS3.java @@ -1,12 +1,5 @@ package com.baeldung.spring.cloud.aws.s3; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.Resource; -import org.springframework.core.io.ResourceLoader; -import org.springframework.core.io.WritableResource; -import org.springframework.core.io.support.ResourcePatternResolver; -import org.springframework.stereotype.Component; - import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -14,14 +7,29 @@ import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.StandardCopyOption; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.aws.core.io.s3.PathMatchingSimpleStorageResourcePatternResolver; +import org.springframework.context.ApplicationContext; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.core.io.WritableResource; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.stereotype.Component; + +import com.amazonaws.services.s3.AmazonS3; + @Component public class SpringCloudS3 { @Autowired ResourceLoader resourceLoader; + private ResourcePatternResolver resourcePatternResolver; + @Autowired - ResourcePatternResolver resourcePatternResolver; + public void setupResolver(ApplicationContext applicationContext, AmazonS3 amazonS3) { + this.resourcePatternResolver = new PathMatchingSimpleStorageResourcePatternResolver(amazonS3, applicationContext); + } public void downloadS3Object(String s3Url) throws IOException { Resource resource = resourceLoader.getResource(s3Url); diff --git a/spring-cloud/spring-cloud-aws/src/main/resources/application.properties b/spring-cloud/spring-cloud-aws/src/main/resources/application.properties index 690eda13a2..e8139326b9 100644 --- a/spring-cloud/spring-cloud-aws/src/main/resources/application.properties +++ b/spring-cloud/spring-cloud-aws/src/main/resources/application.properties @@ -2,7 +2,6 @@ cloud.aws.credentials.accessKey=YourAccessKey cloud.aws.credentials.secretKey=YourSecretKey cloud.aws.region.static=us-east-1 -cloud.aws.rds.spring-cloud-test-db cloud.aws.rds.spring-cloud-test-db.password=se3retpass # These 3 properties are optional diff --git a/spring-cloud/spring-cloud-circuit-breaker/README.md b/spring-cloud/spring-cloud-circuit-breaker/README.md index 040eb0ccee..894be93408 100644 --- a/spring-cloud/spring-cloud-circuit-breaker/README.md +++ b/spring-cloud/spring-cloud-circuit-breaker/README.md @@ -3,3 +3,5 @@ This module contains articles about Spring Cloud Circuit Breaker ### Relevant Articles: + +- [Quick Guide to Spring Cloud Circuit Breaker](https://www.baeldung.com/spring-cloud-circuit-breaker) diff --git a/spring-cloud/spring-cloud-gateway/README.md b/spring-cloud/spring-cloud-gateway/README.md index 9c8e0d443a..90e81fe9a2 100644 --- a/spring-cloud/spring-cloud-gateway/README.md +++ b/spring-cloud/spring-cloud-gateway/README.md @@ -6,3 +6,4 @@ This module contains articles about Spring Cloud Gateway - [Exploring the new Spring Cloud Gateway](http://www.baeldung.com/spring-cloud-gateway) - [Writing Custom Spring Cloud Gateway Filters](https://www.baeldung.com/spring-cloud-custom-gateway-filters) - [Spring Cloud Gateway Routing Predicate Factories](https://www.baeldung.com/spring-cloud-gateway-routing-predicate-factories) +- [Spring Cloud Gateway WebFilter Factories](https://www.baeldung.com/spring-cloud-gateway-webfilter-factories) diff --git a/spring-cloud/spring-cloud-gateway/pom.xml b/spring-cloud/spring-cloud-gateway/pom.xml index 0f62c031cf..c692eed7ec 100644 --- a/spring-cloud/spring-cloud-gateway/pom.xml +++ b/spring-cloud/spring-cloud-gateway/pom.xml @@ -1,7 +1,7 @@ + xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring-cloud-gateway spring-cloud-gateway @@ -49,6 +49,26 @@ spring-cloud-starter-gateway + + + org.springframework.cloud + spring-cloud-starter-circuitbreaker-reactor-resilience4j + + + + + org.springframework.boot + spring-boot-starter-data-redis-reactive + + + + + it.ozimov + embedded-redis + ${redis.version} + test + + org.hibernate hibernate-validator-cdi @@ -69,8 +89,8 @@ test - org.springframework.boot - spring-boot-devtools + org.springframework.boot + spring-boot-devtools @@ -84,11 +104,10 @@ - Greenwich.SR3 - - - 2.1.9.RELEASE + Hoxton.SR3 + 2.2.6.RELEASE 6.0.2.Final + 0.7.2 diff --git a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/webfilters/WebFilterGatewayApplication.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/webfilters/WebFilterGatewayApplication.java new file mode 100644 index 0000000000..852e5cadba --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/webfilters/WebFilterGatewayApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.springcloudgateway.webfilters; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; + +@SpringBootApplication +public class WebFilterGatewayApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(WebFilterGatewayApplication.class) + .profiles("webfilters") + .run(args); + } + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/webfilters/config/ModifyBodyRouteConfig.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/webfilters/config/ModifyBodyRouteConfig.java new file mode 100644 index 0000000000..7b6188b66a --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/webfilters/config/ModifyBodyRouteConfig.java @@ -0,0 +1,49 @@ +package com.baeldung.springcloudgateway.webfilters.config; + +import org.springframework.cloud.gateway.route.RouteLocator; +import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; + +import reactor.core.publisher.Mono; + +@Configuration +public class ModifyBodyRouteConfig { + + @Bean + public RouteLocator routes(RouteLocatorBuilder builder) { + return builder.routes() + .route("modify_request_body", r -> r.path("/post") + .filters(f -> f.modifyRequestBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE, + (exchange, s) -> Mono.just(new Hello(s.toUpperCase())))).uri("https://httpbin.org")) + .build(); + } + + @Bean + public RouteLocator responseRoutes(RouteLocatorBuilder builder) { + return builder.routes() + .route("modify_response_body", r -> r.path("/put/**") + .filters(f -> f.modifyResponseBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE, + (exchange, s) -> Mono.just(new Hello("New Body")))).uri("https://httpbin.org")) + .build(); + } + + static class Hello { + String message; + + public Hello() { } + + public Hello(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + } +} diff --git a/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/webfilters/config/RequestRateLimiterResolverConfig.java b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/webfilters/config/RequestRateLimiterResolverConfig.java new file mode 100644 index 0000000000..f80a742fa6 --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/main/java/com/baeldung/springcloudgateway/webfilters/config/RequestRateLimiterResolverConfig.java @@ -0,0 +1,16 @@ +package com.baeldung.springcloudgateway.webfilters.config; + +import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import reactor.core.publisher.Mono; + +@Configuration +public class RequestRateLimiterResolverConfig { + + @Bean + KeyResolver userKeyResolver() { + return exchange -> Mono.just("1"); + } +} diff --git a/spring-cloud/spring-cloud-gateway/src/main/resources/application-webfilters.yml b/spring-cloud/spring-cloud-gateway/src/main/resources/application-webfilters.yml new file mode 100644 index 0000000000..3348cbbba0 --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/main/resources/application-webfilters.yml @@ -0,0 +1,102 @@ +logging: + level: + org.springframework.cloud.gateway: INFO + reactor.netty.http.client: INFO + +spring: + redis: + host: localhost + port: 6379 + cloud: + gateway: + routes: + - id: request_header_route + uri: https://httpbin.org + predicates: + - Path=/get/** + filters: + - AddRequestHeader=My-Header-Good,Good + - AddRequestHeader=My-Header-Remove,Remove + - AddRequestParameter=var, good + - AddRequestParameter=var2, remove + - MapRequestHeader=My-Header-Good, My-Header-Bad + - MapRequestHeader=My-Header-Set, My-Header-Bad + - SetRequestHeader=My-Header-Set, Set + - RemoveRequestHeader=My-Header-Remove + - RemoveRequestParameter=var2 + - PreserveHostHeader + + - id: response_header_route + uri: https://httpbin.org + predicates: + - Path=/header/post/** + filters: + - AddResponseHeader=My-Header-Good,Good + - AddResponseHeader=My-Header-Set,Good + - AddResponseHeader=My-Header-Rewrite, password=12345678 + - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin + - AddResponseHeader=My-Header-Remove,Remove + - SetResponseHeader=My-Header-Set, Set + - RemoveResponseHeader=My-Header-Remove + - RewriteResponseHeader=My-Header-Rewrite, password=[^&]+, password=*** + - RewriteLocationResponseHeader=AS_IN_REQUEST, Location, , + - StripPrefix=1 + + - id: path_route + uri: https://httpbin.org + predicates: + - Path=/new/post/** + filters: + - RewritePath=/new(?/?.*), $\{segment} + - SetPath=/post + + - id: redirect_route + uri: https://httpbin.org + predicates: + - Path=/fake/post/** + filters: + - RedirectTo=302, https://httpbin.org + + - id: status_route + uri: https://httpbin.org + predicates: + - Path=/delete/** + filters: + - SetStatus=401 + + - id: size_route + uri: https://httpbin.org + predicates: + - Path=/anything + filters: + - name: RequestSize + args: + maxSize: 5000000 + + - id: retry_test + uri: https://httpbin.org + predicates: + - Path=/status/502 + filters: + - name: Retry + args: + retries: 3 + statuses: BAD_GATEWAY + methods: GET,POST + backoff: + firstBackoff: 10ms + maxBackoff: 50ms + factor: 2 + basedOnPreviousValue: false + + - id: request_rate_limiter + uri: https://httpbin.org + predicates: + - Path=/redis/get/** + filters: + - StripPrefix=1 + - name: RequestRateLimiter + args: + redis-rate-limiter.replenishRate: 10 + redis-rate-limiter.burstCapacity: 5 + key-resolver: "#{@userKeyResolver}" \ No newline at end of file diff --git a/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/webfilters/RedisWebFilterFactoriesLiveTest.java b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/webfilters/RedisWebFilterFactoriesLiveTest.java new file mode 100644 index 0000000000..a28eb68775 --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/webfilters/RedisWebFilterFactoriesLiveTest.java @@ -0,0 +1,64 @@ +package com.baeldung.springcloudgateway.webfilters; + +import org.junit.After; +import org.junit.Before; +import org.junit.jupiter.api.RepeatedTest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; + +import redis.embedded.RedisServer; + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@ActiveProfiles("webfilters") +@TestConfiguration +public class RedisWebFilterFactoriesLiveTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(RedisWebFilterFactoriesLiveTest.class); + + private RedisServer redisServer; + + public RedisWebFilterFactoriesLiveTest() { + } + + @Before + public void postConstruct() { + this.redisServer = new RedisServer(6379); + redisServer.start(); + } + + @LocalServerPort + String port; + + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + TestRestTemplate template; + + @RepeatedTest(25) + public void whenCallRedisGetThroughGateway_thenOKStatusOrIsReceived() { + String url = "http://localhost:" + port + "/redis/get"; + + ResponseEntity r = restTemplate.getForEntity(url, String.class); + // assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + LOGGER.info("Received: status->{}, reason->{}, remaining->{}", + r.getStatusCodeValue(), r.getStatusCode().getReasonPhrase(), + r.getHeaders().get("X-RateLimit-Remaining")); + } + + @After + public void preDestroy() { + redisServer.stop(); + } + +} diff --git a/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/webfilters/WebFilterFactoriesLiveTest.java b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/webfilters/WebFilterFactoriesLiveTest.java new file mode 100644 index 0000000000..67e00a42fc --- /dev/null +++ b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/webfilters/WebFilterFactoriesLiveTest.java @@ -0,0 +1,136 @@ +package com.baeldung.springcloudgateway.webfilters; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.assertj.core.api.Condition; +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec; + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@ActiveProfiles("webfilters") +public class WebFilterFactoriesLiveTest { + + @LocalServerPort + String port; + + @Autowired + private WebTestClient client; + + @Autowired + private TestRestTemplate restTemplate; + + @BeforeEach + public void configureClient() { + client = WebTestClient.bindToServer() + .baseUrl("http://localhost:" + port) + .build(); + } + + @Test + public void whenCallGetThroughGateway_thenAllHTTPRequestHeadersParametersAreSet() throws JSONException { + String url = "http://localhost:" + port + "/get"; + ResponseEntity response = restTemplate.getForEntity(url, String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + JSONObject json = new JSONObject(response.getBody()); + JSONObject headers = json.getJSONObject("headers"); + assertThat(headers.getString("My-Header-Good")).isEqualTo("Good"); + assertThat(headers.getString("My-Header-Bad")).isEqualTo("Good"); + assertThat(headers.getString("My-Header-Set")).isEqualTo("Set"); + assertTrue(headers.isNull("My-Header-Remove")); + JSONObject vars = json.getJSONObject("args"); + assertThat(vars.getString("var")).isEqualTo("good"); + } + + @Test + public void whenCallHeaderPostThroughGateway_thenAllHTTPResponseHeadersAreSet() { + ResponseSpec response = client.post() + .uri("/header/post") + .exchange(); + + response.expectStatus() + .isOk() + .expectHeader() + .valueEquals("My-Header-Rewrite", "password=***") + .expectHeader() + .valueEquals("My-Header-Set", "Set") + .expectHeader() + .valueEquals("My-Header-Good", "Good") + .expectHeader() + .doesNotExist("My-Header-Remove"); + } + + @Test + public void whenCallPostThroughGateway_thenBodyIsRetrieved() throws JSONException { + String url = "http://localhost:" + port + "/post"; + + HttpEntity entity = new HttpEntity<>("content", new HttpHeaders()); + + ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + JSONObject json = new JSONObject(response.getBody()); + JSONObject data = json.getJSONObject("json"); + assertThat(data.getString("message")).isEqualTo("CONTENT"); + } + + @Test + public void whenCallPutThroughGateway_thenBodyIsRetrieved() throws JSONException { + String url = "http://localhost:" + port + "/put"; + + HttpEntity entity = new HttpEntity<>("CONTENT", new HttpHeaders()); + + ResponseEntity response = restTemplate.exchange(url, HttpMethod.PUT, entity, String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + JSONObject json = new JSONObject(response.getBody()); + assertThat(json.getString("message")).isEqualTo("New Body"); + } + + @Test + public void whenCallDeleteThroughGateway_thenIsUnauthorizedCodeIsSet() { + ResponseSpec response = client.delete() + .uri("/delete") + .exchange(); + + response.expectStatus() + .isUnauthorized(); + } + + @Test + public void whenCallFakePostThroughGateway_thenIsUnauthorizedCodeIsSet() { + ResponseSpec response = client.post() + .uri("/fake/post") + .exchange(); + + response.expectStatus() + .is3xxRedirection(); + } + + @Test + public void whenCallStatus504ThroughGateway_thenCircuitBreakerIsExecuted() throws JSONException { + String url = "http://localhost:" + port + "/status/504"; + ResponseEntity response = restTemplate.getForEntity(url, String.class); + + JSONObject json = new JSONObject(response.getBody()); + assertThat(json.getString("url")).contains("anything"); + } +} diff --git a/spring-cloud/spring-cloud-gateway/src/test/resources/logback-test.xml b/spring-cloud/spring-cloud-gateway/src/test/resources/logback-test.xml index 6fcdb6317f..6980d119b1 100644 --- a/spring-cloud/spring-cloud-gateway/src/test/resources/logback-test.xml +++ b/spring-cloud/spring-cloud-gateway/src/test/resources/logback-test.xml @@ -3,7 +3,15 @@ + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + \ No newline at end of file diff --git a/spring-core-2/README.md b/spring-core-2/README.md index 947b816db8..10d3080b45 100644 --- a/spring-core-2/README.md +++ b/spring-core-2/README.md @@ -14,4 +14,5 @@ This module contains articles about core Spring functionality - [Spring Events](https://www.baeldung.com/spring-events) - [Spring Null-Safety Annotations](https://www.baeldung.com/spring-null-safety-annotations) - [Using @Autowired in Abstract Classes](https://www.baeldung.com/spring-autowired-abstract-class) +- [Running Setup Data on Startup in Spring](https://www.baeldung.com/running-setup-logic-on-startup-in-spring) - More articles: [[<-- prev]](/spring-core)[[next -->]](/spring-core-3) diff --git a/spring-core-4/README.md b/spring-core-4/README.md index f882c77179..592f4cd011 100644 --- a/spring-core-4/README.md +++ b/spring-core-4/README.md @@ -4,4 +4,6 @@ This module contains articles about core Spring functionality ## Relevant Articles: +- [Creating Spring Beans Through Factory Methods](https://www.baeldung.com/spring-beans-factory-methods) +- [How to dynamically Autowire a Bean in Spring](https://www.baeldung.com/spring-dynamic-autowire) - More articles: [[<-- prev]](/spring-core-3) diff --git a/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/BeanFactoryDynamicAutowireService.java b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/BeanFactoryDynamicAutowireService.java new file mode 100644 index 0000000000..4ad4420489 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/BeanFactoryDynamicAutowireService.java @@ -0,0 +1,27 @@ +package com.baeldung.dynamic.autowire; + +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class BeanFactoryDynamicAutowireService { + private static final String SERVICE_NAME_SUFFIX = "regionService"; + private final BeanFactory beanFactory; + + @Autowired + public BeanFactoryDynamicAutowireService(BeanFactory beanFactory) { + this.beanFactory = beanFactory; + } + + public boolean isServerActive(String isoCountryCode, int serverId) { + RegionService service = beanFactory.getBean(getRegionServiceBeanName(isoCountryCode), RegionService.class); + + return service.isServerActive(serverId); + } + + private String getRegionServiceBeanName(String isoCountryCode) { + return isoCountryCode + SERVICE_NAME_SUFFIX; + } + +} diff --git a/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/CustomMapFromListDynamicAutowireService.java b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/CustomMapFromListDynamicAutowireService.java new file mode 100644 index 0000000000..e04c345d51 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/CustomMapFromListDynamicAutowireService.java @@ -0,0 +1,26 @@ +package com.baeldung.dynamic.autowire; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +@Service +public class CustomMapFromListDynamicAutowireService { + private final Map servicesByCountryCode; + + @Autowired + public CustomMapFromListDynamicAutowireService(List regionServices) { + servicesByCountryCode = regionServices.stream() + .collect(Collectors.toMap(RegionService::getISOCountryCode, Function.identity())); + } + + public boolean isServerActive(String isoCountryCode, int serverId) { + RegionService service = servicesByCountryCode.get(isoCountryCode); + + return service.isServerActive(serverId); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/DynamicAutowireConfig.java b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/DynamicAutowireConfig.java new file mode 100644 index 0000000000..256bd9b495 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/DynamicAutowireConfig.java @@ -0,0 +1,9 @@ +package com.baeldung.dynamic.autowire; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.baeldung.dynamic.autowire") +public class DynamicAutowireConfig { +} diff --git a/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/GBRegionService.java b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/GBRegionService.java new file mode 100644 index 0000000000..8c6a1372d4 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/GBRegionService.java @@ -0,0 +1,16 @@ +package com.baeldung.dynamic.autowire; + +import org.springframework.stereotype.Service; + +@Service("GBregionService") +public class GBRegionService implements RegionService { + @Override + public boolean isServerActive(int serverId) { + return false; + } + + @Override + public String getISOCountryCode() { + return "GB"; + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/RegionService.java b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/RegionService.java new file mode 100644 index 0000000000..a2caf38ab3 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/RegionService.java @@ -0,0 +1,7 @@ +package com.baeldung.dynamic.autowire; + +public interface RegionService { + boolean isServerActive(int serverId); + + String getISOCountryCode(); +} diff --git a/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/USRegionService.java b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/USRegionService.java new file mode 100644 index 0000000000..a2d5f47553 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/dynamic/autowire/USRegionService.java @@ -0,0 +1,16 @@ +package com.baeldung.dynamic.autowire; + +import org.springframework.stereotype.Service; + +@Service("USregionService") +public class USRegionService implements RegionService { + @Override + public boolean isServerActive(int serverId) { + return true; + } + + @Override + public String getISOCountryCode() { + return "US"; + } +} diff --git a/spring-core-4/src/test/java/com/baeldung/dynamic/autowire/DynamicAutowireIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/dynamic/autowire/DynamicAutowireIntegrationTest.java new file mode 100644 index 0000000000..56582ecb66 --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/dynamic/autowire/DynamicAutowireIntegrationTest.java @@ -0,0 +1,33 @@ +package com.baeldung.dynamic.autowire; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = DynamicAutowireConfig.class) +public class DynamicAutowireIntegrationTest { + + @Autowired + private BeanFactoryDynamicAutowireService beanFactoryDynamicAutowireService; + + @Autowired + private CustomMapFromListDynamicAutowireService customMapFromListDynamicAutowireService; + + @Test + public void givenDynamicallyAutowiredBean_whenCheckingServerInGB_thenServerIsNotActive() { + assertThat(beanFactoryDynamicAutowireService.isServerActive("GB", 101), is(false)); + assertThat(customMapFromListDynamicAutowireService.isServerActive("GB", 101), is(false)); + } + + @Test + public void givenDynamicallyAutowiredBean_whenCheckingServerInUS_thenServerIsActive() { + assertThat(beanFactoryDynamicAutowireService.isServerActive("US", 101), is(true)); + assertThat(customMapFromListDynamicAutowireService.isServerActive("US", 101), is(true)); + } +} diff --git a/spring-data-rest-querydsl/pom.xml b/spring-data-rest-querydsl/pom.xml index c0ad43fe0b..5e47f4979e 100644 --- a/spring-data-rest-querydsl/pom.xml +++ b/spring-data-rest-querydsl/pom.xml @@ -9,9 +9,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../parent-boot-1 + ../parent-boot-2 diff --git a/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/AddressRepository.java b/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/AddressRepository.java index 2e88820c98..476a11ea6b 100644 --- a/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/AddressRepository.java +++ b/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/AddressRepository.java @@ -5,13 +5,13 @@ import com.baeldung.entity.QAddress; import com.querydsl.core.types.dsl.StringExpression; import com.querydsl.core.types.dsl.StringPath; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.querydsl.QueryDslPredicateExecutor; +import org.springframework.data.querydsl.QuerydslPredicateExecutor; import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer; import org.springframework.data.querydsl.binding.QuerydslBindings; import org.springframework.data.querydsl.binding.SingleValueBinding; public interface AddressRepository - extends JpaRepository, QueryDslPredicateExecutor
, QuerydslBinderCustomizer { + extends JpaRepository, QuerydslPredicateExecutor
, QuerydslBinderCustomizer { @Override default void customize(final QuerydslBindings bindings, final QAddress root) { bindings.bind(String.class).first((SingleValueBinding) StringExpression::eq); diff --git a/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/UserRepository.java b/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/UserRepository.java index 98ff2ac5e3..b5f32b3624 100644 --- a/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/UserRepository.java +++ b/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/UserRepository.java @@ -5,13 +5,13 @@ import com.baeldung.entity.User; import com.querydsl.core.types.dsl.StringExpression; import com.querydsl.core.types.dsl.StringPath; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.querydsl.QueryDslPredicateExecutor; +import org.springframework.data.querydsl.QuerydslPredicateExecutor; import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer; import org.springframework.data.querydsl.binding.QuerydslBindings; import org.springframework.data.querydsl.binding.SingleValueBinding; public interface UserRepository - extends JpaRepository, QueryDslPredicateExecutor, QuerydslBinderCustomizer { + extends JpaRepository, QuerydslPredicateExecutor, QuerydslBinderCustomizer { @Override default void customize(final QuerydslBindings bindings, final QUser root) { bindings.bind(String.class).first((SingleValueBinding) StringExpression::eq); diff --git a/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/IntegrationTest.java b/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/IntegrationTest.java index 2d3dbc4c74..ad19c441b6 100644 --- a/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/IntegrationTest.java +++ b/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/IntegrationTest.java @@ -13,8 +13,6 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import java.nio.charset.Charset; - import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -24,7 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. public class IntegrationTest { final MediaType contentType = - new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); + new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype()); @Autowired private WebApplicationContext webApplicationContext; diff --git a/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/QuerydslIntegrationTest.java b/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/QuerydslIntegrationTest.java index 11e5ffca05..768d28347b 100644 --- a/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/QuerydslIntegrationTest.java +++ b/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/QuerydslIntegrationTest.java @@ -13,8 +13,6 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import java.nio.charset.Charset; - import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -26,7 +24,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. public class QuerydslIntegrationTest { final MediaType contentType = - new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); + new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype()); @Autowired private WebApplicationContext webApplicationContext; diff --git a/spring-jenkins-pipeline/pom.xml b/spring-jenkins-pipeline/pom.xml index aa6008162c..38d4ed15de 100644 --- a/spring-jenkins-pipeline/pom.xml +++ b/spring-jenkins-pipeline/pom.xml @@ -10,9 +10,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../parent-boot-1 + ../parent-boot-2 diff --git a/spring-jenkins-pipeline/src/test/java/com/baeldung/SomeIntegrationTest.java b/spring-jenkins-pipeline/src/test/java/com/baeldung/SomeIntegrationTest.java index 477a7d2adb..9033d10c5d 100644 --- a/spring-jenkins-pipeline/src/test/java/com/baeldung/SomeIntegrationTest.java +++ b/spring-jenkins-pipeline/src/test/java/com/baeldung/SomeIntegrationTest.java @@ -13,7 +13,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.assertNotEquals; @RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = {SpringJenkinsPipelineApplication.class, TestMongoConfig.class }) +@SpringBootTest(classes = {SpringJenkinsPipelineApplication.class}) public class SomeIntegrationTest { @Autowired private StudentRepository studentRepository; diff --git a/spring-jenkins-pipeline/src/test/java/com/baeldung/TestMongoConfig.java b/spring-jenkins-pipeline/src/test/java/com/baeldung/TestMongoConfig.java deleted file mode 100644 index a85491cf7e..0000000000 --- a/spring-jenkins-pipeline/src/test/java/com/baeldung/TestMongoConfig.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung; - -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration; -import org.springframework.context.annotation.Configuration; - -@Configuration -@EnableAutoConfiguration(exclude = { EmbeddedMongoAutoConfiguration.class }) -public class TestMongoConfig { - -} \ No newline at end of file diff --git a/spring-jinq/pom.xml b/spring-jinq/pom.xml index 29fc3605d7..073808823c 100644 --- a/spring-jinq/pom.xml +++ b/spring-jinq/pom.xml @@ -9,9 +9,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../parent-boot-1 + ../parent-boot-2 @@ -31,6 +31,17 @@ org.hibernate hibernate-entitymanager + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + net.bytebuddy + byte-buddy-dep + ${bytebuddy.version} + @@ -61,7 +72,8 @@ - 1.8.22 + 1.8.29 + 1.10.10 diff --git a/spring-jinq/src/main/resources/application.properties b/spring-jinq/src/main/resources/application.properties index dc73bed0c5..c9440b3b45 100644 --- a/spring-jinq/src/main/resources/application.properties +++ b/spring-jinq/src/main/resources/application.properties @@ -1,4 +1,4 @@ -spring.datasource.url=jdbc:h2:~/jinq +spring.datasource.url=jdbc:h2:~/jinq;;DB_CLOSE_ON_EXIT=FALSE spring.datasource.username=sa spring.datasource.password= diff --git a/spring-kafka/pom.xml b/spring-kafka/pom.xml index d60a2ee506..2b4a0914e6 100644 --- a/spring-kafka/pom.xml +++ b/spring-kafka/pom.xml @@ -33,7 +33,7 @@ - 2.2.7.RELEASE + 2.3.7.RELEASE \ No newline at end of file diff --git a/spring-mobile/pom.xml b/spring-mobile/pom.xml index ff90ac6ecb..465458ba49 100644 --- a/spring-mobile/pom.xml +++ b/spring-mobile/pom.xml @@ -10,9 +10,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../parent-boot-1 + ../parent-boot-2 @@ -23,11 +23,24 @@ org.springframework.mobile spring-mobile-device + ${spring-mobile-device.version} org.springframework.boot spring-boot-starter-freemarker - + + + spring-milestones + Spring Milestones + https://repo.spring.io/libs-milestone + + false + + + + + 2.0.0.M3 + diff --git a/spring-mobile/src/main/java/com/baeldung/AppConfig.java b/spring-mobile/src/main/java/com/baeldung/AppConfig.java new file mode 100644 index 0000000000..efa073ae11 --- /dev/null +++ b/spring-mobile/src/main/java/com/baeldung/AppConfig.java @@ -0,0 +1,36 @@ +package com.baeldung; + +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver; +import org.springframework.mobile.device.DeviceResolverHandlerInterceptor; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class AppConfig implements WebMvcConfigurer { + + @Bean + public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() { + return new DeviceResolverHandlerInterceptor(); + } + + @Bean + public DeviceHandlerMethodArgumentResolver deviceHandlerMethodArgumentResolver() { + return new DeviceHandlerMethodArgumentResolver(); + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(deviceResolverHandlerInterceptor()); + } + + @Override + public void addArgumentResolvers(List argumentResolvers) { + argumentResolvers.add(deviceHandlerMethodArgumentResolver()); + } + +} diff --git a/spring-mobile/src/main/java/com/baeldung/controller/IndexController.java b/spring-mobile/src/main/java/com/baeldung/controller/IndexController.java index 196fb680e7..49880f355a 100644 --- a/spring-mobile/src/main/java/com/baeldung/controller/IndexController.java +++ b/spring-mobile/src/main/java/com/baeldung/controller/IndexController.java @@ -16,13 +16,16 @@ public class IndexController { String deviceType = "browser"; String platform = "browser"; + String viewName = "index"; if (device.isNormal()) { deviceType = "browser"; } else if (device.isMobile()) { deviceType = "mobile"; + viewName = "mobile/index"; } else if (device.isTablet()) { deviceType = "tablet"; + viewName = "tablet/index"; } platform = device.getDevicePlatform().name(); @@ -33,7 +36,7 @@ public class IndexController { LOGGER.info("Client Device Type: " + deviceType + ", Platform: " + platform); - return "index"; + return viewName; } } diff --git a/spring-mobile/src/main/resources/application.properties b/spring-mobile/src/main/resources/application.properties index c0bc91f9ac..7d964f48fb 100644 --- a/spring-mobile/src/main/resources/application.properties +++ b/spring-mobile/src/main/resources/application.properties @@ -1 +1,3 @@ -spring.mobile.devicedelegatingviewresolver.enabled: true \ No newline at end of file +spring.mobile.devicedelegatingviewresolver.enabled: true +spring.freemarker.template-loader-path: classpath:/templates +spring.freemarker.suffix: .ftl \ No newline at end of file diff --git a/spring-mvc-basics-2/README.md b/spring-mvc-basics-2/README.md index e52459bd6e..673b7b1fef 100644 --- a/spring-mvc-basics-2/README.md +++ b/spring-mvc-basics-2/README.md @@ -9,7 +9,7 @@ This module contains articles about Spring MVC - [Servlet Redirect vs Forward](https://www.baeldung.com/servlet-redirect-forward) - [Apache Tiles Integration with Spring MVC](https://www.baeldung.com/spring-mvc-apache-tiles) - [Guide to Spring Email](https://www.baeldung.com/spring-email) -- [Using ThymeLeaf and FreeMarker Emails Templates with Spring](https://www.baeldung.com/thymeleaf-freemarker-email) +- [Using ThymeLeaf and FreeMarker Emails Templates with Spring](https://www.baeldung.com/spring-email-templates) - [Request Method Not Supported (405) in Spring](https://www.baeldung.com/spring-request-method-not-supported-405) - [Spring @RequestParam Annotation](https://www.baeldung.com/spring-request-param) - More articles: [[more -->]](/spring-mvc-basics-3) diff --git a/spring-mvc-java-2/README.md b/spring-mvc-java-2/README.md index b5d5df3cd4..09c8d8b294 100644 --- a/spring-mvc-java-2/README.md +++ b/spring-mvc-java-2/README.md @@ -1,3 +1,6 @@ ### Relevant Articles: - [Cache Headers in Spring MVC](https://www.baeldung.com/spring-mvc-cache-headers) +- [Working with Date Parameters in Spring](https://www.baeldung.com/spring-date-parameters) +- [Spring MVC @PathVariable with a dot (.) gets truncated](https://www.baeldung.com/spring-mvc-pathvariable-dot) +- [A Quick Guide to Spring MVC Matrix Variables](https://www.baeldung.com/spring-mvc-matrix-variables) \ No newline at end of file diff --git a/spring-mvc-java-2/pom.xml b/spring-mvc-java-2/pom.xml index d5b7d087ab..af622321cb 100644 --- a/spring-mvc-java-2/pom.xml +++ b/spring-mvc-java-2/pom.xml @@ -7,14 +7,14 @@ 0.1-SNAPSHOT spring-mvc-java-2 war - + com.baeldung parent-boot-2 0.0.1-SNAPSHOT ../parent-boot-2 - + javax.servlet @@ -26,14 +26,27 @@ spring-webmvc ${spring.mvc.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - + + + spring-mvc-java-2 + + + src/main/resources + true + + + + 4.0.1 5.2.2.RELEASE - \ No newline at end of file diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java b/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheWebConfig.java similarity index 96% rename from spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java rename to spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheWebConfig.java index 2f07912e80..95367077bd 100644 --- a/spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheWebConfig.java @@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit; @EnableWebMvc @Configuration @ComponentScan(basePackages = {"com.baeldung.cache"}) -public class WebConfig implements WebMvcConfigurer { +public class CacheWebConfig implements WebMvcConfigurer { @Override public void addViewControllers(final ViewControllerRegistry registry) { diff --git a/spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeConfig.java b/spring-mvc-java-2/src/main/java/com/baeldung/datetime/DateTimeConfig.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeConfig.java rename to spring-mvc-java-2/src/main/java/com/baeldung/datetime/DateTimeConfig.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeController.java b/spring-mvc-java-2/src/main/java/com/baeldung/datetime/DateTimeController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeController.java rename to spring-mvc-java-2/src/main/java/com/baeldung/datetime/DateTimeController.java diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/matrix/config/MatrixWebConfig.java b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/config/MatrixWebConfig.java new file mode 100644 index 0000000000..489740fd33 --- /dev/null +++ b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/config/MatrixWebConfig.java @@ -0,0 +1,18 @@ +package com.baeldung.matrix.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.util.UrlPathHelper; + +@Configuration +public class MatrixWebConfig implements WebMvcConfigurer { + + @Override + public void configurePathMatch(PathMatchConfigurer configurer) { + final UrlPathHelper urlPathHelper = new UrlPathHelper(); + urlPathHelper.setRemoveSemicolonContent(false); + + configurer.setUrlPathHelper(urlPathHelper); + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/CompanyController.java similarity index 81% rename from spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java rename to spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/CompanyController.java index af1e729c13..7a21ded026 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/CompanyController.java @@ -1,23 +1,16 @@ -package com.baeldung.web.controller; - -import java.util.HashMap; -import java.util.Map; +package com.baeldung.matrix.controller; +import com.baeldung.matrix.model.Company; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; -import org.springframework.web.bind.annotation.MatrixVariable; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; -import com.baeldung.model.Company; +import java.util.HashMap; +import java.util.Map; @Controller public class CompanyController { diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/EmployeeController.java similarity index 86% rename from spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java rename to spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/EmployeeController.java index 251287dff8..3f9de2179a 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/EmployeeController.java @@ -1,32 +1,22 @@ -package com.baeldung.web.controller; +package com.baeldung.matrix.controller; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; +import com.baeldung.matrix.model.Employee; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; -import org.springframework.web.bind.annotation.MatrixVariable; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.SessionAttributes; +import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; -import com.baeldung.model.Employee; +import java.util.*; @SessionAttributes("employees") @Controller public class EmployeeController { - Map employeeMap = new HashMap<>(); + public Map employeeMap = new HashMap<>(); @ModelAttribute("employees") public void initEmployees() { diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/Company.java b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Company.java similarity index 94% rename from spring-mvc-java/src/main/java/com/baeldung/model/Company.java rename to spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Company.java index 558507268a..cdf6cb0fd6 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/model/Company.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Company.java @@ -1,4 +1,4 @@ -package com.baeldung.model; +package com.baeldung.matrix.model; public class Company { diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Employee.java similarity index 97% rename from spring-mvc-java/src/main/java/com/baeldung/model/Employee.java rename to spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Employee.java index fb0a452219..c3384122b4 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Employee.java @@ -1,4 +1,4 @@ -package com.baeldung.model; +package com.baeldung.matrix.model; import javax.xml.bind.annotation.XmlRootElement; diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/CustomWebMvcConfigurationSupport.java b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/CustomWebMvcConfigurationSupport.java similarity index 93% rename from spring-mvc-java/src/main/java/com/baeldung/spring/web/config/CustomWebMvcConfigurationSupport.java rename to spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/CustomWebMvcConfigurationSupport.java index a0dd7358d0..12c208c623 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/CustomWebMvcConfigurationSupport.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/CustomWebMvcConfigurationSupport.java @@ -1,4 +1,4 @@ -package com.baeldung.spring.web.config; +package com.baeldung.pathvariable; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/SiteController.java b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/SiteController.java similarity index 69% rename from spring-mvc-java/src/main/java/com/baeldung/web/controller/SiteController.java rename to spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/SiteController.java index 3867380665..493161b0eb 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/SiteController.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/SiteController.java @@ -1,27 +1,30 @@ -package com.baeldung.web.controller; +package com.baeldung.pathvariable; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +@RestController @RequestMapping("/site") public class SiteController { - @RequestMapping(value = "/{firstValue}/{secondValue}", method = RequestMethod.GET) + @GetMapping("/{firstValue}/{secondValue}") public String requestWithError(@PathVariable("firstValue") String firstValue, @PathVariable("secondValue") String secondValue) { return firstValue + " - " + secondValue; } - @RequestMapping(value = "/{firstValue}/{secondValue:.+}", method = RequestMethod.GET) + @GetMapping("/{firstValue}/{secondValue:.+}") public String requestWithRegex(@PathVariable("firstValue") String firstValue, @PathVariable("secondValue") String secondValue) { return firstValue + " - " + secondValue; } - @RequestMapping(value = "/{firstValue}/{secondValue}/", method = RequestMethod.GET) + @GetMapping("/{firstValue}/{secondValue}/") public String requestWithSlash(@PathVariable("firstValue") String firstValue, @PathVariable("secondValue") String secondValue) { diff --git a/spring-mvc-java-2/src/main/webapp/WEB-INF/mvc-servlet.xml b/spring-mvc-java-2/src/main/webapp/WEB-INF/mvc-servlet.xml new file mode 100644 index 0000000000..00dac5f8cb --- /dev/null +++ b/spring-mvc-java-2/src/main/webapp/WEB-INF/mvc-servlet.xml @@ -0,0 +1,27 @@ + + + + + + + + /WEB-INF/view/ + + + .jsp + + + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/companyHome.jsp b/spring-mvc-java-2/src/main/webapp/WEB-INF/view/companyHome.jsp similarity index 100% rename from spring-mvc-java/src/main/webapp/WEB-INF/view/companyHome.jsp rename to spring-mvc-java-2/src/main/webapp/WEB-INF/view/companyHome.jsp diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/companyView.jsp b/spring-mvc-java-2/src/main/webapp/WEB-INF/view/companyView.jsp similarity index 100% rename from spring-mvc-java/src/main/webapp/WEB-INF/view/companyView.jsp rename to spring-mvc-java-2/src/main/webapp/WEB-INF/view/companyView.jsp diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp b/spring-mvc-java-2/src/main/webapp/WEB-INF/view/employeeHome.jsp similarity index 100% rename from spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp rename to spring-mvc-java-2/src/main/webapp/WEB-INF/view/employeeHome.jsp diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp b/spring-mvc-java-2/src/main/webapp/WEB-INF/view/employeeView.jsp similarity index 100% rename from spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp rename to spring-mvc-java-2/src/main/webapp/WEB-INF/view/employeeView.jsp diff --git a/spring-mvc-java-2/src/main/webapp/WEB-INF/web.xml b/spring-mvc-java-2/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..86a24e7646 --- /dev/null +++ b/spring-mvc-java-2/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,22 @@ + + + Spring MVC Application 2 + + + mvc + + org.springframework.web.servlet.DispatcherServlet + + + contextConfigLocation + /WEB-INF/mvc-servlet.xml + + 1 + + + + mvc + / + + \ No newline at end of file diff --git a/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java b/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java index 7acfe5e480..1e34dd182b 100644 --- a/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java +++ b/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java @@ -19,7 +19,7 @@ import static org.springframework.http.HttpHeaders.IF_UNMODIFIED_SINCE; @ExtendWith(SpringExtension.class) @WebAppConfiguration -@ContextConfiguration(classes = {WebConfig.class, WebConfig.class}) +@ContextConfiguration(classes = {CacheWebConfig.class, CacheWebConfig.class}) public class CacheControlControllerIntegrationTest { @Autowired diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeMvcIntegrationTest.java b/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeMvcIntegrationTest.java similarity index 81% rename from spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeMvcIntegrationTest.java rename to spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeMvcIntegrationTest.java index 86420a5fbd..c061c1efc7 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeMvcIntegrationTest.java +++ b/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeMvcIntegrationTest.java @@ -1,11 +1,7 @@ -package com.baeldung.web.controller; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +package com.baeldung.matrix; +import com.baeldung.matrix.config.MatrixWebConfig; +import com.baeldung.matrix.controller.EmployeeController; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -18,11 +14,13 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import com.baeldung.spring.web.config.WebConfig; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration(classes = WebConfig.class) +@ContextConfiguration(classes = { MatrixWebConfig.class, EmployeeController.class }) public class EmployeeMvcIntegrationTest { @Autowired diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeNoMvcIntegrationTest.java b/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeNoMvcIntegrationTest.java similarity index 86% rename from spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeNoMvcIntegrationTest.java rename to spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeNoMvcIntegrationTest.java index e84c20c973..2ca70cc0b9 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeNoMvcIntegrationTest.java +++ b/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeNoMvcIntegrationTest.java @@ -1,5 +1,8 @@ -package com.baeldung.web.controller; +package com.baeldung.matrix; +import com.baeldung.matrix.config.MatrixWebConfig; +import com.baeldung.matrix.controller.EmployeeController; +import com.baeldung.matrix.model.Employee; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -9,12 +12,9 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -import com.baeldung.model.Employee; -import com.baeldung.spring.web.config.WebConfig; - @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration(classes = WebConfig.class) +@ContextConfiguration(classes = { MatrixWebConfig.class, EmployeeController.class }) public class EmployeeNoMvcIntegrationTest { @Autowired diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index f1263860f9..877d92901a 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -8,12 +8,9 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: - [Integration Testing in Spring](https://www.baeldung.com/integration-testing-in-spring) -- [A Quick Guide to Spring MVC Matrix Variables](https://www.baeldung.com/spring-mvc-matrix-variables) - [File Upload with Spring MVC](https://www.baeldung.com/spring-file-upload) - [Introduction to HtmlUnit](https://www.baeldung.com/htmlunit) - [Upload and Display Excel Files with Spring MVC](https://www.baeldung.com/spring-mvc-excel-files) - [web.xml vs Initializer with Spring](https://www.baeldung.com/spring-xml-vs-java-config) -- [Spring MVC @PathVariable with a dot (.) gets truncated](https://www.baeldung.com/spring-mvc-pathvariable-dot) -- [Working with Date Parameters in Spring](https://www.baeldung.com/spring-date-parameters) - [A Java Web Application Without a web.xml](https://www.baeldung.com/java-web-app-without-web-xml) - [Accessing Spring MVC Model Objects in JavaScript](https://www.baeldung.com/spring-mvc-model-objects-js) diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 079a664a5d..9e3457aa8a 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -69,6 +69,12 @@ commons-io ${commons-io.version} + + commons-fileupload + commons-fileupload + ${commons-fileupload.version} + + @@ -237,6 +243,7 @@ 3.5 1.3.2 2.5 + 1.4 2.2.0 diff --git a/spring-rest-hal-browser/pom.xml b/spring-rest-hal-browser/pom.xml index adef8bf2b0..32a0b52875 100644 --- a/spring-rest-hal-browser/pom.xml +++ b/spring-rest-hal-browser/pom.xml @@ -9,9 +9,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../parent-boot-1 + ../parent-boot-2 @@ -19,25 +19,26 @@ org.springframework.boot spring-boot-starter-web - ${spring-boot.version} org.springframework.boot spring-boot-starter-data-jpa - ${spring-boot.version} org.springframework.data spring-data-rest-hal-browser - ${spring-data.version} com.h2database h2 - ${h2.version} + + + net.bytebuddy + byte-buddy-dep + ${bytebuddy.version} @@ -55,9 +56,7 @@ - 2.0.3.RELEASE - 3.0.8.RELEASE - 1.4.197 + 1.10.10 1.8 1.8 diff --git a/spring-security-modules/pom.xml b/spring-security-modules/pom.xml index 49a0db03ed..7ce33dd3e3 100644 --- a/spring-security-modules/pom.xml +++ b/spring-security-modules/pom.xml @@ -31,6 +31,7 @@ spring-security-mvc-persisted-remember-me spring-security-mvc-socket spring-security-oidc + spring-security-okta spring-security-react spring-security-rest spring-security-rest-basic-auth diff --git a/spring-security-modules/spring-security-core/README.md b/spring-security-modules/spring-security-core/README.md index e42dfecaa0..f28b3abb2b 100644 --- a/spring-security-modules/spring-security-core/README.md +++ b/spring-security-modules/spring-security-core/README.md @@ -8,6 +8,7 @@ This module contains articles about core Spring Security - [Introduction to Spring Method Security](https://www.baeldung.com/spring-security-method-security) - [Overview and Need for DelegatingFilterProxy in Spring](https://www.baeldung.com/spring-delegating-filter-proxy) - [Deny Access on Missing @PreAuthorize to Spring Controller Methods](https://www.baeldung.com/spring-deny-access) +- [Spring Security: Check If a User Has a Role in Java](https://www.baeldung.com/spring-security-check-user-role) ### Build the Project diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/app/controller/TaskController.java b/spring-security-modules/spring-security-core/src/main/java/com/baeldung/app/controller/TaskController.java index 67072b5d61..7e6b2c3d9c 100644 --- a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/app/controller/TaskController.java +++ b/spring-security-modules/spring-security-core/src/main/java/com/baeldung/app/controller/TaskController.java @@ -42,62 +42,4 @@ public class TaskController { return ResponseEntity.ok().body(tasks); } - - /** - * Example of restricting specific endpoints to specific roles using @PreAuthorize. - */ - @GetMapping("/manager") - @PreAuthorize("hasRole('ROLE_MANAGER')") - public ResponseEntity> getAlManagerTasks() { - Iterable tasks = taskService.findAll(); - - return ResponseEntity.ok().body(tasks); - } - - /** - * Example of restricting specific endpoints to specific roles using SecurityContext. - */ - @GetMapping("/actuator") - public ResponseEntity> getAlActuatorTasks() { - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - if (auth != null && auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ACTUATOR"))) - { - return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); - } - - Iterable tasks = taskService.findAll(); - - return ResponseEntity.ok().body(tasks); - } - - /** - * Example of restricting specific endpoints to specific roles using UserDetailsService. - */ - @GetMapping("/admin") - public ResponseEntity> getAlAdminTasks() { - if(userDetailsService != null) { - UserDetails details = userDetailsService.loadUserByUsername("pam"); - if (details != null && details.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ADMIN"))) { - return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); - } - } - - Iterable tasks = taskService.findAll(); - - return ResponseEntity.ok().body(tasks); - } - - /** - * Example of restricting specific endpoints to specific roles using HttpServletRequest. - */ - @GetMapping("/admin2") - public ResponseEntity> getAlAdminTasksUsingServlet(HttpServletRequest request) { - if (!request.isUserInRole("ROLE_ADMIN")) { - return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); - } - - Iterable tasks = taskService.findAll(); - - return ResponseEntity.ok().body(tasks); - } } diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/checkrolejava/App.java b/spring-security-modules/spring-security-core/src/main/java/com/baeldung/checkrolejava/App.java new file mode 100644 index 0000000000..357583a572 --- /dev/null +++ b/spring-security-modules/spring-security-core/src/main/java/com/baeldung/checkrolejava/App.java @@ -0,0 +1,11 @@ +package com.baeldung.checkrolejava; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class App { + public static void main(String[] args) { + SpringApplication.run(com.baeldung.app.App.class, args); + } +} diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/checkrolejava/UnauthorizedException.java b/spring-security-modules/spring-security-core/src/main/java/com/baeldung/checkrolejava/UnauthorizedException.java new file mode 100644 index 0000000000..11fe9f9e5f --- /dev/null +++ b/spring-security-modules/spring-security-core/src/main/java/com/baeldung/checkrolejava/UnauthorizedException.java @@ -0,0 +1,8 @@ +package com.baeldung.checkrolejava; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.UNAUTHORIZED) +public class UnauthorizedException extends RuntimeException { +} diff --git a/spring-security-modules/spring-security-core/src/main/java/com/baeldung/checkrolejava/UserController.java b/spring-security-modules/spring-security-core/src/main/java/com/baeldung/checkrolejava/UserController.java new file mode 100644 index 0000000000..3092e94c7f --- /dev/null +++ b/spring-security-modules/spring-security-core/src/main/java/com/baeldung/checkrolejava/UserController.java @@ -0,0 +1,62 @@ +package com.baeldung.checkrolejava; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +import javax.servlet.http.HttpServletRequest; + +@Controller +public class UserController { + + @Autowired + private UserDetailsService userDetailsService; + + @PreAuthorize("hasRole('ROLE_ADMIN')") + @GetMapping("/user/{id}") + public String getUser(@PathVariable("id") String id) { + return "user"; + } + + @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MANAGER')") + @GetMapping("/users") + public String getUsers() { + return "users"; + } + + @GetMapping("v2/user/{id}") + public String getUserUsingSecurityContext() { + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + if (auth != null && auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ADMIN"))) { + return "user"; + } + + throw new UnauthorizedException(); + } + + @GetMapping("v2/users") + public String getUsersUsingDetailsService() { + UserDetails details = userDetailsService.loadUserByUsername("mike"); + if (details != null && details.getAuthorities().stream() + .anyMatch(a -> a.getAuthority().equals("ADMIN"))) { + return "users"; + } + + throw new UnauthorizedException(); + } + + @GetMapping("v3/users") + public String getUsers(HttpServletRequest request) { + if (request.isUserInRole("ROLE_ADMIN")) { + return "users"; + } + + throw new UnauthorizedException(); + } +} diff --git a/spring-security-modules/spring-security-mvc-boot-2/README.md b/spring-security-modules/spring-security-mvc-boot-2/README.md index 3c95086d21..7c53d03698 100644 --- a/spring-security-modules/spring-security-mvc-boot-2/README.md +++ b/spring-security-modules/spring-security-mvc-boot-2/README.md @@ -10,4 +10,5 @@ The "REST With Spring" Classes: http://github.learnspringsecurity.com - [Multiple Authentication Providers in Spring Security](https://www.baeldung.com/spring-security-multiple-auth-providers) - [Two Login Pages with Spring Security](https://www.baeldung.com/spring-security-two-login-pages) - [HTTPS using Self-Signed Certificate in Spring Boot](https://www.baeldung.com/spring-boot-https-self-signed-certificate) -- [Spring Security: Exploring JDBC Authentication](https://www.baeldung.com/spring-security-jdbc-authentication) \ No newline at end of file +- [Spring Security: Exploring JDBC Authentication](https://www.baeldung.com/spring-security-jdbc-authentication) +- [Spring Security Custom Logout Handler](https://www.baeldung.com/spring-security-custom-logout-handler) diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/test/resources/customlogouthandler/application.properties b/spring-security-modules/spring-security-mvc-boot-2/src/test/resources/customlogouthandler/application.properties index 9edd853f2c..84347c2664 100644 --- a/spring-security-modules/spring-security-mvc-boot-2/src/test/resources/customlogouthandler/application.properties +++ b/spring-security-modules/spring-security-mvc-boot-2/src/test/resources/customlogouthandler/application.properties @@ -1,5 +1,4 @@ -spring.datasource.url=jdbc:postgresql://localhost:5432/test +spring.datasource.driver-class-name=org.h2.Driver +spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.username=test -spring.datasource.password=test - -spring.jpa.hibernate.ddl-auto=create +spring.datasource.password=test \ No newline at end of file diff --git a/spring-security-modules/spring-security-okta/README.md b/spring-security-modules/spring-security-okta/README.md new file mode 100644 index 0000000000..6ea4817e19 --- /dev/null +++ b/spring-security-modules/spring-security-okta/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Spring Security With Okta](https://www.baeldung.com/spring-security-okta) diff --git a/spring-security-modules/spring-security-okta/pom.xml b/spring-security-modules/spring-security-okta/pom.xml new file mode 100644 index 0000000000..c5ff9013b5 --- /dev/null +++ b/spring-security-modules/spring-security-okta/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + spring-security-okta + 1.0-SNAPSHOT + spring-security-okta + war + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + com.okta.spring + okta-spring-boot-starter + ${okta.spring.version} + + + com.okta.spring + okta-spring-sdk + ${okta.spring.version} + + + + + spring-security-okta + + + src/main/resources + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + repackage + + + + + + + + + 1.4.0 + + diff --git a/spring-security-modules/spring-security-okta/src/main/java/com/baeldung/okta/Application.java b/spring-security-modules/spring-security-okta/src/main/java/com/baeldung/okta/Application.java new file mode 100644 index 0000000000..0c5cc94f10 --- /dev/null +++ b/spring-security-modules/spring-security-okta/src/main/java/com/baeldung/okta/Application.java @@ -0,0 +1,13 @@ +package com.baeldung.okta; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-security-modules/spring-security-okta/src/main/java/com/baeldung/okta/controller/AdminController.java b/spring-security-modules/spring-security-okta/src/main/java/com/baeldung/okta/controller/AdminController.java new file mode 100644 index 0000000000..c7786c4006 --- /dev/null +++ b/spring-security-modules/spring-security-okta/src/main/java/com/baeldung/okta/controller/AdminController.java @@ -0,0 +1,43 @@ +package com.baeldung.okta.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.okta.sdk.client.Client; +import com.okta.sdk.resource.user.User; +import com.okta.sdk.resource.user.UserBuilder; +import com.okta.sdk.resource.user.UserList; + +@RestController +public class AdminController { + + @Autowired + public Client client; + + @GetMapping("/users") + public UserList getUsers() { + return client.listUsers(); + } + + @GetMapping("/user") + public UserList searchUserByEmail(@RequestParam String query) { + return client.listUsers(query, null, null, null, null); + } + + @GetMapping("/createUser") + public User createUser() { + char[] tempPassword = {'P','a','$','$','w','0','r','d'}; + User user = UserBuilder.instance() + .setEmail("norman.lewis@email.com") + .setFirstName("Norman") + .setLastName("Lewis") + .setPassword(tempPassword) + .setActive(true) + .buildAndCreate(client); + return user; + } + +} + diff --git a/spring-security-modules/spring-security-okta/src/main/java/com/baeldung/okta/controller/HomeController.java b/spring-security-modules/spring-security-okta/src/main/java/com/baeldung/okta/controller/HomeController.java new file mode 100644 index 0000000000..b8f3ec4e10 --- /dev/null +++ b/spring-security-modules/spring-security-okta/src/main/java/com/baeldung/okta/controller/HomeController.java @@ -0,0 +1,16 @@ +package com.baeldung.okta.controller; + +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.security.oauth2.core.oidc.user.OidcUser; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HomeController { + + @GetMapping("/") + public String home(@AuthenticationPrincipal OidcUser user) { + return "Welcome, "+ user.getFullName() +"!"; + } + +} diff --git a/spring-security-modules/spring-security-okta/src/main/resources/application.properties b/spring-security-modules/spring-security-okta/src/main/resources/application.properties new file mode 100644 index 0000000000..4a584e3c29 --- /dev/null +++ b/spring-security-modules/spring-security-okta/src/main/resources/application.properties @@ -0,0 +1,8 @@ +okta.oauth2.issuer= //Auth server issuer URL +okta.oauth2.client-id= //Client ID of our Okta application +okta.oauth2.client-secret= //Client secret of our Okta application +okta.oauth2.redirect-uri=/authorization-code/callback + +#Okta Spring SDK configs +okta.client.orgUrl= //orgURL +okta.client.token= //token generated \ No newline at end of file diff --git a/spring-session/pom.xml b/spring-session/pom.xml index 42a414afdc..8388efb6c3 100644 --- a/spring-session/pom.xml +++ b/spring-session/pom.xml @@ -10,9 +10,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../parent-boot-1 + ../parent-boot-2 diff --git a/spring-session/spring-session-redis/pom.xml b/spring-session/spring-session-redis/pom.xml index 37402634b0..8d225e06ed 100644 --- a/spring-session/spring-session-redis/pom.xml +++ b/spring-session/spring-session-redis/pom.xml @@ -9,9 +9,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../../parent-boot-1 + ../../parent-boot-2 @@ -25,7 +25,7 @@ org.springframework.session - spring-session + spring-session-data-redis org.springframework.boot @@ -36,6 +36,11 @@ embedded-redis ${embedded-redis.version} + + redis.clients + jedis + jar + diff --git a/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/SecurityConfig.java b/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/SecurityConfig.java index 1da6d9422d..678c98e7eb 100644 --- a/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/SecurityConfig.java +++ b/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/SecurityConfig.java @@ -1,23 +1,31 @@ package com.baeldung.spring.session; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { - + @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { - auth.inMemoryAuthentication().withUser("admin").password("password").roles("ADMIN"); + auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder().encode("password")).roles("ADMIN"); } - + @Override protected void configure(HttpSecurity http) throws Exception { http.httpBasic().and().authorizeRequests().antMatchers("/").hasRole("ADMIN").anyRequest().authenticated(); } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } } diff --git a/spring-session/spring-session-redis/src/test/java/com/baeldung/spring/session/SessionControllerIntegrationTest.java b/spring-session/spring-session-redis/src/test/java/com/baeldung/spring/session/SessionControllerIntegrationTest.java index 7ee0294315..065533c73f 100644 --- a/spring-session/spring-session-redis/src/test/java/com/baeldung/spring/session/SessionControllerIntegrationTest.java +++ b/spring-session/spring-session-redis/src/test/java/com/baeldung/spring/session/SessionControllerIntegrationTest.java @@ -5,7 +5,7 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; diff --git a/spring-soap/README.md b/spring-soap/README.md index c23f0bc6f0..ca5f58c67e 100644 --- a/spring-soap/README.md +++ b/spring-soap/README.md @@ -5,3 +5,4 @@ This module contains articles about SOAP APIs with Spring ### Relevant articles: - [Creating a SOAP Web Service with Spring](https://www.baeldung.com/spring-boot-soap-web-service) +- [Invoking a SOAP Web Service in Spring](https://www.baeldung.com/spring-soap-web-service) diff --git a/spring-social-login/pom.xml b/spring-social-login/pom.xml index 9fa839f1c2..628f439cc0 100644 --- a/spring-social-login/pom.xml +++ b/spring-social-login/pom.xml @@ -8,9 +8,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../parent-boot-1 + ../parent-boot-2 @@ -42,15 +42,16 @@ org.thymeleaf.extras - thymeleaf-extras-springsecurity4 + thymeleaf-extras-springsecurity5 org.springframework.social spring-social-facebook + ${spring.social.facebook.version} - + org.springframework.boot spring-boot-starter-data-jpa @@ -60,6 +61,12 @@ com.h2database h2 + + + net.bytebuddy + byte-buddy-dep + ${bytebuddy.version} + @@ -93,5 +100,10 @@ + + + 1.10.9 + 2.0.3.RELEASE + \ No newline at end of file diff --git a/spring-social-login/src/main/java/com/baeldung/config/Application.java b/spring-social-login/src/main/java/com/baeldung/config/Application.java index 5d083d2d47..c65df6dbfe 100644 --- a/spring-social-login/src/main/java/com/baeldung/config/Application.java +++ b/spring-social-login/src/main/java/com/baeldung/config/Application.java @@ -3,7 +3,7 @@ package com.baeldung.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; -import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication diff --git a/spring-social-login/src/main/java/com/baeldung/config/SecurityConfig.java b/spring-social-login/src/main/java/com/baeldung/config/SecurityConfig.java index 3d3081fef9..152c7b229a 100644 --- a/spring-social-login/src/main/java/com/baeldung/config/SecurityConfig.java +++ b/spring-social-login/src/main/java/com/baeldung/config/SecurityConfig.java @@ -1,8 +1,7 @@ package com.baeldung.config; -import com.baeldung.security.FacebookSignInAdapter; -import com.baeldung.security.FacebookConnectionSignup; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -14,22 +13,27 @@ import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.social.connect.ConnectionFactoryLocator; import org.springframework.social.connect.UsersConnectionRepository; import org.springframework.social.connect.mem.InMemoryUsersConnectionRepository; +import org.springframework.social.connect.support.ConnectionFactoryRegistry; import org.springframework.social.connect.web.ProviderSignInController; +import org.springframework.social.facebook.connect.FacebookConnectionFactory; + +import com.baeldung.security.FacebookConnectionSignup; +import com.baeldung.security.FacebookSignInAdapter; @Configuration @EnableWebSecurity @ComponentScan(basePackages = { "com.baeldung.security" }) public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Value("${spring.social.facebook.appSecret}") + String appSecret; + + @Value("${spring.social.facebook.appId}") + String appId; @Autowired private UserDetailsService userDetailsService; - @Autowired - private ConnectionFactoryLocator connectionFactoryLocator; - - @Autowired - private UsersConnectionRepository usersConnectionRepository; - @Autowired private FacebookConnectionSignup facebookConnectionSignup; @@ -55,7 +59,19 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { @Bean // @Primary public ProviderSignInController providerSignInController() { + ConnectionFactoryLocator connectionFactoryLocator = connectionFactoryLocator(); + UsersConnectionRepository usersConnectionRepository = getUsersConnectionRepository(connectionFactoryLocator); ((InMemoryUsersConnectionRepository) usersConnectionRepository).setConnectionSignUp(facebookConnectionSignup); return new ProviderSignInController(connectionFactoryLocator, usersConnectionRepository, new FacebookSignInAdapter()); } + + private ConnectionFactoryLocator connectionFactoryLocator() { + ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry(); + registry.addConnectionFactory(new FacebookConnectionFactory(appId, appSecret)); + return registry; + } + + private UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) { + return new InMemoryUsersConnectionRepository(connectionFactoryLocator); + } } \ No newline at end of file diff --git a/spring-swagger-codegen/spring-swagger-codegen-app/README.md b/spring-swagger-codegen/spring-swagger-codegen-app/README.md index 1cb9e35d99..8740b17ba3 100644 --- a/spring-swagger-codegen/spring-swagger-codegen-app/README.md +++ b/spring-swagger-codegen/spring-swagger-codegen-app/README.md @@ -1,3 +1,3 @@ ## Spring Swagger Codegen App -This module contains the code for [Generate Spring Boot REST Client with Swagger](http://www.baeldung.com/spring-boot-rest-client-swagger-codegen). +This module contains the code for Generate Spring Boot REST Client with Swagger. diff --git a/spring-thymeleaf-2/README.md b/spring-thymeleaf-2/README.md index d5c5ead43d..a8c067a443 100644 --- a/spring-thymeleaf-2/README.md +++ b/spring-thymeleaf-2/README.md @@ -13,4 +13,5 @@ This module contains articles about Spring with Thymeleaf - [Working with Boolean in Thymeleaf](https://www.baeldung.com/thymeleaf-boolean) - [Working With Custom HTML Attributes in Thymeleaf](https://www.baeldung.com/thymeleaf-custom-html-attributes) - [How to Create an Executable JAR with Maven](https://www.baeldung.com/executable-jar-with-maven) +- [Spring MVC Data and Thymeleaf](https://www.baeldung.com/spring-mvc-thymeleaf-data) - [[<-- prev]](/spring-thymeleaf) diff --git a/spring-thymeleaf-3/src/main/java/com/baeldung/thymeleaf/currencies/CurrenciesController.java b/spring-thymeleaf-3/src/main/java/com/baeldung/thymeleaf/currencies/CurrenciesController.java new file mode 100644 index 0000000000..206cf32683 --- /dev/null +++ b/spring-thymeleaf-3/src/main/java/com/baeldung/thymeleaf/currencies/CurrenciesController.java @@ -0,0 +1,22 @@ +package com.baeldung.thymeleaf.currencies; + +import java.util.List; +import java.util.Locale; +import java.util.Set; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +public class CurrenciesController { + + @GetMapping(value = "/currency") + public String exchange( + @RequestParam(value = "amount", required = false) String amount, + @RequestParam(value = "amountList", required = false) List amountList, + Locale locale) { + + return "currencies/currencies"; + } +} diff --git a/spring-thymeleaf-3/src/main/resources/templates/currencies/currencies.html b/spring-thymeleaf-3/src/main/resources/templates/currencies/currencies.html new file mode 100644 index 0000000000..c2f44265dd --- /dev/null +++ b/spring-thymeleaf-3/src/main/resources/templates/currencies/currencies.html @@ -0,0 +1,21 @@ + + + + + Currency table + + +

Currency format by Locale

+

+ +

Currency Arrays format by Locale

+

+ +

Remove decimal values

+

+ +

Replace decimal points

+

+ + \ No newline at end of file diff --git a/spring-thymeleaf-3/src/test/java/com/baeldung/thymeleaf/currencies/CurrenciesControllerIntegrationTest.java b/spring-thymeleaf-3/src/test/java/com/baeldung/thymeleaf/currencies/CurrenciesControllerIntegrationTest.java new file mode 100644 index 0000000000..02bf8a9ee0 --- /dev/null +++ b/spring-thymeleaf-3/src/test/java/com/baeldung/thymeleaf/currencies/CurrenciesControllerIntegrationTest.java @@ -0,0 +1,68 @@ +package com.baeldung.thymeleaf.currencies; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc(printOnlyOnFailure = false) +public class CurrenciesControllerIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void whenCallCurrencyWithSpanishLocale_ThenReturnProperCurrency() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/currency") + .header("Accept-Language", "es-ES") + .param("amount", "10032.5")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("10.032,50 €"))); + } + + @Test + public void whenCallCurrencyWithUSALocale_ThenReturnProperCurrency() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/currency") + .header("Accept-Language", "en-US") + .param("amount", "10032.5")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("$10,032.50"))); + } + + @Test + public void whenCallCurrencyWithRomanianLocaleWithArrays_ThenReturnLocaleCurrencies() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/currency") + .header("Accept-Language", "ro-RO") + .param("amountList", "10", "20", "30")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("10,00 RON, 20,00 RON, 30,00 RON"))); + } + + @Test + public void whenCallCurrencyWithUSALocaleWithoutDecimal_ThenReturnCurrencyWithoutTrailingZeros() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/currency") + .header("Accept-Language", "en-US") + .param("amount", "10032")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("$10,032"))); + } + + @Test + public void whenCallCurrencyWithUSALocale_ThenReturnReplacedDecimalPoint() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/currency") + .header("Accept-Language", "en-US") + .param("amount", "1.5")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("1,5"))); + } +} diff --git a/stripe/pom.xml b/stripe/pom.xml index 07d2968f5f..48505c9e4e 100644 --- a/stripe/pom.xml +++ b/stripe/pom.xml @@ -11,9 +11,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../parent-boot-1 + ../parent-boot-2 @@ -28,9 +28,6 @@ org.projectlombok lombok - ${lombok.version} - com.stripe diff --git a/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java b/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java index a5c056b659..190911afb3 100644 --- a/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java +++ b/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java @@ -13,4 +13,27 @@ public class ChargeRequest { private Currency currency; private String stripeEmail; private String stripeToken; + public String getDescription() { + return description; + } + public int getAmount() { + return amount; + } + public Currency getCurrency() { + return currency; + } + public String getStripeEmail() { + return stripeEmail; + } + public String getStripeToken() { + return stripeToken; + } + public void setDescription(String description) { + this.description = description; + } + public void setCurrency(Currency currency) { + this.currency = currency; + } + + } diff --git a/stripe/src/main/resources/application.properties b/stripe/src/main/resources/application.properties new file mode 100644 index 0000000000..f36df33897 --- /dev/null +++ b/stripe/src/main/resources/application.properties @@ -0,0 +1,2 @@ +STRIPE_SECRET_KEY= +STRIPE_PUBLIC_KEY= \ No newline at end of file diff --git a/stripe/src/main/resources/static/index.html b/stripe/src/main/resources/static/index.html index 090a01e91d..d7ba2bef91 100644 --- a/stripe/src/main/resources/static/index.html +++ b/stripe/src/main/resources/static/index.html @@ -1,7 +1,7 @@ - + diff --git a/terraform/README.md b/terraform/README.md new file mode 100644 index 0000000000..b2a9539727 --- /dev/null +++ b/terraform/README.md @@ -0,0 +1,4 @@ +### Relevant Articles: + +- [Introduction to Terraform](https://www.baeldung.com/ops/terraform-intro) +- [Best Practices When Using Terraform](https://www.baeldung.com/ops/terraform-best-practices) diff --git a/testing-modules/assertion-libraries/README.md b/testing-modules/assertion-libraries/README.md index d69457fdeb..ca4cc86f7e 100644 --- a/testing-modules/assertion-libraries/README.md +++ b/testing-modules/assertion-libraries/README.md @@ -10,4 +10,4 @@ - [Custom Assertions with AssertJ](http://www.baeldung.com/assertj-custom-assertion) - [Using Conditions with AssertJ Assertions](http://www.baeldung.com/assertj-conditions) - [AssertJ Exception Assertions](http://www.baeldung.com/assertj-exception-assertion) - +- [Asserting Log Messages With JUnit](https://www.baeldung.com/junit-asserting-logs) diff --git a/testing-modules/junit5-annotations/README.md b/testing-modules/junit5-annotations/README.md index 02d4cd652a..bd51bb3d2d 100644 --- a/testing-modules/junit5-annotations/README.md +++ b/testing-modules/junit5-annotations/README.md @@ -7,3 +7,4 @@ This module contains articles about JUnit 5 Annotations - [JUnit 5 Conditional Test Execution with Annotations](https://www.baeldung.com/junit-5-conditional-test-execution) - [JUnit5 Programmatic Extension Registration with @RegisterExtension](https://www.baeldung.com/junit-5-registerextension-annotation) - [Guide to JUnit 5 Parameterized Tests](https://www.baeldung.com/parameterized-tests-junit-5) +- [Writing Templates for Test Cases Using JUnit 5](https://www.baeldung.com/junit5-test-templates) diff --git a/testing-modules/mockito-2/README.md b/testing-modules/mockito-2/README.md index 6c9ddee01d..329228186f 100644 --- a/testing-modules/mockito-2/README.md +++ b/testing-modules/mockito-2/README.md @@ -5,3 +5,4 @@ - [Mockito Strict Stubbing and The UnnecessaryStubbingException](https://www.baeldung.com/mockito-unnecessary-stubbing-exception) - [Mockito and Fluent APIs](https://www.baeldung.com/mockito-fluent-apis) - [Mocking the ObjectMapper readValue() Method](https://www.baeldung.com/mockito-mock-jackson-read-value) +- [Introduction to Mockito’s AdditionalAnswers](https://www.baeldung.com/mockito-additionalanswers) diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 951909b36f..b467b3c503 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -33,6 +33,7 @@ selenium-junit-testng spring-testing test-containers + testing-assertions testng junit-5-basics easymock diff --git a/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/clickusingjavascript/SeleniumJavaScriptClickTest.java b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/clickusingjavascript/SeleniumJavaScriptClickTest.java new file mode 100644 index 0000000000..6d2ab8ef1f --- /dev/null +++ b/testing-modules/selenium-junit-testng/src/test/java/com/baeldung/selenium/clickusingjavascript/SeleniumJavaScriptClickTest.java @@ -0,0 +1,61 @@ +package java.com.baeldung.selenium.clickusingjavascript; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class SeleniumJavaScriptClickTest { + + private WebDriver driver; + private WebDriverWait wait; + + @Before + public void setUp() { + System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); + driver = new ChromeDriver(); + wait = new WebDriverWait(driver, 5000); + } + + @After + public void cleanUp() { + driver.close(); + } + + @Test + public void whenSearchForSeleniumArticles_thenReturnNotEmptyResults() { + driver.get("https://baeldung.com"); + String title = driver.getTitle(); + assertEquals("Baeldung | Java, Spring and Web Development tutorials", title); + + wait.until(ExpectedConditions.elementToBeClickable(By.className("menu-search"))); + WebElement searchButton = driver.findElement(By.className("menu-search")); + clickElement(searchButton); + + wait.until(ExpectedConditions.elementToBeClickable(By.id("search"))); + WebElement searchInput = driver.findElement(By.id("search")); + searchInput.sendKeys("Selenium"); + + wait.until(ExpectedConditions.elementToBeClickable(By.className("btn-search"))); + WebElement seeSearchResultsButton = driver.findElement(By.className("btn-search")); + clickElement(seeSearchResultsButton); + + int seleniumPostsCount = driver.findElements(By.className("post")).size(); + assertTrue(seleniumPostsCount > 0); + } + + private void clickElement(WebElement element) { + JavascriptExecutor executor = (JavascriptExecutor) driver; + executor.executeScript("arguments[0].click();", element); + } + +} diff --git a/testing-modules/testing-assertions/pom.xml b/testing-modules/testing-assertions/pom.xml new file mode 100644 index 0000000000..0a7c4b0860 --- /dev/null +++ b/testing-modules/testing-assertions/pom.xml @@ -0,0 +1,28 @@ + + 4.0.0 + testing-assertions + 0.0.1-SNAPSHOT + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../../parent-java + + + + + ch.qos.logback + logback-classic + 1.2.3 + + + org.assertj + assertj-core + 3.15.0 + test + + + diff --git a/testing-modules/testing-assertions/src/main/java/com/baeldung/junit/log/BusinessWorker.java b/testing-modules/testing-assertions/src/main/java/com/baeldung/junit/log/BusinessWorker.java new file mode 100644 index 0000000000..86cd38824c --- /dev/null +++ b/testing-modules/testing-assertions/src/main/java/com/baeldung/junit/log/BusinessWorker.java @@ -0,0 +1,16 @@ +package com.baeldung.junit.log; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BusinessWorker { + private static Logger LOGGER = LoggerFactory.getLogger(BusinessWorker.class); + + public void generateLogs(String msg) { + LOGGER.trace(msg); + LOGGER.debug(msg); + LOGGER.info(msg); + LOGGER.warn(msg); + LOGGER.error(msg); + } +} diff --git a/testing-modules/testing-assertions/src/main/resources/logback.xml b/testing-modules/testing-assertions/src/main/resources/logback.xml new file mode 100644 index 0000000000..d485da62ff --- /dev/null +++ b/testing-modules/testing-assertions/src/main/resources/logback.xml @@ -0,0 +1,16 @@ + + + + + + %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n + + + + + + + + + + \ No newline at end of file diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java new file mode 100644 index 0000000000..9200caf13d --- /dev/null +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java @@ -0,0 +1,48 @@ +package com.baeldung.junit.log; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.LoggerFactory; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.LoggerContext; + +public class BusinessWorkerUnitTest { + private static MemoryAppender memoryAppender; + private static final String LOGGER_NAME = "com.baeldung.junit.log"; + private static final String MSG = "This is a test message!!!"; + + @Before + public void setup() { + Logger logger = (Logger) LoggerFactory.getLogger(LOGGER_NAME); + memoryAppender = new MemoryAppender(); + memoryAppender.setContext((LoggerContext) LoggerFactory.getILoggerFactory()); + logger.setLevel(Level.DEBUG); + logger.addAppender(memoryAppender); + memoryAppender.start(); + + } + + @After + public void cleanUp() { + memoryAppender.reset(); + memoryAppender.stop(); + } + + @Test + public void test() { + BusinessWorker worker = new BusinessWorker(); + worker.generateLogs(MSG); + + // I check that I only have 4 messages (all but trace) + assertThat(memoryAppender.countEventsForLogger(LOGGER_NAME)).isEqualTo(4); + // I look for a specific message at a specific level, and I only have 1 + assertThat(memoryAppender.search(MSG, Level.INFO).size()).isEqualTo(1); + // I check that the entry that is not present is the trace level + assertThat(memoryAppender.contains(MSG, Level.TRACE)).isFalse(); + } +} diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/junit/log/MemoryAppender.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/junit/log/MemoryAppender.java new file mode 100644 index 0000000000..31c5c69766 --- /dev/null +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/junit/log/MemoryAppender.java @@ -0,0 +1,51 @@ +package com.baeldung.junit.log; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; + +/** + * In memory slf4j appender
+ * Convenient appender to be able to check slf4j invocations + */ +public class MemoryAppender extends ListAppender { + public void reset() { + this.list.clear(); + } + + public boolean contains(String string, Level level) { + return this.list.stream() + .anyMatch(event -> event.getMessage().toString().contains(string) + && event.getLevel().equals(level)); + } + + public int countEventsForLogger(String loggerName) { + return (int) this.list.stream() + .filter(event -> event.getLoggerName().contains(loggerName)).count(); + } + + public List search(String string) { + return this.list.stream() + .filter(event -> event.getMessage().toString().contains(string)) + .collect(Collectors.toList()); + } + + public List search(String string, Level level) { + return this.list.stream() + .filter(event -> event.getMessage().toString().contains(string) + && event.getLevel().equals(level)) + .collect(Collectors.toList()); + } + + public int getSize() { + return this.list.size(); + } + + public List getLoggedEvents() { + return Collections.unmodifiableList(this.list); + } +} diff --git a/testing-modules/testing-assertions/src/test/resources/logback.xml b/testing-modules/testing-assertions/src/test/resources/logback.xml new file mode 100644 index 0000000000..980ce897ff --- /dev/null +++ b/testing-modules/testing-assertions/src/test/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + + %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n + + + + + + + \ No newline at end of file