From 79bf0d99762ec36185c5eb4930512ac1ac139ade Mon Sep 17 00:00:00 2001 From: Sachin kumar Date: Thu, 16 Dec 2021 17:43:43 +0530 Subject: [PATCH 01/63] BAEL-5271 set custom thread name --- .../threads/name/CustomThreadName.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/threads/name/CustomThreadName.java diff --git a/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/threads/name/CustomThreadName.java b/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/threads/name/CustomThreadName.java new file mode 100644 index 0000000000..9ce35d1137 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/threads/name/CustomThreadName.java @@ -0,0 +1,70 @@ +package com.baeldung.concurrent.threads.name; + +public class CustomThreadName { + + public int currentNumber = 1; + + public int N = 5; + + public static void main(String[] args) { + + CustomThreadName test = new CustomThreadName(); + + Thread oddThread = new Thread(() -> { + test.printOddNumber(); + // Uncomment below to set thread name using setName() Method + // Thread.currentThread().setName("ODD"); + }, "ODD"); + // or Uncomment below to set thread name using setName() Method + // oddThread.setName("ODD"); + + Thread evenThread = new Thread(() -> { + test.printEvenNumber(); + // Uncomment below to set thread name using setName() Method + // Thread.currentThread().setName("EVEN"); + }, "EVEN"); + + // evenThread.setName("EVEN"); + + evenThread.start(); + oddThread.start(); + + } + + public void printEvenNumber() { + synchronized (this) { + while (currentNumber < N) { + while (currentNumber % 2 == 1) { + try { + wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println(Thread.currentThread() + .getName() + " --> " + currentNumber); + currentNumber++; + notify(); + } + } + } + + public void printOddNumber() { + synchronized (this) { + while (currentNumber < N) { + while (currentNumber % 2 == 0) { + try { + wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println(Thread.currentThread() + .getName() + " --> " + currentNumber); + currentNumber++; + notify(); + } + } + } + +} From a95db4fe73200e9722df00dc49e23503a64ac1ab Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Sat, 15 Jan 2022 03:27:58 +0100 Subject: [PATCH 02/63] BAEL-5301 Retrying Feign Calls --- .../feign/retry/Custom5xxErrorDecoder.java | 27 ++++++++++ .../baeldung/feign/retry/NaiveRetryer.java | 21 ++++++++ .../retry/ResilientFeignClientBuilder.java | 28 ++++++++++ .../retry/Custom5xxErrorDecoderUnitTest.java | 51 +++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 feign/src/main/java/com/baeldung/feign/retry/Custom5xxErrorDecoder.java create mode 100644 feign/src/main/java/com/baeldung/feign/retry/NaiveRetryer.java create mode 100644 feign/src/main/java/com/baeldung/feign/retry/ResilientFeignClientBuilder.java create mode 100644 feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java diff --git a/feign/src/main/java/com/baeldung/feign/retry/Custom5xxErrorDecoder.java b/feign/src/main/java/com/baeldung/feign/retry/Custom5xxErrorDecoder.java new file mode 100644 index 0000000000..c4edfe219f --- /dev/null +++ b/feign/src/main/java/com/baeldung/feign/retry/Custom5xxErrorDecoder.java @@ -0,0 +1,27 @@ +package com.baeldung.feign.retry; + + +import feign.FeignException; +import feign.Response; +import feign.RetryableException; +import feign.codec.ErrorDecoder; + +import static feign.FeignException.errorStatus; + +public class Custom5xxErrorDecoder implements ErrorDecoder { + @Override + public Exception decode(String methodKey, Response response) { + FeignException exception = errorStatus(methodKey, response); + int status = response.status(); + if (status >= 500) { + return new RetryableException( + response.status(), + exception.getMessage(), + response.request().httpMethod(), + exception, + null, + response.request()); + } + return exception; + } +} diff --git a/feign/src/main/java/com/baeldung/feign/retry/NaiveRetryer.java b/feign/src/main/java/com/baeldung/feign/retry/NaiveRetryer.java new file mode 100644 index 0000000000..5a3c13fadd --- /dev/null +++ b/feign/src/main/java/com/baeldung/feign/retry/NaiveRetryer.java @@ -0,0 +1,21 @@ +package com.baeldung.feign.retry; + +import feign.RetryableException; +import feign.Retryer; + +public class NaiveRetryer implements feign.Retryer { + @Override + public void continueOrPropagate(RetryableException e) { + try { + Thread.sleep(1000L); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw e; + } + } + + @Override + public Retryer clone() { + return new NaiveRetryer(); + } +} diff --git a/feign/src/main/java/com/baeldung/feign/retry/ResilientFeignClientBuilder.java b/feign/src/main/java/com/baeldung/feign/retry/ResilientFeignClientBuilder.java new file mode 100644 index 0000000000..edcaf13242 --- /dev/null +++ b/feign/src/main/java/com/baeldung/feign/retry/ResilientFeignClientBuilder.java @@ -0,0 +1,28 @@ +package com.baeldung.feign.retry; + +import com.baeldung.feign.clients.BookClient; +import feign.Feign; +import feign.Logger; +import feign.Retryer; +import feign.gson.GsonDecoder; +import feign.gson.GsonEncoder; +import feign.okhttp.OkHttpClient; +import feign.slf4j.Slf4jLogger; +import lombok.Getter; + +import java.util.concurrent.TimeUnit; + +@Getter +public class ResilientFeignClientBuilder { + public BookClient bookClient = createClient(BookClient.class, "http://localhost:8081/api/books"); + + public static T createClient(Class type, String uri) { + return Feign.builder() + .client(new OkHttpClient()) + .encoder(new GsonEncoder()) + .decoder(new GsonDecoder()) + .retryer(new Retryer.Default(100L, TimeUnit.SECONDS.toMillis(3L), 5)) + .errorDecoder(new Custom5xxErrorDecoder()) + .target(type, uri); + } +} diff --git a/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java b/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java new file mode 100644 index 0000000000..0b67090268 --- /dev/null +++ b/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java @@ -0,0 +1,51 @@ +package com.baeldung.feign.retry; + +import feign.*; +import feign.codec.ErrorDecoder; +import org.jetbrains.annotations.NotNull; +import org.junit.Test; + +import java.nio.charset.Charset; +import java.util.Collection; +import java.util.HashMap; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class Custom5xxErrorDecoderUnitTest { + @Test + public void given5xxResponse_whenDecode_thenReturnRetryableException() { + // given + ErrorDecoder decoder = new Custom5xxErrorDecoder(); + Response response = responseStub(500); + + // when + Exception exception = decoder.decode("GET", response); + + // then + assertTrue(exception instanceof RetryableException); + } + + @Test + public void given4xxResponse_whenDecode_thenReturnFeignException() { + // given + ErrorDecoder decoder = new Custom5xxErrorDecoder(); + Response response = responseStub(400); + + // when + Exception exception = decoder.decode("GET", response); + + // then + assertTrue(exception instanceof FeignException); + assertFalse(exception instanceof RetryableException); + } + + @NotNull + private Response responseStub(int status) { + return Response.builder() + .request(Request.create( + Request.HttpMethod.GET, "url", new HashMap>(), new byte[0], Charset.defaultCharset(), new RequestTemplate())) + .status(status) + .build(); + } +} From d0735facef7bba13d317b021a91ec22b89231895 Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Sun, 16 Jan 2022 21:31:59 +0100 Subject: [PATCH 03/63] Difference between NIO and NIO.2? --- .../baeldung/niovsnio2/NioVsNio2UnitTest.java | 70 +++++++++++++++++++ .../src/test/resources/nio-vs-nio2.txt | 1 + 2 files changed, 71 insertions(+) create mode 100644 core-java-modules/core-java-nio-2/src/test/java/com/baeldung/niovsnio2/NioVsNio2UnitTest.java create mode 100644 core-java-modules/core-java-nio-2/src/test/resources/nio-vs-nio2.txt diff --git a/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/niovsnio2/NioVsNio2UnitTest.java b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/niovsnio2/NioVsNio2UnitTest.java new file mode 100644 index 0000000000..7413ebda13 --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/niovsnio2/NioVsNio2UnitTest.java @@ -0,0 +1,70 @@ +package com.baeldung.niovsnio2; + +import org.junit.Test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +public class NioVsNio2UnitTest { + + @Test + public void readFromFileUsingFileIO() throws Exception { + File file = new File("src/test/resources/nio-vs-nio2.txt"); + FileInputStream in = new FileInputStream(file); + StringBuilder content = new StringBuilder(); + int data = in.read(); + while (data != -1) { + content.append((char) data); + data = in.read(); + } + in.close(); + assertThat(content.toString()).isEqualTo("Hello from file!"); + } + + @Test + public void readFromFileUsingFileChannel() throws Exception { + RandomAccessFile file = new RandomAccessFile("src/test/resources/nio-vs-nio2.txt", "r"); + FileChannel channel = file.getChannel(); + StringBuilder content = new StringBuilder(); + + ByteBuffer buffer = ByteBuffer.allocate(256); + int bytesRead = channel.read(buffer); + while (bytesRead != -1) { + buffer.flip(); + + while (buffer.hasRemaining()) { + content.append((char) buffer.get()); + } + + buffer.clear(); + bytesRead = channel.read(buffer); + } + file.close(); + + assertThat(content.toString()).isEqualTo("Hello from file!"); + } + + @Test + public void readFromFileUsingNIO2() throws Exception { + List strings = Files.readAllLines(Paths.get("src/test/resources/nio-vs-nio2.txt")); + + assertThat(strings.get(0)).isEqualTo("Hello from file!"); + } + + @Test + public void listFilesUsingWalk() throws Exception { + Path path = Paths.get("src/test"); + Stream walk = Files.walk(path); + walk.forEach(System.out::println); + } +} diff --git a/core-java-modules/core-java-nio-2/src/test/resources/nio-vs-nio2.txt b/core-java-modules/core-java-nio-2/src/test/resources/nio-vs-nio2.txt new file mode 100644 index 0000000000..c12b07297d --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/test/resources/nio-vs-nio2.txt @@ -0,0 +1 @@ +Hello from file! \ No newline at end of file From 7cfd820f5bbc03e8143ebec150fa0f75c5890d9f Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Sun, 23 Jan 2022 09:40:01 +0500 Subject: [PATCH 04/63] Update README.md added link back to article: Generating a Java String of N Repeated Characters --- core-java-modules/core-java-string-algorithms-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-string-algorithms-3/README.md b/core-java-modules/core-java-string-algorithms-3/README.md index e6dbf3a489..ba8509306e 100644 --- a/core-java-modules/core-java-string-algorithms-3/README.md +++ b/core-java-modules/core-java-string-algorithms-3/README.md @@ -4,6 +4,7 @@ This module contains articles about string-related algorithms. ### Relevant Articles: +- [Generating a Java String of N Repeated Characters](https://www.baeldung.com/java-string-of-repeated-characters) - [Check if Two Strings are Anagrams in Java](https://www.baeldung.com/java-strings-anagrams) - [Email Validation in Java](https://www.baeldung.com/java-email-validation-regex) - [Check if the First Letter of a String is Uppercase](https://www.baeldung.com/java-check-first-letter-uppercase) From 4d0b7e68998904c6c747edbd619dbf4b811c607c Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Sun, 23 Jan 2022 09:57:22 +0500 Subject: [PATCH 05/63] Update README.md added link back to article --- core-java-modules/core-java-17/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-17/README.md b/core-java-modules/core-java-17/README.md index 4d1d7a5c49..1f061bd7c1 100644 --- a/core-java-modules/core-java-17/README.md +++ b/core-java-modules/core-java-17/README.md @@ -1,5 +1,6 @@ ### Relevant articles: +- [An Introduction to InstantSource in Java 17](https://www.baeldung.com/java-instantsource) - [Pattern Matching for Switch](https://www.baeldung.com/java-switch-pattern-matching) - [Introduction to HexFormat in Java 17](https://www.baeldung.com/java-hexformat) - [New Features in Java 17](https://www.baeldung.com/java-17-new-features) From 21b2f0127aa7ac3a9f587a246b9c38f2ca36af14 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Sun, 23 Jan 2022 10:08:00 +0500 Subject: [PATCH 06/63] Update README.md add link back to article: Hidden Classes in Java 15 --- core-java-modules/core-java-15/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-15/README.md b/core-java-modules/core-java-15/README.md index de503fbb31..4b163fcfb5 100644 --- a/core-java-modules/core-java-15/README.md +++ b/core-java-modules/core-java-15/README.md @@ -4,4 +4,5 @@ This module contains articles about Java 15. ### Relevant articles +- [Hidden Classes in Java 15](https://www.baeldung.com/java-hidden-classes) - [Sealed Classes and Interfaces in Java 15](https://www.baeldung.com/java-sealed-classes-interfaces) From 118a96846cc38086c0672eb4b2bd2c4a36840771 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Sun, 23 Jan 2022 10:17:43 +0500 Subject: [PATCH 07/63] Create README.md added link back to article: Convert a Maven Build to Gradle --- gradle/maven-to-gradle/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 gradle/maven-to-gradle/README.md diff --git a/gradle/maven-to-gradle/README.md b/gradle/maven-to-gradle/README.md new file mode 100644 index 0000000000..bd6e435c9a --- /dev/null +++ b/gradle/maven-to-gradle/README.md @@ -0,0 +1,3 @@ +## Relevant Articles: + +[Convert a Maven Build to Gradle](https://www.baeldung.com/maven-convert-to-gradle) From 4bebebad691d4ca2cfe886a9a0a2f08121ff8019 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Sun, 23 Jan 2022 10:22:06 +0500 Subject: [PATCH 08/63] Update README.md added link back to article: Get the Name of the Currently Executing Test in JUnit --- testing-modules/junit-5-advanced/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/testing-modules/junit-5-advanced/README.md b/testing-modules/junit-5-advanced/README.md index 7790cb6770..e970ba8c96 100644 --- a/testing-modules/junit-5-advanced/README.md +++ b/testing-modules/junit-5-advanced/README.md @@ -1,5 +1,6 @@ ## Relevant Articles: +- [Get the Name of the Currently Executing Test in JUnit](https://www.baeldung.com/junit-get-name-of-currently-executing-test) - [JUnit 5 TestWatcher API](https://www.baeldung.com/junit-testwatcher) - [JUnit Custom Display Name Generator API](https://www.baeldung.com/junit-custom-display-name-generator) - [@TestInstance Annotation in JUnit 5](https://www.baeldung.com/junit-testinstance-annotation) From de6d285eef568a3095913ab0248dd9fb71b6e4c1 Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Sun, 23 Jan 2022 10:25:53 +0500 Subject: [PATCH 09/63] Update README.md added link back to article: Cassandra Batch in Cassandra Query Language and Java --- persistence-modules/java-cassandra/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/persistence-modules/java-cassandra/README.md b/persistence-modules/java-cassandra/README.md index 735aabdf62..792ef143ab 100644 --- a/persistence-modules/java-cassandra/README.md +++ b/persistence-modules/java-cassandra/README.md @@ -1,4 +1,6 @@ ### Relevant Articles: + +- [Cassandra Batch in Cassandra Query Language and Java](https://www.baeldung.com/java-cql-cassandra-batch) - [A Guide to Cassandra with Java](http://www.baeldung.com/cassandra-with-java) - [Intro to DataStax Java Driver for Apache Cassandra](https://www.baeldung.com/cassandra-datastax-java-driver) - [CQL Data Types](https://www.baeldung.com/cassandra-data-types) From 856f101dc8b4a31d75a1b3106e88c9aa1a2931ba Mon Sep 17 00:00:00 2001 From: Asjad J <97493880+Asjad-J@users.noreply.github.com> Date: Mon, 24 Jan 2022 11:10:50 +0500 Subject: [PATCH 10/63] Update README.md added link back to article: Running a TestNG Project From the Command Line [skip ci] --- testing-modules/testng_command_line/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testing-modules/testng_command_line/README.md b/testing-modules/testng_command_line/README.md index 881477f036..74cdafdbc6 100644 --- a/testing-modules/testng_command_line/README.md +++ b/testing-modules/testng_command_line/README.md @@ -1 +1,3 @@ ### Relevant articles + +- [Running a TestNG Project From the Command Line](https://www.baeldung.com/testng-run-command-line) From ca79812da76361c3486a5104791b0606f967d4fb Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Mon, 24 Jan 2022 10:31:06 +0100 Subject: [PATCH 11/63] JAVA-9555: Fix YahooQuoteManagerImpl --- .../main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java index 8cc68259be..f5c60699c7 100644 --- a/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java +++ b/java-spi/exchange-rate-impl/src/main/java/com/baeldung/rate/impl/YahooQuoteManagerImpl.java @@ -24,7 +24,7 @@ public class YahooQuoteManagerImpl implements QuoteManager { StringBuilder sb = new StringBuilder(); Currency.getAvailableCurrencies().forEach(currency -> { - if (!currency.equals(currency.getCurrencyCode())) { + if (!baseCurrency.equals(currency.getCurrencyCode())) { sb.append(baseCurrency).append(currency.getCurrencyCode()).append("=X").append(","); } }); From e677f7a3ea880349d66e1c5813c9bc80f8ba9a72 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Mon, 24 Jan 2022 23:27:48 +0100 Subject: [PATCH 12/63] BAEL-5301 Fix indentation --- .../baeldung/feign/retry/Custom5xxErrorDecoder.java | 12 ++++++------ .../feign/retry/ResilientFeignClientBuilder.java | 12 ++++++------ .../feign/retry/Custom5xxErrorDecoderUnitTest.java | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/feign/src/main/java/com/baeldung/feign/retry/Custom5xxErrorDecoder.java b/feign/src/main/java/com/baeldung/feign/retry/Custom5xxErrorDecoder.java index c4edfe219f..6ff3c42ba1 100644 --- a/feign/src/main/java/com/baeldung/feign/retry/Custom5xxErrorDecoder.java +++ b/feign/src/main/java/com/baeldung/feign/retry/Custom5xxErrorDecoder.java @@ -15,12 +15,12 @@ public class Custom5xxErrorDecoder implements ErrorDecoder { int status = response.status(); if (status >= 500) { return new RetryableException( - response.status(), - exception.getMessage(), - response.request().httpMethod(), - exception, - null, - response.request()); + response.status(), + exception.getMessage(), + response.request().httpMethod(), + exception, + null, + response.request()); } return exception; } diff --git a/feign/src/main/java/com/baeldung/feign/retry/ResilientFeignClientBuilder.java b/feign/src/main/java/com/baeldung/feign/retry/ResilientFeignClientBuilder.java index edcaf13242..d8285d362a 100644 --- a/feign/src/main/java/com/baeldung/feign/retry/ResilientFeignClientBuilder.java +++ b/feign/src/main/java/com/baeldung/feign/retry/ResilientFeignClientBuilder.java @@ -18,11 +18,11 @@ public class ResilientFeignClientBuilder { public static T createClient(Class type, String uri) { return Feign.builder() - .client(new OkHttpClient()) - .encoder(new GsonEncoder()) - .decoder(new GsonDecoder()) - .retryer(new Retryer.Default(100L, TimeUnit.SECONDS.toMillis(3L), 5)) - .errorDecoder(new Custom5xxErrorDecoder()) - .target(type, uri); + .client(new OkHttpClient()) + .encoder(new GsonEncoder()) + .decoder(new GsonDecoder()) + .retryer(new Retryer.Default(100L, TimeUnit.SECONDS.toMillis(3L), 5)) + .errorDecoder(new Custom5xxErrorDecoder()) + .target(type, uri); } } diff --git a/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java b/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java index 0b67090268..b51a1a65b3 100644 --- a/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java +++ b/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java @@ -44,7 +44,7 @@ public class Custom5xxErrorDecoderUnitTest { private Response responseStub(int status) { return Response.builder() .request(Request.create( - Request.HttpMethod.GET, "url", new HashMap>(), new byte[0], Charset.defaultCharset(), new RequestTemplate())) + Request.HttpMethod.GET, "url", new HashMap>(), new byte[0], Charset.defaultCharset(), new RequestTemplate())) .status(status) .build(); } From c81fe2f74e5d4b3b3996c58c23e49235591371ca Mon Sep 17 00:00:00 2001 From: AttilaUhrin Date: Tue, 25 Jan 2022 06:49:12 +0100 Subject: [PATCH 13/63] [BAEL-3946] Javafx adding ActionListener to button (#11701) * Add implementation for article. * Add new line at the end of FXML layout file. * Remove default prefWidth from button, convert eventHandler to lambda. * Fix FXML file formatting. --- .../ButtonEventHandlerController.java | 64 +++++++++++++++++++ .../baeldung/button/eventhandler/Main.java | 24 +++++++ .../main/resources/button_event-handler.fxml | 16 +++++ 3 files changed, 104 insertions(+) create mode 100644 javafx/src/main/java/com/baeldung/button/eventhandler/ButtonEventHandlerController.java create mode 100644 javafx/src/main/java/com/baeldung/button/eventhandler/Main.java create mode 100644 javafx/src/main/resources/button_event-handler.fxml diff --git a/javafx/src/main/java/com/baeldung/button/eventhandler/ButtonEventHandlerController.java b/javafx/src/main/java/com/baeldung/button/eventhandler/ButtonEventHandlerController.java new file mode 100644 index 0000000000..8d54a366f0 --- /dev/null +++ b/javafx/src/main/java/com/baeldung/button/eventhandler/ButtonEventHandlerController.java @@ -0,0 +1,64 @@ +package com.baeldung.button.eventhandler; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.effect.DropShadow; +import javafx.scene.effect.Effect; +import javafx.scene.input.MouseButton; +import javafx.scene.input.MouseEvent; +import javafx.scene.text.Font; + +public class ButtonEventHandlerController { + + private static final Logger logger = LoggerFactory.getLogger(ButtonEventHandlerController.class); + + @FXML + private Button button; + + @FXML + private Label label; + + @FXML + private void initialize() { + button.setText("Click me"); + + handleClickEvent(); + handleHoverEffect(); + reuseRightClickEventHandler(); + } + + private void handleClickEvent() { + button.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent event) { + logger.info("OnAction {}", event); + } + }); + + button.setOnAction(event -> logger.info("OnAction {}", event)); + button.setOnAction(event -> logger.info("OnAction2 {}", event)); + } + + private void handleHoverEffect() { + Effect shadow = new DropShadow(); + button.setOnMouseEntered(e -> button.setEffect(shadow)); + button.setOnMouseExited(e -> button.setEffect(null)); + } + + private void reuseRightClickEventHandler() { + EventHandler rightClickHandler = event -> { + if (MouseButton.SECONDARY.equals(event.getButton())) { + button.setFont(new Font(button.getFont() + .getSize() + 1)); + } + }; + button.setOnMousePressed(rightClickHandler); + label.setOnMousePressed(rightClickHandler); + } +} diff --git a/javafx/src/main/java/com/baeldung/button/eventhandler/Main.java b/javafx/src/main/java/com/baeldung/button/eventhandler/Main.java new file mode 100644 index 0000000000..0c4e2eaa08 --- /dev/null +++ b/javafx/src/main/java/com/baeldung/button/eventhandler/Main.java @@ -0,0 +1,24 @@ +package com.baeldung.button.eventhandler; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.layout.Pane; +import javafx.stage.Stage; + +public class Main extends Application { + + public static void main(String[] args) { + launch(args); + } + + @Override + public void start(Stage primaryStage) throws Exception { + FXMLLoader loader = new FXMLLoader(getClass().getResource("/button_event-handler.fxml")); + Pane page = loader.load(); + + primaryStage.setTitle("Button event handler"); + primaryStage.setScene(new Scene(page)); + primaryStage.show(); + } +} diff --git a/javafx/src/main/resources/button_event-handler.fxml b/javafx/src/main/resources/button_event-handler.fxml new file mode 100644 index 0000000000..2f7b1eeff6 --- /dev/null +++ b/javafx/src/main/resources/button_event-handler.fxml @@ -0,0 +1,16 @@ + + + + + +
+
+ + + +
From ba226f327b577498d331a82b81343760c7f9e5b9 Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Tue, 25 Jan 2022 22:21:39 +0530 Subject: [PATCH 14/63] JAVA-8292 : Split or move spring-data-jpa-enterprise module --- .../spring-data-jpa-enterprise-2/README.md | 18 ++++++ .../spring-data-jpa-enterprise-2/pom.xml | 58 +++++++++++++++++++ .../main/java/com/baeldung/Application.java | 13 +++++ .../multipledb/MultipleDbApplication.java | 0 .../PersistenceProductAutoConfiguration.java | 1 - .../PersistenceProductConfiguration.java | 0 .../PersistenceUserAutoConfiguration.java | 1 - .../PersistenceUserConfiguration.java | 0 .../dao/product/ProductRepository.java | 0 .../dao/user/PossessionRepository.java | 0 .../multipledb/dao/user/UserRepository.java | 0 .../multipledb/model/product/Product.java | 0 .../multipledb/model/user/Possession.java | 0 .../baeldung/multipledb/model/user/User.java | 0 .../src/main/resources/application.properties | 15 +++++ .../persistence-multiple-db-boot.properties | 0 .../persistence-multiple-db.properties | 0 .../JpaMultipleDBIntegrationTest.java | 0 .../ProductRepositoryIntegrationTest.java | 2 - .../src/test/resources/logback-test.xml | 13 +++++ .../spring-data-jpa-enterprise/README.md | 2 - .../SpringJpaContextIntegrationTest.java | 6 +- 22 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 persistence-modules/spring-data-jpa-enterprise-2/README.md create mode 100644 persistence-modules/spring-data-jpa-enterprise-2/pom.xml create mode 100644 persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/Application.java rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/java/com/baeldung/multipledb/MultipleDbApplication.java (100%) rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/java/com/baeldung/multipledb/PersistenceProductAutoConfiguration.java (97%) rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/java/com/baeldung/multipledb/PersistenceProductConfiguration.java (100%) rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/java/com/baeldung/multipledb/PersistenceUserAutoConfiguration.java (97%) rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/java/com/baeldung/multipledb/PersistenceUserConfiguration.java (100%) rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/java/com/baeldung/multipledb/dao/product/ProductRepository.java (100%) mode change 100755 => 100644 rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java (100%) rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java (100%) rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/java/com/baeldung/multipledb/model/product/Product.java (100%) mode change 100755 => 100644 rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/java/com/baeldung/multipledb/model/user/Possession.java (100%) rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/java/com/baeldung/multipledb/model/user/User.java (100%) create mode 100644 persistence-modules/spring-data-jpa-enterprise-2/src/main/resources/application.properties rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/resources/persistence-multiple-db-boot.properties (100%) rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/main/resources/persistence-multiple-db.properties (100%) rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java (100%) rename persistence-modules/{spring-data-jpa-enterprise => spring-data-jpa-enterprise-2}/src/test/java/com/baeldung/multipledb/ProductRepositoryIntegrationTest.java (97%) create mode 100644 persistence-modules/spring-data-jpa-enterprise-2/src/test/resources/logback-test.xml diff --git a/persistence-modules/spring-data-jpa-enterprise-2/README.md b/persistence-modules/spring-data-jpa-enterprise-2/README.md new file mode 100644 index 0000000000..9bb08cc497 --- /dev/null +++ b/persistence-modules/spring-data-jpa-enterprise-2/README.md @@ -0,0 +1,18 @@ +## Spring Data JPA - Enterprise + +This module contains articles about Spring Data JPA used in enterprise applications such as transactions, sessions, naming conventions and more + +### Relevant Articles: + +- [Spring JPA – Multiple Databases](https://www.baeldung.com/spring-data-jpa-multiple-databases) +- [Pagination and Sorting using Spring Data JPA](https://www.baeldung.com/spring-data-jpa-pagination-sorting) + +### 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-enterprise-2/pom.xml b/persistence-modules/spring-data-jpa-enterprise-2/pom.xml new file mode 100644 index 0000000000..7efa5f504f --- /dev/null +++ b/persistence-modules/spring-data-jpa-enterprise-2/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + spring-data-jpa-enterprise-2 + spring-data-jpa-enterprise-2 + + + 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 + + + org.springframework.boot + spring-boot-starter-cache + + + com.h2database + h2 + + + com.google.guava + guava + ${guava.version} + + + + + src/main/java + + + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..37dbe7dab8 --- /dev/null +++ b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/Application.java @@ -0,0 +1,13 @@ +package com.baeldung; + +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-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/MultipleDbApplication.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/MultipleDbApplication.java similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/MultipleDbApplication.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/MultipleDbApplication.java diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/PersistenceProductAutoConfiguration.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/PersistenceProductAutoConfiguration.java similarity index 97% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/PersistenceProductAutoConfiguration.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/PersistenceProductAutoConfiguration.java index a6f8f0829f..bc7ab57b06 100644 --- a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/PersistenceProductAutoConfiguration.java +++ b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/PersistenceProductAutoConfiguration.java @@ -8,7 +8,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/PersistenceProductConfiguration.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/PersistenceProductConfiguration.java similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/PersistenceProductConfiguration.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/PersistenceProductConfiguration.java diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/PersistenceUserAutoConfiguration.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/PersistenceUserAutoConfiguration.java similarity index 97% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/PersistenceUserAutoConfiguration.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/PersistenceUserAutoConfiguration.java index e04a1621b2..8080885b37 100644 --- a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/PersistenceUserAutoConfiguration.java +++ b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/PersistenceUserAutoConfiguration.java @@ -8,7 +8,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.PropertySource; diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/PersistenceUserConfiguration.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/PersistenceUserConfiguration.java similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/PersistenceUserConfiguration.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/PersistenceUserConfiguration.java diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/product/ProductRepository.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/dao/product/ProductRepository.java old mode 100755 new mode 100644 similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/product/ProductRepository.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/dao/product/ProductRepository.java diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/product/Product.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/model/product/Product.java old mode 100755 new mode 100644 similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/product/Product.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/model/product/Product.java diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/Possession.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/model/user/Possession.java similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/Possession.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/model/user/Possession.java diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/User.java b/persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/model/user/User.java similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/User.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/java/com/baeldung/multipledb/model/user/User.java diff --git a/persistence-modules/spring-data-jpa-enterprise-2/src/main/resources/application.properties b/persistence-modules/spring-data-jpa-enterprise-2/src/main/resources/application.properties new file mode 100644 index 0000000000..0ca1872207 --- /dev/null +++ b/persistence-modules/spring-data-jpa-enterprise-2/src/main/resources/application.properties @@ -0,0 +1,15 @@ + +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=false + +#hibernate.dialect=org.hibernate.dialect.H2Dialect +spring.jpa.properties.hibernate.id.new_generator_mappings=false \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/resources/persistence-multiple-db-boot.properties b/persistence-modules/spring-data-jpa-enterprise-2/src/main/resources/persistence-multiple-db-boot.properties similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/main/resources/persistence-multiple-db-boot.properties rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/resources/persistence-multiple-db-boot.properties diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/resources/persistence-multiple-db.properties b/persistence-modules/spring-data-jpa-enterprise-2/src/main/resources/persistence-multiple-db.properties similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/main/resources/persistence-multiple-db.properties rename to persistence-modules/spring-data-jpa-enterprise-2/src/main/resources/persistence-multiple-db.properties diff --git a/persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java b/persistence-modules/spring-data-jpa-enterprise-2/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java similarity index 100% rename from persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java diff --git a/persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/multipledb/ProductRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-enterprise-2/src/test/java/com/baeldung/multipledb/ProductRepositoryIntegrationTest.java similarity index 97% rename from persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/multipledb/ProductRepositoryIntegrationTest.java rename to persistence-modules/spring-data-jpa-enterprise-2/src/test/java/com/baeldung/multipledb/ProductRepositoryIntegrationTest.java index 9bfba61a3b..831790af95 100644 --- a/persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/multipledb/ProductRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-enterprise-2/src/test/java/com/baeldung/multipledb/ProductRepositoryIntegrationTest.java @@ -18,12 +18,10 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; -import com.baeldung.multipledb.PersistenceProductConfiguration; import com.baeldung.multipledb.dao.product.ProductRepository; import com.baeldung.multipledb.model.product.Product; diff --git a/persistence-modules/spring-data-jpa-enterprise-2/src/test/resources/logback-test.xml b/persistence-modules/spring-data-jpa-enterprise-2/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..1595326253 --- /dev/null +++ b/persistence-modules/spring-data-jpa-enterprise-2/src/test/resources/logback-test.xml @@ -0,0 +1,13 @@ + + + + + [%d{ISO8601}]-[%thread] %-5level %logger - %msg%n + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-enterprise/README.md b/persistence-modules/spring-data-jpa-enterprise/README.md index 42fbecc880..ca89bd54e7 100644 --- a/persistence-modules/spring-data-jpa-enterprise/README.md +++ b/persistence-modules/spring-data-jpa-enterprise/README.md @@ -4,9 +4,7 @@ This module contains articles about Spring Data JPA used in enterprise applicati ### Relevant Articles: -- [Spring JPA – Multiple Databases](https://www.baeldung.com/spring-data-jpa-multiple-databases) - [Spring Data Java 8 Support](https://www.baeldung.com/spring-data-java-8) -- [Pagination and Sorting using Spring Data JPA](https://www.baeldung.com/spring-data-jpa-pagination-sorting) - [DB Integration Tests with Spring Boot and Testcontainers](https://www.baeldung.com/spring-boot-testcontainers-integration-test) - [A Guide to Spring’s Open Session In View](https://www.baeldung.com/spring-open-session-in-view) - [Working with Lazy Element Collections in JPA](https://www.baeldung.com/java-jpa-lazy-collections) diff --git a/persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/SpringJpaContextIntegrationTest.java b/persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/SpringJpaContextIntegrationTest.java index f3697bf39f..ecaa83f3f3 100644 --- a/persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/SpringJpaContextIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/SpringJpaContextIntegrationTest.java @@ -8,14 +8,10 @@ import org.springframework.test.context.junit4.SpringRunner; import com.baeldung.boot.Application; import com.baeldung.boot.config.PersistenceConfiguration; -import com.baeldung.multipledb.PersistenceProductConfiguration; -import com.baeldung.multipledb.PersistenceUserConfiguration; @RunWith(SpringRunner.class) @DataJpaTest(excludeAutoConfiguration = { - PersistenceConfiguration.class, - PersistenceUserConfiguration.class, - PersistenceProductConfiguration.class }) + PersistenceConfiguration.class }) @ContextConfiguration(classes = Application.class) public class SpringJpaContextIntegrationTest { From 480d9fb405f894daadf6dee77aae054ad8058344 Mon Sep 17 00:00:00 2001 From: HarisHashim Date: Wed, 26 Jan 2022 09:28:57 +0800 Subject: [PATCH 15/63] BAEL-5204: Change the font color of a cell with Apache POI (#11554) * BAEL-5204: First commit "Change the font color of a cell with Apache POI" * BAEL-5204: Create class and unit test * BAEL-5204: finalize unit test * BAEL-5204: fix indent * BAEL-5204: add missing excel template --- .../poi/excel/cellstyle/CellStyler.java | 26 +++++++++ .../poi/excel/cellstyle/CellStyle.xlsx | Bin 0 -> 8393 bytes .../excel/cellstyle/CellStylerUnitTest.java | 52 ++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyler.java create mode 100644 apache-poi/src/main/resources/com/baeldung/poi/excel/cellstyle/CellStyle.xlsx create mode 100644 apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStylerUnitTest.java diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyler.java b/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyler.java new file mode 100644 index 0000000000..6d8b303fd3 --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyler.java @@ -0,0 +1,26 @@ +package com.baeldung.poi.excel.cellstyle; + +import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.HorizontalAlignment; +import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.ss.usermodel.Workbook; + +public class CellStyler { + public CellStyle createWarningColor(Workbook workbook) { + CellStyle style = workbook.createCellStyle(); + + Font font = workbook.createFont(); + font.setFontName("Courier New"); + font.setBold(true); + font.setUnderline(Font.U_SINGLE); + font.setColor(HSSFColorPredefined.DARK_RED.getIndex()); + style.setFont(font); + + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + return style; + } +} diff --git a/apache-poi/src/main/resources/com/baeldung/poi/excel/cellstyle/CellStyle.xlsx b/apache-poi/src/main/resources/com/baeldung/poi/excel/cellstyle/CellStyle.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..ca9394246a706801c2db17a2ab89446c69fe505b GIT binary patch literal 8393 zcmeHMgRd^f#c?DzTqMTE&!6T1GF=*bGf51+V7L z_UnKa`uNHa zX*9cDWhlLKV`}1=G%>%k0FFp($Ayn9qP4ygpito@t$iJHQm)^ZXvf;&Z{Y9S!4w~R zqJGuFPh@S({v=N#ggSJ58lY|$TYs;L+YuJfD^66XJao`a1^-6BOhDedNM_NJmyl?o zdY{IDZK=3FPeu(Qz!Dl*IxyHI*r~Q_PVVnmGKRi}^6=u8@C>xsa@;8e9PFf~3y9oZ z8rT%}+ZkPd61I1kz8S=Qg9ZTH-Xa6k|3*uwHkkGVfi-0W>#z{CG&^Q{5uvlZUTSbRcZ+6J$xh>R;24|Em=*K}Ez%H>{==j7?+S(col50htm z0!u|x*(;^NMOxWUhcY!l7{_B$OoC$ahzzA{0Ho*PcKQeHLW2w)n<9FLNq?c7WSRTGC3Ut-~HKD zUkqr_@Zw)^!{qwuU!Df(*Yh3@<@)prVD?lD91MoWQkE?s6zZQO5l4GNat9dzI73hf z4>2>|_MBc$u6AZlPIf#_zks&9=T2Y)Ckt8P#Y?W_*XnAmxp{n$j0#ZUuFy8W(l z6=YbYtC`&rXeNTK$igx~O?O3m-?o`B4n}CFXf%T>q%g>MMR zfkzA|Iq>`b*vxl%JBT?VIV(>NQYs@jb$i(FcNxplQdQ}&VxsJ)SRZ0F0#$I+rH-~$ zTPUiwjqBaBk_S>IrhPcrV4A$4uk??1(GL^_PU}JVm8OJ|w1mn=YyNLc0nOsc>Dw{- zft4&+8h#}=+IrCG{_r~>KjLdOTHeT6B+L-uQH}J`a7byx0V%sCuTm{Z2kc(zP}^&r zEQiYRP{ow7Y^mZ)D1O!$n_GA)Qo1egDg*MQ193CNzo+TrQc|Cod|o>~C%h^(D3Q~W z!xu@W^0?eDh*H#9m9bq0R&}=Y-Q5$-rlsgSkvCEH#^6(FA`y2}Ri^5|uK9h{L#pnY zAQ5`TK;CT1OF3!oGpp4uZ(xLl81^B7U|}StW_`{3Itp_47#jJT_)SLm$-U4N*|jI6 z2^W_~hPQ7v9n?lly^ajA-tj+KZ+g|(4AOq{nfVAaBIS5_*BMUgB;y!Mh;wiQsGkn_ z*7d#|&}TuuGLVG(ppLNJlz01nZs&UI7o5T+qT;hSR9lCAZ3jbx7PpK})4ePH0d@4~ zIqCc_MJi?;agPQ^pc^foX5@WExm^NhAEyVAPF|`Zoyb2+ko6WnI`jvA8}k-F=^vvy zNhs74OprUC@h5G)ZVHk)5?B4upqzZOv8sDdYmQZA=D~F>$AzmR4o0~fhIc51-)G4V z+auzCqf3ZbYQ`~wCM^g}Aq8L{A?WhIvhr8b{3$U=h>Qq*pOrX~og#ephx89N$ec8f zNATK?fIYZv-9{gLK!n(#`nBMzXPP@zq4kZ8RN2f=*KeDp=g!dbLwzX6j!7^TL~win zV_zqzSueUCo?#rdT|ZUv8L{gsyuw~5uTY{W=M(jhaHonKkfRJ@PIg-_$ZFNzUv`)J z_JO%$?uc#ZWdG^fVE^PIVx|8XVR3zl>sSzQVM6Q@E2g^Mc9mLiuG*JA{Vnl>AcKCXYh^p zh|g}umS-uE59&rSAK!3tDn7WIf4tK6>iXElRXQ4Pp4UJRnGYUT+87JVFR${jTSP($ zMOn3*4Ij;j^BD{IB32`MpWLSElgLu(6Rt(!rQvFMR(_RFb|uZcClX9>dhEFdjm|J4 zG8_hSDqKQ5-Hv^WeYxy=8B4rg+bZH9o%1MNMf#q=V2y%@m-XDA4~wPXt(8lmJLC(R z{zqKHqe1BkO3Rgv4)Tc~aryFx^6Mb0EAm29K_CX!%&^BKcn8n211pPmwg$5EOfJz) zr{r|!IUYM&Xnqpkoa5{Ax+m>>cXishrHnM1kz5;u?x50Pv2G|kQ$TusOk=ve2vt}b zqI@ZV(n(H2KbEItH|4TI;8`qx((_FIq~q4Ame&1I?yPxee^<{Uxy{RUGNQezEBc(# z$rFaG;io=Hus2@?FW`?5nrfPxBZjW+iv&tnay56ZCM8l@ww&Vmxfy9$@|^q0+GBUxKBMO#pmRb^3PdcN_U z)vt3iUo$p{Es3p)O46r)#FBsFD>l?oHwB^I@uWX?pFi>CZf)sc$@#PY39TJHSS*b0>$z(J?py z&_LwVjPoJkI6OPskM6$Jq!Y*foL?(3 zUQjGa-b;{$OB54CKDth+1Vp!hO&=%iR;)v!wRK>8BjQmk650SB_f<0LeD;=4K@69& z*n(kpFVbXkl8Q4w;Pz;SU}L?mpAj!FPvl~AZMi1}UHoZ7T)Z$-*46`K|8Fz-#z8yR zhif^+3Gb>`o8xa!Bh>;gFWrQJ3{Sk5FEbK?{H`u8ALrlPz-h7CQVNhCh|zI<8PWFN znK$A3sN&OQ5sx<>^x6DAwb_XTXu2VDFLnT6WJqP(<59EO_JkQD4#(Rkuz#tAve-?M zjn+8Y47sWQ{%-BWRxqn8+i*{h4>uGqZ^UCB=UKeHc_b5;)LcGHLN9vQ;=XCv7mpbH zkP^YJ?j+bWbzk}noT2w%n}Vb`t}fcyk)Sj-YLZ^s)r3xC%qFrIm=XS*>e=kU@B(}3 zP?^JLQGW}X<#34!slce$%Y8%E9P0Kw5E-t?2k&BKENGiM8p%s)0) z!Jvd+Dp0@QjfxfB@pA{|3}_`v=L{S6X2$Q-^nj%fKYqxfON##SnCUDm-)uM~?=T>8{X^YFc6yx}cVp_~i^(`X1pAMoqs}|jI zt2$9v?yf%}%v|$MU%0f$pI}d%X-Sk8qabniosq7-OBvq-E4r^1RB+%$H}P>K{NYe{0rN1YG&FAI1CfLS z5s|JqqLh0|8aF5e_Bj z2W|#bgyrjbdAUkH7()tjF7LCzfh-0fH zJn~ZwC>sjL+__%n&Xe48Zr*$7ez)I>Ht8wn^)}NG+j>EycPhjnTD1g?3nb{l6g~Im z#SIIYbpfx8OT}ji;DWv@dtzGSb8-|oHg}%#f&5p~DgC0-x+m%v%B+=Y+UHZ~LwWYN z%L@s8{Ub=Zr2@dB%*8TA^mxojF1^aZ>$Me>%;n@CW(6`Oa#wYB=_d0hqRN`2sy3478)bzA> zTPN_Vmyn%hi0&I!?Uf2O%Bv|%PiOSrLiCMJHfYZHJneJO*=cVV{Nis@%5(HR&o6PR7F6C{-%fB`K#(ErZ-nJRvAbyYt9hTsv(U)?meM zDK-M~`-T~P6?hW%?TuSer%@?|uVjbb-EvdiqtKY5Mf)^TqpHt`q`6NQx94FQT;OA6 zF<0YYKcct09sjCfTB~F{!-yQ|JwnHD{#V1?+(a=GNo_|_LP=>x2a3^pmQyTEfKB94i4@=6v z*)n++3l2sVfbu5`31;KeJU6Ycy-6aZ!|h>CN@bm=WwW^Cq)N{2HzY{a=bt#_Bc6XC z#1?1K+%(7J@Kql$$)7!n4;`E6E#vE_$~}EZKB!b!^#+c*gl{<2X2H2RGWcTjC=O-d zt#k}Nejm)$X_z&Mp|@J}{AgLE3hH_<&9{_13R5Nk^In2t(BtrqqrL@JwOxW)O{8>; z6cKjlC>kUYmt{(iFs<^u=~d`koO4IMGy`&9w;i0^*|7XIbw4M%y#8uk@7{31nix?e z@EY^LM&=__^3m!bL0+JdWa%k5chR^_AcCSmkVKTquv5g)PE*n?Fu)aO2B)L_?l*S(CxQ<{RvuTD z2rpBpW{`;Oi`T^LTlgPcb)V0A0DoLxeTga&L$ia+VLiZMoh8XzrfeL-JDzj^q1QP> zuoyDfFux4C649mPtlMgT>=A6cgk(dUG6msL&%~k_uvkqvvZ!aM-+!xP?(E5C zPH2X$+IZ*6n5r8@JOXEp>w$BO7$lQq4sC_>78o9KujIA4a);B!T#;-HGGg`96Bs;z z3bF7ugvvW#OXIJVp+IhL*Zv*%qjA+({s_E>Al`(3;NIHQ)zU)K-POj?>L>iMha~TI zgMkub?Mn~3cN_|^KsK|vjHAfd!_z3q3Io^$jRHfkRoHf;#qleS*R_$yYn?zRKDq_b zw!DN-IB`s_od#JV*YYNY@sWBm!`^x#GLxQ-It}uumgUdA2U^cUg=+V8^ck`7qw6jF zT=s0S7|>d;|Fu?>O#S7HHsTu_1XDqX&98-%xw@;9vm2+mldI()ryT#?^CFhfKlzbe zH<&Pd5&at;{XU<9hmcfNHO`@Fw+T`If|ToIH3N=9!&Zl}K2($cEc1n1#|t+FvAJ!Q z%R3v|4kb~z7>b=y`Yd(11=`gB_8IvqT z2s~pvsdXc2h=oVz@d|omTr`D;rA1oU8w~sp*q7oGxjqA%)10XE8U?|`_B*qCbq5s_Vm?~fbqY+pHA@*9pzhcqU+4;XvL`>Nq?M1Sr z6GA-0_mQtCvEgJ8xH@)7rJ4NWlWJ3>~Mn}kdyz1nJ&I8}uY zE)HD0l(8!(8&lirA}-ReO6wqI_GK4=)k|qFVq-QNOpI#_3pvtWG}_udkK{w&?(;^W zUqsuY_*h;4+=T?XWiuI8oQdTgut3`KGxaMh3xRt+rtG6A7(Ugyv*4$@{vP)FeZ84k)Q77(i)E4mU>d33f;|{RM z&FV?d>5=cFK#og8D<1mAo7x+!KjDjXhaC~*|NYST&*l1a{FftSb)~;M`1|qJpN2oi z3Oob3-KEyP-{jTJ{d-%Ja v{;~%Ez9KyQLtTG2|9c4htGPSbU(Ejqi0VqHh}8uE?jl~n2m$Az_;K|=SI=Sd literal 0 HcmV?d00001 diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStylerUnitTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStylerUnitTest.java new file mode 100644 index 0000000000..074e51919a --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStylerUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.poi.excel.cellstyle; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Paths; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Before; +import org.junit.Test; + +public class CellStylerUnitTest { + private static String FILE_NAME = "com/baeldung/poi/excel/cellstyle/CellStyle.xlsx"; + private static final String NEW_FILE_NAME = "CellStyleTest_output.xlsx"; + private String fileLocation; + + @Before + public void setup() throws IOException, URISyntaxException { + fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME) + .toURI()) + .toString(); + } + + @Test + public void testApplyWarningColor() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row1 = sheet.createRow(0); + row1.setHeightInPoints((short) 40); + + CellStyler styler = new CellStyler(); + CellStyle style = styler.createWarningColor(workbook); + + Cell cell1 = row1.createCell(0); + cell1.setCellStyle(style); + cell1.setCellValue("Hello"); + + Cell cell2 = row1.createCell(1); + cell2.setCellStyle(style); + cell2.setCellValue("world!"); + + FileOutputStream outputStream = new FileOutputStream(NEW_FILE_NAME); + workbook.write(outputStream); + outputStream.close(); + workbook.close(); + } +} From a5defe6121e639ccc79a96d15d8f365acd7b87b2 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:26:50 +0800 Subject: [PATCH 16/63] Update README.md --- core-java-modules/core-java-security-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-security-3/README.md b/core-java-modules/core-java-security-3/README.md index 30cfd8a947..8623fa5aa8 100644 --- a/core-java-modules/core-java-security-3/README.md +++ b/core-java-modules/core-java-security-3/README.md @@ -8,4 +8,5 @@ This module contains articles about core Java Security - [Enabling Unlimited Strength Cryptography in Java](https://www.baeldung.com/jce-enable-unlimited-strength) - [Initialization Vector for Encryption](https://www.baeldung.com/java-encryption-iv) - [HMAC in Java](https://www.baeldung.com/java-hmac) +- [Generating a Secure AES Key in Java](https://www.baeldung.com/java-secure-aes-key) - More articles: [[<-- prev]](/core-java-modules/core-java-security-2) From 34e7cc8112a2328c16ccc0a1542e7dfefd9eec52 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:31:09 +0800 Subject: [PATCH 17/63] Update README.md --- java-collections-maps-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/java-collections-maps-3/README.md b/java-collections-maps-3/README.md index 87817331b5..75842c85a4 100644 --- a/java-collections-maps-3/README.md +++ b/java-collections-maps-3/README.md @@ -6,3 +6,4 @@ - [Optimizing HashMap’s Performance](https://www.baeldung.com/java-hashmap-optimize-performance) - [Update the Value Associated With a Key in a HashMap](https://www.baeldung.com/java-hashmap-update-value-by-key) - [Java Map – keySet() vs. entrySet() vs. values() Methods](https://www.baeldung.com/java-map-entries-methods) +- [Java IdentityHashMap Class and Its Use Cases](https://www.baeldung.com/java-identityhashmap) From af7c764ce9499eed8ca1536087bad3b4658bc57e Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:35:42 +0800 Subject: [PATCH 18/63] Update README.md --- core-java-modules/core-java-lang-oop-constructors/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-lang-oop-constructors/README.md b/core-java-modules/core-java-lang-oop-constructors/README.md index 6083f1d0df..ddd0ec6afb 100644 --- a/core-java-modules/core-java-lang-oop-constructors/README.md +++ b/core-java-modules/core-java-lang-oop-constructors/README.md @@ -10,3 +10,4 @@ This module contains article about constructors in Java - [Throwing Exceptions in Constructors](https://www.baeldung.com/java-constructors-exceptions) - [Constructors in Java Abstract Classes](https://www.baeldung.com/java-abstract-classes-constructors) - [Java Implicit Super Constructor is Undefined Error](https://www.baeldung.com/java-implicit-super-constructor-is-undefined-error) +- [Constructor Specification in Java](https://www.baeldung.com/java-constructor-specification) From 85c929b74bab6bf63e7ef5e129ca8cefcd811fee Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:38:14 +0800 Subject: [PATCH 19/63] Update README.md --- persistence-modules/spring-data-jpa-repo-2/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/persistence-modules/spring-data-jpa-repo-2/README.md b/persistence-modules/spring-data-jpa-repo-2/README.md index be5bab1b56..6403510e6f 100644 --- a/persistence-modules/spring-data-jpa-repo-2/README.md +++ b/persistence-modules/spring-data-jpa-repo-2/README.md @@ -1,7 +1,9 @@ ## Spring Data JPA - Repositories ### Relevant Articles: + - [Introduction to Spring Data JPA](https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa) - [Performance Difference Between save() and saveAll() in Spring Data](https://www.baeldung.com/spring-data-save-saveall) - [LIKE Queries in Spring JPA Repositories](https://www.baeldung.com/spring-jpa-like-queries) +- [How to Access EntityManager with Spring Data](https://www.baeldung.com/spring-data-entitymanager) - More articles: [[<-- prev]](../spring-data-jpa-repo) From f1bdc807b55af870efa4d99e7f2fb6569c9d00ea Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:39:48 +0800 Subject: [PATCH 20/63] Update README.md --- core-java-modules/core-java-concurrency-collections-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-concurrency-collections-2/README.md b/core-java-modules/core-java-concurrency-collections-2/README.md index 6ad6529efc..692c218395 100644 --- a/core-java-modules/core-java-concurrency-collections-2/README.md +++ b/core-java-modules/core-java-concurrency-collections-2/README.md @@ -2,4 +2,5 @@ - [Introduction to Lock Striping](https://www.baeldung.com/java-lock-stripping) - [Guide to the Java TransferQueue](http://www.baeldung.com/java-transfer-queue) +- [Java Concurrent HashSet Equivalent to ConcurrentHashMap](https://www.baeldung.com/java-concurrent-hashset-concurrenthashmap) - [[<-- Prev]](/core-java-modules/core-java-concurrency-collections) From f92a336512c6c676b0c602fba0ad65a5c0c7d762 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:41:44 +0800 Subject: [PATCH 21/63] Update README.md --- core-java-modules/core-java-jndi/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-jndi/README.md b/core-java-modules/core-java-jndi/README.md index b0b23fc0d0..9c6f489841 100644 --- a/core-java-modules/core-java-jndi/README.md +++ b/core-java-modules/core-java-jndi/README.md @@ -3,3 +3,4 @@ - [Java Naming and Directory Interface Overview](https://www.baeldung.com/jndi) - [LDAP Authentication Using Pure Java](https://www.baeldung.com/java-ldap-auth) +- [Testing LDAP Connections With Java](https://www.baeldung.com/java-test-ldap-connections) From 3874415836e757b78b4ebc3986903d5268326caa Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:45:09 +0800 Subject: [PATCH 22/63] Update README.md --- core-java-modules/core-java-nio-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-nio-2/README.md b/core-java-modules/core-java-nio-2/README.md index 52efa0330a..9152a494d8 100644 --- a/core-java-modules/core-java-nio-2/README.md +++ b/core-java-modules/core-java-nio-2/README.md @@ -11,4 +11,5 @@ This module contains articles about core Java non-blocking input and output (IO) - [How to Lock a File in Java](https://www.baeldung.com/java-lock-files) - [Java NIO DatagramChannel](https://www.baeldung.com/java-nio-datagramchannel) - [Java – Path vs File](https://www.baeldung.com/java-path-vs-file) +- [What Is the Difference Between NIO and NIO.2?](https://www.baeldung.com/java-nio-vs-nio-2) - [[<-- Prev]](/core-java-modules/core-java-nio) From 61030e497fa37630bfd9fe58547bfd5e6f69a18e Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:47:15 +0800 Subject: [PATCH 23/63] Update README.md --- javafx/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/javafx/README.md b/javafx/README.md index 8ef06eb012..5e034adb38 100644 --- a/javafx/README.md +++ b/javafx/README.md @@ -3,6 +3,8 @@ This module contains articles about JavaFX. ### Relevant Articles: + - [Introduction to JavaFX](https://www.baeldung.com/javafx) - [Display Custom Items in JavaFX ListView](https://www.baeldung.com/javafx-listview-display-custom-items) +- [Adding EventHandler to JavaFX Button](https://www.baeldung.com/javafx-button-eventhandler) From 2b6ef895558923377f8d2f5e333f26d8ba5aeca3 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:48:52 +0800 Subject: [PATCH 24/63] Update README.md --- core-java-modules/core-java-security-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-security-3/README.md b/core-java-modules/core-java-security-3/README.md index 8623fa5aa8..31969cd270 100644 --- a/core-java-modules/core-java-security-3/README.md +++ b/core-java-modules/core-java-security-3/README.md @@ -9,4 +9,5 @@ This module contains articles about core Java Security - [Initialization Vector for Encryption](https://www.baeldung.com/java-encryption-iv) - [HMAC in Java](https://www.baeldung.com/java-hmac) - [Generating a Secure AES Key in Java](https://www.baeldung.com/java-secure-aes-key) +- [Computing an X509 Certificate’s Thumbprint in Java](https://www.baeldung.com/java-x509-certificate-thumbprint) - More articles: [[<-- prev]](/core-java-modules/core-java-security-2) From 21316676200ddea1dc5a835b25e5aa7612807f45 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:59:31 +0800 Subject: [PATCH 25/63] Update README.md --- spring-cloud/spring-cloud-gateway/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spring-cloud/spring-cloud-gateway/README.md b/spring-cloud/spring-cloud-gateway/README.md index 90e81fe9a2..6b199977e3 100644 --- a/spring-cloud/spring-cloud-gateway/README.md +++ b/spring-cloud/spring-cloud-gateway/README.md @@ -3,7 +3,9 @@ This module contains articles about Spring Cloud Gateway ### Relevant Articles: + - [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) +- [Using Spring Cloud Gateway with OAuth 2.0 Patterns](https://www.baeldung.com/spring-cloud-gateway-oauth2) From 7aaf834ff862c1248c765129fe89dfaca38edb9c Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 26 Jan 2022 18:02:03 +0800 Subject: [PATCH 26/63] Update README.md --- apache-poi/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/apache-poi/README.md b/apache-poi/README.md index 34e7631087..ed30d9a4f3 100644 --- a/apache-poi/README.md +++ b/apache-poi/README.md @@ -14,4 +14,5 @@ This module contains articles about Apache POI. - [Set Background Color of a Cell with Apache POI](https://www.baeldung.com/apache-poi-background-color) - [Add Borders to Excel Cells With Apache POI](https://www.baeldung.com/apache-poi-add-borders) - [Reading Values From Excel in Java](https://www.baeldung.com/java-read-dates-excel) +- [Change Cell Font Style with Apache POI](https://www.baeldung.com/apache-poi-change-cell-font) - More articles: [[next -->]](../apache-poi-2) From 57f91f9948a4350e851e42eba42638ccb8139daa Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Thu, 27 Jan 2022 10:15:16 +0100 Subject: [PATCH 27/63] JAVA-9605: Disable security for the integration tests --- .../secondservice/SecondServiceIntegrationTest.java | 4 +++- .../customfilters/service/ServiceIntegrationTest.java | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/secondservice/SecondServiceIntegrationTest.java b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/secondservice/SecondServiceIntegrationTest.java index 6b2a432d20..f4b3d0f00d 100644 --- a/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/secondservice/SecondServiceIntegrationTest.java +++ b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/secondservice/SecondServiceIntegrationTest.java @@ -2,12 +2,14 @@ package com.baeldung.springcloudgateway.customfilters.secondservice; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; import org.springframework.test.web.reactive.server.WebTestClient; import com.baeldung.springcloudgateway.customfilters.secondservice.web.SecondServiceRestController; -@WebFluxTest(SecondServiceRestController.class) +@WebFluxTest(controllers = SecondServiceRestController.class, + excludeAutoConfiguration = ReactiveSecurityAutoConfiguration.class) public class SecondServiceIntegrationTest { @Autowired diff --git a/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/service/ServiceIntegrationTest.java b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/service/ServiceIntegrationTest.java index bfb3f23f0d..9990cd003c 100644 --- a/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/service/ServiceIntegrationTest.java +++ b/spring-cloud/spring-cloud-gateway/src/test/java/com/baeldung/springcloudgateway/customfilters/service/ServiceIntegrationTest.java @@ -2,13 +2,15 @@ package com.baeldung.springcloudgateway.customfilters.service; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration; import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; import org.springframework.http.HttpHeaders; import org.springframework.test.web.reactive.server.WebTestClient; import com.baeldung.springcloudgateway.customfilters.service.web.ServiceRestController; -@WebFluxTest(ServiceRestController.class) +@WebFluxTest(controllers = ServiceRestController.class, + excludeAutoConfiguration = ReactiveSecurityAutoConfiguration.class) public class ServiceIntegrationTest { @Autowired From 57495b7f15da1be2b561097ca7cc0f8a2de010a6 Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Fri, 28 Jan 2022 09:49:18 +0530 Subject: [PATCH 28/63] Remove unwanted dependencies --- .../spring-data-jpa-enterprise-2/pom.xml | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/persistence-modules/spring-data-jpa-enterprise-2/pom.xml b/persistence-modules/spring-data-jpa-enterprise-2/pom.xml index 7efa5f504f..f35ee378d1 100644 --- a/persistence-modules/spring-data-jpa-enterprise-2/pom.xml +++ b/persistence-modules/spring-data-jpa-enterprise-2/pom.xml @@ -22,14 +22,6 @@ org.springframework.boot spring-boot-starter-data-jpa - - org.springframework.boot - spring-boot-starter-data-jdbc - - - org.springframework.boot - spring-boot-starter-cache - com.h2database h2 @@ -41,18 +33,4 @@ - - src/main/java - - - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - - - - \ No newline at end of file From a8ce62042fb912b941b30fa775c630c8f6b21daf Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Fri, 28 Jan 2022 22:20:11 +0000 Subject: [PATCH 29/63] [JAVA-9019] Move sysout lines to info logging --- .../baeldung/collections/iterators/Iterators.java | 13 ++++++------- .../baeldung/collections/bitset/BitSetUnitTest.java | 10 +++++++--- .../src/test/resources/logback.xml | 13 +++++++++++++ 3 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 core-java-modules/core-java-collections-3/src/test/resources/logback.xml diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/iterators/Iterators.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/iterators/Iterators.java index 23e6bbda77..4daf7dd9d1 100644 --- a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/iterators/Iterators.java +++ b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/iterators/Iterators.java @@ -1,17 +1,16 @@ package com.baeldung.collections.iterators; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.ArrayList; import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; -/** - * Source code https://github.com/eugenp/tutorials - * - * @author Santosh Thakur - */ - public class Iterators { + private static final Logger LOG = LoggerFactory.getLogger(Iterators.class); + public static int failFast1() { ArrayList numbers = new ArrayList<>(); @@ -44,7 +43,7 @@ public class Iterators { } } - System.out.println("using iterator's remove method = " + numbers); + LOG.debug("using iterator's remove method = {}", numbers); iterator = numbers.iterator(); while (iterator.hasNext()) { diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/bitset/BitSetUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/bitset/BitSetUnitTest.java index d9340f45c1..7141dbf79f 100644 --- a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/bitset/BitSetUnitTest.java +++ b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/bitset/BitSetUnitTest.java @@ -3,6 +3,8 @@ package com.baeldung.collections.bitset; import org.junit.Test; import org.openjdk.jol.info.ClassLayout; import org.openjdk.jol.info.GraphLayout; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.BitSet; @@ -10,18 +12,20 @@ import static org.assertj.core.api.Assertions.assertThat; public class BitSetUnitTest { + private static final Logger LOG = LoggerFactory.getLogger(BitSetUnitTest.class); + @Test public void givenBoolArray_whenMemoryLayout_thenConsumeMoreThanOneBit() { boolean[] bits = new boolean[1024 * 1024]; - System.out.println(ClassLayout.parseInstance(bits).toPrintable()); + LOG.debug(ClassLayout.parseInstance(bits).toPrintable()); } @Test public void givenBitSet_whenMemoryLayout_thenConsumeOneBitPerFlag() { BitSet bitSet = new BitSet(1024 * 1024); - System.out.println(GraphLayout.parseInstance(bitSet).toPrintable()); + LOG.debug(GraphLayout.parseInstance(bitSet).toPrintable()); } @Test @@ -157,7 +161,7 @@ public class BitSetUnitTest { BitSet bitSet = new BitSet(); bitSet.set(15, 25); - bitSet.stream().forEach(System.out::println); + bitSet.stream().forEach(bit -> LOG.debug(String.valueOf(bit))); assertThat(bitSet.stream().count()).isEqualTo(10); } diff --git a/core-java-modules/core-java-collections-3/src/test/resources/logback.xml b/core-java-modules/core-java-collections-3/src/test/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file From f72971181ef5722619a60c927ee59439a05fa46b Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Fri, 28 Jan 2022 22:50:49 +0000 Subject: [PATCH 30/63] [JAVA-9019] Logging clean up --- .../concurrent/prioritytaskexecution/Job.java | 15 ++++-- .../forkjoin/CustomRecursiveAction.java | 10 ++-- .../PriorityJobSchedulerUnitTest.java | 16 +++---- .../Java8ForkJoinIntegrationTest.java | 19 ++++---- .../thread/join/ThreadJoinUnitTest.java | 48 +++++++++---------- .../src/test/resources/logback.xml | 13 +++++ 6 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 core-java-modules/core-java-concurrency-advanced-2/src/test/resources/logback.xml diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java b/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java index 9900d1c63d..b0866cb7f0 100644 --- a/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java +++ b/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java @@ -1,9 +1,15 @@ package com.baeldung.concurrent.prioritytaskexecution; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class Job implements Runnable { - private String jobName; - private JobPriority jobPriority; - + + private static final Logger LOGGER = LoggerFactory.getLogger(Job.class); + + private final String jobName; + private final JobPriority jobPriority; + public Job(String jobName, JobPriority jobPriority) { this.jobName = jobName; this.jobPriority = jobPriority != null ? jobPriority : JobPriority.MEDIUM; @@ -16,8 +22,7 @@ public class Job implements Runnable { @Override public void run() { try { - System.out.println("Job:" + jobName + - " Priority:" + jobPriority); + LOGGER.debug("Job:{} Priority:{}", jobName, jobPriority); Thread.sleep(1000); } catch (InterruptedException ignored) { } diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java b/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java index ebe59e33b1..26d620ba07 100644 --- a/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java +++ b/core-java-modules/core-java-concurrency-advanced-2/src/main/java/com/baeldung/forkjoin/CustomRecursiveAction.java @@ -1,19 +1,21 @@ package com.baeldung.forkjoin; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.RecursiveAction; -import java.util.logging.Logger; public class CustomRecursiveAction extends RecursiveAction { + final Logger logger = LoggerFactory.getLogger(CustomRecursiveAction.class); + private String workLoad = ""; private static final int THRESHOLD = 4; - private static Logger logger = Logger.getAnonymousLogger(); - public CustomRecursiveAction(String workLoad) { this.workLoad = workLoad; } @@ -43,7 +45,7 @@ public class CustomRecursiveAction extends RecursiveAction { private void processing(String work) { String result = work.toUpperCase(); - logger.info("This result - (" + result + ") - was processed by " + Thread.currentThread() + logger.debug("This result - (" + result + ") - was processed by " + Thread.currentThread() .getName()); } } diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java index 1e67fe45c1..20e6a5ef5b 100644 --- a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java +++ b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java @@ -3,9 +3,9 @@ package com.baeldung.concurrent.prioritytaskexecution; import org.junit.Test; public class PriorityJobSchedulerUnitTest { - private static int POOL_SIZE = 1; - private static int QUEUE_SIZE = 10; - + private static final int POOL_SIZE = 1; + private static final int QUEUE_SIZE = 10; + @Test public void whenMultiplePriorityJobsQueued_thenHighestPriorityJobIsPicked() { Job job1 = new Job("Job1", JobPriority.LOW); @@ -14,19 +14,19 @@ public class PriorityJobSchedulerUnitTest { Job job4 = new Job("Job4", JobPriority.MEDIUM); Job job5 = new Job("Job5", JobPriority.LOW); Job job6 = new Job("Job6", JobPriority.HIGH); - + PriorityJobScheduler pjs = new PriorityJobScheduler(POOL_SIZE, QUEUE_SIZE); - + pjs.scheduleJob(job1); pjs.scheduleJob(job2); pjs.scheduleJob(job3); pjs.scheduleJob(job4); pjs.scheduleJob(job5); pjs.scheduleJob(job6); - + // ensure no tasks is pending before closing the scheduler while (pjs.getQueuedTaskCount() != 0); - + // delay to avoid job sleep (added for demo) being interrupted try { Thread.sleep(2000); @@ -34,7 +34,7 @@ public class PriorityJobSchedulerUnitTest { Thread.currentThread().interrupt(); throw new RuntimeException(e); } - + pjs.closeScheduler(); } } diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/forkjoin/Java8ForkJoinIntegrationTest.java b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/forkjoin/Java8ForkJoinIntegrationTest.java index c469fe0195..ec10e2be79 100644 --- a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/forkjoin/Java8ForkJoinIntegrationTest.java +++ b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/forkjoin/Java8ForkJoinIntegrationTest.java @@ -1,18 +1,15 @@ package com.baeldung.forkjoin; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import com.baeldung.forkjoin.util.PoolUtil; +import org.junit.Before; +import org.junit.Test; import java.util.Random; import java.util.concurrent.ForkJoinPool; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.forkjoin.CustomRecursiveAction; -import com.baeldung.forkjoin.CustomRecursiveTask; -import com.baeldung.forkjoin.util.PoolUtil; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; public class Java8ForkJoinIntegrationTest { @@ -63,11 +60,11 @@ public class Java8ForkJoinIntegrationTest { ForkJoinPool forkJoinPool = ForkJoinPool.commonPool(); forkJoinPool.execute(customRecursiveTask); - int result = customRecursiveTask.join(); + customRecursiveTask.join(); assertTrue(customRecursiveTask.isDone()); forkJoinPool.submit(customRecursiveTask); - int resultTwo = customRecursiveTask.join(); + customRecursiveTask.join(); assertTrue(customRecursiveTask.isDone()); } diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/thread/join/ThreadJoinUnitTest.java b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/thread/join/ThreadJoinUnitTest.java index 85cff51eb3..dc30ce6c74 100644 --- a/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/thread/join/ThreadJoinUnitTest.java +++ b/core-java-modules/core-java-concurrency-advanced-2/src/test/java/com/baeldung/thread/join/ThreadJoinUnitTest.java @@ -1,12 +1,12 @@ package com.baeldung.thread.join; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.util.logging.Logger; - import org.junit.Ignore; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Demonstrates Thread.join behavior. @@ -14,55 +14,55 @@ import org.junit.Test; */ public class ThreadJoinUnitTest { - final static Logger LOGGER = Logger.getLogger(ThreadJoinUnitTest.class.getName()); + private static final Logger LOGGER = LoggerFactory.getLogger(ThreadJoinUnitTest.class); - class SampleThread extends Thread { - public int processingCount = 0; + static class SampleThread extends Thread { + public int processingCount; SampleThread(int processingCount) { this.processingCount = processingCount; - LOGGER.info("Thread " + this.getName() + " created"); + LOGGER.debug("Thread " + this.getName() + " created"); } @Override public void run() { - LOGGER.info("Thread " + this.getName() + " started"); + LOGGER.debug("Thread " + this.getName() + " started"); while (processingCount > 0) { try { Thread.sleep(1000); // Simulate some work being done by thread } catch (InterruptedException e) { - LOGGER.info("Thread " + this.getName() + " interrupted."); + LOGGER.debug("Thread " + this.getName() + " interrupted."); } processingCount--; - LOGGER.info("Inside Thread " + this.getName() + ", processingCount = " + processingCount); + LOGGER.debug("Inside Thread " + this.getName() + ", processingCount = " + processingCount); } - LOGGER.info("Thread " + this.getName() + " exiting"); + LOGGER.debug("Thread " + this.getName() + " exiting"); } } @Test public void givenNewThread_whenJoinCalled_returnsImmediately() throws InterruptedException { Thread t1 = new SampleThread(0); - LOGGER.info("Invoking join."); + LOGGER.debug("Invoking join."); t1.join(); - LOGGER.info("Returned from join"); - LOGGER.info("Thread state is" + t1.getState()); + LOGGER.debug("Returned from join"); + LOGGER.debug("Thread state is" + t1.getState()); assertFalse(t1.isAlive()); } @Test - public void givenStartedThread_whenJoinCalled_waitsTillCompletion() + public void givenStartedThread_whenJoinCalled_waitsTillCompletion() throws InterruptedException { Thread t2 = new SampleThread(1); t2.start(); - LOGGER.info("Invoking join."); + LOGGER.debug("Invoking join."); t2.join(); - LOGGER.info("Returned from join"); + LOGGER.debug("Returned from join"); assertFalse(t2.isAlive()); } @Test - public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout() + public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout() throws InterruptedException { Thread t3 = new SampleThread(10); t3.start(); @@ -72,18 +72,18 @@ public class ThreadJoinUnitTest { @Test @Ignore - public void givenThreadTerminated_checkForEffect_notGuaranteed() + public void givenThreadTerminated_checkForEffect_notGuaranteed() throws InterruptedException { SampleThread t4 = new SampleThread(10); t4.start(); //not guaranteed to stop even if t4 finishes. do { - - } while (t4.processingCount > 0); + + } while (t4.processingCount > 0); } @Test - public void givenJoinWithTerminatedThread_checkForEffect_guaranteed() + public void givenJoinWithTerminatedThread_checkForEffect_guaranteed() throws InterruptedException { SampleThread t4 = new SampleThread(10); t4.start(); diff --git a/core-java-modules/core-java-concurrency-advanced-2/src/test/resources/logback.xml b/core-java-modules/core-java-concurrency-advanced-2/src/test/resources/logback.xml new file mode 100644 index 0000000000..b2fa0488f3 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-2/src/test/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + From c69343c8537443d278ff8fa14da586efb93ff3d8 Mon Sep 17 00:00:00 2001 From: sebx59 Date: Sat, 29 Jan 2022 06:58:08 +0100 Subject: [PATCH 31/63] Example class for BAEL-4888 (#11728) code used to illustrate the article about retrieving the MongoDB ID --- .../mongo/objectid/RetrieveIdExample.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/objectid/RetrieveIdExample.java diff --git a/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/objectid/RetrieveIdExample.java b/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/objectid/RetrieveIdExample.java new file mode 100644 index 0000000000..74279bbfcd --- /dev/null +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/mongo/objectid/RetrieveIdExample.java @@ -0,0 +1,57 @@ +package com.baeldung.mongo.objectid; + +import java.util.Date; + +import org.bson.Document; +import org.bson.types.ObjectId; + +import com.mongodb.MongoClient; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; + +public class RetrieveIdExample { + + public static void main(String[] args) { + + try ( MongoClient mongoClient = new MongoClient("localhost", 27017) ) { + + MongoDatabase database = mongoClient.getDatabase("myMongoDb"); + MongoCollection collection = database.getCollection("example"); + +// Create document with user-generated ID + ObjectId generatedId = new ObjectId(); + + System.out.println(generatedId.toString()); + + Document document = new Document(); + document.put("_id", generatedId); + document.put("name", "Shubham"); + document.put("company", "Baeldung"); + collection.insertOne(document); + +// Check that the ID of the document is still the one we set + System.out.println(document.getObjectId("_id").equals(generatedId)); + + +// Create a second document by injecting the ID in the constructor + + ObjectId generatedId2 = ObjectId.get(); + + Document document2 = new Document("_id", generatedId2); + document2.put("name", "Shubham"); + document2.put("company", "Baeldung"); + collection.insertOne(document2); + + Date creationDate = generatedId.getDate(); + System.out.println(creationDate); + + int timestamp = generatedId.getTimestamp(); + + } catch (Exception e) { + + System.out.println(e.getMessage()); + e.printStackTrace(); + } + } + +} From fc145be4a87f3994725f6f7bd360f35296521401 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Sat, 29 Jan 2022 16:24:15 +0000 Subject: [PATCH 32/63] [JAVA-9019] Logging clean up --- .../core-java-io/src/main/resources/log4j.properties | 6 ------ .../src/main/resources/log4jstructuraldp.properties | 9 --------- .../core-java-io/src/main/resources/logback.xml | 5 ----- 3 files changed, 20 deletions(-) delete mode 100644 core-java-modules/core-java-io/src/main/resources/log4j.properties delete mode 100644 core-java-modules/core-java-io/src/main/resources/log4jstructuraldp.properties diff --git a/core-java-modules/core-java-io/src/main/resources/log4j.properties b/core-java-modules/core-java-io/src/main/resources/log4j.properties deleted file mode 100644 index 621cf01735..0000000000 --- a/core-java-modules/core-java-io/src/main/resources/log4j.properties +++ /dev/null @@ -1,6 +0,0 @@ -log4j.rootLogger=DEBUG, A1 - -log4j.appender.A1=org.apache.log4j.ConsoleAppender - -log4j.appender.A1.layout=org.apache.log4j.PatternLayout -log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n \ No newline at end of file diff --git a/core-java-modules/core-java-io/src/main/resources/log4jstructuraldp.properties b/core-java-modules/core-java-io/src/main/resources/log4jstructuraldp.properties deleted file mode 100644 index 5bc2bfe4b9..0000000000 --- a/core-java-modules/core-java-io/src/main/resources/log4jstructuraldp.properties +++ /dev/null @@ -1,9 +0,0 @@ - -# Root logger -log4j.rootLogger=INFO, file, stdout - -# Write to console -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.Target=System.out -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n \ No newline at end of file diff --git a/core-java-modules/core-java-io/src/main/resources/logback.xml b/core-java-modules/core-java-io/src/main/resources/logback.xml index 617917dca2..c404714347 100644 --- a/core-java-modules/core-java-io/src/main/resources/logback.xml +++ b/core-java-modules/core-java-io/src/main/resources/logback.xml @@ -7,13 +7,8 @@ - - - - - From e046bc734b03023f959d008e143e2d8337cc0e6b Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Sat, 29 Jan 2022 23:23:37 +0100 Subject: [PATCH 33/63] BAEL-5301 Fix indentation --- .../feign/retry/Custom5xxErrorDecoderUnitTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java b/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java index b51a1a65b3..8c43b42b76 100644 --- a/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java +++ b/feign/src/test/java/com/baeldung/feign/retry/Custom5xxErrorDecoderUnitTest.java @@ -43,9 +43,9 @@ public class Custom5xxErrorDecoderUnitTest { @NotNull private Response responseStub(int status) { return Response.builder() - .request(Request.create( - Request.HttpMethod.GET, "url", new HashMap>(), new byte[0], Charset.defaultCharset(), new RequestTemplate())) - .status(status) - .build(); + .request(Request.create( + Request.HttpMethod.GET, "url", new HashMap<>(), new byte[0], Charset.defaultCharset(), new RequestTemplate())) + .status(status) + .build(); } } From 6f6b8e23e00267cde3b424bcf6923ca4450430d4 Mon Sep 17 00:00:00 2001 From: makapszenna <66560584+makapszenna@users.noreply.github.com> Date: Mon, 31 Jan 2022 04:40:53 +0100 Subject: [PATCH 34/63] BAEL-5308 An Introduction to Domain Graph Service (DGS) Framework (#11718) Co-authored-by: Adrianna Zychewicz --- graphql/graphql-dgs/pom.xml | 91 +++++++++++++++++++ .../java/com/bealdung/graphqlDGS/Album.java | 25 +++++ .../graphqlDGS/AlbumsDataFetcher.java | 28 ++++++ .../com/bealdung/graphqlDGS/Application.java | 13 +++ .../src/main/resources/application.properties | 1 + .../src/main/resources/schema/schema.graphqls | 9 ++ pom.xml | 1 + 7 files changed, 168 insertions(+) create mode 100644 graphql/graphql-dgs/pom.xml create mode 100644 graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/Album.java create mode 100644 graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/AlbumsDataFetcher.java create mode 100644 graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/Application.java create mode 100644 graphql/graphql-dgs/src/main/resources/application.properties create mode 100644 graphql/graphql-dgs/src/main/resources/schema/schema.graphqls diff --git a/graphql/graphql-dgs/pom.xml b/graphql/graphql-dgs/pom.xml new file mode 100644 index 0000000000..1dc3630276 --- /dev/null +++ b/graphql/graphql-dgs/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + com.baeldung.graphql-dgs + graphql-dgs + 1.0 + graphql-dgs + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../.. + + + + + + com.netflix.graphql.dgs + graphql-dgs-platform-dependencies + 4.1.0 + pom + import + + + + + + + org.springframework.boot + spring-boot-starter + 2.6.2 + + + + org.springframework.boot + spring-boot-starter-test + 2.6.2 + test + + + + com.netflix.graphql.dgs.codegen + graphql-dgs-codegen-client-core + 5.1.14 + + + + org.springframework.boot + spring-boot-starter-web + 2.6.2 + + + + com.netflix.graphql.dgs + graphql-dgs-spring-boot-starter + 4.9.15 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 2.6.2 + + + io.github.deweyjose + graphqlcodegen-maven-plugin + 1.14 + + + + generate + + + + + + src/main/resources/schema/schema.graphqls + + com.bealdung.graphqldgs.generated + + + + + + diff --git a/graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/Album.java b/graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/Album.java new file mode 100644 index 0000000000..7e53eb08fe --- /dev/null +++ b/graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/Album.java @@ -0,0 +1,25 @@ +package com.bealdung.graphqlDGS; + +public class Album { + private final String title; + private final String artist; + private final Integer recordNo; + + public Album(String title, String artist, Integer recordNo) { + this.title = title; + this.recordNo = recordNo; + this.artist = artist; + } + + public String getTitle() { + return title; + } + + public String getArtist() { + return artist; + } + + public Integer getRecordNo() { + return recordNo; + } +} diff --git a/graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/AlbumsDataFetcher.java b/graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/AlbumsDataFetcher.java new file mode 100644 index 0000000000..8fda98b941 --- /dev/null +++ b/graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/AlbumsDataFetcher.java @@ -0,0 +1,28 @@ +package com.bealdung.graphqlDGS; + +import com.netflix.graphql.dgs.DgsComponent; +import com.netflix.graphql.dgs.DgsQuery; +import com.netflix.graphql.dgs.InputArgument; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +@DgsComponent +public class AlbumsDataFetcher { + + private final List albums = Arrays.asList( + new Album("Rumours", "Fleetwood Mac", 20), + new Album("What's Going On", "Marvin Gaye", 10), + new Album("Pet Sounds", "The Beach Boys", 12) + ); + + @DgsQuery + public List albums(@InputArgument String titleFilter) { + if(titleFilter == null) { + return albums; + } + return albums.stream() + .filter(s -> s.getTitle().contains(titleFilter)) + .collect(Collectors.toList()); + } +} diff --git a/graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/Application.java b/graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/Application.java new file mode 100644 index 0000000000..ffe8cb2a4e --- /dev/null +++ b/graphql/graphql-dgs/src/main/java/com/bealdung/graphqlDGS/Application.java @@ -0,0 +1,13 @@ +package com.bealdung.graphqlDGS; + +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/graphql/graphql-dgs/src/main/resources/application.properties b/graphql/graphql-dgs/src/main/resources/application.properties new file mode 100644 index 0000000000..8693adcbd6 --- /dev/null +++ b/graphql/graphql-dgs/src/main/resources/application.properties @@ -0,0 +1 @@ +server.servlet.context-path= diff --git a/graphql/graphql-dgs/src/main/resources/schema/schema.graphqls b/graphql/graphql-dgs/src/main/resources/schema/schema.graphqls new file mode 100644 index 0000000000..90691a7204 --- /dev/null +++ b/graphql/graphql-dgs/src/main/resources/schema/schema.graphqls @@ -0,0 +1,9 @@ +type Query { + albums(titleFilter: String): [Album] +} + +type Album { + title: String + artist: String + recordNo: Int +} diff --git a/pom.xml b/pom.xml index 6219bbc692..a5b6b31022 100644 --- a/pom.xml +++ b/pom.xml @@ -422,6 +422,7 @@ graphql/graphql-java + graphql/graphql-dgs grpc gson guava-modules From 69e04d5ac0ebe9cff0d102fe74305100975aaedc Mon Sep 17 00:00:00 2001 From: ashleyfrieze Date: Mon, 31 Jan 2022 03:49:04 +0000 Subject: [PATCH 35/63] BAEL-5354 Add improvement to cover JDK11 solution to the repeated string problem (#11756) --- .../core-java-string-algorithms-3/pom.xml | 3 ++- .../RepeatedCharacterStringUnitTest.java | 12 ++++++++++++ core-java-modules/pom.xml | 1 - pom.xml | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-string-algorithms-3/pom.xml b/core-java-modules/core-java-string-algorithms-3/pom.xml index c140fce584..dc8ad3851d 100644 --- a/core-java-modules/core-java-string-algorithms-3/pom.xml +++ b/core-java-modules/core-java-string-algorithms-3/pom.xml @@ -49,8 +49,9 @@ + 11 + 11 1.7 3.12.0 - \ No newline at end of file diff --git a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/repeatedcharstring/RepeatedCharacterStringUnitTest.java b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/repeatedcharstring/RepeatedCharacterStringUnitTest.java index 42697c781e..81e78e8fd0 100644 --- a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/repeatedcharstring/RepeatedCharacterStringUnitTest.java +++ b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/repeatedcharstring/RepeatedCharacterStringUnitTest.java @@ -19,6 +19,18 @@ class RepeatedCharacterStringUnitTest { private static final String EXPECTED_STRING = "aaaaaaa"; private static final int N = 7; + @Test + void givenSingleCharacterString_whenRepeat_thenStringCreated() { + String newString = "a".repeat(N); + assertEquals(EXPECTED_STRING, newString); + } + + @Test + void givenMultiCharacterString_whenRepeat_thenStringCreated() { + String newString = "-->".repeat(5); + assertEquals("-->-->-->-->-->", newString); + } + @Test void givenString_whenStringBuilderUsed_thenStringCreated() { StringBuilder builder = new StringBuilder(N); diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index d9da5a845b..809aa72966 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -107,7 +107,6 @@ core-java-streams-4 core-java-string-algorithms core-java-string-algorithms-2 - core-java-string-algorithms-3 core-java-string-apis core-java-string-conversions core-java-string-conversions-2 diff --git a/pom.xml b/pom.xml index a5b6b31022..f2a230357c 100644 --- a/pom.xml +++ b/pom.xml @@ -1333,6 +1333,7 @@ core-java-modules/core-java-io-conversions-2 core-java-modules/core-java-jpms core-java-modules/core-java-os + core-java-modules/core-java-string-algorithms-3 core-java-modules/core-java-string-operations-3 core-java-modules/core-java-string-operations-4 core-java-modules/core-java-time-measurements From 01ca4575ba28529baaad915528f45d014d534a96 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Mon, 31 Jan 2022 12:45:28 +0000 Subject: [PATCH 36/63] [JAVA-9604] Refactor and code clean-up for Metrics article --- .../spring-boot-actuator/README.md | 1 + .../spring-boot-actuator/pom.xml | 18 +++ .../baeldung/metrics/MetricsApplication.java | 42 ++++++ .../java/com/baeldung/metrics}/WebConfig.java | 13 +- .../metrics/controller/MetricsController.java | 41 ++++++ .../metrics/filter}/MetricFilter.java | 30 +++-- .../service}/ActuatorMetricService.java | 67 +++++----- .../service}/CustomActuatorMetricService.java | 54 ++++---- .../service/InMemoryMetricService.java | 112 ++++++++++++++++ .../metrics/service/MetricService.java | 7 + .../resources/application-metrics.properties | 9 ++ .../src/main/webapp/WEB-INF/api-servlet.xml | 0 .../webapp/WEB-INF/view/metrics}/graph.jsp | 2 +- .../webapp/WEB-INF/view/metrics}/homepage.jsp | 0 .../src/main/webapp/WEB-INF/web.xml | 4 +- .../MetricsApplicationIntegrationTest.java | 64 +++++++++ .../spring-rest-testing/README.md | 1 - .../spring-rest-testing/pom.xml | 28 ---- .../java/com/baeldung/spring/Application.java | 21 +-- .../web/controller/RootController.java | 52 -------- .../web/metric/IActuatorMetricService.java | 5 - .../metric/ICustomActuatorMetricService.java | 8 -- .../baeldung/web/metric/IMetricService.java | 14 -- .../baeldung/web/metric/MetricService.java | 122 ------------------ .../src/main/resources/application.properties | 1 - 25 files changed, 373 insertions(+), 343 deletions(-) create mode 100644 spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/MetricsApplication.java rename {spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/spring => spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics}/WebConfig.java (81%) create mode 100644 spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/controller/MetricsController.java rename {spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric => spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/filter}/MetricFilter.java (64%) rename {spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric => spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service}/ActuatorMetricService.java (61%) rename {spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric => spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service}/CustomActuatorMetricService.java (68%) create mode 100644 spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/InMemoryMetricService.java create mode 100644 spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/MetricService.java create mode 100644 spring-boot-modules/spring-boot-actuator/src/main/resources/application-metrics.properties rename {spring-web-modules/spring-rest-testing => spring-boot-modules/spring-boot-actuator}/src/main/webapp/WEB-INF/api-servlet.xml (100%) rename {spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/view => spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/view/metrics}/graph.jsp (94%) rename {spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/view => spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/view/metrics}/homepage.jsp (100%) rename {spring-web-modules/spring-rest-testing => spring-boot-modules/spring-boot-actuator}/src/main/webapp/WEB-INF/web.xml (92%) create mode 100644 spring-boot-modules/spring-boot-actuator/src/test/java/com/baeldung/metrics/MetricsApplicationIntegrationTest.java delete mode 100644 spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/controller/RootController.java delete mode 100644 spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/IActuatorMetricService.java delete mode 100644 spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/ICustomActuatorMetricService.java delete mode 100644 spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/IMetricService.java delete mode 100644 spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/MetricService.java diff --git a/spring-boot-modules/spring-boot-actuator/README.md b/spring-boot-modules/spring-boot-actuator/README.md index 59f7e929da..ea43377ed2 100644 --- a/spring-boot-modules/spring-boot-actuator/README.md +++ b/spring-boot-modules/spring-boot-actuator/README.md @@ -12,3 +12,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Health Indicators in Spring Boot](https://www.baeldung.com/spring-boot-health-indicators) - [How to Enable All Endpoints in Spring Boot Actuator](https://www.baeldung.com/spring-boot-actuator-enable-endpoints) - [Spring Boot Startup Actuator Endpoint](https://www.baeldung.com/spring-boot-actuator-startup) +- [Metrics for your Spring REST API](https://www.baeldung.com/spring-rest-api-metrics) diff --git a/spring-boot-modules/spring-boot-actuator/pom.xml b/spring-boot-modules/spring-boot-actuator/pom.xml index b2c7a4d28e..1ccf436bbf 100644 --- a/spring-boot-modules/spring-boot-actuator/pom.xml +++ b/spring-boot-modules/spring-boot-actuator/pom.xml @@ -23,6 +23,10 @@ org.springframework.boot spring-boot-starter-web + + org.apache.tomcat.embed + tomcat-embed-jasper + org.springframework.boot spring-boot-starter-data-jpa @@ -35,6 +39,16 @@ com.h2database h2 + + javax.servlet + javax.servlet-api + provided + + + javax.servlet + jstl + runtime + org.springframework.boot spring-boot-starter-test @@ -51,6 +65,10 @@ spring-security-test test + + org.awaitility + awaitility + diff --git a/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/MetricsApplication.java b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/MetricsApplication.java new file mode 100644 index 0000000000..729b3c0b96 --- /dev/null +++ b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/MetricsApplication.java @@ -0,0 +1,42 @@ +package com.baeldung.metrics; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration; +import org.springframework.boot.actuate.autoconfigure.security.servlet.SecurityRequestMatchersManagementContextConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.web.context.request.RequestContextListener; + +import javax.servlet.ServletContext; + +@EnableScheduling +@ComponentScan("com.baeldung.metrics") +@SpringBootApplication +public class MetricsApplication extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(MetricsApplication.class); + } + + @Override + public void onStartup(ServletContext sc) { + // Manages the lifecycle of the root application context + sc.addListener(new RequestContextListener()); + } + + public static void main(final String[] args) { + // only load properties for this application + System.setProperty("spring.config.location", "classpath:application-metrics.properties"); + SpringApplication.run(MetricsApplication.class, args); + } + +} \ No newline at end of file diff --git a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/spring/WebConfig.java b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/WebConfig.java similarity index 81% rename from spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/spring/WebConfig.java rename to spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/WebConfig.java index e35acb0bf0..4c38e4dbad 100644 --- a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/spring/WebConfig.java +++ b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/WebConfig.java @@ -1,4 +1,4 @@ -package com.baeldung.spring; +package com.baeldung.metrics; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -10,14 +10,10 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration -@ComponentScan("com.baeldung.web") +@ComponentScan("com.baeldung.metrics") @EnableWebMvc public class WebConfig implements WebMvcConfigurer { - public WebConfig() { - super(); - } - @Bean public ViewResolver viewResolver() { final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); @@ -26,11 +22,10 @@ public class WebConfig implements WebMvcConfigurer { return viewResolver; } - // API @Override public void addViewControllers(final ViewControllerRegistry registry) { - registry.addViewController("/graph.html"); - registry.addViewController("/homepage.html"); + registry.addViewController("/metrics/graph.html"); + registry.addViewController("/metrics/homepage.html"); } } \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/controller/MetricsController.java b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/controller/MetricsController.java new file mode 100644 index 0000000000..e52ddd70f1 --- /dev/null +++ b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/controller/MetricsController.java @@ -0,0 +1,41 @@ +package com.baeldung.metrics.controller; + +import com.baeldung.metrics.service.InMemoryMetricService; +import com.baeldung.metrics.service.MetricService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.Map; + +@Controller +@RequestMapping(value = "/metrics") +@ResponseBody +public class MetricsController { + + @Autowired + private InMemoryMetricService metricService; + + // change the qualifier to use the in-memory implementation + @Autowired + @Qualifier("customActuatorMetricService") + private MetricService graphMetricService; + + @GetMapping(value = "/metric") + public Map> getMetric() { + return metricService.getFullMetric(); + } + + @GetMapping(value = "/status-metric") + public Map getStatusMetric() { + return metricService.getStatusMetric(); + } + + @GetMapping(value = "/metric-graph-data") + public Object[][] getMetricData() { + return graphMetricService.getGraphData(); + } +} diff --git a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/MetricFilter.java b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/filter/MetricFilter.java similarity index 64% rename from spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/MetricFilter.java rename to spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/filter/MetricFilter.java index dee63b226f..0f7579f060 100644 --- a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/MetricFilter.java +++ b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/filter/MetricFilter.java @@ -1,4 +1,11 @@ -package com.baeldung.web.metric; +package com.baeldung.metrics.filter; + +import com.baeldung.metrics.service.CustomActuatorMetricService; +import com.baeldung.metrics.service.InMemoryMetricService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -9,24 +16,23 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import org.springframework.web.context.support.WebApplicationContextUtils; - @Component public class MetricFilter implements Filter { @Autowired - private IMetricService metricService; + private InMemoryMetricService metricService; @Autowired - private ICustomActuatorMetricService actMetricService; + private CustomActuatorMetricService actMetricService; @Override - public void init(final FilterConfig config) throws ServletException { + public void init(final FilterConfig config) { if (metricService == null || actMetricService == null) { - metricService = (IMetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("metricService"); - actMetricService = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean(CustomActuatorMetricService.class); + WebApplicationContext appContext = WebApplicationContextUtils + .getRequiredWebApplicationContext(config.getServletContext()); + + metricService = appContext.getBean(InMemoryMetricService.class); + actMetricService = appContext.getBean(CustomActuatorMetricService.class); } } @@ -42,8 +48,4 @@ public class MetricFilter implements Filter { actMetricService.increaseCount(status); } - @Override - public void destroy() { - - } } diff --git a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/ActuatorMetricService.java b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/ActuatorMetricService.java similarity index 61% rename from spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/ActuatorMetricService.java rename to spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/ActuatorMetricService.java index 8c26fa04a0..3eef265c02 100644 --- a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/ActuatorMetricService.java +++ b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/ActuatorMetricService.java @@ -1,32 +1,31 @@ -package com.baeldung.web.metric; +package com.baeldung.metrics.service; + +import io.micrometer.core.instrument.Counter; +import io.micrometer.core.instrument.Meter; +import io.micrometer.core.instrument.MeterRegistry; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Service; - -import io.micrometer.core.instrument.Counter; -import io.micrometer.core.instrument.Meter; -import io.micrometer.core.instrument.MeterRegistry; - @Service -public class ActuatorMetricService implements IActuatorMetricService { +public class ActuatorMetricService implements MetricService { + + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm"); @Autowired private MeterRegistry publicMetrics; - private final List> statusMetricsByMinute; + private final List> statusMetricsByMinute; private final List statusList; - private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public ActuatorMetricService() { - super(); - statusMetricsByMinute = new ArrayList>(); - statusList = new ArrayList(); + statusMetricsByMinute = new ArrayList<>(); + statusList = new ArrayList<>(); } @Override @@ -36,19 +35,19 @@ public class ActuatorMetricService implements IActuatorMetricService { final int rowCount = statusMetricsByMinute.size() + 1; final Object[][] result = new Object[rowCount][colCount]; result[0][0] = "Time"; - int j = 1; + int j = 1; for (final String status : statusList) { result[0][j] = status; j++; } for (int i = 1; i < rowCount; i++) { - result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i)))); + result[i][0] = DATE_FORMAT.format(new Date(current.getTime() - (60000L * (rowCount - i)))); } List minuteOfStatuses; - List last = new ArrayList(); + List last = new ArrayList<>(); for (int i = 1; i < rowCount; i++) { minuteOfStatuses = statusMetricsByMinute.get(i - 1); @@ -64,11 +63,9 @@ public class ActuatorMetricService implements IActuatorMetricService { return result; } - // Non - API - - @Scheduled(fixedDelay = 60000) + @Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds:60000}") private void exportMetrics() { - final ArrayList lastMinuteStatuses = initializeStatuses(statusList.size()); + final List lastMinuteStatuses = initializeStatuses(statusList.size()); for (final Meter counterMetric : publicMetrics.getMeters()) { updateMetrics(counterMetric, lastMinuteStatuses); @@ -77,34 +74,32 @@ public class ActuatorMetricService implements IActuatorMetricService { statusMetricsByMinute.add(lastMinuteStatuses); } - private ArrayList initializeStatuses(final int size) { - final ArrayList counterList = new ArrayList(); + private List initializeStatuses(int size) { + List counterList = new ArrayList<>(); for (int i = 0; i < size; i++) { counterList.add(0); } return counterList; } - private void updateMetrics(final Meter counterMetric, final ArrayList statusCount) { - String status = ""; - int index = -1; - int oldCount = 0; + private void updateMetrics(Meter counterMetric, List statusCount) { - if (counterMetric.getId().getName().contains("counter.status.")) { - status = counterMetric.getId().getName().substring(15, 18); // example 404, 200 + String metricName = counterMetric.getId().getName(); + + if (metricName.contains("counter.status.")) { + // example 404, 200 + String status = metricName.substring(15, 18); appendStatusIfNotExist(status, statusCount); - index = statusList.indexOf(status); - oldCount = statusCount.get(index) == null ? 0 : statusCount.get(index); + int index = statusList.indexOf(status); + int oldCount = statusCount.get(index) == null ? 0 : statusCount.get(index); statusCount.set(index, (int)((Counter) counterMetric).count() + oldCount); } } - private void appendStatusIfNotExist(final String status, final ArrayList statusCount) { + private void appendStatusIfNotExist(String status, List statusCount) { if (!statusList.contains(status)) { statusList.add(status); statusCount.add(0); } } - - // -} \ No newline at end of file +} diff --git a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/CustomActuatorMetricService.java b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/CustomActuatorMetricService.java similarity index 68% rename from spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/CustomActuatorMetricService.java rename to spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/CustomActuatorMetricService.java index ee17825b7c..9b4ccaa875 100644 --- a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/CustomActuatorMetricService.java +++ b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/CustomActuatorMetricService.java @@ -1,40 +1,36 @@ -package com.baeldung.web.metric; +package com.baeldung.metrics.service; + +import io.micrometer.core.instrument.Counter; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.search.Search; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Service; - -import io.micrometer.core.instrument.Counter; -import io.micrometer.core.instrument.MeterRegistry; -import io.micrometer.core.instrument.search.Search; - @Service -public class CustomActuatorMetricService implements ICustomActuatorMetricService { +public class CustomActuatorMetricService implements MetricService { + + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm"); @Autowired private MeterRegistry registry; - private final List> statusMetricsByMinute; + private final List> statusMetricsByMinute; private final List statusList; - private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public CustomActuatorMetricService() { - super(); - statusMetricsByMinute = new ArrayList>(); - statusList = new ArrayList(); + statusMetricsByMinute = new ArrayList<>(); + statusList = new ArrayList<>(); } - // API - - @Override - public void increaseCount(final int status) { + public void increaseCount(int status) { String counterName = "counter.status." + status; - registry.counter(counterName).increment(1); + registry.counter(counterName).increment(); if (!statusList.contains(counterName)) { statusList.add(counterName); } @@ -55,7 +51,7 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService } for (int i = 1; i < rowCount; i++) { - result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i)))); + result[i][0] = DATE_FORMAT.format(new Date(current.getTime() - (60000L * (rowCount - i)))); } List minuteOfStatuses; @@ -72,19 +68,17 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService return result; } - // Non - API - - @Scheduled(fixedDelay = 60000) + @Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds:60000}") private void exportMetrics() { - final ArrayList statusCount = new ArrayList(); + List statusCount = new ArrayList<>(); for (final String status : statusList) { Search search = registry.find(status); - if (search != null) { - Counter counter = search.counter(); - statusCount.add(counter != null ? ((int) counter.count()) : 0); - registry.remove(counter); - } else { + Counter counter = search.counter(); + if (counter == null) { statusCount.add(0); + } else { + statusCount.add((int) counter.count()); + registry.remove(counter); } } statusMetricsByMinute.add(statusCount); diff --git a/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/InMemoryMetricService.java b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/InMemoryMetricService.java new file mode 100644 index 0000000000..0be5c21727 --- /dev/null +++ b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/InMemoryMetricService.java @@ -0,0 +1,112 @@ +package com.baeldung.metrics.service; + +import org.springframework.stereotype.Service; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +@Service +public class InMemoryMetricService implements MetricService { + + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + + private final Map> metricMap; + private final Map statusMetric; + private final Map> timeMap; + + public InMemoryMetricService() { + metricMap = new ConcurrentHashMap<>(); + statusMetric = new ConcurrentHashMap<>(); + timeMap = new ConcurrentHashMap<>(); + } + + public void increaseCount(String request, int status) { + increaseMainMetric(request, status); + increaseStatusMetric(status); + updateTimeMap(status); + } + + public Map> getFullMetric() { + return metricMap; + } + + public Map getStatusMetric() { + return statusMetric; + } + + public Object[][] getGraphData() { + final int colCount = statusMetric.keySet().size() + 1; + final Set allStatus = statusMetric.keySet(); + final int rowCount = timeMap.keySet().size() + 1; + + final Object[][] result = new Object[rowCount][colCount]; + result[0][0] = "Time"; + + int j = 1; + for (final int status : allStatus) { + result[0][j] = status; + j++; + } + int i = 1; + Map tempMap; + for (final Entry> entry : timeMap.entrySet()) { + result[i][0] = entry.getKey(); + tempMap = entry.getValue(); + for (j = 1; j < colCount; j++) { + result[i][j] = tempMap.get((Integer) result[0][j]); + if (result[i][j] == null) { + result[i][j] = 0; + } + } + i++; + } + + for (int k = 1; k < result[0].length; k++) { + result[0][k] = result[0][k].toString(); + } + + return result; + } + + private void increaseMainMetric(String request, int status) { + Map statusMap = metricMap.get(request); + if (statusMap == null) { + statusMap = new ConcurrentHashMap<>(); + } + + Integer count = statusMap.get(status); + if (count == null) { + count = 1; + } else { + count++; + } + statusMap.put(status, count); + metricMap.put(request, statusMap); + } + + private void increaseStatusMetric(int status) { + statusMetric.merge(status, 1, Integer::sum); + } + + private void updateTimeMap(int status) { + final String time = DATE_FORMAT.format(new Date()); + Map statusMap = timeMap.get(time); + if (statusMap == null) { + statusMap = new ConcurrentHashMap<>(); + } + + Integer count = statusMap.get(status); + if (count == null) { + count = 1; + } else { + count++; + } + statusMap.put(status, count); + timeMap.put(time, statusMap); + } + +} diff --git a/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/MetricService.java b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/MetricService.java new file mode 100644 index 0000000000..3642102b67 --- /dev/null +++ b/spring-boot-modules/spring-boot-actuator/src/main/java/com/baeldung/metrics/service/MetricService.java @@ -0,0 +1,7 @@ +package com.baeldung.metrics.service; + +public interface MetricService { + + Object[][] getGraphData(); + +} diff --git a/spring-boot-modules/spring-boot-actuator/src/main/resources/application-metrics.properties b/spring-boot-modules/spring-boot-actuator/src/main/resources/application-metrics.properties new file mode 100644 index 0000000000..5f753a0c62 --- /dev/null +++ b/spring-boot-modules/spring-boot-actuator/src/main/resources/application-metrics.properties @@ -0,0 +1,9 @@ +management.endpoints.web.exposure.include=info,health,metrics + +# JPA and Security is not required for Metrics application +spring.autoconfigure.exclude= org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, \ + org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, \ + org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, \ + org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration, \ + org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration, \ + org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration diff --git a/spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/api-servlet.xml b/spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/api-servlet.xml similarity index 100% rename from spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/api-servlet.xml rename to spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/api-servlet.xml diff --git a/spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/view/graph.jsp b/spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/view/metrics/graph.jsp similarity index 94% rename from spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/view/graph.jsp rename to spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/view/metrics/graph.jsp index e1d5fdc987..75976557a0 100644 --- a/spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/view/graph.jsp +++ b/spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/view/metrics/graph.jsp @@ -11,7 +11,7 @@ }); function drawChart() { - $.get("", + $.get("", function(mydata) { var data = google.visualization.arrayToDataTable(mydata); diff --git a/spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/view/homepage.jsp b/spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/view/metrics/homepage.jsp similarity index 100% rename from spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/view/homepage.jsp rename to spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/view/metrics/homepage.jsp diff --git a/spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/web.xml b/spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/web.xml similarity index 92% rename from spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/web.xml rename to spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/web.xml index 7129b6b4af..84566ba93d 100644 --- a/spring-web-modules/spring-rest-testing/src/main/webapp/WEB-INF/web.xml +++ b/spring-boot-modules/spring-boot-actuator/src/main/webapp/WEB-INF/web.xml @@ -16,7 +16,7 @@ contextConfigLocation - com.baeldung.spring + com.baeldung.metrics @@ -37,7 +37,7 @@ metricFilter - com.baeldung.web.metric.MetricFilter + com.baeldung.metrics.filter.MetricFilter diff --git a/spring-boot-modules/spring-boot-actuator/src/test/java/com/baeldung/metrics/MetricsApplicationIntegrationTest.java b/spring-boot-modules/spring-boot-actuator/src/test/java/com/baeldung/metrics/MetricsApplicationIntegrationTest.java new file mode 100644 index 0000000000..c83d4625dc --- /dev/null +++ b/spring-boot-modules/spring-boot-actuator/src/test/java/com/baeldung/metrics/MetricsApplicationIntegrationTest.java @@ -0,0 +1,64 @@ +package com.baeldung.metrics; + +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.web.client.TestRestTemplate; +import org.springframework.test.context.ActiveProfiles; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@SpringBootTest( + classes = MetricsApplication.class, + webEnvironment = RANDOM_PORT, + properties = {"fixedDelay.in.milliseconds=2000"} +) +@ActiveProfiles("metrics") +class MetricsApplicationIntegrationTest { + + @Autowired + private TestRestTemplate restTemplate; + + @Test + void givenStatuses_WhenScheduledMethodExecuted_ExpectCountsAreAggregated() { + restTemplate.getForObject("/metrics/metric/notFound", String.class); + restTemplate.getForObject("/metrics/metric", String.class); + + await().untilAsserted(() -> { + Object[][] statusCounts = restTemplate.getForObject("/metrics/metric-graph-data", Object[][].class); + + assertThat(statusCounts[0]).contains("counter.status.200", "counter.status.404"); + + List requestCounts = getRequestCounts(statusCounts); + verify404RequestFrom(requestCounts); + verify200RequestsFrom(requestCounts); + }); + } + + private static void verify200RequestsFrom(List requestCounts) { + assertThat(requestCounts.size()).isGreaterThan(1); + } + + private static void verify404RequestFrom(List requestCounts) { + assertThat(requestCounts).contains(1); + } + + private static List getRequestCounts(Object[][] statusCounts) { + List requestCounts = new ArrayList<>(); + for (int i = 1; i < statusCounts.length; i++) { + for (int j = 1; j < statusCounts[i].length; j++) { + Integer count = (Integer) statusCounts[i][j]; + if (count >= 1) { + requestCounts.add(count); + } + } + } + return requestCounts; + } + +} diff --git a/spring-web-modules/spring-rest-testing/README.md b/spring-web-modules/spring-rest-testing/README.md index 2a77b5dded..806e67b7ec 100644 --- a/spring-web-modules/spring-rest-testing/README.md +++ b/spring-web-modules/spring-rest-testing/README.md @@ -11,7 +11,6 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com ### Relevant Articles: - [Integration Testing with the Maven Cargo plugin](https://www.baeldung.com/integration-testing-with-the-maven-cargo-plugin) -- [Metrics for your Spring REST API](https://www.baeldung.com/spring-rest-api-metrics) - [Testing Exceptions with Spring MockMvc](https://www.baeldung.com/spring-mvc-test-exceptions) ### Build the Project diff --git a/spring-web-modules/spring-rest-testing/pom.xml b/spring-web-modules/spring-rest-testing/pom.xml index 52c1f6418e..1390898bf9 100644 --- a/spring-web-modules/spring-rest-testing/pom.xml +++ b/spring-web-modules/spring-rest-testing/pom.xml @@ -21,19 +21,10 @@ org.springframework.boot spring-boot-starter-web - - org.springframework.boot - spring-boot-starter-actuator - org.aspectj aspectjweaver - - org.apache.tomcat.embed - tomcat-embed-jasper - provided - org.springframework @@ -69,14 +60,6 @@ org.springframework spring-expression - - org.springframework - spring-web - - - org.springframework - spring-webmvc - org.springframework.data spring-data-commons @@ -137,17 +120,6 @@ net.bytebuddy byte-buddy - - - javax.servlet - javax.servlet-api - provided - - - javax.servlet - jstl - runtime - com.fasterxml.jackson.core diff --git a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/spring/Application.java b/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/spring/Application.java index c35c4d7e5e..78c9b88ddb 100644 --- a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/spring/Application.java +++ b/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/spring/Application.java @@ -1,38 +1,19 @@ package com.baeldung.spring; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; - import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.web.context.request.RequestContextListener; /** * Main Application Class - uses Spring Boot. Just run this as a normal Java * class to run up a Jetty Server (on http://localhost:8082/spring-rest-full) * */ -@EnableScheduling @EnableAutoConfiguration @ComponentScan("com.baeldung") @SpringBootApplication -public class Application extends SpringBootServletInitializer { - - @Override - protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { - return application.sources(Application.class); - } - - @Override - public void onStartup(ServletContext sc) throws ServletException { - // Manages the lifecycle of the root application context - sc.addListener(new RequestContextListener()); - } +public class Application { public static void main(final String[] args) { SpringApplication.run(Application.class, args); diff --git a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/controller/RootController.java b/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/controller/RootController.java deleted file mode 100644 index 005f6f023b..0000000000 --- a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/controller/RootController.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.baeldung.web.controller; - -import java.util.Map; - -import com.baeldung.web.metric.IActuatorMetricService; -import com.baeldung.web.metric.IMetricService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; - -@Controller -@RequestMapping(value = "/auth/") -public class RootController { - - @Autowired - private IMetricService metricService; - - @Autowired - private IActuatorMetricService actMetricService; - - public RootController() { - super(); - } - - // API - - @RequestMapping(value = "/metric", method = RequestMethod.GET) - @ResponseBody - public Map getMetric() { - return metricService.getFullMetric(); - } - - @RequestMapping(value = "/status-metric", method = RequestMethod.GET) - @ResponseBody - public Map getStatusMetric() { - return metricService.getStatusMetric(); - } - - @RequestMapping(value = "/metric-graph", method = RequestMethod.GET) - @ResponseBody - public Object[][] drawMetric() { - final Object[][] result = metricService.getGraphData(); - for (int i = 1; i < result[0].length; i++) { - result[0][i] = result[0][i].toString(); - } - return result; - } - - -} diff --git a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/IActuatorMetricService.java b/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/IActuatorMetricService.java deleted file mode 100644 index 60bb43ee00..0000000000 --- a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/IActuatorMetricService.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.baeldung.web.metric; - -public interface IActuatorMetricService { - Object[][] getGraphData(); -} diff --git a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/ICustomActuatorMetricService.java b/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/ICustomActuatorMetricService.java deleted file mode 100644 index 5126252e27..0000000000 --- a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/ICustomActuatorMetricService.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.baeldung.web.metric; - -public interface ICustomActuatorMetricService { - - void increaseCount(final int status); - - Object[][] getGraphData(); -} diff --git a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/IMetricService.java b/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/IMetricService.java deleted file mode 100644 index b8dfa60215..0000000000 --- a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/IMetricService.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.web.metric; - -import java.util.Map; - -public interface IMetricService { - - void increaseCount(final String request, final int status); - - Map getFullMetric(); - - Map getStatusMetric(); - - Object[][] getGraphData(); -} diff --git a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/MetricService.java b/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/MetricService.java deleted file mode 100644 index fd3cccab3e..0000000000 --- a/spring-web-modules/spring-rest-testing/src/main/java/com/baeldung/web/metric/MetricService.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.baeldung.web.metric; - -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -import org.springframework.stereotype.Service; - -@Service -public class MetricService implements IMetricService { - - private ConcurrentMap> metricMap; - private ConcurrentMap statusMetric; - private ConcurrentMap> timeMap; - private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); - - public MetricService() { - super(); - metricMap = new ConcurrentHashMap>(); - statusMetric = new ConcurrentHashMap(); - timeMap = new ConcurrentHashMap>(); - } - - // API - - @Override - public void increaseCount(final String request, final int status) { - increaseMainMetric(request, status); - increaseStatusMetric(status); - updateTimeMap(status); - } - - @Override - public Map getFullMetric() { - return metricMap; - } - - @Override - public Map getStatusMetric() { - return statusMetric; - } - - @Override - public Object[][] getGraphData() { - final int colCount = statusMetric.keySet().size() + 1; - final Set allStatus = statusMetric.keySet(); - final int rowCount = timeMap.keySet().size() + 1; - - final Object[][] result = new Object[rowCount][colCount]; - result[0][0] = "Time"; - - int j = 1; - for (final int status : allStatus) { - result[0][j] = status; - j++; - } - int i = 1; - ConcurrentMap tempMap; - for (final Entry> entry : timeMap.entrySet()) { - result[i][0] = entry.getKey(); - tempMap = entry.getValue(); - for (j = 1; j < colCount; j++) { - result[i][j] = tempMap.get(result[0][j]); - if (result[i][j] == null) { - result[i][j] = 0; - } - } - i++; - } - - return result; - } - - // NON-API - - private void increaseMainMetric(final String request, final int status) { - ConcurrentHashMap statusMap = metricMap.get(request); - if (statusMap == null) { - statusMap = new ConcurrentHashMap(); - } - - Integer count = statusMap.get(status); - if (count == null) { - count = 1; - } else { - count++; - } - statusMap.put(status, count); - metricMap.put(request, statusMap); - } - - private void increaseStatusMetric(final int status) { - final Integer statusCount = statusMetric.get(status); - if (statusCount == null) { - statusMetric.put(status, 1); - } else { - statusMetric.put(status, statusCount + 1); - } - } - - private void updateTimeMap(final int status) { - final String time = dateFormat.format(new Date()); - ConcurrentHashMap statusMap = timeMap.get(time); - if (statusMap == null) { - statusMap = new ConcurrentHashMap(); - } - - Integer count = statusMap.get(status); - if (count == null) { - count = 1; - } else { - count++; - } - statusMap.put(status, count); - timeMap.put(time, statusMap); - } - -} diff --git a/spring-web-modules/spring-rest-testing/src/main/resources/application.properties b/spring-web-modules/spring-rest-testing/src/main/resources/application.properties index 52d93b4cff..cb9d72e41f 100644 --- a/spring-web-modules/spring-rest-testing/src/main/resources/application.properties +++ b/spring-web-modules/spring-rest-testing/src/main/resources/application.properties @@ -1,3 +1,2 @@ server.port=8082 server.servlet.context-path=/spring-rest-full -endpoints.metrics.enabled=true \ No newline at end of file From ef127cbdfbf77969468df8a597df3a27f285fa79 Mon Sep 17 00:00:00 2001 From: freelansam <79205526+freelansam@users.noreply.github.com> Date: Mon, 31 Jan 2022 21:48:39 +0530 Subject: [PATCH 37/63] JAVA-9566: Update apache-poi version and articles (#11748) * JAVA-9566: Update apache-poi version and articles * JAVA-9566: Update apache-poi version and articles --- apache-poi/pom.xml | 2 +- .../baeldung/poi/excel/ExcelPOIHelper.java | 27 +++++++++---------- .../com/baeldung/poi/excel/ExcelUtility.java | 1 + .../poi/excel/cellstyle/CellStyler.java | 1 - .../jexcel/JExcelIntegrationTest.java | 27 +++++++------------ .../poi/excel/ExcelIntegrationTest.java | 21 +++++---------- .../cellstyle/CellStyleHandlerUnitTest.java | 16 ++++++----- 7 files changed, 41 insertions(+), 54 deletions(-) diff --git a/apache-poi/pom.xml b/apache-poi/pom.xml index 5471c77961..1a0f77b025 100644 --- a/apache-poi/pom.xml +++ b/apache-poi/pom.xml @@ -33,7 +33,7 @@ - 4.1.1 + 5.2.0 1.0.6 diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelPOIHelper.java b/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelPOIHelper.java index b6b0cbef20..6c2a8c68f7 100644 --- a/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelPOIHelper.java +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelPOIHelper.java @@ -1,25 +1,24 @@ package com.baeldung.poi.excel; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFFont; -import org.apache.poi.ss.usermodel.DateUtil; -import org.apache.poi.ss.usermodel.FillPatternType; -import java.io.File; -import java.io.FileOutputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Map; -import java.util.HashMap; -import java.util.ArrayList; -import java.util.List; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelPOIHelper { @@ -33,7 +32,7 @@ public class ExcelPOIHelper { for (Row row : sheet) { data.put(i, new ArrayList()); for (Cell cell : row) { - switch (cell.getCellTypeEnum()) { + switch (cell.getCellType()) { case STRING: data.get(i) .add(cell.getRichStringCellValue() diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelUtility.java b/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelUtility.java index 50bbfbbe3c..f22b0f094f 100644 --- a/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelUtility.java +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelUtility.java @@ -44,6 +44,7 @@ public class ExcelUtility { } } inputStream.close(); + baeuldungWorkBook.close(); } catch (IOException e) { throw e; diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyler.java b/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyler.java index 6d8b303fd3..0f8f8b6cd9 100644 --- a/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyler.java +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyler.java @@ -1,7 +1,6 @@ package com.baeldung.poi.excel.cellstyle; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined; -import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.HorizontalAlignment; diff --git a/apache-poi/src/test/java/com/baeldung/jexcel/JExcelIntegrationTest.java b/apache-poi/src/test/java/com/baeldung/jexcel/JExcelIntegrationTest.java index 41efd9d9ba..abf3d3d50c 100644 --- a/apache-poi/src/test/java/com/baeldung/jexcel/JExcelIntegrationTest.java +++ b/apache-poi/src/test/java/com/baeldung/jexcel/JExcelIntegrationTest.java @@ -1,25 +1,18 @@ package com.baeldung.jexcel; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import jxl.read.biff.BiffException; -import java.util.Map; -import java.util.ArrayList; -import java.util.List; - -import com.baeldung.jexcel.JExcelHelper; - -import jxl.write.WriteException; -import jxl.read.biff.BiffException; - import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import org.junit.Test; -import org.junit.Before; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; + import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import jxl.read.biff.BiffException; +import jxl.write.WriteException; public class JExcelIntegrationTest { diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelIntegrationTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelIntegrationTest.java index 5d7ccb9b94..d2fa08e9ea 100644 --- a/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelIntegrationTest.java +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelIntegrationTest.java @@ -1,22 +1,15 @@ package com.baeldung.poi.excel; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import jxl.read.biff.BiffException; -import java.util.Map; -import java.util.ArrayList; -import java.util.List; - -import com.baeldung.poi.excel.ExcelPOIHelper; - import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import org.junit.Test; -import org.junit.Before; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; + import org.junit.After; +import org.junit.Before; +import org.junit.Test; public class ExcelIntegrationTest { diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStyleHandlerUnitTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStyleHandlerUnitTest.java index e131db8e56..1aa6445511 100644 --- a/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStyleHandlerUnitTest.java +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStyleHandlerUnitTest.java @@ -1,17 +1,19 @@ package com.baeldung.poi.excel.cellstyle; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.Before; -import org.junit.Test; +import static org.junit.Assert.assertEquals; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.net.URISyntaxException; import java.nio.file.Paths; -import static org.junit.Assert.assertEquals; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Before; +import org.junit.Test; public class CellStyleHandlerUnitTest { private static final String FILE_NAME = "cellstyle/CellStyleHandlerTest.xlsx"; From a7980094d19e9e90f1c8e948ce42d792b27d3592 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Tue, 1 Feb 2022 14:08:56 +0000 Subject: [PATCH 38/63] [JAVA-9021] Clean up uncommitted artifacts (#11754) --- apache-poi/.gitignore | 2 + .../core-java-lang-math-2/.gitignore | 4 +- .../ExternalizableUnitTest.java | 31 +- .../serialization/PersonUnitTest.java | 34 +- .../serialization/SerializationUnitTest.java | 22 +- .../com/baeldung/jaxb/gen/ObjectFactory.java | 96 +++--- .../com/baeldung/jaxb/gen/UserRequest.java | 174 +++++----- .../com/baeldung/jaxb/gen/UserResponse.java | 298 +++++++++--------- .../com/baeldung/jaxb/gen/package-info.java | 4 +- .../java/org/w3/_2001/xmlschema/Adapter1.java | 46 +-- 10 files changed, 382 insertions(+), 329 deletions(-) diff --git a/apache-poi/.gitignore b/apache-poi/.gitignore index 9552c1e63d..ac62b36227 100644 --- a/apache-poi/.gitignore +++ b/apache-poi/.gitignore @@ -1,3 +1,5 @@ *.docx temp.xls temp.xlsx + +CellStyleTest_output.xlsx \ No newline at end of file diff --git a/core-java-modules/core-java-lang-math-2/.gitignore b/core-java-modules/core-java-lang-math-2/.gitignore index 30b2b7442c..e4c6fe9355 100644 --- a/core-java-modules/core-java-lang-math-2/.gitignore +++ b/core-java-modules/core-java-lang-math-2/.gitignore @@ -1,4 +1,6 @@ /target/ .settings/ .classpath -.project \ No newline at end of file +.project + +*.log diff --git a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/externalizable/ExternalizableUnitTest.java b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/externalizable/ExternalizableUnitTest.java index 651364fb13..9012928d92 100644 --- a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/externalizable/ExternalizableUnitTest.java +++ b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/externalizable/ExternalizableUnitTest.java @@ -1,14 +1,29 @@ package com.baeldung.externalizable; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import java.io.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; public class ExternalizableUnitTest { - private final static String OUTPUT_FILE = "externalizable.txt"; + private final static String OUTPUT_FILE_NAME = "externalizable.txt"; + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + private File outputFile; + + @Before + public void setUp() throws Exception { + outputFile = tempFolder.newFile(OUTPUT_FILE_NAME); + } @Test public void whenSerializing_thenUseExternalizable() throws IOException, ClassNotFoundException { @@ -18,7 +33,7 @@ public class ExternalizableUnitTest { c.setCode(374); c.setName("Armenia"); - FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE); + FileOutputStream fileOutputStream = new FileOutputStream(outputFile); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); c.writeExternal(objectOutputStream); @@ -26,7 +41,7 @@ public class ExternalizableUnitTest { objectOutputStream.close(); fileOutputStream.close(); - FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE); + FileInputStream fileInputStream = new FileInputStream(outputFile); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Country c2 = new Country(); @@ -35,8 +50,8 @@ public class ExternalizableUnitTest { objectInputStream.close(); fileInputStream.close(); - assertTrue(c2.getCode() == c.getCode()); - assertTrue(c2.getName().equals(c.getName())); + assertEquals(c2.getCode(), c.getCode()); + assertEquals(c2.getName(), c.getName()); } @Test @@ -49,7 +64,7 @@ public class ExternalizableUnitTest { r.setClimate("Mediterranean"); r.setPopulation(120.000); - FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE); + FileOutputStream fileOutputStream = new FileOutputStream(outputFile); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); r.writeExternal(objectOutputStream); @@ -57,7 +72,7 @@ public class ExternalizableUnitTest { objectOutputStream.close(); fileOutputStream.close(); - FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE); + FileInputStream fileInputStream = new FileInputStream(outputFile); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Region r2 = new Region(); @@ -66,6 +81,6 @@ public class ExternalizableUnitTest { objectInputStream.close(); fileInputStream.close(); - assertTrue(r2.getPopulation() == null); + assertNull(r2.getPopulation()); } } diff --git a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/PersonUnitTest.java b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/PersonUnitTest.java index dbcdf0ec9d..47dc0b9293 100644 --- a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/PersonUnitTest.java +++ b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/PersonUnitTest.java @@ -1,36 +1,54 @@ package com.baeldung.serialization; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class PersonUnitTest { + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + private File outputFile; + + private File outputFile2; + + @Before + public void setUp() throws Exception { + outputFile = tempFolder.newFile("yourfile.txt"); + outputFile2 = tempFolder.newFile("yourfile2.txt"); + } + @Test public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException { Person p = new Person(); p.setAge(20); p.setName("Joe"); - FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt"); + FileOutputStream fileOutputStream = new FileOutputStream(outputFile); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(p); objectOutputStream.flush(); objectOutputStream.close(); - FileInputStream fileInputStream = new FileInputStream("yofile.txt"); + FileInputStream fileInputStream = new FileInputStream(outputFile); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Person p2 = (Person) objectInputStream.readObject(); objectInputStream.close(); - assertTrue(p2.getAge() == p.getAge()); - assertTrue(p2.getName().equals(p.getName())); + assertEquals(p2.getAge(), p.getAge()); + assertEquals(p2.getName(), p.getName()); } @Test @@ -46,19 +64,19 @@ public class PersonUnitTest { e.setPerson(p); e.setAddress(a); - FileOutputStream fileOutputStream = new FileOutputStream("yofile2.txt"); + FileOutputStream fileOutputStream = new FileOutputStream(outputFile2); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(e); objectOutputStream.flush(); objectOutputStream.close(); - FileInputStream fileInputStream = new FileInputStream("yofile2.txt"); + FileInputStream fileInputStream = new FileInputStream(outputFile2); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Employee e2 = (Employee) objectInputStream.readObject(); objectInputStream.close(); - assertTrue(e2.getPerson().getAge() == e.getPerson().getAge()); - assertTrue(e2.getAddress().getHouseNumber() == (e.getAddress().getHouseNumber())); + assertEquals(e2.getPerson().getAge(), e.getPerson().getAge()); + assertEquals(e2.getAddress().getHouseNumber(), (e.getAddress().getHouseNumber())); } } diff --git a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/SerializationUnitTest.java b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/SerializationUnitTest.java index dde9beb2bb..a7cbc0599b 100644 --- a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/SerializationUnitTest.java +++ b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/SerializationUnitTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; @@ -13,17 +14,32 @@ import java.io.ObjectOutputStream; import java.io.Serializable; import org.apache.commons.lang3.SerializationUtils; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import com.baeldung.util.MySerializationUtils; +import org.junit.rules.TemporaryFolder; public class SerializationUnitTest { + private final static String OUTPUT_FILE_NAME = "yourfile.txt"; + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + private File outputFile; + + @Before + public void setUp() throws Exception { + outputFile = tempFolder.newFile(OUTPUT_FILE_NAME); + } + @Test(expected = NotSerializableException.class) public void whenSerializing_ThenThrowsError() throws IOException { Address address = new Address(); address.setHouseNumber(10); - FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt"); + FileOutputStream fileOutputStream = new FileOutputStream(outputFile); try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) { objectOutputStream.writeObject(address); } @@ -35,12 +51,12 @@ public class SerializationUnitTest { p.setAge(20); p.setName("Joe"); - FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt"); + FileOutputStream fileOutputStream = new FileOutputStream(outputFile); try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) { objectOutputStream.writeObject(p); } - FileInputStream fileInputStream = new FileInputStream("yofile.txt"); + FileInputStream fileInputStream = new FileInputStream(outputFile); try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) { Person p2 = (Person) objectInputStream.readObject(); assertEquals(p2.getAge(), p.getAge()); diff --git a/jaxb/src/main/java/com/baeldung/jaxb/gen/ObjectFactory.java b/jaxb/src/main/java/com/baeldung/jaxb/gen/ObjectFactory.java index 26cd5814ac..0a3da677ce 100644 --- a/jaxb/src/main/java/com/baeldung/jaxb/gen/ObjectFactory.java +++ b/jaxb/src/main/java/com/baeldung/jaxb/gen/ObjectFactory.java @@ -1,48 +1,48 @@ - -package com.baeldung.jaxb.gen; - -import javax.xml.bind.annotation.XmlRegistry; - - -/** - * This object contains factory methods for each - * Java content interface and Java element interface - * generated in the com.baeldung.jaxb.gen package. - *

An ObjectFactory allows you to programatically - * construct new instances of the Java representation - * for XML content. The Java representation of XML - * content can consist of schema derived interfaces - * and classes representing the binding of schema - * type definitions, element declarations and model - * groups. Factory methods for each of these are - * provided in this class. - * - */ -@XmlRegistry -public class ObjectFactory { - - - /** - * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.baeldung.jaxb.gen - * - */ - public ObjectFactory() { - } - - /** - * Create an instance of {@link UserRequest } - * - */ - public UserRequest createUserRequest() { - return new UserRequest(); - } - - /** - * Create an instance of {@link UserResponse } - * - */ - public UserResponse createUserResponse() { - return new UserResponse(); - } - -} + +package com.baeldung.jaxb.gen; + +import javax.xml.bind.annotation.XmlRegistry; + + +/** + * This object contains factory methods for each + * Java content interface and Java element interface + * generated in the com.baeldung.jaxb.gen package. + *

An ObjectFactory allows you to programatically + * construct new instances of the Java representation + * for XML content. The Java representation of XML + * content can consist of schema derived interfaces + * and classes representing the binding of schema + * type definitions, element declarations and model + * groups. Factory methods for each of these are + * provided in this class. + * + */ +@XmlRegistry +public class ObjectFactory { + + + /** + * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.baeldung.jaxb.gen + * + */ + public ObjectFactory() { + } + + /** + * Create an instance of {@link UserRequest } + * + */ + public UserRequest createUserRequest() { + return new UserRequest(); + } + + /** + * Create an instance of {@link UserResponse } + * + */ + public UserResponse createUserResponse() { + return new UserResponse(); + } + +} diff --git a/jaxb/src/main/java/com/baeldung/jaxb/gen/UserRequest.java b/jaxb/src/main/java/com/baeldung/jaxb/gen/UserRequest.java index 4cfbeb8d46..1c1abc61a6 100644 --- a/jaxb/src/main/java/com/baeldung/jaxb/gen/UserRequest.java +++ b/jaxb/src/main/java/com/baeldung/jaxb/gen/UserRequest.java @@ -1,87 +1,87 @@ - -package com.baeldung.jaxb.gen; - -import java.io.Serializable; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlType; - - -/** - *

Java class for UserRequest complex type. - * - *

The following schema fragment specifies the expected content contained within this class. - * - *

- * <complexType name="UserRequest">
- *   <complexContent>
- *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
- *       <sequence>
- *         <element name="id" type="{http://www.w3.org/2001/XMLSchema}int"/>
- *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
- *       </sequence>
- *     </restriction>
- *   </complexContent>
- * </complexType>
- * 
- * - * - */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "UserRequest", propOrder = { - "id", - "name" -}) -@XmlRootElement(name = "userRequest") -public class UserRequest - implements Serializable -{ - - private final static long serialVersionUID = -1L; - protected int id; - @XmlElement(required = true) - protected String name; - - /** - * Gets the value of the id property. - * - */ - public int getId() { - return id; - } - - /** - * Sets the value of the id property. - * - */ - public void setId(int value) { - this.id = value; - } - - /** - * Gets the value of the name property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getName() { - return name; - } - - /** - * Sets the value of the name property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setName(String value) { - this.name = value; - } - -} + +package com.baeldung.jaxb.gen; + +import java.io.Serializable; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + + +/** + *

Java class for UserRequest complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+ * <complexType name="UserRequest">
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <sequence>
+ *         <element name="id" type="{http://www.w3.org/2001/XMLSchema}int"/>
+ *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *       </sequence>
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "UserRequest", propOrder = { + "id", + "name" +}) +@XmlRootElement(name = "userRequest") +public class UserRequest + implements Serializable +{ + + private final static long serialVersionUID = -1L; + protected int id; + @XmlElement(required = true) + protected String name; + + /** + * Gets the value of the id property. + * + */ + public int getId() { + return id; + } + + /** + * Sets the value of the id property. + * + */ + public void setId(int value) { + this.id = value; + } + + /** + * Gets the value of the name property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getName() { + return name; + } + + /** + * Sets the value of the name property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setName(String value) { + this.name = value; + } + +} diff --git a/jaxb/src/main/java/com/baeldung/jaxb/gen/UserResponse.java b/jaxb/src/main/java/com/baeldung/jaxb/gen/UserResponse.java index d86778403a..b80405e4a9 100644 --- a/jaxb/src/main/java/com/baeldung/jaxb/gen/UserResponse.java +++ b/jaxb/src/main/java/com/baeldung/jaxb/gen/UserResponse.java @@ -1,149 +1,149 @@ - -package com.baeldung.jaxb.gen; - -import java.io.Serializable; -import java.util.Calendar; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlSchemaType; -import javax.xml.bind.annotation.XmlType; -import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; -import org.w3._2001.xmlschema.Adapter1; - - -/** - *

Java class for UserResponse complex type. - * - *

The following schema fragment specifies the expected content contained within this class. - * - *

- * <complexType name="UserResponse">
- *   <complexContent>
- *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
- *       <sequence>
- *         <element name="id" type="{http://www.w3.org/2001/XMLSchema}int"/>
- *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
- *         <element name="gender" type="{http://www.w3.org/2001/XMLSchema}string"/>
- *         <element name="created" type="{http://www.w3.org/2001/XMLSchema}dateTime"/>
- *       </sequence>
- *     </restriction>
- *   </complexContent>
- * </complexType>
- * 
- * - * - */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlType(name = "UserResponse", propOrder = { - "id", - "name", - "gender", - "created" -}) -@XmlRootElement(name = "userResponse") -public class UserResponse - implements Serializable -{ - - private final static long serialVersionUID = -1L; - protected int id; - @XmlElement(required = true) - protected String name; - @XmlElement(required = true) - protected String gender; - @XmlElement(required = true, type = String.class) - @XmlJavaTypeAdapter(Adapter1 .class) - @XmlSchemaType(name = "dateTime") - protected Calendar created; - - /** - * Gets the value of the id property. - * - */ - public int getId() { - return id; - } - - /** - * Sets the value of the id property. - * - */ - public void setId(int value) { - this.id = value; - } - - /** - * Gets the value of the name property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getName() { - return name; - } - - /** - * Sets the value of the name property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setName(String value) { - this.name = value; - } - - /** - * Gets the value of the gender property. - * - * @return - * possible object is - * {@link String } - * - */ - public String getGender() { - return gender; - } - - /** - * Sets the value of the gender property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setGender(String value) { - this.gender = value; - } - - /** - * Gets the value of the created property. - * - * @return - * possible object is - * {@link String } - * - */ - public Calendar getCreated() { - return created; - } - - /** - * Sets the value of the created property. - * - * @param value - * allowed object is - * {@link String } - * - */ - public void setCreated(Calendar value) { - this.created = value; - } - -} + +package com.baeldung.jaxb.gen; + +import java.io.Serializable; +import java.util.Calendar; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlSchemaType; +import javax.xml.bind.annotation.XmlType; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import org.w3._2001.xmlschema.Adapter1; + + +/** + *

Java class for UserResponse complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+ * <complexType name="UserResponse">
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <sequence>
+ *         <element name="id" type="{http://www.w3.org/2001/XMLSchema}int"/>
+ *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *         <element name="gender" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *         <element name="created" type="{http://www.w3.org/2001/XMLSchema}dateTime"/>
+ *       </sequence>
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "UserResponse", propOrder = { + "id", + "name", + "gender", + "created" +}) +@XmlRootElement(name = "userResponse") +public class UserResponse + implements Serializable +{ + + private final static long serialVersionUID = -1L; + protected int id; + @XmlElement(required = true) + protected String name; + @XmlElement(required = true) + protected String gender; + @XmlElement(required = true, type = String.class) + @XmlJavaTypeAdapter(Adapter1 .class) + @XmlSchemaType(name = "dateTime") + protected Calendar created; + + /** + * Gets the value of the id property. + * + */ + public int getId() { + return id; + } + + /** + * Sets the value of the id property. + * + */ + public void setId(int value) { + this.id = value; + } + + /** + * Gets the value of the name property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getName() { + return name; + } + + /** + * Sets the value of the name property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setName(String value) { + this.name = value; + } + + /** + * Gets the value of the gender property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getGender() { + return gender; + } + + /** + * Sets the value of the gender property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setGender(String value) { + this.gender = value; + } + + /** + * Gets the value of the created property. + * + * @return + * possible object is + * {@link String } + * + */ + public Calendar getCreated() { + return created; + } + + /** + * Sets the value of the created property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setCreated(Calendar value) { + this.created = value; + } + +} diff --git a/jaxb/src/main/java/com/baeldung/jaxb/gen/package-info.java b/jaxb/src/main/java/com/baeldung/jaxb/gen/package-info.java index 6384eab27f..639d00179c 100644 --- a/jaxb/src/main/java/com/baeldung/jaxb/gen/package-info.java +++ b/jaxb/src/main/java/com/baeldung/jaxb/gen/package-info.java @@ -1,2 +1,2 @@ -@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.baeldung.com/jaxb/gen", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) -package com.baeldung.jaxb.gen; +@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.baeldung.com/jaxb/gen", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) +package com.baeldung.jaxb.gen; diff --git a/jaxb/src/main/java/org/w3/_2001/xmlschema/Adapter1.java b/jaxb/src/main/java/org/w3/_2001/xmlschema/Adapter1.java index b4865b5510..54b3c360dc 100644 --- a/jaxb/src/main/java/org/w3/_2001/xmlschema/Adapter1.java +++ b/jaxb/src/main/java/org/w3/_2001/xmlschema/Adapter1.java @@ -1,23 +1,23 @@ - -package org.w3._2001.xmlschema; - -import java.util.Calendar; -import javax.xml.bind.annotation.adapters.XmlAdapter; - -public class Adapter1 - extends XmlAdapter -{ - - - public Calendar unmarshal(String value) { - return (javax.xml.bind.DatatypeConverter.parseDateTime(value)); - } - - public String marshal(Calendar value) { - if (value == null) { - return null; - } - return (javax.xml.bind.DatatypeConverter.printDateTime(value)); - } - -} + +package org.w3._2001.xmlschema; + +import java.util.Calendar; +import javax.xml.bind.annotation.adapters.XmlAdapter; + +public class Adapter1 + extends XmlAdapter +{ + + + public Calendar unmarshal(String value) { + return (javax.xml.bind.DatatypeConverter.parseDateTime(value)); + } + + public String marshal(Calendar value) { + if (value == null) { + return null; + } + return (javax.xml.bind.DatatypeConverter.printDateTime(value)); + } + +} From 5290d7ab8733331674029cfb3337707960864461 Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Tue, 1 Feb 2022 21:52:22 -0600 Subject: [PATCH 39/63] BAEL-5308: add link back to article (#11764) --- graphql/graphql-dgs/README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 graphql/graphql-dgs/README.md diff --git a/graphql/graphql-dgs/README.md b/graphql/graphql-dgs/README.md new file mode 100644 index 0000000000..6614891b31 --- /dev/null +++ b/graphql/graphql-dgs/README.md @@ -0,0 +1,7 @@ +## GraphQL Domain Graph Service (DGS) + +This module contains articles about GraphQL using the Netflix Domain Graph Service (DGS). + +## Relevant articles: + +- [An Introduction to Domain Graph Service (DGS) Framework](https://www.baeldung.com/spring-boot-domain-graph-service) From fe6b84fd5f8307c6811d8ec40d6cc32c84d7f4f1 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 2 Feb 2022 17:44:32 +0800 Subject: [PATCH 40/63] Update README.md --- persistence-modules/java-mongodb/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/java-mongodb/README.md b/persistence-modules/java-mongodb/README.md index 34acd60c57..fe30c2999e 100644 --- a/persistence-modules/java-mongodb/README.md +++ b/persistence-modules/java-mongodb/README.md @@ -12,3 +12,4 @@ This module contains articles about MongoDB in Java. - [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations) - [BSON to JSON Document Conversion in Java](https://www.baeldung.com/java-convert-bson-to-json) - [How to Check Field Existence in MongoDB?](https://www.baeldung.com/mongodb-check-field-exists) +- [Get Last Inserted Document ID in MongoDB With Java Driver](https://www.baeldung.com/java-mongodb-last-inserted-id) From 44d4ca1278bd349e4844a290515f95852bc060c6 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 2 Feb 2022 17:48:53 +0800 Subject: [PATCH 41/63] Update README.md --- feign/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/feign/README.md b/feign/README.md index 03051671d5..eeff55a4d4 100644 --- a/feign/README.md +++ b/feign/README.md @@ -5,4 +5,5 @@ This module contains articles about Feign ### Relevant Articles: - [Intro to Feign](https://www.baeldung.com/intro-to-feign) +- [Retrying Feign Calls](https://www.baeldung.com/feign-retry) From 17f39de3ab5f307483c09e24016e3f2ca67a2ec2 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Wed, 2 Feb 2022 20:28:21 +0000 Subject: [PATCH 42/63] [JAVA-9839] Cleanup test files --- .../rdd/TransformationsUnitTest.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/apache-spark/src/test/java/com/baeldung/differences/rdd/TransformationsUnitTest.java b/apache-spark/src/test/java/com/baeldung/differences/rdd/TransformationsUnitTest.java index 4b2d9e1127..01e7d3adfc 100644 --- a/apache-spark/src/test/java/com/baeldung/differences/rdd/TransformationsUnitTest.java +++ b/apache-spark/src/test/java/com/baeldung/differences/rdd/TransformationsUnitTest.java @@ -1,9 +1,9 @@ package com.baeldung.differences.rdd; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.apache.spark.SparkConf; import org.apache.spark.api.java.JavaRDD; @@ -12,12 +12,15 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import java.io.File; + public class TransformationsUnitTest { - + public static final String COMMA_DELIMITER = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"; + private static JavaSparkContext sc; private static JavaRDD tourists; - + @BeforeClass public static void init() { SparkConf conf = new SparkConf().setAppName("uppercaseCountries") @@ -25,8 +28,11 @@ public class TransformationsUnitTest { sc = new JavaSparkContext(conf); tourists = sc.textFile("data/Tourist.csv") .filter(line -> !line.startsWith("Region")); //filter header row + + // delete previous output dir and files + FileUtils.deleteQuietly(new File("data/output")); } - + @AfterClass public static void cleanup() { sc.close(); @@ -39,9 +45,9 @@ public class TransformationsUnitTest { return columns[1].toUpperCase(); }) .distinct(); - + upperCaseCountries.saveAsTextFile("data/output/uppercase.txt"); - + upperCaseCountries.foreach(country -> { //replace non alphanumerical characters country = country.replaceAll("[^a-zA-Z]", ""); @@ -52,9 +58,9 @@ public class TransformationsUnitTest { @Test public void whenFilterByCountry_thenShowRequestedCountryRecords() { JavaRDD touristsInMexico = tourists.filter(line -> line.split(COMMA_DELIMITER)[1].equals("Mexico")); - + touristsInMexico.saveAsTextFile("data/output/touristInMexico.txt"); - + touristsInMexico.foreach(record -> { assertEquals("Mexico", record.split(COMMA_DELIMITER)[1]); }); From 7274b31c4414f7229f653c1c23f6458919ed647f Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Thu, 3 Feb 2022 10:21:00 +0100 Subject: [PATCH 43/63] JAVA-9840: Upgrade parent-boot-2 to 2.6.3 --- parent-boot-2/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parent-boot-2/pom.xml b/parent-boot-2/pom.xml index 109bf3b64f..b7f33de237 100644 --- a/parent-boot-2/pom.xml +++ b/parent-boot-2/pom.xml @@ -88,7 +88,7 @@ 3.3.0 1.0.22.RELEASE - 2.6.1 + 2.6.3 1.9.1 From 533f2014a84d6c517e368f62d8b0cf162a7d43f1 Mon Sep 17 00:00:00 2001 From: Gaetano Piazzolla Date: Thu, 3 Feb 2022 11:17:55 +0100 Subject: [PATCH 44/63] BAEL-5319 - Remove Basic Error Controller in Swagger (#11743) * BAEL-5319 - Remove Basic Error Controller in Swagger * BAEL-5319 - fixed style * BAEL-5319 - removed comments and private info * BAEL-5319 - removed private info * two space indent for line continuation, optimized import. Co-authored-by: gpiazzolla --- .../SpringBootSwaggerConfApplication.java | 13 +++++++ .../configuration/SwaggerConfiguration.java | 37 +++++++++++++++++++ .../ErrorControllerExcludedForAnnotation.java | 17 +++++++++ .../ErrorControllerExcludedForApiIgnore.java | 21 +++++++++++ .../ErrorControllerExcludedForPath.java | 19 ++++++++++ .../controller/RegularRestController.java | 33 +++++++++++++++++ .../ErrorControllerExcludedForPackage.java | 17 +++++++++ .../src/main/resources/application.properties | 1 + ...ExcludeErrorControllerIntegrationTest.java | 33 +++++++++++++++++ 9 files changed, 191 insertions(+) create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/SpringBootSwaggerConfApplication.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/configuration/SwaggerConfiguration.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForAnnotation.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForApiIgnore.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForPath.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/RegularRestController.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/excluded/ErrorControllerExcludedForPackage.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/test/java/com/baeldung/swaggerconf/SwaggerConfExcludeErrorControllerIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/SpringBootSwaggerConfApplication.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/SpringBootSwaggerConfApplication.java new file mode 100644 index 0000000000..1d2e129f26 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/SpringBootSwaggerConfApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.swaggerconf; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootSwaggerConfApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootSwaggerConfApplication.class, args); + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/configuration/SwaggerConfiguration.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/configuration/SwaggerConfiguration.java new file mode 100644 index 0000000000..a78335a2fe --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/configuration/SwaggerConfiguration.java @@ -0,0 +1,37 @@ +package com.baeldung.swaggerconf.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RestController; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +import java.util.Collections; + +import static springfox.documentation.builders.PathSelectors.regex; + +@Configuration +@EnableSwagger2 +public class SwaggerConfiguration { + + private ApiInfo apiInfo() { + return new ApiInfo("My REST API", "Some custom description of API.", "API TOS", "Terms of service", + new Contact("General UserName", "www.baeldung.com", "user-name@gmail.com"), + "License of API", "API license URL", Collections.emptyList()); + } + + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.baeldung.swaggerconf.controller")) + .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) + .paths(regex("/good-path/.*")) + .build(); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForAnnotation.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForAnnotation.java new file mode 100644 index 0000000000..65059bf446 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForAnnotation.java @@ -0,0 +1,17 @@ +package com.baeldung.swaggerconf.controller; + +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestMapping; + +@Component +@RequestMapping("good-path/error-excluded-annotation") +public class ErrorControllerExcludedForAnnotation extends BasicErrorController { + + public ErrorControllerExcludedForAnnotation(ErrorAttributes errorAttributes, ServerProperties serverProperties) { + super(errorAttributes, serverProperties.getError()); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForApiIgnore.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForApiIgnore.java new file mode 100644 index 0000000000..88208d8446 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForApiIgnore.java @@ -0,0 +1,21 @@ +package com.baeldung.swaggerconf.controller; + +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import springfox.documentation.annotations.ApiIgnore; + +@Component +@RequestMapping("good-path/error-excluded-apiignore") +@RestController +@ApiIgnore +public class ErrorControllerExcludedForApiIgnore extends BasicErrorController { + + public ErrorControllerExcludedForApiIgnore(ErrorAttributes errorAttributes, ServerProperties serverProperties) { + super(errorAttributes, serverProperties.getError()); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForPath.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForPath.java new file mode 100644 index 0000000000..5cea4f5ef9 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForPath.java @@ -0,0 +1,19 @@ +package com.baeldung.swaggerconf.controller; + +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Component +@RequestMapping("wrong-path/error-excluded-path") +@RestController +public class ErrorControllerExcludedForPath extends BasicErrorController { + + public ErrorControllerExcludedForPath(ErrorAttributes errorAttributes, ServerProperties serverProperties) { + super(errorAttributes, serverProperties.getError()); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/RegularRestController.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/RegularRestController.java new file mode 100644 index 0000000000..163826e626 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/RegularRestController.java @@ -0,0 +1,33 @@ +package com.baeldung.swaggerconf.controller; + +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.time.LocalDate; +import java.time.LocalTime; + +@RestController +@RequestMapping("good-path") +public class RegularRestController { + + @ApiOperation(value = "This method is used to get the author name.") + @GetMapping("/getAuthor") + public String getAuthor() { + return "Name Surname"; + } + + @ApiOperation(value = "This method is used to get the current date.") + @GetMapping("/getDate") + public LocalDate getDate() { + return LocalDate.now(); + } + + @ApiOperation(value = "This method is used to get the current time.") + @GetMapping("/getTime") + public LocalTime getTime() { + return LocalTime.now(); + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/excluded/ErrorControllerExcludedForPackage.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/excluded/ErrorControllerExcludedForPackage.java new file mode 100644 index 0000000000..a3b5a3fe63 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/excluded/ErrorControllerExcludedForPackage.java @@ -0,0 +1,17 @@ +package com.baeldung.swaggerconf.excluded; + +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestMapping; + +@Component +@RequestMapping("good-path/error-excluded-package") +public class ErrorControllerExcludedForPackage extends BasicErrorController { + + public ErrorControllerExcludedForPackage(ErrorAttributes errorAttributes, ServerProperties serverProperties) { + super(errorAttributes, serverProperties.getError()); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/resources/application.properties b/spring-boot-modules/spring-boot-swagger/src/main/resources/application.properties index e69de29bb2..a4e98652bc 100644 --- a/spring-boot-modules/spring-boot-swagger/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-swagger/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger/src/test/java/com/baeldung/swaggerconf/SwaggerConfExcludeErrorControllerIntegrationTest.java b/spring-boot-modules/spring-boot-swagger/src/test/java/com/baeldung/swaggerconf/SwaggerConfExcludeErrorControllerIntegrationTest.java new file mode 100644 index 0000000000..c0a5c54d69 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/test/java/com/baeldung/swaggerconf/SwaggerConfExcludeErrorControllerIntegrationTest.java @@ -0,0 +1,33 @@ +package com.baeldung.swaggerconf; + +import org.junit.Assert; +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.MvcResult; +import org.springframework.test.web.servlet.ResultActions; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@AutoConfigureMockMvc +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class SwaggerConfExcludeErrorControllerIntegrationTest { + + @Autowired + private MockMvc mvc; + + @Test + public void whenCallingSwaggerJSON_stringObjectDoesNotContainAnyErrorControllers() throws Exception { + ResultActions resultActions = mvc.perform(get("/v2/api-docs")).andExpect(status().isOk()); + MvcResult result = resultActions.andReturn(); + String content = result.getResponse().getContentAsString(); + Assert.assertNotNull(content); + Assert.assertFalse(content.contains("error-controller")); + } +} \ No newline at end of file From 15bd0c096653d431ab79772e7276806504b52723 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Thu, 3 Feb 2022 11:23:05 +0100 Subject: [PATCH 45/63] JAVA-9840: Upgrade spring-core version to 5.3.15 --- persistence-modules/spring-data-mongodb-reactive/pom.xml | 2 +- persistence-modules/spring-mybatis/pom.xml | 2 +- spring-boot-modules/spring-boot-2/pom.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/persistence-modules/spring-data-mongodb-reactive/pom.xml b/persistence-modules/spring-data-mongodb-reactive/pom.xml index 0e4efed6e6..ac2bf14635 100644 --- a/persistence-modules/spring-data-mongodb-reactive/pom.xml +++ b/persistence-modules/spring-data-mongodb-reactive/pom.xml @@ -124,7 +124,7 @@
- 5.3.13 + 5.3.15 4.5.2 3.3.1.RELEASE 3.2.6 diff --git a/persistence-modules/spring-mybatis/pom.xml b/persistence-modules/spring-mybatis/pom.xml index eb0ebd723d..65a8581f97 100644 --- a/persistence-modules/spring-mybatis/pom.xml +++ b/persistence-modules/spring-mybatis/pom.xml @@ -78,7 +78,7 @@ - 5.3.13 + 5.3.15 2.0.6 3.5.2 diff --git a/spring-boot-modules/spring-boot-2/pom.xml b/spring-boot-modules/spring-boot-2/pom.xml index 65bacb7c47..08dc517fa0 100644 --- a/spring-boot-modules/spring-boot-2/pom.xml +++ b/spring-boot-modules/spring-boot-2/pom.xml @@ -63,7 +63,7 @@ 2.14.1 - 5.3.13 + 5.3.15 11 11 From 7728df176e1a15512f59ef37f580eea9011cbfe8 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Thu, 3 Feb 2022 10:37:47 +0000 Subject: [PATCH 46/63] [JAVA-9616] Add simple demo application for mvn wrapper --- spring-boot-modules/spring-boot-artifacts/pom.xml | 8 ++++---- .../com/baeldung/wrapper/DemoApplication.java | 12 ++++++++++++ .../java/com/baeldung/wrapper/DemoController.java | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 spring-boot-modules/spring-boot-artifacts/src/main/java/com/baeldung/wrapper/DemoApplication.java create mode 100644 spring-boot-modules/spring-boot-artifacts/src/main/java/com/baeldung/wrapper/DemoController.java diff --git a/spring-boot-modules/spring-boot-artifacts/pom.xml b/spring-boot-modules/spring-boot-artifacts/pom.xml index 6bf75491eb..0292dc95cf 100644 --- a/spring-boot-modules/spring-boot-artifacts/pom.xml +++ b/spring-boot-modules/spring-boot-artifacts/pom.xml @@ -101,7 +101,7 @@ maven-failsafe-plugin 2.18 - integration-tests @@ -110,7 +110,7 @@ verify - **/ExternalPropertyFileLoaderIntegrationTest.java @@ -186,7 +186,7 @@ - com.baeldung.boot.Application + com.baeldung.wrapper.DemoApplication 3.1.1 3.3.7-1 2.2 @@ -195,4 +195,4 @@ 4.5.8 - \ No newline at end of file + diff --git a/spring-boot-modules/spring-boot-artifacts/src/main/java/com/baeldung/wrapper/DemoApplication.java b/spring-boot-modules/spring-boot-artifacts/src/main/java/com/baeldung/wrapper/DemoApplication.java new file mode 100644 index 0000000000..d0a7e96bec --- /dev/null +++ b/spring-boot-modules/spring-boot-artifacts/src/main/java/com/baeldung/wrapper/DemoApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.wrapper; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication(scanBasePackages = { "com.baeldung" }) +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-artifacts/src/main/java/com/baeldung/wrapper/DemoController.java b/spring-boot-modules/spring-boot-artifacts/src/main/java/com/baeldung/wrapper/DemoController.java new file mode 100644 index 0000000000..530a1d584e --- /dev/null +++ b/spring-boot-modules/spring-boot-artifacts/src/main/java/com/baeldung/wrapper/DemoController.java @@ -0,0 +1,15 @@ +package com.baeldung.wrapper; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class DemoController { + + @GetMapping(value = "/demo") + public String demo(Model model) { + return "index"; + } + +} From 27143e9157f0da61db584cbb8fe0d9cb84fd8b5a Mon Sep 17 00:00:00 2001 From: kwoyke Date: Thu, 3 Feb 2022 13:52:37 +0100 Subject: [PATCH 47/63] JAVA-9512: Delete garbage file (#11766) --- book | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 book diff --git a/book b/book deleted file mode 100644 index e69de29bb2..0000000000 From b15df898347e1c8f8e508e7d1c5b66d21dd44f01 Mon Sep 17 00:00:00 2001 From: Andres Luzuriaga Date: Fri, 7 Jan 2022 23:56:49 -0500 Subject: [PATCH 48/63] BAEL-4498 JDBC Connection Status --- .../ConnectionValidation.java | 65 +++++++++++++++++++ .../ConnectionValidationUnitTest.java | 38 +++++++++++ 2 files changed, 103 insertions(+) create mode 100644 persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/connectionstatus/ConnectionValidation.java create mode 100644 persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/connectionstatus/ConnectionValidationUnitTest.java diff --git a/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/connectionstatus/ConnectionValidation.java b/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/connectionstatus/ConnectionValidation.java new file mode 100644 index 0000000000..b6818d25c2 --- /dev/null +++ b/persistence-modules/core-java-persistence-2/src/main/java/com/baeldung/connectionstatus/ConnectionValidation.java @@ -0,0 +1,65 @@ +package com.baeldung.connectionstatus; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class ConnectionValidation +{ + + public static Connection getConnection() + throws Exception + { + Class.forName("org.h2.Driver"); + String url = "jdbc:h2:mem:testdb"; + return DriverManager.getConnection(url, "user", "password"); + } + + public static void runIfOpened(Connection connection) + throws SQLException + { + if (connection != null && !connection.isClosed()) { + // run sql statements + } + else { + // handle closed connection + } + } + + public static void runIfValid(Connection connection) + throws SQLException + { + // Try to validate connection with a 5 seconds timeout + if (connection.isValid(5)) { + // run sql statements + } + else { + // handle invalid connection + } + } + + public static void runIfConnectionValid(Connection connection) + { + if (isConnectionValid(connection)) { + // run sql statements + } + else { + // handle invalid connection + } + } + + public static boolean isConnectionValid(Connection connection) + { + try { + if (connection != null && !connection.isClosed()) { + // Running a simple validation query + connection.prepareStatement("SELECT 1"); + return true; + } + } + catch (SQLException e) { + // log some useful data here + } + return false; + } +} diff --git a/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/connectionstatus/ConnectionValidationUnitTest.java b/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/connectionstatus/ConnectionValidationUnitTest.java new file mode 100644 index 0000000000..26356931cc --- /dev/null +++ b/persistence-modules/core-java-persistence-2/src/test/java/com/baeldung/connectionstatus/ConnectionValidationUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.connectionstatus; + +import org.junit.jupiter.api.Test; + +import java.sql.Connection; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ConnectionValidationUnitTest +{ + @Test + void givenConnectionObject_whenCreated_thenIsNotClosed() + throws Exception + { + Connection connection = ConnectionValidation.getConnection(); + assertNotNull(connection); + assertFalse(connection.isClosed()); + } + + @Test + void givenConnectionObject_whenCreated_thenIsValid() + throws Exception + { + Connection connection = ConnectionValidation.getConnection(); + assertTrue(connection.isValid(0)); + } + + @Test + void givenConnectionObject_whenValidated_thenIsValid() + throws Exception + { + Connection connection = ConnectionValidation.getConnection(); + assertTrue(ConnectionValidation.isConnectionValid(connection)); + } + +} From 694041bc875d10fc4e1f143987e70a505331ba7e Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Thu, 3 Feb 2022 21:32:04 +0000 Subject: [PATCH 49/63] [JAVA-9814] Cleanup apache-olingo module --- apache-olingo/{olingo2 => }/.gitignore | 1 + apache-olingo/Samples.md | 18 ++++---- apache-olingo/{olingo2 => }/pom.xml | 20 ++++----- .../olingo2/CarsODataJPAServiceFactory.java | 13 ++---- .../examples/olingo2/JerseyConfig.java | 43 ++++++++----------- .../olingo2/Olingo2SampleApplication.java | 2 +- .../examples/olingo2/domain/CarMaker.java | 0 .../examples/olingo2/domain/CarModel.java | 0 .../src/main/resources/application.yml | 0 .../{olingo2 => }/src/main/resources/data.sql | 0 .../src/main/resources/logback.xml | 0 ...ngo2SampleApplicationIntegrationTest.java} | 2 +- .../src/test/resources/logback-test.xml | 0 .../src/test/resources/olingo2-queries.json | 0 pom.xml | 4 +- 15 files changed, 46 insertions(+), 57 deletions(-) rename apache-olingo/{olingo2 => }/.gitignore (99%) rename apache-olingo/{olingo2 => }/pom.xml (85%) rename apache-olingo/{olingo2 => }/src/main/java/com/baeldung/examples/olingo2/CarsODataJPAServiceFactory.java (97%) rename apache-olingo/{olingo2 => }/src/main/java/com/baeldung/examples/olingo2/JerseyConfig.java (89%) rename apache-olingo/{olingo2 => }/src/main/java/com/baeldung/examples/olingo2/Olingo2SampleApplication.java (88%) rename apache-olingo/{olingo2 => }/src/main/java/com/baeldung/examples/olingo2/domain/CarMaker.java (100%) rename apache-olingo/{olingo2 => }/src/main/java/com/baeldung/examples/olingo2/domain/CarModel.java (100%) rename apache-olingo/{olingo2 => }/src/main/resources/application.yml (100%) rename apache-olingo/{olingo2 => }/src/main/resources/data.sql (100%) rename apache-olingo/{olingo2 => }/src/main/resources/logback.xml (100%) rename apache-olingo/{olingo2/src/test/java/com/baeldung/examples/olingo2/Olingo2SampleApplicationUnitTest.java => src/test/java/com/baeldung/examples/olingo2/Olingo2SampleApplicationIntegrationTest.java} (84%) rename apache-olingo/{olingo2 => }/src/test/resources/logback-test.xml (100%) rename apache-olingo/{olingo2 => }/src/test/resources/olingo2-queries.json (100%) diff --git a/apache-olingo/olingo2/.gitignore b/apache-olingo/.gitignore similarity index 99% rename from apache-olingo/olingo2/.gitignore rename to apache-olingo/.gitignore index 153c9335eb..b85b366850 100644 --- a/apache-olingo/olingo2/.gitignore +++ b/apache-olingo/.gitignore @@ -27,3 +27,4 @@ HELP.md ### VS Code ### .vscode/ + diff --git a/apache-olingo/Samples.md b/apache-olingo/Samples.md index def8971d64..66f514b4d9 100644 --- a/apache-olingo/Samples.md +++ b/apache-olingo/Samples.md @@ -4,15 +4,15 @@ This following table contains test URLs that can be used with the Olingo V2 demo | URL | Description | |------------------------------------------|-------------------------------------------------| -| `http://localhost:8180/odata/$metadata` | fetch OData metadata document | -| `http://localhost:8180/odata/CarMakers?$top=10&$skip=10` | Get 10 entities starting at offset 10 | -| `http://localhost:8180/odata/CarMakers?$count` | Return total count of entities in this set | -| `http://localhost:8180/odata/CarMakers?$filter=startswith(Name,'B')` | Return entities where the *Name* property starts with 'B' | -| `http://localhost:8180/odata/CarModels?$filter=Year eq 2008 and CarMakerDetails/Name eq 'BWM'` | Return *CarModel* entities where the *Name* property of its maker starts with 'B' | -| `http://localhost:8180/odata/CarModels(1L)?$expand=CarMakerDetails` | Return the *CarModel* with primary key '1', along with its maker| -| `http://localhost:8180/odata/CarModels(1L)?$select=Name,Sku` | Return the *CarModel* with primary key '1', returing only its *Name* and *Sku* properties | -| `http://localhost:8180/odata/CarModels?$orderBy=Name asc,Sku desc` | Return *CarModel* entities, ordered by the their *Name* and *Sku* properties | -| `http://localhost:8180/odata/CarModels?$format=json` | Return *CarModel* entities, using a JSON representation| +| `http://localhost:8080/odata/$metadata` | fetch OData metadata document | +| `http://localhost:8080/odata/CarMakers?$top=10&$skip=10` | Get 10 entities starting at offset 10 | +| `http://localhost:8080/odata/CarMakers?$count` | Return total count of entities in this set | +| `http://localhost:8080/odata/CarMakers?$filter=startswith(Name,'B')` | Return entities where the *Name* property starts with 'B' | +| `http://localhost:8080/odata/CarModels?$filter=Year eq 2008 and CarMakerDetails/Name eq 'BWM'` | Return *CarModel* entities where the *Name* property of its maker starts with 'B' | +| `http://localhost:8080/odata/CarModels(1L)?$expand=CarMakerDetails` | Return the *CarModel* with primary key '1', along with its maker| +| `http://localhost:8080/odata/CarModels(1L)?$select=Name,Sku` | Return the *CarModel* with primary key '1', returing only its *Name* and *Sku* properties | +| `http://localhost:8080/odata/CarModels?$orderBy=Name asc,Sku desc` | Return *CarModel* entities, ordered by the their *Name* and *Sku* properties | +| `http://localhost:8080/odata/CarModels?$format=json` | Return *CarModel* entities, using a JSON representation| diff --git a/apache-olingo/olingo2/pom.xml b/apache-olingo/pom.xml similarity index 85% rename from apache-olingo/olingo2/pom.xml rename to apache-olingo/pom.xml index c86428d054..5de0dfd511 100644 --- a/apache-olingo/olingo2/pom.xml +++ b/apache-olingo/pom.xml @@ -3,16 +3,16 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung.examples.olingo2 - olingo2 - olingo2 - Sample Olingo 2 Project + com.baeldung.examples.olingo + apache-olingo + apache-olingo + Sample Apache Olingo Project com.baeldung parent-boot-2 0.0.1-SNAPSHOT - ../../parent-boot-2 + ../parent-boot-2 @@ -43,7 +43,7 @@ org.apache.olingo olingo-odata2-core - ${olingo2.version} + ${olingo.version} @@ -55,12 +55,12 @@ org.apache.olingo olingo-odata2-jpa-processor-core - ${olingo2.version} + ${olingo.version} org.apache.olingo olingo-odata2-jpa-processor-ref - ${olingo2.version} + ${olingo.version} org.eclipse.persistence @@ -80,7 +80,7 @@ - 2.0.11 + 2.0.11 - \ No newline at end of file + diff --git a/apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/CarsODataJPAServiceFactory.java b/apache-olingo/src/main/java/com/baeldung/examples/olingo2/CarsODataJPAServiceFactory.java similarity index 97% rename from apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/CarsODataJPAServiceFactory.java rename to apache-olingo/src/main/java/com/baeldung/examples/olingo2/CarsODataJPAServiceFactory.java index 55155732c8..343af93de9 100644 --- a/apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/CarsODataJPAServiceFactory.java +++ b/apache-olingo/src/main/java/com/baeldung/examples/olingo2/CarsODataJPAServiceFactory.java @@ -27,11 +27,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -/** - * ODataJPAServiceFactory implementation for our sample domain - * @author Philippe - * - */ @Component public class CarsODataJPAServiceFactory extends ODataJPAServiceFactory { @@ -44,7 +39,7 @@ public class CarsODataJPAServiceFactory extends ODataJPAServiceFactory { /** * This method will be called by Olingo on every request to - * initialize the ODataJPAContext that will be used. + * initialize the ODataJPAContext that will be used. */ @Override public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException { @@ -54,14 +49,14 @@ public class CarsODataJPAServiceFactory extends ODataJPAServiceFactory { ODataContext octx = ctx.getODataContext(); HttpServletRequest request = (HttpServletRequest)octx.getParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT); EntityManager em = (EntityManager)request.getAttribute(JerseyConfig.EntityManagerFilter.EM_REQUEST_ATTRIBUTE); - + // Here we're passing the EM that was created by the EntityManagerFilter (see JerseyConfig) ctx.setEntityManager(new EntityManagerWrapper(em)); ctx.setPersistenceUnitName("default"); - + // We're managing the EM's lifecycle, so we must inform Olingo that it should not // try to manage transactions and/or persistence sessions - ctx.setContainerManaged(true); + ctx.setContainerManaged(true); return ctx; } diff --git a/apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/JerseyConfig.java b/apache-olingo/src/main/java/com/baeldung/examples/olingo2/JerseyConfig.java similarity index 89% rename from apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/JerseyConfig.java rename to apache-olingo/src/main/java/com/baeldung/examples/olingo2/JerseyConfig.java index 02e5ce5adf..f300f552fe 100644 --- a/apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/JerseyConfig.java +++ b/apache-olingo/src/main/java/com/baeldung/examples/olingo2/JerseyConfig.java @@ -1,6 +1,12 @@ - package com.baeldung.examples.olingo2; +package com.baeldung.examples.olingo2; -import java.io.IOException; +import org.apache.olingo.odata2.api.ODataServiceFactory; +import org.apache.olingo.odata2.core.rest.ODataRootLocator; +import org.apache.olingo.odata2.core.rest.app.ODataApplication; +import org.glassfish.jersey.server.ResourceConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; @@ -15,28 +21,15 @@ import javax.ws.rs.container.ContainerResponseFilter; import javax.ws.rs.core.Context; import javax.ws.rs.ext.Provider; -import org.apache.olingo.odata2.api.ODataServiceFactory; -import org.apache.olingo.odata2.core.rest.ODataRootLocator; -import org.apache.olingo.odata2.core.rest.app.ODataApplication; -import org.glassfish.jersey.server.ResourceConfig; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -/** - * Jersey JAX-RS configuration - * @author Philippe - * - */ @Component @ApplicationPath("/odata") public class JerseyConfig extends ResourceConfig { - - - public JerseyConfig(CarsODataJPAServiceFactory serviceFactory, EntityManagerFactory emf) { - + + + public JerseyConfig(CarsODataJPAServiceFactory serviceFactory, EntityManagerFactory emf) { + ODataApplication app = new ODataApplication(); - + app .getClasses() .forEach( c -> { @@ -46,11 +39,11 @@ public class JerseyConfig extends ResourceConfig { register(c); } }); - - register(new CarsRootLocator(serviceFactory)); + + register(new CarsRootLocator(serviceFactory)); register( new EntityManagerFilter(emf)); } - + /** * This filter handles the EntityManager transaction lifecycle. * @author Philippe @@ -72,7 +65,7 @@ public class JerseyConfig extends ResourceConfig { } @Override - public void filter(ContainerRequestContext ctx) throws IOException { + public void filter(ContainerRequestContext ctx) { log.info("[I60] >>> filter"); EntityManager em = this.emf.createEntityManager(); httpRequest.setAttribute(EM_REQUEST_ATTRIBUTE, em); @@ -85,7 +78,7 @@ public class JerseyConfig extends ResourceConfig { } @Override - public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) { log.info("[I68] <<< filter"); EntityManager em = (EntityManager) httpRequest.getAttribute(EM_REQUEST_ATTRIBUTE); diff --git a/apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/Olingo2SampleApplication.java b/apache-olingo/src/main/java/com/baeldung/examples/olingo2/Olingo2SampleApplication.java similarity index 88% rename from apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/Olingo2SampleApplication.java rename to apache-olingo/src/main/java/com/baeldung/examples/olingo2/Olingo2SampleApplication.java index f76e0e00e3..7210f4c812 100644 --- a/apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/Olingo2SampleApplication.java +++ b/apache-olingo/src/main/java/com/baeldung/examples/olingo2/Olingo2SampleApplication.java @@ -7,7 +7,7 @@ import org.springframework.boot.web.servlet.support.SpringBootServletInitializer @SpringBootApplication public class Olingo2SampleApplication extends SpringBootServletInitializer { - public static void main(String[] args) { + public static void main(String[] args) { SpringApplication.run(Olingo2SampleApplication.class); } } diff --git a/apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/domain/CarMaker.java b/apache-olingo/src/main/java/com/baeldung/examples/olingo2/domain/CarMaker.java similarity index 100% rename from apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/domain/CarMaker.java rename to apache-olingo/src/main/java/com/baeldung/examples/olingo2/domain/CarMaker.java diff --git a/apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/domain/CarModel.java b/apache-olingo/src/main/java/com/baeldung/examples/olingo2/domain/CarModel.java similarity index 100% rename from apache-olingo/olingo2/src/main/java/com/baeldung/examples/olingo2/domain/CarModel.java rename to apache-olingo/src/main/java/com/baeldung/examples/olingo2/domain/CarModel.java diff --git a/apache-olingo/olingo2/src/main/resources/application.yml b/apache-olingo/src/main/resources/application.yml similarity index 100% rename from apache-olingo/olingo2/src/main/resources/application.yml rename to apache-olingo/src/main/resources/application.yml diff --git a/apache-olingo/olingo2/src/main/resources/data.sql b/apache-olingo/src/main/resources/data.sql similarity index 100% rename from apache-olingo/olingo2/src/main/resources/data.sql rename to apache-olingo/src/main/resources/data.sql diff --git a/apache-olingo/olingo2/src/main/resources/logback.xml b/apache-olingo/src/main/resources/logback.xml similarity index 100% rename from apache-olingo/olingo2/src/main/resources/logback.xml rename to apache-olingo/src/main/resources/logback.xml diff --git a/apache-olingo/olingo2/src/test/java/com/baeldung/examples/olingo2/Olingo2SampleApplicationUnitTest.java b/apache-olingo/src/test/java/com/baeldung/examples/olingo2/Olingo2SampleApplicationIntegrationTest.java similarity index 84% rename from apache-olingo/olingo2/src/test/java/com/baeldung/examples/olingo2/Olingo2SampleApplicationUnitTest.java rename to apache-olingo/src/test/java/com/baeldung/examples/olingo2/Olingo2SampleApplicationIntegrationTest.java index 74033a49cd..402c288673 100644 --- a/apache-olingo/olingo2/src/test/java/com/baeldung/examples/olingo2/Olingo2SampleApplicationUnitTest.java +++ b/apache-olingo/src/test/java/com/baeldung/examples/olingo2/Olingo2SampleApplicationIntegrationTest.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest -public class Olingo2SampleApplicationUnitTest { +public class Olingo2SampleApplicationIntegrationTest { @Test public void contextLoads() { diff --git a/apache-olingo/olingo2/src/test/resources/logback-test.xml b/apache-olingo/src/test/resources/logback-test.xml similarity index 100% rename from apache-olingo/olingo2/src/test/resources/logback-test.xml rename to apache-olingo/src/test/resources/logback-test.xml diff --git a/apache-olingo/olingo2/src/test/resources/olingo2-queries.json b/apache-olingo/src/test/resources/olingo2-queries.json similarity index 100% rename from apache-olingo/olingo2/src/test/resources/olingo2-queries.json rename to apache-olingo/src/test/resources/olingo2-queries.json diff --git a/pom.xml b/pom.xml index f2a230357c..f5378f9961 100644 --- a/pom.xml +++ b/pom.xml @@ -353,7 +353,7 @@ apache-cxf apache-kafka apache-libraries - apache-olingo/olingo2 + apache-olingo apache-poi apache-rocketmq apache-shiro @@ -839,7 +839,7 @@ apache-cxf apache-kafka apache-libraries - apache-olingo/olingo2 + apache-olingo apache-poi apache-rocketmq apache-shiro From a61eb46b22847c6ea65d710bf38b2d8d158d08b1 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Fri, 4 Feb 2022 11:46:52 +0100 Subject: [PATCH 50/63] JAVA-9840: Keep spring-data-jpa-filtering on Spring Boot 2.6.1 --- persistence-modules/spring-data-jpa-filtering/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/spring-data-jpa-filtering/pom.xml b/persistence-modules/spring-data-jpa-filtering/pom.xml index 287a3136fd..9c10f4a997 100644 --- a/persistence-modules/spring-data-jpa-filtering/pom.xml +++ b/persistence-modules/spring-data-jpa-filtering/pom.xml @@ -67,6 +67,7 @@ com.baeldung.boot.Application 1.10.6 42.2.5 + 2.6.1 \ No newline at end of file From 9a8d9227b752dc5ea2e182d6eafc2f0f5f52c9c2 Mon Sep 17 00:00:00 2001 From: Ralf Ueberfuhr <40685729+ueberfuhr@users.noreply.github.com> Date: Fri, 4 Feb 2022 17:58:16 +0100 Subject: [PATCH 51/63] BAEL-5323: Spring Boot with multiple data sources (#11741) * BAEL-5323: Spring Boot with multiple data sources * BAEL-5323: small fixes * BAEL-5323: fix test class * BAEL-5323: Refactor configuration classes * BAEL-5323: Set proeprty to private * BAEL-5323: rename test class to follow naming conventions --- spring-boot-modules/pom.xml | 1 + .../.gitignore | 1 + .../spring-boot-multiple-datasources/pom.xml | 58 +++++++++++++++++++ .../MultipleDatasourcesApplication.java | 13 +++++ .../spring/datasources/todos/Todo.java | 48 +++++++++++++++ .../todos/TodoDatasourceConfiguration.java | 28 +++++++++ .../todos/TodoJpaConfiguration.java | 40 +++++++++++++ .../datasources/todos/TodoRepository.java | 6 ++ .../spring/datasources/topics/Topic.java | 39 +++++++++++++ .../topics/TopicDatasourceConfiguration.java | 26 +++++++++ .../topics/TopicJpaConfiguration.java | 41 +++++++++++++ .../datasources/topics/TopicRepository.java | 6 ++ .../src/main/resources/application.yml | 23 ++++++++ .../MultipleDatasourcesIntegrationTest.java | 39 +++++++++++++ 14 files changed, 369 insertions(+) create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/.gitignore create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/pom.xml create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/MultipleDatasourcesApplication.java create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/Todo.java create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoDatasourceConfiguration.java create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoJpaConfiguration.java create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoRepository.java create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/Topic.java create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicDatasourceConfiguration.java create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicJpaConfiguration.java create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicRepository.java create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/main/resources/application.yml create mode 100644 spring-boot-modules/spring-boot-multiple-datasources/src/test/java/com/baeldung/spring/datasources/MultipleDatasourcesIntegrationTest.java diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index ac0deaa9e5..4925530a35 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -52,6 +52,7 @@ spring-boot-libraries spring-boot-libraries-2 spring-boot-logging-log4j2 + spring-boot-multiple-datasources spring-boot-mvc spring-boot-mvc-2 spring-boot-mvc-3 diff --git a/spring-boot-modules/spring-boot-multiple-datasources/.gitignore b/spring-boot-modules/spring-boot-multiple-datasources/.gitignore new file mode 100644 index 0000000000..87a3fce287 --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/.gitignore @@ -0,0 +1 @@ +.local-db diff --git a/spring-boot-modules/spring-boot-multiple-datasources/pom.xml b/spring-boot-modules/spring-boot-multiple-datasources/pom.xml new file mode 100644 index 0000000000..d66095bc2c --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + spring-boot-multiple-datasources + 0.1.0-SNAPSHOT + spring-boot-multiple-datasources + jar + Module For Spring Boot With Multiple Datasources + + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + ../pom.xml + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + provided + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.springframework.boot + spring-boot-starter-test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + 2.6.3 + + + diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/MultipleDatasourcesApplication.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/MultipleDatasourcesApplication.java new file mode 100644 index 0000000000..efdff387df --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/MultipleDatasourcesApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.spring.datasources; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MultipleDatasourcesApplication { + + public static void main(String[] args) { + SpringApplication.run(MultipleDatasourcesApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/Todo.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/Todo.java new file mode 100644 index 0000000000..6f8557e258 --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/Todo.java @@ -0,0 +1,48 @@ +package com.baeldung.spring.datasources.todos; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Todo { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String title; + private boolean completed; + + public Todo() { + } + + public Todo(String title) { + this.title = title; + } + + 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 boolean isCompleted() { + return completed; + } + + public void setCompleted(boolean completed) { + this.completed = completed; + } + +} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoDatasourceConfiguration.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoDatasourceConfiguration.java new file mode 100644 index 0000000000..3bfc8e2855 --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoDatasourceConfiguration.java @@ -0,0 +1,28 @@ +package com.baeldung.spring.datasources.todos; + +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +import javax.sql.DataSource; + +@Configuration +public class TodoDatasourceConfiguration { + + @Bean + @ConfigurationProperties("spring.datasource.todos") + public DataSourceProperties todosDataSourceProperties() { + return new DataSourceProperties(); + } + + @Bean + @Primary + public DataSource todosDataSource() { + return todosDataSourceProperties() + .initializeDataSourceBuilder() + .build(); + } + +} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoJpaConfiguration.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoJpaConfiguration.java new file mode 100644 index 0000000000..655a3a55c2 --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoJpaConfiguration.java @@ -0,0 +1,40 @@ +package com.baeldung.spring.datasources.todos; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; +import java.util.Objects; + +@Configuration +@EnableTransactionManagement +@EnableJpaRepositories( + basePackageClasses = Todo.class, + entityManagerFactoryRef = "todosEntityManagerFactory", + transactionManagerRef = "todosTransactionManager" +) +public class TodoJpaConfiguration { + + @Bean + public LocalContainerEntityManagerFactoryBean todosEntityManagerFactory( + @Qualifier("todosDataSource") DataSource dataSource, + EntityManagerFactoryBuilder builder) { + return builder + .dataSource(dataSource) + .packages(Todo.class) + .build(); + } + + @Bean + public PlatformTransactionManager todosTransactionManager( + @Qualifier("todosEntityManagerFactory") LocalContainerEntityManagerFactoryBean todosEntityManagerFactory) { + return new JpaTransactionManager(Objects.requireNonNull(todosEntityManagerFactory.getObject())); + } +} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoRepository.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoRepository.java new file mode 100644 index 0000000000..09fb8c6500 --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/todos/TodoRepository.java @@ -0,0 +1,6 @@ +package com.baeldung.spring.datasources.todos; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface TodoRepository extends JpaRepository { +} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/Topic.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/Topic.java new file mode 100644 index 0000000000..1d1f20f111 --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/Topic.java @@ -0,0 +1,39 @@ +package com.baeldung.spring.datasources.topics; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Topic { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String title; + + public Topic() { + } + + public Topic(String title) { + this.title = title; + } + + 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; + } + +} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicDatasourceConfiguration.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicDatasourceConfiguration.java new file mode 100644 index 0000000000..a06983d681 --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicDatasourceConfiguration.java @@ -0,0 +1,26 @@ +package com.baeldung.spring.datasources.topics; + +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.sql.DataSource; + +@Configuration +public class TopicDatasourceConfiguration { + + @Bean + @ConfigurationProperties("spring.datasource.topics") + public DataSourceProperties topicsDataSourceProperties() { + return new DataSourceProperties(); + } + + @Bean + public DataSource topicsDataSource() { + return topicsDataSourceProperties() + .initializeDataSourceBuilder() + .build(); + } + +} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicJpaConfiguration.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicJpaConfiguration.java new file mode 100644 index 0000000000..d800813b8c --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicJpaConfiguration.java @@ -0,0 +1,41 @@ +package com.baeldung.spring.datasources.topics; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.sql.DataSource; +import java.util.Objects; + +@Configuration +@EnableTransactionManagement +@EnableJpaRepositories( + basePackageClasses = Topic.class, + entityManagerFactoryRef = "topicsEntityManagerFactory", + transactionManagerRef = "topicsTransactionManager" +) +public class TopicJpaConfiguration { + + @Bean + public LocalContainerEntityManagerFactoryBean topicsEntityManagerFactory( + @Qualifier("topicsDataSource") DataSource dataSource, + EntityManagerFactoryBuilder builder + ) { + return builder + .dataSource(dataSource) + .packages(Topic.class) + .build(); + } + + @Bean + public PlatformTransactionManager topicsTransactionManager( + @Qualifier("topicsEntityManagerFactory") LocalContainerEntityManagerFactoryBean topicsEntityManagerFactory) { + return new JpaTransactionManager(Objects.requireNonNull(topicsEntityManagerFactory.getObject())); + } +} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicRepository.java b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicRepository.java new file mode 100644 index 0000000000..499b72650a --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/src/main/java/com/baeldung/spring/datasources/topics/TopicRepository.java @@ -0,0 +1,6 @@ +package com.baeldung.spring.datasources.topics; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface TopicRepository extends JpaRepository { +} diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/main/resources/application.yml b/spring-boot-modules/spring-boot-multiple-datasources/src/main/resources/application.yml new file mode 100644 index 0000000000..4754acfc0f --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/src/main/resources/application.yml @@ -0,0 +1,23 @@ +spring: + datasource: + todos: + url: jdbc:h2:./.local-db/todos;DB_CLOSE_DELAY=-1;MODE=DB2;AUTO_SERVER=TRUE + username: sa + password: null + driverClassName: org.h2.Driver + topics: + url: jdbc:h2:./.local-db/topics;DB_CLOSE_DELAY=-1;MODE=DB2;AUTO_SERVER=TRUE + username: sa + password: null + driverClassName: org.h2.Driver + h2: + console: + enabled: true + path: /h2-console + jpa: + generate-ddl: true + hibernate: + ddl-auto: update + properties: + hibernate: + dialect: org.hibernate.dialect.H2Dialect diff --git a/spring-boot-modules/spring-boot-multiple-datasources/src/test/java/com/baeldung/spring/datasources/MultipleDatasourcesIntegrationTest.java b/spring-boot-modules/spring-boot-multiple-datasources/src/test/java/com/baeldung/spring/datasources/MultipleDatasourcesIntegrationTest.java new file mode 100644 index 0000000000..397992f6c8 --- /dev/null +++ b/spring-boot-modules/spring-boot-multiple-datasources/src/test/java/com/baeldung/spring/datasources/MultipleDatasourcesIntegrationTest.java @@ -0,0 +1,39 @@ +package com.baeldung.spring.datasources; + +import com.baeldung.spring.datasources.todos.Todo; +import com.baeldung.spring.datasources.todos.TodoRepository; +import com.baeldung.spring.datasources.topics.Topic; +import com.baeldung.spring.datasources.topics.TopicRepository; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; + +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; + +@DataJpaTest // no test database! +class MultipleDatasourcesIntegrationTest { + + @Autowired + TodoRepository todoRepo; + @Autowired + TopicRepository topicRepo; + + @Test + void shouldSaveTodoToTodoDB() { + Todo todo = new Todo("test"); + Todo saved =todoRepo.save(todo); + Optional result= todoRepo.findById(saved.getId()); + assertThat(result).isPresent(); + } + + @Test + void shouldSaveTopicToTopicDB() { + Topic todo = new Topic("test"); + Topic saved =topicRepo.save(todo); + Optional result= topicRepo.findById(saved.getId()); + assertThat(result).isPresent(); + } + +} From d70d23f8e1ce9d04dd858092f08580b842e852f6 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Fri, 4 Feb 2022 11:19:43 +0000 Subject: [PATCH 52/63] [JAVA-9585] Convert Cassandra integration tests to Live tests --- .../java/com/baeldung/SpringContextTest.java | 12 ++--- ...nTest.java => BookRepositoryLiveTest.java} | 39 ++++++++------ ...st.java => CassandraTemplateLiveTest.java} | 51 ++++++++++-------- ...ationTest.java => CqlQueriesLiveTest.java} | 54 ++++++++++--------- 4 files changed, 84 insertions(+), 72 deletions(-) rename persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/{BookRepositoryIntegrationTest.java => BookRepositoryLiveTest.java} (92%) rename persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/{CassandraTemplateIntegrationTest.java => CassandraTemplateLiveTest.java} (93%) rename persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/{CqlQueriesIntegrationTest.java => CqlQueriesLiveTest.java} (90%) diff --git a/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/SpringContextTest.java b/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/SpringContextTest.java index 22bd5c6551..83b7b227b3 100644 --- a/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/SpringContextTest.java +++ b/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/SpringContextTest.java @@ -32,10 +32,10 @@ public class SpringContextTest { public static final String KEYSPACE_ACTIVATE_QUERY = "USE testKeySpace;"; public static final String DATA_TABLE_NAME = "book"; - + @Autowired private CassandraAdminOperations adminTemplate; - + @BeforeClass public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException { EmbeddedCassandraServerHelper.startEmbeddedCassandra(); @@ -47,14 +47,14 @@ public class SpringContextTest { } @Before - public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException { - adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap()); + public void createTable() { + adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<>()); } - + @Test public void whenSpringContextIsBootstrapped_thenNoExceptions() { } - + @After public void dropTable() { adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME)); diff --git a/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java b/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/BookRepositoryLiveTest.java similarity index 92% rename from persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java rename to persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/BookRepositoryLiveTest.java index 55e968d6f2..d5758c3574 100644 --- a/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/BookRepositoryLiveTest.java @@ -1,17 +1,15 @@ package com.baeldung.spring.data.cassandra.repository; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; - -import java.io.IOException; -import java.util.HashMap; - +import com.baeldung.spring.data.cassandra.config.CassandraConfig; import com.baeldung.spring.data.cassandra.model.Book; +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.utils.UUIDs; +import com.google.common.collect.ImmutableSet; import org.apache.cassandra.exceptions.ConfigurationException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.thrift.transport.TTransportException; -import com.baeldung.spring.data.cassandra.config.CassandraConfig; import org.cassandraunit.utils.EmbeddedCassandraServerHelper; import org.junit.After; import org.junit.AfterClass; @@ -25,15 +23,24 @@ import org.springframework.data.cassandra.core.CassandraAdminOperations; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.Session; -import com.datastax.driver.core.utils.UUIDs; -import com.google.common.collect.ImmutableSet; +import java.io.IOException; +import java.util.HashMap; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +/** + * Live test for Cassandra testing. + * + * This can be converted to IntegrationTest once cassandra-unit tests can be executed in parallel and + * multiple test servers started as part of test suite. + * + * Open cassandra-unit issue for parallel execution: https://github.com/jsevellec/cassandra-unit/issues/155 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = CassandraConfig.class) -public class BookRepositoryIntegrationTest { - private static final Log LOGGER = LogFactory.getLog(BookRepositoryIntegrationTest.class); +public class BookRepositoryLiveTest { + private static final Log LOGGER = LogFactory.getLog(BookRepositoryLiveTest.class); public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };"; @@ -47,8 +54,6 @@ public class BookRepositoryIntegrationTest { @Autowired private CassandraAdminOperations adminTemplate; - // - @BeforeClass public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException { EmbeddedCassandraServerHelper.startEmbeddedCassandra(); @@ -62,8 +67,8 @@ public class BookRepositoryIntegrationTest { } @Before - public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException { - adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap()); + public void createTable() { + adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<>()); } @Test diff --git a/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CassandraTemplateIntegrationTest.java b/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CassandraTemplateLiveTest.java similarity index 93% rename from persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CassandraTemplateIntegrationTest.java rename to persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CassandraTemplateLiveTest.java index 90ae68ba98..bc05302d13 100644 --- a/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CassandraTemplateIntegrationTest.java +++ b/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CassandraTemplateLiveTest.java @@ -1,17 +1,13 @@ package com.baeldung.spring.data.cassandra.repository; -import static junit.framework.TestCase.assertNull; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - import com.baeldung.spring.data.cassandra.config.CassandraConfig; import com.baeldung.spring.data.cassandra.model.Book; +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.querybuilder.QueryBuilder; +import com.datastax.driver.core.querybuilder.Select; +import com.datastax.driver.core.utils.UUIDs; +import com.google.common.collect.ImmutableSet; import org.apache.cassandra.exceptions.ConfigurationException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -30,17 +26,28 @@ import org.springframework.data.cassandra.core.CassandraOperations; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.Session; -import com.datastax.driver.core.querybuilder.QueryBuilder; -import com.datastax.driver.core.querybuilder.Select; -import com.datastax.driver.core.utils.UUIDs; -import com.google.common.collect.ImmutableSet; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import static junit.framework.TestCase.assertNull; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; + +/** + * Live test for Cassandra testing. + * + * This can be converted to IntegrationTest once cassandra-unit tests can be executed in parallel and + * multiple test servers started as part of test suite. + * + * Open cassandra-unit issue for parallel execution: https://github.com/jsevellec/cassandra-unit/issues/155 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = CassandraConfig.class) -public class CassandraTemplateIntegrationTest { - private static final Log LOGGER = LogFactory.getLog(CassandraTemplateIntegrationTest.class); +public class CassandraTemplateLiveTest { + private static final Log LOGGER = LogFactory.getLog(CassandraTemplateLiveTest.class); public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };"; @@ -54,8 +61,6 @@ public class CassandraTemplateIntegrationTest { @Autowired private CassandraOperations cassandraTemplate; - // - @BeforeClass public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException { EmbeddedCassandraServerHelper.startEmbeddedCassandra(); @@ -69,8 +74,8 @@ public class CassandraTemplateIntegrationTest { } @Before - public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException { - adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap()); + public void createTable() { + adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<>()); } @Test @@ -111,7 +116,7 @@ public class CassandraTemplateIntegrationTest { } @Test - public void whenDeletingASelectedBook_thenNotAvailableOnRetrieval() throws InterruptedException { + public void whenDeletingASelectedBook_thenNotAvailableOnRetrieval() { final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "OReilly Media", ImmutableSet.of("Computer", "Software")); cassandraTemplate.insert(javaBook); cassandraTemplate.delete(javaBook); diff --git a/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CqlQueriesIntegrationTest.java b/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CqlQueriesLiveTest.java similarity index 90% rename from persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CqlQueriesIntegrationTest.java rename to persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CqlQueriesLiveTest.java index f948218807..e1c67a1724 100644 --- a/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CqlQueriesIntegrationTest.java +++ b/persistence-modules/spring-data-cassandra/src/test/java/com/baeldung/spring/data/cassandra/repository/CqlQueriesLiveTest.java @@ -1,19 +1,16 @@ package com.baeldung.spring.data.cassandra.repository; -import static junit.framework.TestCase.assertEquals; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; - -import org.apache.cassandra.exceptions.ConfigurationException; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.thrift.transport.TTransportException; import com.baeldung.spring.data.cassandra.config.CassandraConfig; import com.baeldung.spring.data.cassandra.model.Book; +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.querybuilder.Insert; +import com.datastax.driver.core.querybuilder.QueryBuilder; +import com.datastax.driver.core.querybuilder.Select; +import com.datastax.driver.core.utils.UUIDs; +import com.google.common.collect.ImmutableSet; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.cassandraunit.utils.EmbeddedCassandraServerHelper; import org.junit.After; import org.junit.AfterClass; @@ -28,18 +25,25 @@ import org.springframework.data.cassandra.core.CassandraOperations; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.Session; -import com.datastax.driver.core.querybuilder.Insert; -import com.datastax.driver.core.querybuilder.QueryBuilder; -import com.datastax.driver.core.querybuilder.Select; -import com.datastax.driver.core.utils.UUIDs; -import com.google.common.collect.ImmutableSet; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; +import static junit.framework.TestCase.assertEquals; + +/** + * Live test for Cassandra testing. + * + * This can be converted to IntegrationTest once cassandra-unit tests can be executed in parallel and + * multiple test servers started as part of test suite. + * + * Open cassandra-unit issue for parallel execution: https://github.com/jsevellec/cassandra-unit/issues/155 + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = CassandraConfig.class) -public class CqlQueriesIntegrationTest { - private static final Log LOGGER = LogFactory.getLog(CqlQueriesIntegrationTest.class); +public class CqlQueriesLiveTest { + private static final Log LOGGER = LogFactory.getLog(CqlQueriesLiveTest.class); public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };"; @@ -53,10 +57,8 @@ public class CqlQueriesIntegrationTest { @Autowired private CassandraOperations cassandraTemplate; - // - @BeforeClass - public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException { + public static void startCassandraEmbedded() throws Exception { EmbeddedCassandraServerHelper.startEmbeddedCassandra(25000); final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build(); LOGGER.info("Server Started at 127.0.0.1:9142... "); @@ -68,8 +70,8 @@ public class CqlQueriesIntegrationTest { } @Before - public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException { - adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap()); + public void createTable() { + adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<>()); } @Test From b97323ea902e5cae9360a1b177b921d8084534d9 Mon Sep 17 00:00:00 2001 From: Jose Vinicius <42288345+josephdsbr@users.noreply.github.com> Date: Sat, 5 Feb 2022 16:22:16 -0300 Subject: [PATCH 53/63] BAEL-5124 - Adding a module with examples of printing thread info in the log file using log4j (#11704) * adding a example module for printing thread info in log file using log4j * - fix: removing submodule * - fix: Adding submodule * - fix: Removing `.gitignore`, `README.md`, and `application.properties`. - fix: Removing Spring boot and adding log4j2 from Maven Central * - fix: Changing module name as suggested to log4j2-thread-info. - fix: Fixing log path in log4j2 configuration * - feat: Removing log4j2-thread-info module - feat: Adding example code into existing log4j2 module * - fix: Removing unnecessary logging * - fix: Separating thread info in the logging pattern * - fix: package name to lowercase * lowercasing package name and variable name * changing package name in configuration * - fix: formatting * - fix: formatting imports --- .../log4j2threadinfo/Log4j2ThreadInfo.java | 18 +++++++++++ .../log4j2/src/main/resources/log4j2.xml | 30 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2threadinfo/Log4j2ThreadInfo.java create mode 100644 logging-modules/log4j2/src/main/resources/log4j2.xml diff --git a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2threadinfo/Log4j2ThreadInfo.java b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2threadinfo/Log4j2ThreadInfo.java new file mode 100644 index 0000000000..2c9b8870e2 --- /dev/null +++ b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2threadinfo/Log4j2ThreadInfo.java @@ -0,0 +1,18 @@ +package com.baeldung.logging.log4j2threadinfo; + +import java.util.stream.IntStream; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class Log4j2ThreadInfo { + private static final Logger logger = LogManager.getLogger(Log4j2ThreadInfo.class); + + public static void main(String[] args) { + IntStream.range(0, 5).forEach(i -> { + Runnable runnable = () -> logger.info("Logging info"); + Thread thread = new Thread(runnable); + thread.start(); + }); + } +} diff --git a/logging-modules/log4j2/src/main/resources/log4j2.xml b/logging-modules/log4j2/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..6c0bc75a94 --- /dev/null +++ b/logging-modules/log4j2/src/main/resources/log4j2.xml @@ -0,0 +1,30 @@ + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} --- thread_id="%tid" thread_name="%tn" thread_priority="%tp" --- [%p] %m%n + + + + + + ${LOG_PATTERN} + + + + + + + + + + + + + + + + + + \ No newline at end of file From 3f46e530e7ff0793a57a67ef6a18753591459d97 Mon Sep 17 00:00:00 2001 From: chrys exaucet Date: Sat, 5 Feb 2022 21:28:05 +0000 Subject: [PATCH 54/63] BAEL-5123: Annotation Processor for Hibernate Validator (#11673) * feat(wip): add Hibernate Validator AP example * chore: change AP version to 6.2.0.Final See https://hibernate.atlassian.net/browse/HV-1868 * revert(style): remove unnecessary formatter See https://github.com/eugenp/tutorials/pull/11673#discussion_r784444016 * fix: remove unauthorised annotation The AP was triggering a compile time error because the annotation @NotBlank is disallowed for the Profile data type. That's why the CI was failing. This customer class is unrelated to the article being written. * fix(style): revert indentation issue See https://github.com/eugenp/tutorials/pull/11673/files#r788146422 --- javaxval/pom.xml | 35 ++++++- .../container/validation/Customer.java | 1 - .../hibernate/validator/ap/Message.java | 95 +++++++++++++++++++ 3 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 javaxval/src/main/java/com/baeldung/javaxval/hibernate/validator/ap/Message.java diff --git a/javaxval/pom.xml b/javaxval/pom.xml index 57369c6f52..7d009f3280 100644 --- a/javaxval/pom.xml +++ b/javaxval/pom.xml @@ -15,7 +15,7 @@ - org.hibernate + org.hibernate.validator hibernate-validator ${hibernate-validator.version} @@ -36,10 +36,41 @@ + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.version} + + ${maven.compiler.source} + ${maven.compiler.target} + true + + -Averbose=true + -AmethodConstraintsSupported=true + -AdiagnosticKind=ERROR + + + + org.hibernate.validator + hibernate-validator-annotation-processor + ${hibernate-validator.ap.version} + + + + + + + 6.0.13.Final + 6.2.0.Final + 3.6.1 + 1.8 + 1.8 3.0.0 5.0.2.RELEASE - \ No newline at end of file + diff --git a/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java b/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java index 03811635ee..d990647721 100644 --- a/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java +++ b/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java @@ -20,7 +20,6 @@ public class Customer { @PositiveOrZero private OptionalInt numberOfOrders; - @NotBlank private Profile profile; public String getName() { diff --git a/javaxval/src/main/java/com/baeldung/javaxval/hibernate/validator/ap/Message.java b/javaxval/src/main/java/com/baeldung/javaxval/hibernate/validator/ap/Message.java new file mode 100644 index 0000000000..55d6dafad7 --- /dev/null +++ b/javaxval/src/main/java/com/baeldung/javaxval/hibernate/validator/ap/Message.java @@ -0,0 +1,95 @@ +package com.baeldung.javaxval.hibernate.validator.ap; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Past; +import java.util.List; +import java.util.Optional; + +public class Message { + + @NotNull(message = "Content cannot be null") + private String content; + + private boolean isDelivered; + + private List<@NotBlank String> recipients; + + // uncomment in order to trigger AP annotation detection + // The annotation @Past is disallowed for this data type. + // @Past + private String createdAt; + + public String getContent() { + return content; + } + + public Message(String content, boolean isDelivered, List<@NotBlank String> recipients, String createdAt) { + this.content = content; + this.isDelivered = isDelivered; + this.recipients = recipients; + this.createdAt = createdAt; + } + + // uncomment in order to trigger AP annotation detection + // The annotation @Min is disallowed for the return type of this method. + // @Min(3) + public boolean broadcast() { + // setup a logic + // to send to recipients + return true; + } + + // uncomment in order to trigger AP annotation detection + // Void methods may not be annotated with constraint annotations. + // @NotNull + public void archive() { + // archive the message + } + + // uncomment in order to trigger AP annotation detection + // Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods. + // NOTE: add -AmethodConstraintsSupported=false to compiler args before + // @AssertTrue + public boolean delete() { + // delete the message + return false; + } + + public void setContent(String content) { + this.content = content; + } + + public boolean isDelivered() { + return isDelivered; + } + + public void setDelivered(boolean delivered) { + isDelivered = delivered; + } + + public List getRecipients() { + return recipients; + } + + public void setRecipients(List recipients) { + this.recipients = recipients; + } + + public void setCreatedAt(String createdAt) { + this.createdAt = createdAt; + } + + public String getName() { + return content; + } + + public void setName(String content) { + this.content = content; + } + + public Optional<@Past String> getCreatedAt() { + return Optional.ofNullable(createdAt); + } + +} From 80c333185156d8138414d3a2910baad11f92acdb Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Sun, 6 Feb 2022 18:31:38 +0000 Subject: [PATCH 55/63] [JAVA-9806] Upgrade Maven Cargo plugin to fix error --- .../spring-security-web-login/README.md | 11 ++++++++++- .../spring-security-web-login/pom.xml | 14 +++----------- .../src/main/java/com/baeldung/AppInitializer.java | 11 +++++------ .../security/CustomAccessDeniedHandler.java | 13 +++++-------- .../CustomAuthenticationFailureHandler.java | 2 +- .../security/CustomLogoutSuccessHandler.java | 6 ------ .../security/config/SecSecurityConfig.java | 6 +----- .../baeldung/spring/ChannelSecSecurityConfig.java | 4 ---- .../main/java/com/baeldung/spring/MvcConfig.java | 6 ------ .../baeldung/spring/RedirectionSecurityConfig.java | 4 ---- 10 files changed, 25 insertions(+), 52 deletions(-) diff --git a/spring-security-modules/spring-security-web-login/README.md b/spring-security-modules/spring-security-web-login/README.md index 9521a430c2..8b9c1583f6 100644 --- a/spring-security-modules/spring-security-web-login/README.md +++ b/spring-security-modules/spring-security-web-login/README.md @@ -1,6 +1,6 @@ ## Spring Security Login -This module contains articles about login mechanisms with Spring Security +This module contains articles about login mechanisms with Spring Security. ### The Course The "Learn Spring Security" Classes: http://github.learnspringsecurity.com @@ -17,3 +17,12 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com ``` mvn clean install ``` + +### Run the Project + +- Run the application using Maven Cargo plugin. +``` +mvn cargo:run +``` +- Go to the login page at http://localhost:8082/spring-security-web-login/login.html +- Login using ```user1/user1Pass``` details. \ No newline at end of file diff --git a/spring-security-modules/spring-security-web-login/pom.xml b/spring-security-modules/spring-security-web-login/pom.xml index ac5393c1a0..3d9947c519 100644 --- a/spring-security-modules/spring-security-web-login/pom.xml +++ b/spring-security-modules/spring-security-web-login/pom.xml @@ -137,17 +137,9 @@ org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} + cargo-maven3-plugin + ${cargo-maven3-plugin.version} - true - - jetty8x - embedded - - - - 8082 @@ -160,7 +152,7 @@ - 1.6.1 + 1.9.9 \ No newline at end of file diff --git a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/AppInitializer.java b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/AppInitializer.java index 4f38d190eb..cbdf8dc684 100644 --- a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/AppInitializer.java +++ b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/AppInitializer.java @@ -1,9 +1,5 @@ package com.baeldung; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRegistration; - import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; @@ -11,10 +7,13 @@ import org.springframework.web.context.support.GenericWebApplicationContext; import org.springframework.web.filter.DelegatingFilterProxy; import org.springframework.web.servlet.DispatcherServlet; +import javax.servlet.ServletContext; +import javax.servlet.ServletRegistration; + public class AppInitializer implements WebApplicationInitializer { @Override - public void onStartup(final ServletContext sc) throws ServletException { + public void onStartup(final ServletContext sc) { AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); @@ -24,7 +23,7 @@ public class AppInitializer implements WebApplicationInitializer { ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); appServlet.setLoadOnStartup(1); appServlet.addMapping("/"); - + sc.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain")) .addMappingForUrlPatterns(null, false, "/*"); diff --git a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomAccessDeniedHandler.java b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomAccessDeniedHandler.java index 9d9fa81dc0..39355202e3 100644 --- a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomAccessDeniedHandler.java +++ b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomAccessDeniedHandler.java @@ -1,25 +1,22 @@ package com.baeldung.security; -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.springframework.security.access.AccessDeniedException; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.web.access.AccessDeniedHandler; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + public class CustomAccessDeniedHandler implements AccessDeniedHandler { public static final Logger LOG = LoggerFactory.getLogger(CustomAccessDeniedHandler.class); @Override - public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exc) throws IOException, ServletException { + public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exc) throws IOException { Authentication auth = SecurityContextHolder.getContext() .getAuthentication(); if (auth != null) { diff --git a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomAuthenticationFailureHandler.java b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomAuthenticationFailureHandler.java index 410d3f1ce9..ce3f299dac 100644 --- a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomAuthenticationFailureHandler.java +++ b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomAuthenticationFailureHandler.java @@ -13,7 +13,7 @@ import java.util.Calendar; public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler { @Override - public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException { + public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException { httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value()); String jsonPayload = "{\"message\" : \"%s\", \"timestamp\" : \"%s\" }"; diff --git a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomLogoutSuccessHandler.java b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomLogoutSuccessHandler.java index 7949eee69a..ee3bd62958 100644 --- a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomLogoutSuccessHandler.java +++ b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/CustomLogoutSuccessHandler.java @@ -12,12 +12,6 @@ import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuc public class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler implements LogoutSuccessHandler { - public CustomLogoutSuccessHandler() { - super(); - } - - // API - @Override public void onLogoutSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException, ServletException { final String refererUrl = request.getHeader("Referer"); diff --git a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/config/SecSecurityConfig.java b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/config/SecSecurityConfig.java index 15fa06aa3e..cfdaf7a13e 100644 --- a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/config/SecSecurityConfig.java +++ b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/security/config/SecSecurityConfig.java @@ -23,10 +23,6 @@ import com.baeldung.security.CustomLogoutSuccessHandler; @Profile("!https") public class SecSecurityConfig extends WebSecurityConfigurerAdapter { - public SecSecurityConfig() { - super(); - } - @Override protected void configure(final AuthenticationManagerBuilder auth) throws Exception { // @formatter:off @@ -81,7 +77,7 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter { public AuthenticationFailureHandler authenticationFailureHandler() { return new CustomAuthenticationFailureHandler(); } - + @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); diff --git a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/ChannelSecSecurityConfig.java b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/ChannelSecSecurityConfig.java index e9a6a9e120..151df18d11 100644 --- a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/ChannelSecSecurityConfig.java +++ b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/ChannelSecSecurityConfig.java @@ -17,10 +17,6 @@ import com.baeldung.security.CustomLogoutSuccessHandler; @Profile("https") public class ChannelSecSecurityConfig extends WebSecurityConfigurerAdapter { - public ChannelSecSecurityConfig() { - super(); - } - @Override protected void configure(final AuthenticationManagerBuilder auth) throws Exception { // @formatter:off diff --git a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/MvcConfig.java b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/MvcConfig.java index 082477c98c..7b27d637be 100644 --- a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/MvcConfig.java +++ b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/MvcConfig.java @@ -13,12 +13,6 @@ import org.springframework.web.servlet.view.JstlView; @Configuration public class MvcConfig implements WebMvcConfigurer { - public MvcConfig() { - super(); - } - - // API - @Override public void addViewControllers(final ViewControllerRegistry registry) { diff --git a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/RedirectionSecurityConfig.java b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/RedirectionSecurityConfig.java index 3516438a6e..b27d999d56 100644 --- a/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/RedirectionSecurityConfig.java +++ b/spring-security-modules/spring-security-web-login/src/main/java/com/baeldung/spring/RedirectionSecurityConfig.java @@ -11,10 +11,6 @@ import org.springframework.security.web.authentication.SavedRequestAwareAuthenti //@Profile("!https") public class RedirectionSecurityConfig extends WebSecurityConfigurerAdapter { - public RedirectionSecurityConfig() { - super(); - } - @Override protected void configure(final AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() From ed09368f21ea9fac5eea77db3b86f41466552d00 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Mon, 7 Feb 2022 11:37:48 +0530 Subject: [PATCH 56/63] BAEL-5327 - Set a description and an example in Swagger (#11749) * BAEL-5327 - Set a description and an example in Swagger * BAEL-5327 - two space indentation for line continuation Co-authored-by: Abhinav Pandey --- .../SwaggerExampleApplication.java | 13 ++++++ .../swaggerexample/config/SwaggerConfig.java | 38 +++++++++++++++ .../controller/ProductController.java | 34 ++++++++++++++ .../swaggerexample/entity/Product.java | 46 +++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/SwaggerExampleApplication.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/config/SwaggerConfig.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/controller/ProductController.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/entity/Product.java diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/SwaggerExampleApplication.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/SwaggerExampleApplication.java new file mode 100644 index 0000000000..4859224bb2 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/SwaggerExampleApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.swaggerexample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SwaggerExampleApplication { + + public static void main(String[] args) { + SpringApplication.run(SwaggerExampleApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/config/SwaggerConfig.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/config/SwaggerConfig.java new file mode 100644 index 0000000000..32dbe87bae --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/config/SwaggerConfig.java @@ -0,0 +1,38 @@ +package com.baeldung.swaggerexample.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +import java.util.Collections; + +@Configuration +@EnableWebMvc +public class SwaggerConfig { + + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.baeldung.swaggerexample")) + .paths(PathSelectors.any()) + .build(); + } + + private ApiInfo apiInfo() { + return new ApiInfo( + "Products API", + "API to let you add and view product", + "0.0.1", + "Terms of service", + new Contact("John Doe", "www.example.com", "myemail@company.com"), + "License of API", "API license URL", Collections.emptyList()); + } +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/controller/ProductController.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/controller/ProductController.java new file mode 100644 index 0000000000..7500c01bc0 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/controller/ProductController.java @@ -0,0 +1,34 @@ +package com.baeldung.swaggerexample.controller; + +import com.baeldung.swaggerexample.entity.Product; +import io.swagger.annotations.*; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@ApiOperation("Products API") +public class ProductController { + + @ApiOperation(value = "Create a new product", notes = "Creates a new product as per the request body") + @ApiResponses(value = { + @ApiResponse(code = 201, message = "Successfully created"), + @ApiResponse(code = 400, message = "Bad request - The product is not valid"), + @ApiResponse(code = 500, message = "Internal server error - Something went wrong") + }) + @PostMapping(value = "/products") + public ResponseEntity createProduct(@RequestBody Product product) { + return new ResponseEntity<>(HttpStatus.CREATED); + } + + @ApiOperation(value = "Get a product by id", notes = "Returns a product as per the id") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Successfully retrieved"), + @ApiResponse(code = 404, message = "Not found - The product was not found") + }) + @GetMapping("/products/{id}") + public ResponseEntity getProduct(@PathVariable("id") @ApiParam(name = "id", value = "Product id", example = "1") Long id) { + //retrieval logic + return ResponseEntity.ok(new Product(1, "Product 1", "$21.99")); + } +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/entity/Product.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/entity/Product.java new file mode 100644 index 0000000000..122addadd5 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerexample/entity/Product.java @@ -0,0 +1,46 @@ +package com.baeldung.swaggerexample.entity; + +import io.swagger.annotations.ApiModelProperty; + +import java.io.Serializable; + +public class Product implements Serializable { + @ApiModelProperty(notes = "Product ID", example = "1", required = true) + private Long id; + @ApiModelProperty(notes = "Product name", example = "Product 1", required = false) + private String name; + @ApiModelProperty(notes = "Product price", example = "$100.00", required = true) + private String price; + + // constructor and getter/setters + + public Product(long id, String name, String price) { + this.id = id; + this.name = name; + this.price = price; + } + + 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 getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } +} From e1212155fe8259d68bb9c12cfa22f466cff5a50c Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Mon, 7 Feb 2022 12:18:51 +0000 Subject: [PATCH 57/63] [JAVA-9895] Split concurrency-basic-2 module --- .../core-java-concurrency-basic-2/README.md | 3 +-- .../src/main/resources/logback.xml | 6 ----- .../core-java-concurrency-basic-3/README.md | 8 ++++++ .../core-java-concurrency-basic-3/pom.xml | 27 +++++++++++++++++++ .../interrupt/CustomInterruptedException.java | 0 .../interrupt/InterruptExample.java | 0 .../src/main/resources/logback.xml | 13 +++++++++ .../interrupt/InterruptExampleUnitTest.java | 10 +++---- core-java-modules/pom.xml | 1 + 9 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 core-java-modules/core-java-concurrency-basic-3/README.md create mode 100644 core-java-modules/core-java-concurrency-basic-3/pom.xml rename core-java-modules/{core-java-concurrency-basic-2 => core-java-concurrency-basic-3}/src/main/java/com/baeldung/concurrent/interrupt/CustomInterruptedException.java (100%) rename core-java-modules/{core-java-concurrency-basic-2 => core-java-concurrency-basic-3}/src/main/java/com/baeldung/concurrent/interrupt/InterruptExample.java (100%) create mode 100644 core-java-modules/core-java-concurrency-basic-3/src/main/resources/logback.xml rename core-java-modules/{core-java-concurrency-basic-2 => core-java-concurrency-basic-3}/src/test/java/com/baeldung/concurrent/interrupt/InterruptExampleUnitTest.java (81%) diff --git a/core-java-modules/core-java-concurrency-basic-2/README.md b/core-java-modules/core-java-concurrency-basic-2/README.md index 1dad941e57..af46046709 100644 --- a/core-java-modules/core-java-concurrency-basic-2/README.md +++ b/core-java-modules/core-java-concurrency-basic-2/README.md @@ -12,7 +12,6 @@ This module contains articles about basic Java concurrency - [Guide to AtomicMarkableReference](https://www.baeldung.com/java-atomicmarkablereference) - [Why are Local Variables Thread-Safe in Java](https://www.baeldung.com/java-local-variables-thread-safe) - [How to Stop Execution After a Certain Time in Java](https://www.baeldung.com/java-stop-execution-after-certain-time) -- [How to Handle InterruptedException in Java](https://www.baeldung.com/java-interrupted-exception) - [How to Get the Number of Threads in a Java Process](https://www.baeldung.com/java-get-number-of-threads) - [Set the Name of a Thread in Java](https://www.baeldung.com/java-set-thread-name) -- [[<-- Prev]](/core-java-modules/core-java-concurrency-basic) +- [[<-- Prev]](../core-java-concurrency-basic)[[Next -->]](../core-java-concurrency-basic-3) diff --git a/core-java-modules/core-java-concurrency-basic-2/src/main/resources/logback.xml b/core-java-modules/core-java-concurrency-basic-2/src/main/resources/logback.xml index 56af2d397e..7d900d8ea8 100644 --- a/core-java-modules/core-java-concurrency-basic-2/src/main/resources/logback.xml +++ b/core-java-modules/core-java-concurrency-basic-2/src/main/resources/logback.xml @@ -7,12 +7,6 @@ - - - - - - diff --git a/core-java-modules/core-java-concurrency-basic-3/README.md b/core-java-modules/core-java-concurrency-basic-3/README.md new file mode 100644 index 0000000000..477f37e00a --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/README.md @@ -0,0 +1,8 @@ +## Core Java Concurrency Basic + +This module contains articles about basic Java concurrency. + +### Relevant Articles: + +- [How to Handle InterruptedException in Java](https://www.baeldung.com/java-interrupted-exception) +- [[<-- Prev]](../core-java-concurrency-basic-2) diff --git a/core-java-modules/core-java-concurrency-basic-3/pom.xml b/core-java-modules/core-java-concurrency-basic-3/pom.xml new file mode 100644 index 0000000000..20615e3250 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + core-java-concurrency-basic-3 + 0.1.0-SNAPSHOT + core-java-concurrency-basic-3 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + core-java-concurrency-basic-3 + + + src/main/resources + true + + + + + diff --git a/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/CustomInterruptedException.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/interrupt/CustomInterruptedException.java similarity index 100% rename from core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/CustomInterruptedException.java rename to core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/interrupt/CustomInterruptedException.java diff --git a/core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/InterruptExample.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/interrupt/InterruptExample.java similarity index 100% rename from core-java-modules/core-java-concurrency-basic-2/src/main/java/com/baeldung/concurrent/interrupt/InterruptExample.java rename to core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/interrupt/InterruptExample.java diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/resources/logback.xml b/core-java-modules/core-java-concurrency-basic-3/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/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/core-java-modules/core-java-concurrency-basic-2/src/test/java/com/baeldung/concurrent/interrupt/InterruptExampleUnitTest.java b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/interrupt/InterruptExampleUnitTest.java similarity index 81% rename from core-java-modules/core-java-concurrency-basic-2/src/test/java/com/baeldung/concurrent/interrupt/InterruptExampleUnitTest.java rename to core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/interrupt/InterruptExampleUnitTest.java index 1ea3f9aa9b..d11ac670f3 100644 --- a/core-java-modules/core-java-concurrency-basic-2/src/test/java/com/baeldung/concurrent/interrupt/InterruptExampleUnitTest.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/interrupt/InterruptExampleUnitTest.java @@ -1,7 +1,7 @@ package com.baeldung.concurrent.interrupt; -import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -9,23 +9,23 @@ public class InterruptExampleUnitTest { @Test public void whenPropagateException_thenThrowsInterruptedException() { - assertThrows(InterruptedException.class, () -> InterruptExample.propagateException()); + assertThrows(InterruptedException.class, InterruptExample::propagateException); } @Test public void whenRestoreTheState_thenReturnsTrue() { assertTrue(InterruptExample.restoreTheState()); } - + @Test public void whenThrowCustomException_thenContainsExpectedMessage() { - Exception exception = assertThrows(CustomInterruptedException.class, () -> InterruptExample.throwCustomException()); + Exception exception = assertThrows(CustomInterruptedException.class, InterruptExample::throwCustomException); String expectedMessage = "This thread was interrupted"; String actualMessage = exception.getMessage(); assertTrue(actualMessage.contains(expectedMessage)); } - + @Test public void whenHandleWithCustomException_thenReturnsTrue() throws CustomInterruptedException{ assertTrue(InterruptExample.handleWithCustomException()); diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 809aa72966..8404d75024 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -46,6 +46,7 @@ core-java-concurrency-advanced-4 core-java-concurrency-basic core-java-concurrency-basic-2 + core-java-concurrency-basic-3 core-java-concurrency-collections core-java-concurrency-collections-2 core-java-console From 289b9c0fa8dd0f46191036e9e822d8608969a12c Mon Sep 17 00:00:00 2001 From: Sarath Chandra Date: Mon, 7 Feb 2022 23:00:56 +0530 Subject: [PATCH 58/63] [BAEL-5261] Customizing Zuul Exceptions (#11736) * [BAEL-5261] Customizing Zuul Exceptions * [BAEL-5261] Customizing Zuul Exceptions * [BAEL-5261] Code sanitizing --- .../filter/CustomZuulErrorApplication.java | 15 +++ .../zuul/filter/CustomZuulErrorFilter.java | 62 ++++++++++++ .../cloud/zuul/filter/ZuulConfiguration.java | 98 +++++++++++++++++++ .../src/main/resources/application.yml | 5 +- .../filter/CustomZuulErrorFilterLiveTest.java | 24 +++++ 5 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorApplication.java create mode 100644 spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorFilter.java create mode 100644 spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/ZuulConfiguration.java create mode 100644 spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/test/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorFilterLiveTest.java diff --git a/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorApplication.java b/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorApplication.java new file mode 100644 index 0000000000..ae8c3040ef --- /dev/null +++ b/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.cloud.zuul.filter; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; + +@EnableZuulProxy +@SpringBootApplication +public class CustomZuulErrorApplication extends SpringBootServletInitializer { + + public static void main(String[] args) { + SpringApplication.run(CustomZuulErrorApplication.class, args); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorFilter.java b/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorFilter.java new file mode 100644 index 0000000000..d3e52843eb --- /dev/null +++ b/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorFilter.java @@ -0,0 +1,62 @@ +package com.baeldung.spring.cloud.zuul.filter; + +import java.net.ConnectException; +import java.time.Instant; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.context.RequestContext; +import com.netflix.zuul.exception.ZuulException; + +@Component +public class CustomZuulErrorFilter extends ZuulFilter { + + private static final Logger log = LoggerFactory.getLogger(CustomZuulErrorFilter.class); + + private static final String RESPONSE_BODY = "{\n" + " \"timestamp\": " + "\"" + Instant.now() + .toString() + "\"" + ",\n" + " \"status\": 503,\n" + " \"error\": \"Service Unavailable\"\n" + "}"; + + @Override + public Object run() { + RequestContext context = RequestContext.getCurrentContext(); + Throwable throwable = context.getThrowable(); + + if (throwable instanceof ZuulException) { + final ZuulException zuulException = (ZuulException) throwable; + log.error("Zuul exception: " + zuulException.getMessage()); + + if (throwable.getCause().getCause().getCause() instanceof ConnectException) { + + // reset throwable to prevent further error handling in follow up filters + context.remove("throwable"); + + // set custom response attributes + context.setResponseBody(RESPONSE_BODY); + context.getResponse() + .setContentType("application/json"); + context.setResponseStatusCode(503); + } + + } + return null; + } + + @Override + public boolean shouldFilter() { + return true; + } + + @Override + public int filterOrder() { + return -1; + } + + @Override + public String filterType() { + return "error"; + } + +} diff --git a/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/ZuulConfiguration.java b/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/ZuulConfiguration.java new file mode 100644 index 0000000000..3122862050 --- /dev/null +++ b/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/java/com/baeldung/spring/cloud/zuul/filter/ZuulConfiguration.java @@ -0,0 +1,98 @@ +package com.baeldung.spring.cloud.zuul.filter; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.boot.web.servlet.error.ErrorController; +import org.springframework.cglib.proxy.Callback; +import org.springframework.cglib.proxy.CallbackFilter; +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; +import org.springframework.cglib.proxy.NoOp; +import org.springframework.cloud.netflix.zuul.filters.RouteLocator; +import org.springframework.cloud.netflix.zuul.web.ZuulController; +import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Fix for Zuul configuration with Spring Boot 2.5.x + Zuul - "NoSuchMethodError: ErrorController.getErrorPath()": + */ +@Configuration +public class ZuulConfiguration { + /** + * The path returned by ErrorController.getErrorPath() with Spring Boot < 2.5 + * (and no longer available on Spring Boot >= 2.5). + */ + private static final String ERROR_PATH = "/error"; + private static final String METHOD = "lookupHandler"; + + /** + * Constructs a new bean post-processor for Zuul. + * + * @param routeLocator the route locator. + * @param zuulController the Zuul controller. + * @param errorController the error controller. + * @return the new bean post-processor. + */ + @Bean + public ZuulPostProcessor zuulPostProcessor(@Autowired RouteLocator routeLocator, @Autowired ZuulController zuulController, @Autowired(required = false) ErrorController errorController) { + return new ZuulPostProcessor(routeLocator, zuulController, errorController); + } + + private enum LookupHandlerCallbackFilter implements CallbackFilter { + INSTANCE; + + @Override + public int accept(Method method) { + if (METHOD.equals(method.getName())) { + return 0; + } + return 1; + } + } + + private enum LookupHandlerMethodInterceptor implements MethodInterceptor { + INSTANCE; + + @Override + public Object intercept(Object target, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { + if (ERROR_PATH.equals(args[0])) { + // by entering this branch we avoid the ZuulHandlerMapping.lookupHandler method to trigger the + // NoSuchMethodError + return null; + } + return methodProxy.invokeSuper(target, args); + } + } + + private static final class ZuulPostProcessor implements BeanPostProcessor { + + private final RouteLocator routeLocator; + private final ZuulController zuulController; + private final boolean hasErrorController; + + ZuulPostProcessor(RouteLocator routeLocator, ZuulController zuulController, ErrorController errorController) { + this.routeLocator = routeLocator; + this.zuulController = zuulController; + this.hasErrorController = (errorController != null); + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (hasErrorController && (bean instanceof ZuulHandlerMapping)) { + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(ZuulHandlerMapping.class); + enhancer.setCallbackFilter(LookupHandlerCallbackFilter.INSTANCE); // only for lookupHandler + enhancer.setCallbacks(new Callback[] { LookupHandlerMethodInterceptor.INSTANCE, NoOp.INSTANCE }); + Constructor ctor = ZuulHandlerMapping.class.getConstructors()[0]; + return enhancer.create(ctor.getParameterTypes(), new Object[] { routeLocator, zuulController }); + } + return bean; + } + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/resources/application.yml b/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/resources/application.yml index 855020a40e..bd517b8a6b 100644 --- a/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/resources/application.yml +++ b/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/main/resources/application.yml @@ -1,5 +1,8 @@ zuul: + SendErrorFilter: + post: + disable: true routes: foos: path: /foos/** - url: http://localhost:8081/spring-zuul-foos-resource/foos + url: http://localhost:8081/spring-zuul-foos-resource/foos \ No newline at end of file diff --git a/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/test/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorFilterLiveTest.java b/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/test/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorFilterLiveTest.java new file mode 100644 index 0000000000..da2588966d --- /dev/null +++ b/spring-cloud/spring-cloud-zuul/spring-zuul-ui/src/test/java/com/baeldung/spring/cloud/zuul/filter/CustomZuulErrorFilterLiveTest.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.cloud.zuul.filter; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import io.restassured.RestAssured; +import io.restassured.response.Response; + +public class CustomZuulErrorFilterLiveTest { + + @Test + public void whenSendRequestWithoutService_thenCustomError() { + final Response response = RestAssured.get("http://localhost:8080/foos/1"); + assertEquals(503, response.getStatusCode()); + } + + @Test + public void whenSendRequestWithoutCustomErrorFilter_thenError() { + final Response response = RestAssured.get("http://localhost:8080/foos/1"); + assertEquals(500, response.getStatusCode()); + } + +} From 616195b6b2a3f7f787884582d2ee3f31d5f70806 Mon Sep 17 00:00:00 2001 From: chrys exaucet Date: Mon, 7 Feb 2022 20:04:22 +0000 Subject: [PATCH 59/63] Bael 5123: uncomment @NotBlank annotation (#11788) * revert: comment Hibernate Validator AP dependency See http://jira.baeldung.com/browse/BAEL-5123 * revert: restore @NotBlank annotation See http://jira.baeldung.com/browse/BAEL-5123 --- javaxval/pom.xml | 4 ++++ .../com/baeldung/javaxval/container/validation/Customer.java | 1 + 2 files changed, 5 insertions(+) diff --git a/javaxval/pom.xml b/javaxval/pom.xml index 7d009f3280..4131ddeb97 100644 --- a/javaxval/pom.xml +++ b/javaxval/pom.xml @@ -36,6 +36,8 @@ + + + 6.0.13.Final 6.2.0.Final diff --git a/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java b/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java index d990647721..03811635ee 100644 --- a/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java +++ b/javaxval/src/main/java/com/baeldung/javaxval/container/validation/Customer.java @@ -20,6 +20,7 @@ public class Customer { @PositiveOrZero private OptionalInt numberOfOrders; + @NotBlank private Profile profile; public String getName() { From a63ea1944df5e66dca00e58d4166d4f9a1309be4 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Tue, 8 Feb 2022 12:11:51 +0800 Subject: [PATCH 60/63] Update README.md --- core-java-modules/multimodulemavenproject/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/multimodulemavenproject/README.md b/core-java-modules/multimodulemavenproject/README.md index fc4ca60b6b..13d87c36ec 100644 --- a/core-java-modules/multimodulemavenproject/README.md +++ b/core-java-modules/multimodulemavenproject/README.md @@ -1,3 +1,4 @@ ## Relevant Articles - [Multi-Module Maven Application with Java Modules](https://www.baeldung.com/maven-multi-module-project-java-jpms) +- [Importing Maven Project into Eclipse](https://www.baeldung.com/maven-import-eclipse) From 1bf44ff8ef31887bc85fd77f09ad7b69b0f64216 Mon Sep 17 00:00:00 2001 From: vaibhav007jain <72961247+vaibhav007jain@users.noreply.github.com> Date: Tue, 8 Feb 2022 13:45:38 +0530 Subject: [PATCH 61/63] BAEL-5280: added code for ArrayIndexOutOfBoundsException. (#11772) * commited initial code for hexagonal architecture * Deleting to check in again * Deleing to check in again * Push first code for Hexagonal Architecture * final code with UT for JSON to Java conversion * removed hexagonal-architecture code from last commit * BEL-5071 updated README * BAEL-5071: Undo README changes and added a nested object in the JSON example. * BAEL-5071: fixed whitespace/indentation in JsonToJavaClassConversion.java * BAEL-5151: Added code for getting the first of a String * BAEL-5151: Renamed Unit Test * BAEL-5151: Moved the files from core-java-string-operations to core-java-string-operations-3 * BAEL-5151: Replaced tabs with white spaces. * BAEL-5228: Adding code for approaches to concatening null string in java * BAEL-5228: Moved file to correct folder and added a UT. * BAEL-5228: corrected import statements. * BAEL-5228: corrected last commit. * BAEL-5228: removed extra import. * BAEL-5228: renamed UT * BAEL-5280: added code for ArrayIndexOutOfBoundsException. * BAEL-5280: moved code for ArrayIndexOutOfBoundsException in a new module. * BAEL-5280: Fixed tab/spaces in pom.xml. * BAEL-5280: Fixed indentation in ArrayIndexOutOfBoundsExceptionDemoUnitTest.java. Co-authored-by: Vaibhav Jain --- .../core-java-exceptions-4/pom.xml | 47 +++++++++++++++++++ .../ArrayIndexOutOfBoundsExceptionDemo.java | 31 ++++++++++++ ...IndexOutOfBoundsExceptionDemoUnitTest.java | 34 ++++++++++++++ core-java-modules/pom.xml | 1 + 4 files changed, 113 insertions(+) create mode 100644 core-java-modules/core-java-exceptions-4/pom.xml create mode 100644 core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemo.java create mode 100644 core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemoUnitTest.java diff --git a/core-java-modules/core-java-exceptions-4/pom.xml b/core-java-modules/core-java-exceptions-4/pom.xml new file mode 100644 index 0000000000..cc81fdc40b --- /dev/null +++ b/core-java-modules/core-java-exceptions-4/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + com.baeldung.exceptions + core-java-exceptions-4 + 0.1.0-SNAPSHOT + core-java-exceptions-4 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + com.h2database + h2 + ${h2.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + + + + + + + + + + + 1.4.191 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemo.java b/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemo.java new file mode 100644 index 0000000000..6b320976a6 --- /dev/null +++ b/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemo.java @@ -0,0 +1,31 @@ +package com.baeldung.exception.arrayindexoutofbounds; + +import java.util.Arrays; +import java.util.List; + +public class ArrayIndexOutOfBoundsExceptionDemo { + + public static void main(String[] args) { + int[] numbers = new int[] { 1, 2, 3, 4, 5 }; + + getArrayElementAtIndex(numbers, 5); + getListElementAtIndex(5); + addArrayElementsUsingLoop(numbers); + } + + public static void addArrayElementsUsingLoop(int[] numbers) { + int sum = 0; + for (int i = 0; i <= numbers.length; i++) { + sum += numbers[i]; + } + } + + public static int getListElementAtIndex(int index) { + List numbersList = Arrays.asList(1, 2, 3, 4, 5); + return numbersList.get(index); + } + + public static int getArrayElementAtIndex(int[] numbers, int index) { + return numbers[index]; + } +} diff --git a/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemoUnitTest.java b/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemoUnitTest.java new file mode 100644 index 0000000000..e1ad2b8021 --- /dev/null +++ b/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/arrayindexoutofbounds/ArrayIndexOutOfBoundsExceptionDemoUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.exception.arrayindexoutofbounds; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class ArrayIndexOutOfBoundsExceptionDemoUnitTest { + + private static int[] numbers; + + @BeforeAll + public static void setUp() { + numbers = new int[] { 1, 2, 3, 4, 5 }; + } + + @Test + void givenAnArrayOfSizeFive_whenAccessedElementBeyondRange_thenShouldThrowArrayIndexOutOfBoundsException() { + assertThrows(ArrayIndexOutOfBoundsException.class, + () -> ArrayIndexOutOfBoundsExceptionDemo.addArrayElementsUsingLoop(numbers)); + } + + @Test + void givenAnArrayOfSizeFive_whenAccessedAnElementAtIndexEqualToSize_thenShouldThrowArrayIndexOutOfBoundsException() { + assertThrows(ArrayIndexOutOfBoundsException.class, + () -> ArrayIndexOutOfBoundsExceptionDemo.getArrayElementAtIndex(numbers, 5)); + } + + @Test + void givenAListReturnedByArraysAsListMethod_whenAccessedAnElementAtIndexEqualToSize_thenShouldThrowArrayIndexOutOfBoundsException() { + assertThrows(ArrayIndexOutOfBoundsException.class, + () -> ArrayIndexOutOfBoundsExceptionDemo.getListElementAtIndex(5)); + } +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 8404d75024..a25cf11454 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -56,6 +56,7 @@ core-java-exceptions core-java-exceptions-2 core-java-exceptions-3 + core-java-exceptions-4 core-java-function core-java-functional core-java-io From 61dd13d64e02b3ef8434dd3faefa87014ad69b5f Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Tue, 8 Feb 2022 15:48:06 +0100 Subject: [PATCH 62/63] init commit (#11767) --- .../controller/StudentRestController.java | 11 +++++++ .../NoConverterFoundIntegrationTest.java | 30 +++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/controller/StudentRestController.java b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/controller/StudentRestController.java index 21cb98710d..4119b624e1 100644 --- a/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/controller/StudentRestController.java +++ b/spring-boot-modules/spring-boot-data-2/src/main/java/com/baeldung/boot/noconverterfound/controller/StudentRestController.java @@ -1,5 +1,6 @@ package com.baeldung.boot.noconverterfound.controller; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -18,4 +19,14 @@ public class StudentRestController { return ResponseEntity.ok(new Student(id, "John", "Wiliams", "AA")); } + @GetMapping(value = "/student/v2/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity getV2(@PathVariable("id") int id) { + return ResponseEntity.ok(new Student(id, "Kevin", "Cruyff", "AA")); + } + + @GetMapping(value = "/student/v3/{id}", produces = MediaType.APPLICATION_XML_VALUE) + public ResponseEntity getV3(@PathVariable("id") int id) { + return ResponseEntity.ok(new Student(id, "Robert", "Miller", "BB")); + } + } diff --git a/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/noconverterfound/NoConverterFoundIntegrationTest.java b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/noconverterfound/NoConverterFoundIntegrationTest.java index f8ded91e65..6f89ef0e58 100644 --- a/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/noconverterfound/NoConverterFoundIntegrationTest.java +++ b/spring-boot-modules/spring-boot-data-2/src/test/java/com/baeldung/boot/noconverterfound/NoConverterFoundIntegrationTest.java @@ -4,6 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import org.junit.Test; import org.junit.runner.RunWith; @@ -25,16 +26,16 @@ public class NoConverterFoundIntegrationTest { /* Remove Getters from Student class to successfully run this test case * @Test public void whenGettersNotDefined_thenThrowException() throws Exception { - + String url = "/api/student/1"; - + this.mockMvc.perform(get(url)) .andExpect(status().isInternalServerError()) .andExpect(result -> assertThat(result.getResolvedException()) .isInstanceOf(HttpMessageNotWritableException.class)) .andExpect(result -> assertThat(result.getResolvedException().getMessage()) .contains("No converter found for return value of type")); - + } */ @@ -44,9 +45,28 @@ public class NoConverterFoundIntegrationTest { String url = "/api/student/2"; this.mockMvc.perform(get(url)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.firstName").value("John")); + .andExpect(status().isOk()) + .andExpect(jsonPath("$.firstName").value("John")); + } + @Test + public void whenJsonConverterIsFound_thenReturnResponse() throws Exception { + String url = "/api/student/v2/1"; + + this.mockMvc.perform(get(url)) + .andExpect(status().isOk()) + .andExpect(content().json("{'id':1,'firstName':'Kevin','lastName':'Cruyff', 'grade':'AA'}")); + } + + @Test + public void whenConverterNotFound_thenThrowException() throws Exception { + String url = "/api/student/v3/1"; + + this.mockMvc.perform(get(url)) + .andExpect(status().isInternalServerError()) + .andExpect(result -> assertThat(result.getResolvedException()).isInstanceOf(HttpMessageNotWritableException.class)) + .andExpect(result -> assertThat(result.getResolvedException() + .getMessage()).contains("No converter for [class com.baeldung.boot.noconverterfound.model.Student] with preset Content-Type")); } } From d1792871d58a5280ff18efb655a8614b97e8dc50 Mon Sep 17 00:00:00 2001 From: thibaultfaure Date: Tue, 8 Feb 2022 19:11:06 -0300 Subject: [PATCH 63/63] =?UTF-8?q?Code=20for=20the=20Spring=20@Autowired=20?= =?UTF-8?q?Field=20Null=20=E2=80=93=20Common=20Causes=20and=20Solutions=20?= =?UTF-8?q?article=20(#11792)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-5-autowiring-beans/pom.xml | 24 ++++++++++++++++ .../java/com/baeldung/autowiring/App.java | 14 ++++++++++ .../controller/CorrectController.java | 18 ++++++++++++ .../controller/FlawedController.java | 15 ++++++++++ .../autowiring/service/MyComponent.java | 10 +++++++ .../autowiring/service/MyService.java | 19 +++++++++++++ .../service/MyServiceConfiguration.java | 14 ++++++++++ .../CorrectControllerIntegrationTest.java | 23 +++++++++++++++ .../FlawedControllerIntegrationTest.java | 28 +++++++++++++++++++ 9 files changed, 165 insertions(+) create mode 100644 spring-5-autowiring-beans/pom.xml create mode 100644 spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/App.java create mode 100644 spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/controller/CorrectController.java create mode 100644 spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/controller/FlawedController.java create mode 100644 spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyComponent.java create mode 100644 spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyService.java create mode 100644 spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyServiceConfiguration.java create mode 100644 spring-5-autowiring-beans/src/test/java/com/baeldung/autowiring/controller/CorrectControllerIntegrationTest.java create mode 100644 spring-5-autowiring-beans/src/test/java/com/baeldung/autowiring/controller/FlawedControllerIntegrationTest.java diff --git a/spring-5-autowiring-beans/pom.xml b/spring-5-autowiring-beans/pom.xml new file mode 100644 index 0000000000..32b56cc9ad --- /dev/null +++ b/spring-5-autowiring-beans/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + spring-5-autowiring-beans + 0.0.1-SNAPSHOT + spring-5-autowiring-beans + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + + diff --git a/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/App.java b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/App.java new file mode 100644 index 0000000000..d2d0db7a60 --- /dev/null +++ b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/App.java @@ -0,0 +1,14 @@ +package com.baeldung.autowiring; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +public class App { + + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } + +} diff --git a/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/controller/CorrectController.java b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/controller/CorrectController.java new file mode 100644 index 0000000000..e0c0d7eeac --- /dev/null +++ b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/controller/CorrectController.java @@ -0,0 +1,18 @@ +package com.baeldung.autowiring.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; + +import com.baeldung.autowiring.service.MyService; + +@Controller +public class CorrectController { + + @Autowired + MyService myService; + + public String control() { + return myService.serve(); + } + +} diff --git a/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/controller/FlawedController.java b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/controller/FlawedController.java new file mode 100644 index 0000000000..673e686f79 --- /dev/null +++ b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/controller/FlawedController.java @@ -0,0 +1,15 @@ +package com.baeldung.autowiring.controller; + +import org.springframework.stereotype.Controller; + +import com.baeldung.autowiring.service.MyService; + +@Controller +public class FlawedController { + + public String control() { + MyService userService = new MyService(); + return userService.serve(); + } + +} diff --git a/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyComponent.java b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyComponent.java new file mode 100644 index 0000000000..c04ca3f4ba --- /dev/null +++ b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyComponent.java @@ -0,0 +1,10 @@ +package com.baeldung.autowiring.service; + +import org.springframework.stereotype.Component; + +@Component +public class MyComponent { + + public void doWork() {} + +} diff --git a/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyService.java b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyService.java new file mode 100644 index 0000000000..3443dc05de --- /dev/null +++ b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyService.java @@ -0,0 +1,19 @@ +package com.baeldung.autowiring.service; + +import org.springframework.beans.factory.annotation.Autowired; + +/** + * The bean corresponding to this class is defined in MyServiceConfiguration + * Alternatively, you could choose to decorate this class with @Component or @Service + */ +public class MyService { + + @Autowired + MyComponent myComponent; + + public String serve() { + myComponent.doWork(); + return "success"; + } + +} diff --git a/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyServiceConfiguration.java b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyServiceConfiguration.java new file mode 100644 index 0000000000..e30e4f770e --- /dev/null +++ b/spring-5-autowiring-beans/src/main/java/com/baeldung/autowiring/service/MyServiceConfiguration.java @@ -0,0 +1,14 @@ +package com.baeldung.autowiring.service; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MyServiceConfiguration { + + @Bean + MyService myService() { + return new MyService(); + } + +} diff --git a/spring-5-autowiring-beans/src/test/java/com/baeldung/autowiring/controller/CorrectControllerIntegrationTest.java b/spring-5-autowiring-beans/src/test/java/com/baeldung/autowiring/controller/CorrectControllerIntegrationTest.java new file mode 100644 index 0000000000..3807641edd --- /dev/null +++ b/spring-5-autowiring-beans/src/test/java/com/baeldung/autowiring/controller/CorrectControllerIntegrationTest.java @@ -0,0 +1,23 @@ +package com.baeldung.autowiring.controller; + +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.test.context.junit.jupiter.SpringExtension; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +@ExtendWith(SpringExtension.class) +@SpringBootTest +public class CorrectControllerIntegrationTest { + + @Autowired + CorrectController controller; + + @Test + void whenControl_ThenRunSuccessfully() { + assertDoesNotThrow(() -> controller.control()); + } + +} diff --git a/spring-5-autowiring-beans/src/test/java/com/baeldung/autowiring/controller/FlawedControllerIntegrationTest.java b/spring-5-autowiring-beans/src/test/java/com/baeldung/autowiring/controller/FlawedControllerIntegrationTest.java new file mode 100644 index 0000000000..79d446604f --- /dev/null +++ b/spring-5-autowiring-beans/src/test/java/com/baeldung/autowiring/controller/FlawedControllerIntegrationTest.java @@ -0,0 +1,28 @@ +package com.baeldung.autowiring.controller; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +@ExtendWith(SpringExtension.class) +@SpringBootTest +public class FlawedControllerIntegrationTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(FlawedControllerIntegrationTest.class); + + @Autowired + FlawedController myController; + + @Test + void whenControl_ThenThrowNullPointerException() { + NullPointerException npe = assertThrows(NullPointerException.class, () -> myController.control()); + LOGGER.error("Got a NullPointerException", npe); + } + +}