From 5c281eb0169a2b8fe4be6c582dad5be9305731ad Mon Sep 17 00:00:00 2001 From: vunamtien Date: Tue, 1 Nov 2022 22:38:06 +0700 Subject: [PATCH 01/13] calculate standard deviation (#12963) Co-authored-by: tienvn4 --- .../standarddeviation/StandardDeviation.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 core-java-modules/core-java-lang-math-3/src/main/java/com/baeldung/math/standarddeviation/StandardDeviation.java diff --git a/core-java-modules/core-java-lang-math-3/src/main/java/com/baeldung/math/standarddeviation/StandardDeviation.java b/core-java-modules/core-java-lang-math-3/src/main/java/com/baeldung/math/standarddeviation/StandardDeviation.java new file mode 100644 index 0000000000..623f7a96eb --- /dev/null +++ b/core-java-modules/core-java-lang-math-3/src/main/java/com/baeldung/math/standarddeviation/StandardDeviation.java @@ -0,0 +1,35 @@ +package com.baeldung.math.standarddeviation; + +import java.util.Arrays; + +public class StandardDeviation { + + public static double calculateStandardDeviation(double[] array) { + + // get the sum of array + double sum = 0.0; + for (double i : array) { + sum += i; + } + + // get the mean of array + int length = array.length; + double mean = sum / length; + + // calculate the standard deviation + double standardDeviation = 0.0; + for (double num : array) { + standardDeviation += Math.pow(num - mean, 2); + } + + return Math.sqrt(standardDeviation / length); + } + + public static void main(String[] args) { + double[] array = {25, 5, 45, 68, 61, 46, 24, 95}; + System.out.println("List of elements: " + Arrays.toString(array)); + + double standardDeviation = calculateStandardDeviation(array); + System.out.format("Standard Deviation = %.6f", standardDeviation); + } +} From 0e99a7f9cc0f68a4209a27d2d31b85399d6752ed Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Tue, 1 Nov 2022 13:01:30 -0300 Subject: [PATCH 02/13] bael-5851 --- .../src/main/java/com/baeldung/factorygeneric/DateNotifier.java | 2 +- .../src/main/java/com/baeldung/factorygeneric/Main.java | 2 +- .../src/main/java/com/baeldung/factorygeneric/Notifier.java | 2 +- .../main/java/com/baeldung/factorygeneric/NotifierFactory.java | 2 +- .../src/main/java/com/baeldung/factorygeneric/Record.java | 2 +- .../main/java/com/baeldung/factorygeneric/StringNotifier.java | 2 +- .../com/baeldung/factorygeneric/FactoryGenericUnitTest.java | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/DateNotifier.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/DateNotifier.java index 931fd29445..b48c432a32 100644 --- a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/DateNotifier.java +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/DateNotifier.java @@ -1,4 +1,4 @@ -package com.baeldung.factoryGeneric; +package com.baeldung.factorygeneric; import java.util.Date; diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Main.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Main.java index 9ee570f4c4..0b3da9013d 100644 --- a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Main.java +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Main.java @@ -1,4 +1,4 @@ -package com.baeldung.factoryGeneric; +package com.baeldung.factorygeneric; import java.util.Date; diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Notifier.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Notifier.java index 8c7d9e4a56..bfda24bc15 100644 --- a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Notifier.java +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Notifier.java @@ -1,4 +1,4 @@ -package com.baeldung.factoryGeneric; +package com.baeldung.factorygeneric; public interface Notifier { diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/NotifierFactory.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/NotifierFactory.java index 67b980f198..ff1091d18f 100644 --- a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/NotifierFactory.java +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/NotifierFactory.java @@ -1,4 +1,4 @@ -package com.baeldung.factoryGeneric; +package com.baeldung.factorygeneric; import java.util.Date; diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Record.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Record.java index 0f99e1f988..1dbe94f2e7 100644 --- a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Record.java +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Record.java @@ -1,4 +1,4 @@ -package com.baeldung.factoryGeneric; +package com.baeldung.factorygeneric; import java.util.Date; diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/StringNotifier.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/StringNotifier.java index 124bfa9a5a..576085f267 100644 --- a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/StringNotifier.java +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/StringNotifier.java @@ -1,4 +1,4 @@ -package com.baeldung.factoryGeneric; +package com.baeldung.factorygeneric; public class StringNotifier implements Notifier { diff --git a/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factorygeneric/FactoryGenericUnitTest.java b/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factorygeneric/FactoryGenericUnitTest.java index 4dd9880b74..6fabfc22dc 100644 --- a/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factorygeneric/FactoryGenericUnitTest.java +++ b/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factorygeneric/FactoryGenericUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.factoryGeneric; +package com.baeldung.factorygeneric; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; From 879c0b8825770cc9b8ed57baf347a8ef65a51294 Mon Sep 17 00:00:00 2001 From: "thibault.faure" Date: Thu, 6 Oct 2022 21:43:20 +0200 Subject: [PATCH 03/13] BAEL-5629 fix integration test for jcache article --- libraries-data/pom.xml | 2 +- .../com/baeldung/jcache/CacheLoaderIntegrationTest.java | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libraries-data/pom.xml b/libraries-data/pom.xml index f0f5338560..a6f8730538 100644 --- a/libraries-data/pom.xml +++ b/libraries-data/pom.xml @@ -174,7 +174,7 @@ 2.8.2 1.1.1 1.5.0 - 3.8.4 + 5.2.0 0.15.0 2.2.0 1.6.0.1 diff --git a/libraries-data/src/test/java/com/baeldung/jcache/CacheLoaderIntegrationTest.java b/libraries-data/src/test/java/com/baeldung/jcache/CacheLoaderIntegrationTest.java index 8017418eba..510083280d 100644 --- a/libraries-data/src/test/java/com/baeldung/jcache/CacheLoaderIntegrationTest.java +++ b/libraries-data/src/test/java/com/baeldung/jcache/CacheLoaderIntegrationTest.java @@ -16,21 +16,22 @@ import org.junit.Test; public class CacheLoaderIntegrationTest { private static final String CACHE_NAME = "SimpleCache"; + private static final String HAZELCAST_MEMBER_CACHING_PROVIDER = "com.hazelcast.cache.HazelcastMemberCachingProvider"; private Cache cache; @Before public void setup() { // Adding fully qualified class name because of multiple Cache Provider (Ignite and Hazelcast) - CachingProvider cachingProvider = Caching.getCachingProvider("com.hazelcast.cache.HazelcastCachingProvider"); + CachingProvider cachingProvider = Caching.getCachingProvider(HAZELCAST_MEMBER_CACHING_PROVIDER); CacheManager cacheManager = cachingProvider.getCacheManager(); MutableConfiguration config = new MutableConfiguration().setReadThrough(true).setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<>(new SimpleCacheLoader())); - this.cache = cacheManager.createCache("SimpleCache", config); + this.cache = cacheManager.createCache( CACHE_NAME, config ); } @After public void tearDown() { - Caching.getCachingProvider("com.hazelcast.cache.HazelcastCachingProvider").getCacheManager().destroyCache(CACHE_NAME); + Caching.getCachingProvider(HAZELCAST_MEMBER_CACHING_PROVIDER).getCacheManager().destroyCache(CACHE_NAME); } @Test From 89c4243d4af1fc140bf8d94f111756fc4636ba81 Mon Sep 17 00:00:00 2001 From: edizor <113095366+edizor@users.noreply.github.com> Date: Wed, 2 Nov 2022 20:35:07 +0800 Subject: [PATCH 04/13] Update README.md --- spring-5-webflux-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-5-webflux-2/README.md b/spring-5-webflux-2/README.md index 862ca72ca9..e64c88c61d 100644 --- a/spring-5-webflux-2/README.md +++ b/spring-5-webflux-2/README.md @@ -5,3 +5,4 @@ This module contains articles about Spring 5 WebFlux ## Relevant articles: - [Spring Webflux and @Cacheable Annotation](https://www.baeldung.com/spring-webflux-cacheable) - [Comparison Between Mono’s doOnNext() and doOnSuccess()](https://www.baeldung.com/mono-doonnext-doonsuccess) +- [How to Access the First Element of a Flux](https://www.baeldung.com/java-flux-first-element) From 65e0d74536425f0b39d53a20e2dc4a29d512b529 Mon Sep 17 00:00:00 2001 From: edizor <113095366+edizor@users.noreply.github.com> Date: Wed, 2 Nov 2022 20:41:57 +0800 Subject: [PATCH 05/13] Update README.md --- spring-reactive-modules/spring-5-reactive-client-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-reactive-modules/spring-5-reactive-client-2/README.md b/spring-reactive-modules/spring-5-reactive-client-2/README.md index 341271e5ad..067a87a9d5 100644 --- a/spring-reactive-modules/spring-5-reactive-client-2/README.md +++ b/spring-reactive-modules/spring-5-reactive-client-2/README.md @@ -6,4 +6,5 @@ This module contains articles about reactive Spring 5 WebClient The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles +- [Limiting the Requests per Second With WebClient](https://www.baeldung.com/spring-webclient-limit-requests-per-second) - More articles: [[<-- prev]](../spring-5-reactive-client) From cde43cc5a371a7a667ae7a24ebcf9767ea4ba9aa Mon Sep 17 00:00:00 2001 From: edizor <113095366+edizor@users.noreply.github.com> Date: Wed, 2 Nov 2022 20:43:48 +0800 Subject: [PATCH 06/13] Update README.md --- spring-boot-modules/spring-boot-properties-3/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-properties-3/README.md b/spring-boot-modules/spring-boot-properties-3/README.md index 1bd6724885..77c6815649 100644 --- a/spring-boot-modules/spring-boot-properties-3/README.md +++ b/spring-boot-modules/spring-boot-properties-3/README.md @@ -8,4 +8,5 @@ - [How to Define a Map in YAML for a POJO?](https://www.baeldung.com/yaml-map-pojo) - [Using application.yml vs application.properties in Spring Boot](https://www.baeldung.com/spring-boot-yaml-vs-properties) - [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties) -- [IntelliJ – Cannot Resolve Spring Boot Configuration Properties Error](https://www.baeldung.com/intellij-resolve-spring-boot-configuration-properties) \ No newline at end of file +- [IntelliJ – Cannot Resolve Spring Boot Configuration Properties Error](https://www.baeldung.com/intellij-resolve-spring-boot-configuration-properties) +- [Log Properties in a Spring Boot Application](https://www.baeldung.com/spring-boot-log-properties) From 759a40f3ed8213b04743373ef885932444a5f7a7 Mon Sep 17 00:00:00 2001 From: edizor <113095366+edizor@users.noreply.github.com> Date: Wed, 2 Nov 2022 21:03:03 +0800 Subject: [PATCH 07/13] Update README.md [skip ci] --- algorithms-modules/algorithms-sorting-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/algorithms-modules/algorithms-sorting-2/README.md b/algorithms-modules/algorithms-sorting-2/README.md index b31cfceb42..f8a675ed8a 100644 --- a/algorithms-modules/algorithms-sorting-2/README.md +++ b/algorithms-modules/algorithms-sorting-2/README.md @@ -4,4 +4,5 @@ - [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers) - [Guide to In-Place Sorting Algorithm Works with a Java Implementation](https://www.baeldung.com/java-in-place-sorting) - [Partitioning and Sorting Arrays with Many Repeated Entries with Java Examples](https://www.baeldung.com/java-sorting-arrays-with-repeated-entries) +- [Gravity/Bead Sort in Java](https://www.baeldung.com/java-gravity-bead-sort) - More articles: [[<-- prev]](/algorithms-sorting) From b447862fa5746000137854563bf59a59469db86b Mon Sep 17 00:00:00 2001 From: edizor <113095366+edizor@users.noreply.github.com> Date: Wed, 2 Nov 2022 21:08:28 +0800 Subject: [PATCH 08/13] Create README.md [skip ci] --- core-java-modules/core-java-networking-4/README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 core-java-modules/core-java-networking-4/README.md diff --git a/core-java-modules/core-java-networking-4/README.md b/core-java-modules/core-java-networking-4/README.md new file mode 100644 index 0000000000..355f0c447e --- /dev/null +++ b/core-java-modules/core-java-networking-4/README.md @@ -0,0 +1,2 @@ +## Relevant Articles: +- [Difference Between URI.create() and new URI()](https://www.baeldung.com/java-uri-create-and-new-uri) From 448a31803626c17dc360bd427a23b09378fa4b01 Mon Sep 17 00:00:00 2001 From: edizor <113095366+edizor@users.noreply.github.com> Date: Wed, 2 Nov 2022 21:12:34 +0800 Subject: [PATCH 09/13] Update README.md [skip ci] --- testing-modules/junit-5/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/testing-modules/junit-5/README.md b/testing-modules/junit-5/README.md index cefc8e1707..9f1fd53ee5 100644 --- a/testing-modules/junit-5/README.md +++ b/testing-modules/junit-5/README.md @@ -8,3 +8,4 @@ - [Determine the Execution Time of JUnit Tests](https://www.baeldung.com/junit-test-execution-time) - [@BeforeAll and @AfterAll in Non-Static Methods](https://www.baeldung.com/java-beforeall-afterall-non-static) - [The java.lang.NoClassDefFoundError in JUnit](https://www.baeldung.com/junit-noclassdeffounderror) +- [assertAll() vs Multiple Assertions in JUnit5](https://github.com/eugenp/tutorials/tree/master/testing-modules/junit-5) From eb81b01c1900b293886d43a45ba6c082c3e961b7 Mon Sep 17 00:00:00 2001 From: edizor <113095366+edizor@users.noreply.github.com> Date: Wed, 2 Nov 2022 21:14:44 +0800 Subject: [PATCH 10/13] Update README.md [skip ci] --- spring-boot-modules/spring-boot-groovy/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-groovy/README.md b/spring-boot-modules/spring-boot-groovy/README.md index 73edafb9c0..0897cc92bc 100644 --- a/spring-boot-modules/spring-boot-groovy/README.md +++ b/spring-boot-modules/spring-boot-groovy/README.md @@ -6,4 +6,5 @@ This module contains articles about Spring with Groovy ### Relevant Articles: - [Building a Simple Web Application with Spring Boot and Groovy](https://www.baeldung.com/spring-boot-groovy-web-app) -- [Groovy Bean Definitions](https://www.baeldung.com/spring-groovy-beans) \ No newline at end of file +- [Groovy Bean Definitions](https://www.baeldung.com/spring-groovy-beans) +- [Using Groovy in Spring](https://www.baeldung.com/groovy/spring-using-groovy) From 3962acdd92a9d8dd41db616974cffa959ff6fb99 Mon Sep 17 00:00:00 2001 From: edizor <113095366+edizor@users.noreply.github.com> Date: Wed, 2 Nov 2022 21:17:34 +0800 Subject: [PATCH 11/13] Update README.md [skip ci] --- core-java-modules/core-java-collections-set-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-collections-set-2/README.md b/core-java-modules/core-java-collections-set-2/README.md index 17ed810323..48c70084ca 100644 --- a/core-java-modules/core-java-collections-set-2/README.md +++ b/core-java-modules/core-java-collections-set-2/README.md @@ -1,3 +1,4 @@ ## Relevant articles - [Using Streams to Collect Into a TreeSet](https://www.baeldung.com/java-stream-collect-into-treeset) +- [A Guide to LinkedHashSet in Java](https://www.baeldung.com/java-linkedhashset) From 87a562291ee2fdca6926de7716ef0661d71ffca0 Mon Sep 17 00:00:00 2001 From: Satyarth Shankar <55573874+satyarth9@users.noreply.github.com> Date: Thu, 3 Nov 2022 13:49:56 +0530 Subject: [PATCH 12/13] retry when rxjava (#12961) * BAEL-5751 test commit for checkin builds * BAEL-5751 compiled with java 8 * BAEL-5751 small update * BAEL-5751 added the core code * BAEL-5751 moved code to a different module * BAEL-5751 using assertArrayEquals * BAEL-5751 new line at the end of file * BAEL-5738 retry with delay in rxJava * Update RxJavaRetryWithDelayUnitTest.java Co-authored-by: Grzegorz Piwowarek --- .../rxjava/RxJavaRetryWithDelayUnitTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 rxjava-modules/rxjava-core/src/test/java/com/baeldung/rxjava/RxJavaRetryWithDelayUnitTest.java diff --git a/rxjava-modules/rxjava-core/src/test/java/com/baeldung/rxjava/RxJavaRetryWithDelayUnitTest.java b/rxjava-modules/rxjava-core/src/test/java/com/baeldung/rxjava/RxJavaRetryWithDelayUnitTest.java new file mode 100644 index 0000000000..c6c8a461a3 --- /dev/null +++ b/rxjava-modules/rxjava-core/src/test/java/com/baeldung/rxjava/RxJavaRetryWithDelayUnitTest.java @@ -0,0 +1,62 @@ +package com.baeldung.rxjava; +import io.reactivex.Observable; +import org.junit.Test; + +import java.util.concurrent.TimeUnit; + +public class RxJavaRetryWithDelayUnitTest { + + @Test + public void givenObservable_whenSuccess_thenOnNext(){ + Observable.just(remoteCallSuccess()) + .subscribe(success -> { + System.out.println("Success"); + System.out.println(success); + }, err -> { + System.out.println("Error"); + System.out.println(err); + }); + } + + + @Test + public void givenObservable_whenError_thenOnError(){ + Observable.just(remoteCallError()) + .subscribe(success -> { + System.out.println("Success"); + System.out.println(success); + }, err -> { + System.out.println("Error"); + System.out.println(err); + }); + } + + @Test + public void givenError_whenRetry_thenCanDelay(){ + Observable.just(remoteCallError()) + .retryWhen(attempts -> { + return attempts.flatMap(err -> { + if (customChecker(err)) { + return Observable.timer(5000, TimeUnit.MILLISECONDS); + } else { + return Observable.error(err); + } + }); + }); + } + + + private String remoteCallSuccess(){ + return "Success"; + } + + private String remoteCallError(){ + // consider a network call that failed over here. + return "Error"; + } + + private boolean customChecker(Throwable t){ + // this will include custom logic that decides whether resubscription should occur or not + return true; + } +} From 90812f01eefd213b14f615faa64906b869b15990 Mon Sep 17 00:00:00 2001 From: Jonathan Cook Date: Thu, 3 Nov 2022 17:40:26 +0100 Subject: [PATCH 13/13] BAEL-5236 - Apache Camel Exception Handling (#12973) * BAEL-4706 - Spring Boot with Spring Batch * BAEL-3948 - Fix test(s) in spring-batch which leaves repository.sqlite changed * BAEL-4736 - Convert JSONArray to List of Object using camel-jackson * BAEL-4756 - Mockito MockSettings * BAEL-4756 - Mockito MockSettings - fix spelling * BAEL-2674 - Upgrade the Okhttp article * BAEL-4204 - Adding Interceptors in OkHTTP * BAEL-4836 - Mocking Static Methods with Mockito * BAEL-4205 - A Guide to Events in OkHTTP * BAEL-5408 - Update Camel version in spring-boot-camel module * BAEL-5234 - Apache Camel Routes Testing in Spring Boot * BAEL-5234 - Apache Camel Routes Testing in Spring Boot * BAEL-5237 - Apache Camel Conditional Routing * BAEL-5236 - Apache Camel Exception Handling Co-authored-by: Jonathan Cook --- .../ExceptionHandlingSpringApplication.java | 13 +++++++ .../ExceptionHandlingWithDoTryRoute.java | 26 ++++++++++++++ ...ptionHandlingWithExceptionClauseRoute.java | 24 +++++++++++++ .../exception/ExceptionLoggingProcessor.java | 30 ++++++++++++++++ .../exception/ExceptionThrowingRoute.java | 30 ++++++++++++++++ ...galArgumentExceptionThrowingProcessor.java | 20 +++++++++++ ...ceptionHandlingWithDoTryRouteUnitTest.java | 30 ++++++++++++++++ ...dlingWithExceptionClauseRouteUnitTest.java | 30 ++++++++++++++++ .../ExceptionThrowingRouteUnitTest.java | 36 +++++++++++++++++++ ...entExceptionThrowingProcessorUnitTest.java | 16 +++++++++ 10 files changed, 255 insertions(+) create mode 100644 spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingSpringApplication.java create mode 100644 spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingWithDoTryRoute.java create mode 100644 spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingWithExceptionClauseRoute.java create mode 100644 spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionLoggingProcessor.java create mode 100644 spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionThrowingRoute.java create mode 100644 spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/IllegalArgumentExceptionThrowingProcessor.java create mode 100644 spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionHandlingWithDoTryRouteUnitTest.java create mode 100644 spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionHandlingWithExceptionClauseRouteUnitTest.java create mode 100644 spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionThrowingRouteUnitTest.java create mode 100644 spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/IllegalArgumentExceptionThrowingProcessorUnitTest.java diff --git a/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingSpringApplication.java b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingSpringApplication.java new file mode 100644 index 0000000000..df4550d9d5 --- /dev/null +++ b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingSpringApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.camel.exception; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ExceptionHandlingSpringApplication { + + public static void main(String[] args) { + SpringApplication.run(ExceptionHandlingSpringApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingWithDoTryRoute.java b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingWithDoTryRoute.java new file mode 100644 index 0000000000..ce3cfc129b --- /dev/null +++ b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingWithDoTryRoute.java @@ -0,0 +1,26 @@ +package com.baeldung.camel.exception; + +import java.io.IOException; + +import org.apache.camel.builder.RouteBuilder; +import org.springframework.stereotype.Component; + +@Component +public class ExceptionHandlingWithDoTryRoute extends RouteBuilder { + + @Override + public void configure() throws Exception { + + from("direct:start-handling-exception") + .routeId("exception-handling-route") + .doTry() + .process(new IllegalArgumentExceptionThrowingProcessor()) + .to("mock:received") + .doCatch(IOException.class, IllegalArgumentException.class) + .to("mock:caught") + .doFinally() + .to("mock:finally") + .end(); + + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingWithExceptionClauseRoute.java b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingWithExceptionClauseRoute.java new file mode 100644 index 0000000000..3a438e2402 --- /dev/null +++ b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionHandlingWithExceptionClauseRoute.java @@ -0,0 +1,24 @@ +package com.baeldung.camel.exception; + +import org.apache.camel.builder.RouteBuilder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ExceptionHandlingWithExceptionClauseRoute extends RouteBuilder { + + @Autowired + private ExceptionLoggingProcessor exceptionLogger; + + @Override + public void configure() throws Exception { + onException(IllegalArgumentException.class).process(exceptionLogger) + .handled(true) + .to("mock:handled"); + + from("direct:start-exception-clause") + .routeId("exception-clause-route") + .process(new IllegalArgumentExceptionThrowingProcessor()) + .to("mock:received"); + } +} diff --git a/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionLoggingProcessor.java b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionLoggingProcessor.java new file mode 100644 index 0000000000..84e4072888 --- /dev/null +++ b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionLoggingProcessor.java @@ -0,0 +1,30 @@ +package com.baeldung.camel.exception; + +import java.util.Map; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +public class ExceptionLoggingProcessor implements Processor { + + private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionLoggingProcessor.class); + + @Override + public void process(Exchange exchange) throws Exception { + Map headersMap = exchange.getIn().getHeaders(); + + if (!headersMap.isEmpty()) { + headersMap.entrySet() + .stream() + .forEach(e -> LOGGER.info("Header key [{}] -||- Header value [{}]", e.getKey(), e.getValue())); + } else { + LOGGER.info("Empty header"); + } + + } + +} diff --git a/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionThrowingRoute.java b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionThrowingRoute.java new file mode 100644 index 0000000000..752aabaf1a --- /dev/null +++ b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/ExceptionThrowingRoute.java @@ -0,0 +1,30 @@ +package com.baeldung.camel.exception; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +public class ExceptionThrowingRoute extends RouteBuilder { + + private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionThrowingRoute.class); + + @Override + public void configure() throws Exception { + + from("direct:start-exception") + .routeId("exception-throwing-route") + .process(new Processor() { + + @Override + public void process(Exchange exchange) throws Exception { + LOGGER.error("Exception Thrown"); + throw new IllegalArgumentException("An exception happened on purpose"); + + } + }).to("mock:received"); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/IllegalArgumentExceptionThrowingProcessor.java b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/IllegalArgumentExceptionThrowingProcessor.java new file mode 100644 index 0000000000..461a4e6553 --- /dev/null +++ b/spring-boot-modules/spring-boot-camel/src/main/java/com/baeldung/camel/exception/IllegalArgumentExceptionThrowingProcessor.java @@ -0,0 +1,20 @@ +package com.baeldung.camel.exception; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +public class IllegalArgumentExceptionThrowingProcessor implements Processor { + + private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionLoggingProcessor.class); + + @Override + public void process(Exchange exchange) throws Exception { + LOGGER.error("Exception Thrown"); + throw new IllegalArgumentException("An exception happened on purpose"); + } + +} diff --git a/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionHandlingWithDoTryRouteUnitTest.java b/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionHandlingWithDoTryRouteUnitTest.java new file mode 100644 index 0000000000..23d3b1a392 --- /dev/null +++ b/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionHandlingWithDoTryRouteUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.camel.exception; + +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +@CamelSpringBootTest +class ExceptionHandlingWithDoTryRouteUnitTest { + + @Autowired + private ProducerTemplate template; + + @EndpointInject("mock:caught") + private MockEndpoint mock; + + @Test + void whenSendHeaders_thenExceptionRaisedAndHandledSuccessfully() throws Exception { + mock.expectedMessageCount(1); + + template.sendBodyAndHeader("direct:start-handling-exception", null, "fruit", "Banana"); + + mock.assertIsSatisfied(); + } + +} diff --git a/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionHandlingWithExceptionClauseRouteUnitTest.java b/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionHandlingWithExceptionClauseRouteUnitTest.java new file mode 100644 index 0000000000..28d672bd64 --- /dev/null +++ b/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionHandlingWithExceptionClauseRouteUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.camel.exception; + +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +@CamelSpringBootTest +class ExceptionHandlingWithExceptionClauseRouteUnitTest { + + @Autowired + private ProducerTemplate template; + + @EndpointInject("mock:handled") + private MockEndpoint mock; + + @Test + void whenSendHeaders_thenExceptionRaisedAndHandledSuccessfully() throws Exception { + mock.expectedMessageCount(1); + + template.sendBodyAndHeader("direct:start-exception-clause", null, "fruit", "Banana"); + + mock.assertIsSatisfied(); + } + +} diff --git a/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionThrowingRouteUnitTest.java b/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionThrowingRouteUnitTest.java new file mode 100644 index 0000000000..6e6944fce8 --- /dev/null +++ b/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/ExceptionThrowingRouteUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.camel.exception; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.ExchangePattern; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +@CamelSpringBootTest +class ExceptionThrowingRouteUnitTest { + + @Autowired + private ProducerTemplate template; + + @Test + void whenSendBody_thenExceptionRaisedSuccessfully() { + CamelContext context = template.getCamelContext(); + Exchange exchange = context.getEndpoint("direct:start-exception") + .createExchange(ExchangePattern.InOut); + + exchange.getIn().setBody("Hello Baeldung"); + Exchange out = template.send("direct:start-exception", exchange); + + assertTrue(out.isFailed(), "Should be failed"); + assertTrue(out.getException() instanceof IllegalArgumentException, "Should be IllegalArgumentException"); + assertEquals("An exception happened on purpose", out.getException().getMessage()); + } + +} diff --git a/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/IllegalArgumentExceptionThrowingProcessorUnitTest.java b/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/IllegalArgumentExceptionThrowingProcessorUnitTest.java new file mode 100644 index 0000000000..a95abdfd27 --- /dev/null +++ b/spring-boot-modules/spring-boot-camel/src/test/java/com/baeldung/camel/exception/IllegalArgumentExceptionThrowingProcessorUnitTest.java @@ -0,0 +1,16 @@ +package com.baeldung.camel.exception; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class IllegalArgumentExceptionThrowingProcessorUnitTest { + + @Test + void whenProcessed_thenIllegalArgumentExceptionRaisedSuccessfully() { + assertThrows(IllegalArgumentException.class, () -> { + new IllegalArgumentExceptionThrowingProcessor().process(null); + }); + } + +}