From 51be79991023cbc8d1a7ca2ed400eb2659fbf2ca Mon Sep 17 00:00:00 2001 From: Anurag Date: Fri, 22 Mar 2019 17:13:00 +0530 Subject: [PATCH 01/26] [BAEL-1951] Guide to FileChannel --- .../filechannel/FileChannelUnitTest.java | 181 ++++++++++++++++++ .../src/test/resources/test_truncate.txt | 1 + .../test_write_using_filechannel.txt | 1 + 3 files changed, 183 insertions(+) create mode 100644 core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java create mode 100644 core-java-io/src/test/resources/test_truncate.txt create mode 100644 core-java-io/src/test/resources/test_write_using_filechannel.txt diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java new file mode 100644 index 0000000000..497ce33f0d --- /dev/null +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -0,0 +1,181 @@ +package com.baeldung.filechannel; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +import org.junit.Test; + +public class FileChannelUnitTest { + @Test + public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + String expected_value = "Hello world"; + String file = "src/test/resources/test_read.in"; + RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); + int noOfBytesRead = channel.read(buff); + + while (noOfBytesRead != -1) { + if (buff.hasArray()) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + noOfBytesRead = channel.read(buff); + } + } + + assertEquals(expected_value, out.toString()); + out.close(); + channel.close(); + reader.close(); + } + + @Test + public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { + String expected_value = "Hello world"; + String file = "src/test/resources/test_read.in"; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + FileInputStream fin = new FileInputStream(file); + FileChannel channel = fin.getChannel(); + + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); + int noOfBytesRead = channel.read(buff); + + while (noOfBytesRead != -1) { + if (buff.hasArray()) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + noOfBytesRead = channel.read(buff); + } + } + + assertEquals(expected_value, out.toString()); + channel.close(); + fin.close(); + } + + @Test + public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + String input = "Hello world"; + String file = "src/test/resources/test_write_using_filechannel.txt"; + + RandomAccessFile writer = new RandomAccessFile(file, "rw"); + FileChannel channel = writer.getChannel(); + + ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); + while (buff.hasRemaining()) { + channel.write(buff); + } + channel.close(); + writer.close(); + + // verify + RandomAccessFile reader = new RandomAccessFile(file, "r"); + assertEquals(input, reader.readLine()); + reader.close(); + } + + @Test + public void whenWriteWithFileChannelUsingFileOutputStream_thenCorrect() throws IOException { + String input = "Hello world"; + String file = "src/test/resources/test_write_using_filechannel.txt"; + + FileOutputStream fout = new FileOutputStream(file); + FileChannel channel = fout.getChannel(); + + ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); + while (buff.hasRemaining()) { + channel.write(buff); + } + + channel.close(); + fout.close(); + + // verify + RandomAccessFile reader = new RandomAccessFile(file, "r"); + assertEquals(input, reader.readLine()); + reader.close(); + } + + @Test + public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { + long expected_value = 11; + String file = "src/test/resources/test_read.in"; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel(); + + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); + int noOfBytesRead = channel.read(buff); + + while (noOfBytesRead != -1) { + if (buff.hasArray()) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + noOfBytesRead = channel.read(buff); + } + } + + assertEquals(expected_value, channel.position()); + + channel.position(4); + assertEquals(4, channel.position()); + + channel.close(); + reader.close(); + } + + @Test + public void whenGetFileSize_thenCorrect() throws IOException { + long expectedSize = 11; + String file = "src/test/resources/test_read.in"; + RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel(); + + long imageFileSize = channel.size(); + assertEquals(expectedSize, imageFileSize); + + channel.close(); + reader.close(); + } + + @Test + public void whenTruncateFile_thenCorrect() throws IOException { + long expectedSize = 5; + String input = "this is a test input"; + String file = "src/test/resources/test_truncate.txt"; + + FileOutputStream fout = new FileOutputStream(file); + FileChannel channel = fout.getChannel(); + + ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); + channel.write(buff); + buff.flip(); + + channel = channel.truncate(5); + assertEquals(expectedSize, channel.size()); + + fout.close(); + channel.close(); + } +} diff --git a/core-java-io/src/test/resources/test_truncate.txt b/core-java-io/src/test/resources/test_truncate.txt new file mode 100644 index 0000000000..26d3b38cdd --- /dev/null +++ b/core-java-io/src/test/resources/test_truncate.txt @@ -0,0 +1 @@ +this \ No newline at end of file diff --git a/core-java-io/src/test/resources/test_write_using_filechannel.txt b/core-java-io/src/test/resources/test_write_using_filechannel.txt new file mode 100644 index 0000000000..70c379b63f --- /dev/null +++ b/core-java-io/src/test/resources/test_write_using_filechannel.txt @@ -0,0 +1 @@ +Hello world \ No newline at end of file From 0a542104143cbaccb2990e3c684bb45f0b729fc5 Mon Sep 17 00:00:00 2001 From: Anurag Date: Fri, 22 Mar 2019 17:48:47 +0530 Subject: [PATCH 02/26] minor fix --- .../test/java/com/baeldung/filechannel/FileChannelUnitTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index 497ce33f0d..312ec7a459 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -66,6 +66,7 @@ public class FileChannelUnitTest { } assertEquals(expected_value, out.toString()); + out.close(); channel.close(); fin.close(); } @@ -141,6 +142,7 @@ public class FileChannelUnitTest { channel.position(4); assertEquals(4, channel.position()); + out.close(); channel.close(); reader.close(); } From 19ec10a6306c56846f2ad18c957c57d1b9f78007 Mon Sep 17 00:00:00 2001 From: mprevisic Date: Sat, 23 Mar 2019 19:04:55 +0100 Subject: [PATCH 03/26] BAEL-1970 spring logging level while testing --- .../java/com/baeldung/testlog/TestLog.java | 22 ++++++ .../testloglevel/TestLogLevelApplication.java | 15 ++++ .../testloglevel/TestLogLevelController.java | 29 ++++++++ ...ltiProfileTestLogLevelIntegrationTest.java | 70 +++++++++++++++++++ .../LogbackTestLogLevelIntegrationTest.java | 70 +++++++++++++++++++ ...estLogLevelWithProfileIntegrationTest.java | 68 ++++++++++++++++++ .../application-logback-test2.properties | 1 + ...pplication-logback-testloglevel.properties | 1 + .../application-testloglevel.properties | 2 + .../test/resources/logback-multiprofile.xml | 18 +++++ .../test/resources/logback-testloglevel.xml | 13 ++++ 11 files changed, 309 insertions(+) create mode 100644 spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java create mode 100644 spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java create mode 100644 spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java create mode 100644 spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java create mode 100644 spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java create mode 100644 spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java create mode 100644 spring-boot-testing/src/test/resources/application-logback-test2.properties create mode 100644 spring-boot-testing/src/test/resources/application-logback-testloglevel.properties create mode 100644 spring-boot-testing/src/test/resources/application-testloglevel.properties create mode 100644 spring-boot-testing/src/test/resources/logback-multiprofile.xml create mode 100644 spring-boot-testing/src/test/resources/logback-testloglevel.xml diff --git a/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java b/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java new file mode 100644 index 0000000000..bc5d5700e5 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java @@ -0,0 +1,22 @@ +package com.baeldung.testlog; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TestLog { + + Logger LOG = LoggerFactory.getLogger(this.getClass()); + + public void info(String msg) { + LOG.info(msg); + } + + public void error(String msg) { + LOG.error(msg); + } + + public void trace(String msg) { + LOG.trace(msg); + } + +} diff --git a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java new file mode 100644 index 0000000000..315bd735b9 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.testloglevel; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.baeldung.boot.Application; + +@SpringBootApplication +public class TestLogLevelApplication { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java new file mode 100644 index 0000000000..7349c20b65 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java @@ -0,0 +1,29 @@ +package com.baeldung.testloglevel; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.testlog.TestLog; + +@RestController +public class TestLogLevelController { + + Logger LOG = LoggerFactory.getLogger(this.getClass()); + + @GetMapping("/testLogLevel") + public String testLogLevel() { + LOG.trace("This is a TRACE log"); + LOG.debug("This is a DEBUG log"); + LOG.info("This is an INFO log"); + LOG.error("This is an ERROR log"); + + new TestLog().trace("This is a TRACE log in another package"); + new TestLog().info("This is an INFO log in another package"); + new TestLog().error("This is an ERROR log in another package"); + + return "Added some log output to console..."; + } + +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java new file mode 100644 index 0000000000..5192a1f00e --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java @@ -0,0 +1,70 @@ +package com.baeldung.testloglevel; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class) +@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) +@ActiveProfiles("logback-test2") +public class LogbackMultiProfileTestLogLevelIntegrationTest { + + @Autowired + private TestRestTemplate restTemplate; + + @Rule + public OutputCapture outputCapture = new OutputCapture(); + + private String baseUrl = "/testLogLevel"; + + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintTraceLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("TRACE"); + } + + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenNoTraceLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("TRACE"); + } + + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintErrorLogs() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("ERROR"); + assertThatOutputContainsLogForOtherPackages("ERROR"); + } + + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } + + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } + + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } + +} diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java new file mode 100644 index 0000000000..c0571265a9 --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java @@ -0,0 +1,70 @@ +package com.baeldung.testloglevel; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, classes = TestLogLevelApplication.class) +@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) +@ActiveProfiles("logback-testloglevel") +public class LogbackTestLogLevelIntegrationTest { + + @Autowired + private TestRestTemplate restTemplate; + + @Rule + public OutputCapture outputCapture = new OutputCapture(); + + private String baseUrl = "/testLogLevel"; + + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("DEBUG"); + } + + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); + } + + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintErrorLogs() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("ERROR"); + assertThatOutputContainsLogForOtherPackages("ERROR"); + } + + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } + + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } + + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } + +} diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java new file mode 100644 index 0000000000..976e7d5f76 --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java @@ -0,0 +1,68 @@ +package com.baeldung.testloglevel; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, classes = TestLogLevelApplication.class) +@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) +@ActiveProfiles("testloglevel") +public class TestLogLevelWithProfileIntegrationTest { + + @Autowired + private TestRestTemplate restTemplate; + + @Rule + public OutputCapture outputCapture = new OutputCapture(); + + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("DEBUG"); + } + + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); + } + + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintInfoLogs() { + ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("INFO"); + assertThatOutputContainsLogForOtherPackages("INFO"); + } + + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } + + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } + + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } + +} diff --git a/spring-boot-testing/src/test/resources/application-logback-test2.properties b/spring-boot-testing/src/test/resources/application-logback-test2.properties new file mode 100644 index 0000000000..aeed46e3ca --- /dev/null +++ b/spring-boot-testing/src/test/resources/application-logback-test2.properties @@ -0,0 +1 @@ +logging.config=classpath:logback-multiprofile.xml \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties b/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties new file mode 100644 index 0000000000..81d9b2e7c6 --- /dev/null +++ b/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties @@ -0,0 +1 @@ +logging.config=classpath:logback-testloglevel.xml \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/application-testloglevel.properties b/spring-boot-testing/src/test/resources/application-testloglevel.properties new file mode 100644 index 0000000000..b5adb4cc11 --- /dev/null +++ b/spring-boot-testing/src/test/resources/application-testloglevel.properties @@ -0,0 +1,2 @@ +logging.level.com.baeldung.testloglevel=DEBUG +logging.level.root=INFO \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/logback-multiprofile.xml b/spring-boot-testing/src/test/resources/logback-multiprofile.xml new file mode 100644 index 0000000000..3711f1ec71 --- /dev/null +++ b/spring-boot-testing/src/test/resources/logback-multiprofile.xml @@ -0,0 +1,18 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/logback-testloglevel.xml b/spring-boot-testing/src/test/resources/logback-testloglevel.xml new file mode 100644 index 0000000000..b02462a155 --- /dev/null +++ b/spring-boot-testing/src/test/resources/logback-testloglevel.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file From 92b67b36a68ff098532cfaa2546791cfb91b3aed Mon Sep 17 00:00:00 2001 From: Anurag Date: Sun, 24 Mar 2019 16:12:45 +0530 Subject: [PATCH 04/26] changes in read code. --- .../baeldung/filechannel/FileChannelUnitTest.java | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index 312ec7a459..c3d7e5610c 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; +import java.nio.charset.StandardCharsets; import org.junit.Test; @@ -26,17 +27,15 @@ public class FileChannelUnitTest { bufferSize = (int) channel.size(); } ByteBuffer buff = ByteBuffer.allocate(bufferSize); - int noOfBytesRead = channel.read(buff); - while (noOfBytesRead != -1) { + while (channel.read(buff) > 0) { if (buff.hasArray()) { out.write(buff.array(), 0, buff.position()); buff.clear(); - noOfBytesRead = channel.read(buff); } } - assertEquals(expected_value, out.toString()); + assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); out.close(); channel.close(); reader.close(); @@ -55,13 +54,11 @@ public class FileChannelUnitTest { bufferSize = (int) channel.size(); } ByteBuffer buff = ByteBuffer.allocate(bufferSize); - int noOfBytesRead = channel.read(buff); - while (noOfBytesRead != -1) { + while (channel.read(buff) > 0) { if (buff.hasArray()) { out.write(buff.array(), 0, buff.position()); buff.clear(); - noOfBytesRead = channel.read(buff); } } @@ -127,13 +124,11 @@ public class FileChannelUnitTest { bufferSize = (int) channel.size(); } ByteBuffer buff = ByteBuffer.allocate(bufferSize); - int noOfBytesRead = channel.read(buff); - while (noOfBytesRead != -1) { + while (channel.read(buff) > 0) { if (buff.hasArray()) { out.write(buff.array(), 0, buff.position()); buff.clear(); - noOfBytesRead = channel.read(buff); } } From 8054742acba0410da7d599ddace1c18121aa1fad Mon Sep 17 00:00:00 2001 From: Anurag Date: Sun, 24 Mar 2019 16:20:10 +0530 Subject: [PATCH 05/26] minor fix. --- .../test/java/com/baeldung/filechannel/FileChannelUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index c3d7e5610c..35ba9aca65 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -62,7 +62,7 @@ public class FileChannelUnitTest { } } - assertEquals(expected_value, out.toString()); + assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); out.close(); channel.close(); fin.close(); From f0797a0c04e6526f4b0067d13084d0b51a423d08 Mon Sep 17 00:00:00 2001 From: Sushant Date: Thu, 28 Mar 2019 07:19:55 +0100 Subject: [PATCH 06/26] Multi set --- .../baeldung/guava/GuavaMultiSetUnitTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java diff --git a/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java b/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java new file mode 100644 index 0000000000..4f28f8ebfc --- /dev/null +++ b/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java @@ -0,0 +1,20 @@ +package org.baeldung.guava; + +import com.google.common.collect.HashMultiset; +import com.google.common.collect.Multiset; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class GuavaMultiSetUnitTest { + + @Test + public void add_multipleValues() { + Multiset bookStore = HashMultiset.create(); + bookStore.add("Potter"); + bookStore.add("Potter"); + + assertThat(bookStore.contains("Potter")).isTrue(); + assertThat(bookStore.count("Potter")).isEqualTo(2); + } +} From af98a10b23067e486ca7d149e5e93bad55801a2f Mon Sep 17 00:00:00 2001 From: Sushant Date: Thu, 28 Mar 2019 07:33:31 +0100 Subject: [PATCH 07/26] Fix --- .../src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java b/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java index 4f28f8ebfc..faadc349d0 100644 --- a/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java +++ b/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java @@ -15,6 +15,6 @@ public class GuavaMultiSetUnitTest { bookStore.add("Potter"); assertThat(bookStore.contains("Potter")).isTrue(); - assertThat(bookStore.count("Potter")).isEqualTo(2); + assertThat(bookStore.count("Potter")).isEqualTo(3); } } From 72d38fd1cac1b61fabbbdb6aa903dd5666a583ce Mon Sep 17 00:00:00 2001 From: Sushant Date: Sun, 31 Mar 2019 15:10:04 +0300 Subject: [PATCH 08/26] Add unit test --- .../baeldung/guava/GuavaMultiSetUnitTest.java | 69 ++++++++++++++++--- 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java b/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java index faadc349d0..b3f097cf9a 100644 --- a/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java +++ b/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java @@ -4,17 +4,70 @@ import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; import org.junit.Test; +import java.util.HashMap; +import java.util.Map; + import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; public class GuavaMultiSetUnitTest { - @Test - public void add_multipleValues() { - Multiset bookStore = HashMultiset.create(); - bookStore.add("Potter"); - bookStore.add("Potter"); + @Test + public void givenMultiSet_whenAddAndRemoveValues_shouldReturnCorrectCount() { + Multiset bookStore = HashMultiset.create(); + bookStore.add("Potter"); + bookStore.add("Potter"); + bookStore.add("Potter"); - assertThat(bookStore.contains("Potter")).isTrue(); - assertThat(bookStore.count("Potter")).isEqualTo(3); - } + assertThat(bookStore.contains("Potter")).isTrue(); + assertThat(bookStore.count("Potter")).isEqualTo(3); + + bookStore.remove("Potter"); + assertThat(bookStore.contains("Potter")).isTrue(); + assertThat(bookStore.count("Potter")).isEqualTo(2); + } + + @Test + public void givenMultiSet_whenSetCount_shouldReturnCorrectCount() { + Multiset bookStore = HashMultiset.create(); + bookStore.setCount("Potter", 50); //add 50 to the count + assertThat(bookStore.count("Potter")).isEqualTo(50); + } + + @Test + public void givenMultiSet_whenSettingNegativeCount_shouldThrowException() { + Multiset bookStore = HashMultiset.create(); + assertThatThrownBy(() -> bookStore.setCount("Potter", -1)).isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void givenMultiSet_whenSettingCountWithOldCount_shouldReturnCorrectValue() { + Multiset bookStore = HashMultiset.create(); + assertThat(bookStore.setCount("Potter", 0, 2)).isTrue(); + assertThat(bookStore.setCount("Potter", 50, 5)).isFalse(); + } + + @Test + public void givenMap_compareMultiSetOperations() { + Map bookStore = new HashMap<>(); + bookStore.put("Potter", 1); + bookStore.put("Potter", 2); + bookStore.put("Potter", 3); + + assertThat(bookStore.containsKey("Potter")).isTrue(); + assertThat(bookStore.get("Potter")).isEqualTo(3); + + bookStore.put("Potter", null); + assertThat(bookStore.containsKey("Potter")).isTrue(); + + bookStore.put("Potter", -1); + assertThat(bookStore.containsKey("Potter")).isTrue(); + + bookStore.put("Potter", 2); + assertThat(bookStore.containsKey("Potter")).isTrue(); + assertThat(bookStore.get("Potter")).isEqualTo(2); + + bookStore.put("Potter", 52); + assertThat(bookStore.get("Potter")).isEqualTo(52); + } } From 45bf5898d5128b12ca2d5d3a1c3323dd0bf1dab9 Mon Sep 17 00:00:00 2001 From: Sushant Date: Sun, 31 Mar 2019 17:26:08 +0300 Subject: [PATCH 09/26] Remove comment --- .../org/baeldung/guava/GuavaMultiSetUnitTest.java | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java b/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java index b3f097cf9a..3d75cc38a3 100644 --- a/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java +++ b/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java @@ -30,7 +30,7 @@ public class GuavaMultiSetUnitTest { @Test public void givenMultiSet_whenSetCount_shouldReturnCorrectCount() { Multiset bookStore = HashMultiset.create(); - bookStore.setCount("Potter", 50); //add 50 to the count + bookStore.setCount("Potter", 50); assertThat(bookStore.count("Potter")).isEqualTo(50); } @@ -50,24 +50,18 @@ public class GuavaMultiSetUnitTest { @Test public void givenMap_compareMultiSetOperations() { Map bookStore = new HashMap<>(); - bookStore.put("Potter", 1); - bookStore.put("Potter", 2); bookStore.put("Potter", 3); assertThat(bookStore.containsKey("Potter")).isTrue(); assertThat(bookStore.get("Potter")).isEqualTo(3); + bookStore.put("Potter", 2); + assertThat(bookStore.get("Potter")).isEqualTo(2); + bookStore.put("Potter", null); assertThat(bookStore.containsKey("Potter")).isTrue(); bookStore.put("Potter", -1); assertThat(bookStore.containsKey("Potter")).isTrue(); - - bookStore.put("Potter", 2); - assertThat(bookStore.containsKey("Potter")).isTrue(); - assertThat(bookStore.get("Potter")).isEqualTo(2); - - bookStore.put("Potter", 52); - assertThat(bookStore.get("Potter")).isEqualTo(52); } } From 5957f65495b2a15a41f61465006862056817928f Mon Sep 17 00:00:00 2001 From: mprevisic Date: Sat, 6 Apr 2019 20:42:44 +0200 Subject: [PATCH 10/26] BAEL-1970 improved examples after code review --- .../baeldung/component/OtherComponent.java | 19 +++++ .../java/com/baeldung/testlog/TestLog.java | 22 ------ .../testloglevel/TestLogLevelApplication.java | 8 +-- .../testloglevel/TestLogLevelController.java | 30 ++++---- .../LogbackTestLogLevelIntegrationTest.java | 4 +- ...estLogLevelWithProfileIntegrationTest.java | 70 ++++++++++--------- .../application-logback-test.properties | 1 + ...pplication-logback-testloglevel.properties | 1 - ...es => application-logging-test.properties} | 0 ...back-testloglevel.xml => logback-test.xml} | 2 +- 10 files changed, 79 insertions(+), 78 deletions(-) create mode 100644 spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java delete mode 100644 spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java create mode 100644 spring-boot-testing/src/test/resources/application-logback-test.properties delete mode 100644 spring-boot-testing/src/test/resources/application-logback-testloglevel.properties rename spring-boot-testing/src/test/resources/{application-testloglevel.properties => application-logging-test.properties} (100%) rename spring-boot-testing/src/test/resources/{logback-testloglevel.xml => logback-test.xml} (95%) diff --git a/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java b/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java new file mode 100644 index 0000000000..61f5b440c3 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java @@ -0,0 +1,19 @@ +package com.baeldung.component; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +public class OtherComponent { + + private static final Logger LOG = LoggerFactory.getLogger(OtherComponent.class); + + public void processData() { + LOG.trace("This is a TRACE log from OtherComponent"); + LOG.debug("This is a DEBUG log from OtherComponent"); + LOG.info("This is an INFO log from OtherComponent"); + LOG.error("This is an ERROR log from OtherComponent"); + } + +} diff --git a/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java b/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java deleted file mode 100644 index bc5d5700e5..0000000000 --- a/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.testlog; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class TestLog { - - Logger LOG = LoggerFactory.getLogger(this.getClass()); - - public void info(String msg) { - LOG.info(msg); - } - - public void error(String msg) { - LOG.error(msg); - } - - public void trace(String msg) { - LOG.trace(msg); - } - -} diff --git a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java index 315bd735b9..ed8218c6a3 100644 --- a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java +++ b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java @@ -5,11 +5,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import com.baeldung.boot.Application; -@SpringBootApplication +@SpringBootApplication(scanBasePackages = {"com.baeldung.testloglevel", "com.baeldung.component"}) public class TestLogLevelApplication { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } } diff --git a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java index 7349c20b65..22078562b4 100644 --- a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java +++ b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java @@ -2,28 +2,30 @@ package com.baeldung.testloglevel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import com.baeldung.testlog.TestLog; +import com.baeldung.component.OtherComponent; @RestController public class TestLogLevelController { - Logger LOG = LoggerFactory.getLogger(this.getClass()); + private static final Logger LOG = LoggerFactory.getLogger(TestLogLevelController.class); - @GetMapping("/testLogLevel") - public String testLogLevel() { - LOG.trace("This is a TRACE log"); - LOG.debug("This is a DEBUG log"); - LOG.info("This is an INFO log"); - LOG.error("This is an ERROR log"); + @Autowired + private OtherComponent otherComponent; - new TestLog().trace("This is a TRACE log in another package"); - new TestLog().info("This is an INFO log in another package"); - new TestLog().error("This is an ERROR log in another package"); + @GetMapping("/testLogLevel") + public String testLogLevel() { + LOG.trace("This is a TRACE log"); + LOG.debug("This is a DEBUG log"); + LOG.info("This is an INFO log"); + LOG.error("This is an ERROR log"); - return "Added some log output to console..."; - } + otherComponent.processData(); -} \ No newline at end of file + return "Added some log output to console..."; + } + +} diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java index c0571265a9..5fb40a2f36 100644 --- a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java @@ -17,9 +17,9 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, classes = TestLogLevelApplication.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class) @EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) -@ActiveProfiles("logback-testloglevel") +@ActiveProfiles("logback-test") public class LogbackTestLogLevelIntegrationTest { @Autowired diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java index 976e7d5f76..5609ce6c01 100644 --- a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java @@ -17,52 +17,54 @@ import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, classes = TestLogLevelApplication.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class) @EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) -@ActiveProfiles("testloglevel") +@ActiveProfiles("logging-test") public class TestLogLevelWithProfileIntegrationTest { - @Autowired - private TestRestTemplate restTemplate; + @Autowired + private TestRestTemplate restTemplate; - @Rule - public OutputCapture outputCapture = new OutputCapture(); + @Rule + public OutputCapture outputCapture = new OutputCapture(); - @Test - public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + private String baseUrl = "/testLogLevel"; - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("DEBUG"); - } + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - @Test - public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("DEBUG"); + } - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); - } + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - @Test - public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintInfoLogs() { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); + } - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("INFO"); - assertThatOutputContainsLogForOtherPackages("INFO"); - } + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintInfoLogs() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - private void assertThatOutputContainsLogForOurPackage(String level) { - assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("INFO"); + assertThatOutputContainsLogForOtherPackages("INFO"); + } - private void assertThatOutputDoesntContainLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); - } + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } - private void assertThatOutputContainsLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); - } + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } + + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } } diff --git a/spring-boot-testing/src/test/resources/application-logback-test.properties b/spring-boot-testing/src/test/resources/application-logback-test.properties new file mode 100644 index 0000000000..587302fa01 --- /dev/null +++ b/spring-boot-testing/src/test/resources/application-logback-test.properties @@ -0,0 +1 @@ +logging.config=classpath:logback-test.xml diff --git a/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties b/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties deleted file mode 100644 index 81d9b2e7c6..0000000000 --- a/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties +++ /dev/null @@ -1 +0,0 @@ -logging.config=classpath:logback-testloglevel.xml \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/application-testloglevel.properties b/spring-boot-testing/src/test/resources/application-logging-test.properties similarity index 100% rename from spring-boot-testing/src/test/resources/application-testloglevel.properties rename to spring-boot-testing/src/test/resources/application-logging-test.properties diff --git a/spring-boot-testing/src/test/resources/logback-testloglevel.xml b/spring-boot-testing/src/test/resources/logback-test.xml similarity index 95% rename from spring-boot-testing/src/test/resources/logback-testloglevel.xml rename to spring-boot-testing/src/test/resources/logback-test.xml index b02462a155..9c6df17024 100644 --- a/spring-boot-testing/src/test/resources/logback-testloglevel.xml +++ b/spring-boot-testing/src/test/resources/logback-test.xml @@ -10,4 +10,4 @@ - \ No newline at end of file + From fe53a23d41bb69d36f2ea5e7885d214b461cbd81 Mon Sep 17 00:00:00 2001 From: Anurag Goyal Date: Sun, 7 Apr 2019 16:48:07 +0530 Subject: [PATCH 11/26] updated post review comments --- .../filechannel/FileChannelUnitTest.java | 288 ++++++++++-------- 1 file changed, 156 insertions(+), 132 deletions(-) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index 35ba9aca65..37b203241b 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -1,6 +1,7 @@ package com.baeldung.filechannel; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; @@ -8,171 +9,194 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; +import java.nio.channels.OverlappingFileLockException; import java.nio.charset.StandardCharsets; import org.junit.Test; public class FileChannelUnitTest { - @Test - public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { - String expected_value = "Hello world"; - String file = "src/test/resources/test_read.in"; - RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + @Test + public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + String expected_value = "Hello world"; + String file = "src/test/resources/test_read.in"; + + try (RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream();) { - while (channel.read(buff) > 0) { - if (buff.hasArray()) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } - } + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); - out.close(); - channel.close(); - reader.close(); - } + while (channel.read(buff) > 0) { + if (buff.hasArray()) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } + } - @Test - public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { - String expected_value = "Hello world"; - String file = "src/test/resources/test_read.in"; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - FileInputStream fin = new FileInputStream(file); - FileChannel channel = fin.getChannel(); + assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); + } + } - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + + @Test + public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { + String expected_value = "Hello world"; + String file = "src/test/resources/test_read.in"; - while (channel.read(buff) > 0) { - if (buff.hasArray()) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } - } + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + FileInputStream fin = new FileInputStream(file); + FileChannel channel = fin.getChannel();) { - assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); - out.close(); - channel.close(); - fin.close(); - } + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - @Test - public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { - String input = "Hello world"; - String file = "src/test/resources/test_write_using_filechannel.txt"; + while (channel.read(buff) > 0) { + if (buff.hasArray()) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } + } - RandomAccessFile writer = new RandomAccessFile(file, "rw"); - FileChannel channel = writer.getChannel(); + assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); - ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); - while (buff.hasRemaining()) { - channel.write(buff); - } - channel.close(); - writer.close(); + } + } + + + @Test + public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { + String expected_value = "world"; + String file = "src/test/resources/test_read.in"; - // verify - RandomAccessFile reader = new RandomAccessFile(file, "r"); - assertEquals(input, reader.readLine()); - reader.close(); - } + try (RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream();) { - @Test - public void whenWriteWithFileChannelUsingFileOutputStream_thenCorrect() throws IOException { - String input = "Hello world"; - String file = "src/test/resources/test_write_using_filechannel.txt"; + MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); - FileOutputStream fout = new FileOutputStream(file); - FileChannel channel = fout.getChannel(); + if(buff.hasRemaining()) { + byte[] data = new byte[buff.remaining()]; + buff.get(data); + assertEquals(expected_value, new String(data, StandardCharsets.UTF_8)); + + } + + } + } + + + @Test + public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() + throws IOException { + String expected = "Hello world"; + String file = "src/test/resources/test_write_using_filechannel.txt"; + + try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); + FileChannel channel = writer.getChannel();){ + ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); + + if (buff.hasRemaining()) { + channel.write(buff); + } + + // verify + RandomAccessFile reader = new RandomAccessFile(file, "r"); + assertEquals(expected, reader.readLine()); + reader.close(); + } + } + + + @Test + public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { + String file = "src/test/resources/test_read.in"; - ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); - while (buff.hasRemaining()) { - channel.write(buff); - } + try (RandomAccessFile reader = new RandomAccessFile(file, "rw"); + FileChannel channel = reader.getChannel(); + FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE );){ + + + assertNotNull(fileLock); + + }catch(OverlappingFileLockException | IOException ex) { + + + } + + } + - channel.close(); - fout.close(); + @Test + public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { + long expected_value = 11; + String file = "src/test/resources/test_read.in"; - // verify - RandomAccessFile reader = new RandomAccessFile(file, "r"); - assertEquals(input, reader.readLine()); - reader.close(); - } + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel();) { - @Test - public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { - long expected_value = 11; - String file = "src/test/resources/test_read.in"; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel(); + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } - while (channel.read(buff) > 0) { - if (buff.hasArray()) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } - } + assertEquals(expected_value, channel.position()); - assertEquals(expected_value, channel.position()); + channel.position(4); + assertEquals(4, channel.position()); - channel.position(4); - assertEquals(4, channel.position()); + } + } - out.close(); - channel.close(); - reader.close(); - } + + @Test + public void whenGetFileSize_thenCorrect() throws IOException { + long expectedSize = 11; + String file = "src/test/resources/test_read.in"; + + try (RandomAccessFile reader = new RandomAccessFile(file, "r"); + FileChannel channel = reader.getChannel();) { - @Test - public void whenGetFileSize_thenCorrect() throws IOException { - long expectedSize = 11; - String file = "src/test/resources/test_read.in"; - RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel(); + long imageFileSize = channel.size(); + assertEquals(expectedSize, imageFileSize); + } - long imageFileSize = channel.size(); - assertEquals(expectedSize, imageFileSize); + } - channel.close(); - reader.close(); - } + @Test + public void whenTruncateFile_thenCorrect() throws IOException { + long expectedSize = 5; + String input = "this is a test input"; + String file = "src/test/resources/test_truncate.txt"; - @Test - public void whenTruncateFile_thenCorrect() throws IOException { - long expectedSize = 5; - String input = "this is a test input"; - String file = "src/test/resources/test_truncate.txt"; + FileOutputStream fout = new FileOutputStream(file); + FileChannel channel = fout.getChannel(); - FileOutputStream fout = new FileOutputStream(file); - FileChannel channel = fout.getChannel(); + ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); + channel.write(buff); + buff.flip(); - ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); - channel.write(buff); - buff.flip(); + channel = channel.truncate(5); + assertEquals(expectedSize, channel.size()); - channel = channel.truncate(5); - assertEquals(expectedSize, channel.size()); - - fout.close(); - channel.close(); - } + fout.close(); + channel.close(); + } } From 27559b3a7fab1bd1e97e5d3690eb07a76afe2ed0 Mon Sep 17 00:00:00 2001 From: Sushant Date: Sun, 7 Apr 2019 15:14:30 +0300 Subject: [PATCH 12/26] Split into separate test cases --- .../baeldung/guava/GuavaMultiSetUnitTest.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java b/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java index 3d75cc38a3..47411031b9 100644 --- a/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java +++ b/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java @@ -13,7 +13,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; public class GuavaMultiSetUnitTest { @Test - public void givenMultiSet_whenAddAndRemoveValues_shouldReturnCorrectCount() { + public void givenMultiSet_whenAddingValues_shouldReturnCorrectCount() { Multiset bookStore = HashMultiset.create(); bookStore.add("Potter"); bookStore.add("Potter"); @@ -21,10 +21,17 @@ public class GuavaMultiSetUnitTest { assertThat(bookStore.contains("Potter")).isTrue(); assertThat(bookStore.count("Potter")).isEqualTo(3); + } + + @Test + public void givenMultiSet_whenRemovingValues_shouldReturnCorrectCount() { + Multiset bookStore = HashMultiset.create(); + bookStore.add("Potter"); + bookStore.add("Potter"); bookStore.remove("Potter"); assertThat(bookStore.contains("Potter")).isTrue(); - assertThat(bookStore.count("Potter")).isEqualTo(2); + assertThat(bookStore.count("Potter")).isEqualTo(1); } @Test @@ -37,14 +44,32 @@ public class GuavaMultiSetUnitTest { @Test public void givenMultiSet_whenSettingNegativeCount_shouldThrowException() { Multiset bookStore = HashMultiset.create(); - assertThatThrownBy(() -> bookStore.setCount("Potter", -1)).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> bookStore.setCount("Potter", -1)) + .isInstanceOf(IllegalArgumentException.class); } @Test - public void givenMultiSet_whenSettingCountWithOldCount_shouldReturnCorrectValue() { + public void givenMultiSet_whenSettingCountWithEmptySet_shouldBeSuccessful() { Multiset bookStore = HashMultiset.create(); assertThat(bookStore.setCount("Potter", 0, 2)).isTrue(); - assertThat(bookStore.setCount("Potter", 50, 5)).isFalse(); + } + + @Test + public void givenMultiSet_whenSettingCountWithCorrectValue_shouldBeSuccessful() { + Multiset bookStore = HashMultiset.create(); + bookStore.add("Potter"); + bookStore.add("Potter"); + + assertThat(bookStore.setCount("Potter", 2, 52)).isTrue(); + } + + @Test + public void givenMultiSet_whenSettingCountWithIncorrectValue_shouldFail() { + Multiset bookStore = HashMultiset.create(); + bookStore.add("Potter"); + bookStore.add("Potter"); + + assertThat(bookStore.setCount("Potter", 5, 52)).isFalse(); } @Test From 2674f7abff53137b5680ac2d9ecc5c86bbc3e9cb Mon Sep 17 00:00:00 2001 From: mprevisic Date: Sat, 6 Apr 2019 20:45:08 +0200 Subject: [PATCH 13/26] BAEL-1970 minor log message correction --- .../main/java/com/baeldung/component/OtherComponent.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java b/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java index 61f5b440c3..97f001a9e5 100644 --- a/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java +++ b/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java @@ -10,10 +10,10 @@ public class OtherComponent { private static final Logger LOG = LoggerFactory.getLogger(OtherComponent.class); public void processData() { - LOG.trace("This is a TRACE log from OtherComponent"); - LOG.debug("This is a DEBUG log from OtherComponent"); - LOG.info("This is an INFO log from OtherComponent"); - LOG.error("This is an ERROR log from OtherComponent"); + LOG.trace("This is a TRACE log from another package"); + LOG.debug("This is a DEBUG log from another package"); + LOG.info("This is an INFO log from another package"); + LOG.error("This is an ERROR log from another package"); } } From 7186d96b12c4906243828b5875a2bc4e10e7121f Mon Sep 17 00:00:00 2001 From: mprevisic Date: Wed, 10 Apr 2019 15:28:37 +0200 Subject: [PATCH 14/26] BAEL-1970 fixed formatting (use spaces instead of tabs) --- ...ltiProfileTestLogLevelIntegrationTest.java | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java index 5192a1f00e..7a1eb4adbe 100644 --- a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java @@ -22,49 +22,49 @@ import org.springframework.test.context.junit4.SpringRunner; @ActiveProfiles("logback-test2") public class LogbackMultiProfileTestLogLevelIntegrationTest { - @Autowired - private TestRestTemplate restTemplate; + @Autowired + private TestRestTemplate restTemplate; - @Rule - public OutputCapture outputCapture = new OutputCapture(); + @Rule + public OutputCapture outputCapture = new OutputCapture(); - private String baseUrl = "/testLogLevel"; + private String baseUrl = "/testLogLevel"; - @Test - public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintTraceLogsForOurPackage() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintTraceLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("TRACE"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("TRACE"); + } - @Test - public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenNoTraceLogsForOtherPackages() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenNoTraceLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputDoesntContainLogForOtherPackages("TRACE"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("TRACE"); + } - @Test - public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintErrorLogs() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintErrorLogs() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("ERROR"); - assertThatOutputContainsLogForOtherPackages("ERROR"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("ERROR"); + assertThatOutputContainsLogForOtherPackages("ERROR"); + } - private void assertThatOutputContainsLogForOurPackage(String level) { - assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); - } + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } - private void assertThatOutputDoesntContainLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); - } + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } - private void assertThatOutputContainsLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); - } + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } } From c3bd97f39a67cb38f3adf850b1f88d3642b3ff3b Mon Sep 17 00:00:00 2001 From: mprevisic Date: Wed, 10 Apr 2019 15:52:47 +0200 Subject: [PATCH 15/26] BAEL-1970 fixed formatting in xml files --- .../test/resources/logback-multiprofile.xml | 34 +++++++++---------- .../src/test/resources/logback-test.xml | 22 ++++++------ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/spring-boot-testing/src/test/resources/logback-multiprofile.xml b/spring-boot-testing/src/test/resources/logback-multiprofile.xml index 3711f1ec71..09ca6e4cac 100644 --- a/spring-boot-testing/src/test/resources/logback-multiprofile.xml +++ b/spring-boot-testing/src/test/resources/logback-multiprofile.xml @@ -1,18 +1,18 @@ - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - - - - - - \ No newline at end of file + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + diff --git a/spring-boot-testing/src/test/resources/logback-test.xml b/spring-boot-testing/src/test/resources/logback-test.xml index 9c6df17024..312af7c2bf 100644 --- a/spring-boot-testing/src/test/resources/logback-test.xml +++ b/spring-boot-testing/src/test/resources/logback-test.xml @@ -1,13 +1,13 @@ - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + From d9540eff75f2a525ae6a73c97ba004e7a493d866 Mon Sep 17 00:00:00 2001 From: Anurag Goyal Date: Thu, 11 Apr 2019 14:52:55 +0530 Subject: [PATCH 16/26] changes for review comments --- .../filechannel/FileChannelUnitTest.java | 239 ++++++++---------- 1 file changed, 105 insertions(+), 134 deletions(-) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index 37b203241b..d883972369 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -21,10 +21,8 @@ public class FileChannelUnitTest { @Test public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { - String expected_value = "Hello world"; - String file = "src/test/resources/test_read.in"; - - try (RandomAccessFile reader = new RandomAccessFile(file, "r"); + + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); FileChannel channel = reader.getChannel(); ByteArrayOutputStream out = new ByteArrayOutputStream();) { @@ -34,130 +32,108 @@ public class FileChannelUnitTest { } ByteBuffer buff = ByteBuffer.allocate(bufferSize); - while (channel.read(buff) > 0) { - if (buff.hasArray()) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } - } - - assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); - } - } - - - @Test - public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { - String expected_value = "Hello world"; - String file = "src/test/resources/test_read.in"; - - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - FileInputStream fin = new FileInputStream(file); - FileChannel channel = fin.getChannel();) { - - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); - - while (channel.read(buff) > 0) { - if (buff.hasArray()) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } - } - - assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8)); - - } - } - - - @Test - public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { - String expected_value = "world"; - String file = "src/test/resources/test_read.in"; - - try (RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel(); - ByteArrayOutputStream out = new ByteArrayOutputStream();) { - - MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); - - if(buff.hasRemaining()) { - byte[] data = new byte[buff.remaining()]; - buff.get(data); - assertEquals(expected_value, new String(data, StandardCharsets.UTF_8)); - - } - - } - } - - - @Test - public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() - throws IOException { - String expected = "Hello world"; - String file = "src/test/resources/test_write_using_filechannel.txt"; - - try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); - FileChannel channel = writer.getChannel();){ - ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); - - if (buff.hasRemaining()) { - channel.write(buff); - } - - // verify - RandomAccessFile reader = new RandomAccessFile(file, "r"); - assertEquals(expected, reader.readLine()); - reader.close(); - } - } - - - @Test - public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { - String file = "src/test/resources/test_read.in"; - - try (RandomAccessFile reader = new RandomAccessFile(file, "rw"); - FileChannel channel = reader.getChannel(); - FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE );){ - - - assertNotNull(fileLock); - - }catch(OverlappingFileLockException | IOException ex) { - - - } - - } - - - @Test - public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { - long expected_value = 11; - String file = "src/test/resources/test_read.in"; - - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel();) { - - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); - while (channel.read(buff) > 0) { out.write(buff.array(), 0, buff.position()); buff.clear(); } - assertEquals(expected_value, channel.position()); + assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); + } + } + + @Test + public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { + + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + FileInputStream fin = new FileInputStream("src/test/resources/test_read.in"); + FileChannel channel = fin.getChannel();) { + + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); + + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } + + assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); + + } + } + + @Test + public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { + + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream();) { + + MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); + + if (buff.hasRemaining()) { + byte[] data = new byte[buff.remaining()]; + buff.get(data); + assertEquals("world", new String(data, StandardCharsets.UTF_8)); + + } + + } + } + + @Test + public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + String file = "src/test/resources/test_write_using_filechannel.txt"; + try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); + FileChannel channel = writer.getChannel();) { + ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); + + if (buff.hasRemaining()) { + channel.write(buff); + } + + // verify + RandomAccessFile reader = new RandomAccessFile(file, "r"); + assertEquals("Hello world", reader.readLine()); + reader.close(); + } + } + + @Test + public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw"); + FileChannel channel = reader.getChannel(); + FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) { + + assertNotNull(fileLock); + + } catch (OverlappingFileLockException | IOException ex) { + + } + + } + + @Test + public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { + + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel();) { + + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); + + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } + + assertEquals(11, channel.position()); channel.position(4); assertEquals(4, channel.position()); @@ -165,28 +141,23 @@ public class FileChannelUnitTest { } } - @Test public void whenGetFileSize_thenCorrect() throws IOException { - long expectedSize = 11; - String file = "src/test/resources/test_read.in"; - - try (RandomAccessFile reader = new RandomAccessFile(file, "r"); - FileChannel channel = reader.getChannel();) { + + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel();) { long imageFileSize = channel.size(); - assertEquals(expectedSize, imageFileSize); + assertEquals(11, imageFileSize); } } @Test public void whenTruncateFile_thenCorrect() throws IOException { - long expectedSize = 5; String input = "this is a test input"; - String file = "src/test/resources/test_truncate.txt"; - FileOutputStream fout = new FileOutputStream(file); + FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt"); FileChannel channel = fout.getChannel(); ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); @@ -194,7 +165,7 @@ public class FileChannelUnitTest { buff.flip(); channel = channel.truncate(5); - assertEquals(expectedSize, channel.size()); + assertEquals(5, channel.size()); fout.close(); channel.close(); From fece0dc4b8bcbb870c5a524719a30a93529deb7f Mon Sep 17 00:00:00 2001 From: Anurag Goyal Date: Thu, 11 Apr 2019 19:06:41 +0530 Subject: [PATCH 17/26] changes as per review comments --- .../filechannel/FileChannelUnitTest.java | 230 +++++++++--------- 1 file changed, 111 insertions(+), 119 deletions(-) diff --git a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java index d883972369..6964ba80e3 100644 --- a/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java +++ b/core-java-io/src/test/java/com/baeldung/filechannel/FileChannelUnitTest.java @@ -12,162 +12,154 @@ import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; -import java.nio.channels.OverlappingFileLockException; import java.nio.charset.StandardCharsets; import org.junit.Test; public class FileChannelUnitTest { - @Test - public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + @Test + public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); - FileChannel channel = reader.getChannel(); - ByteArrayOutputStream out = new ByteArrayOutputStream();) { + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream();) { - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - while (channel.read(buff) > 0) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } - assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); - } - } + String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8); - @Test - public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { + assertEquals("Hello world", fileContent); + } + } - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - FileInputStream fin = new FileInputStream("src/test/resources/test_read.in"); - FileChannel channel = fin.getChannel();) { + @Test + public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException { - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + FileInputStream fin = new FileInputStream("src/test/resources/test_read.in"); + FileChannel channel = fin.getChannel();) { - while (channel.read(buff) > 0) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8)); + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } + String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8); - } - } + assertEquals("Hello world", fileContent); + } + } - @Test - public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { + @Test + public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException { - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); - FileChannel channel = reader.getChannel(); - ByteArrayOutputStream out = new ByteArrayOutputStream();) { + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel(); + ByteArrayOutputStream out = new ByteArrayOutputStream();) { - MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); + MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5); - if (buff.hasRemaining()) { - byte[] data = new byte[buff.remaining()]; - buff.get(data); - assertEquals("world", new String(data, StandardCharsets.UTF_8)); + if (buff.hasRemaining()) { + byte[] data = new byte[buff.remaining()]; + buff.get(data); + assertEquals("world", new String(data, StandardCharsets.UTF_8)); + } + } + } - } + @Test + public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { + String file = "src/test/resources/test_write_using_filechannel.txt"; + try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); + FileChannel channel = writer.getChannel();) { + ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); - } - } + channel.write(buff); - @Test - public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException { - String file = "src/test/resources/test_write_using_filechannel.txt"; - try (RandomAccessFile writer = new RandomAccessFile(file, "rw"); - FileChannel channel = writer.getChannel();) { - ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8)); + // now we verify whether the file was written correctly + RandomAccessFile reader = new RandomAccessFile(file, "r"); + assertEquals("Hello world", reader.readLine()); + reader.close(); + } + } - if (buff.hasRemaining()) { - channel.write(buff); - } + @Test + public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw"); + FileChannel channel = reader.getChannel(); + FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) { - // verify - RandomAccessFile reader = new RandomAccessFile(file, "r"); - assertEquals("Hello world", reader.readLine()); - reader.close(); - } - } + assertNotNull(fileLock); + } + } - @Test - public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException { - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw"); - FileChannel channel = reader.getChannel(); - FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) { + @Test + public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { - assertNotNull(fileLock); + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); + RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel();) { - } catch (OverlappingFileLockException | IOException ex) { + int bufferSize = 1024; + if (bufferSize > channel.size()) { + bufferSize = (int) channel.size(); + } + ByteBuffer buff = ByteBuffer.allocate(bufferSize); - } + while (channel.read(buff) > 0) { + out.write(buff.array(), 0, buff.position()); + buff.clear(); + } - } + // the original file is 11 bytes long, so that's where the position pointer should be + assertEquals(11, channel.position()); - @Test - public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException { + channel.position(4); + assertEquals(4, channel.position()); + } + } - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); - FileChannel channel = reader.getChannel();) { + @Test + public void whenGetFileSize_thenCorrect() throws IOException { - int bufferSize = 1024; - if (bufferSize > channel.size()) { - bufferSize = (int) channel.size(); - } - ByteBuffer buff = ByteBuffer.allocate(bufferSize); + try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); + FileChannel channel = reader.getChannel();) { - while (channel.read(buff) > 0) { - out.write(buff.array(), 0, buff.position()); - buff.clear(); - } + // the original file is 11 bytes long, so that's where the position pointer should be + assertEquals(11, channel.size()); + } + } - assertEquals(11, channel.position()); + @Test + public void whenTruncateFile_thenCorrect() throws IOException { + String input = "this is a test input"; - channel.position(4); - assertEquals(4, channel.position()); + FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt"); + FileChannel channel = fout.getChannel(); - } - } + ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); + channel.write(buff); + buff.flip(); - @Test - public void whenGetFileSize_thenCorrect() throws IOException { + channel = channel.truncate(5); + assertEquals(5, channel.size()); - try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); - FileChannel channel = reader.getChannel();) { - - long imageFileSize = channel.size(); - assertEquals(11, imageFileSize); - } - - } - - @Test - public void whenTruncateFile_thenCorrect() throws IOException { - String input = "this is a test input"; - - FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt"); - FileChannel channel = fout.getChannel(); - - ByteBuffer buff = ByteBuffer.wrap(input.getBytes()); - channel.write(buff); - buff.flip(); - - channel = channel.truncate(5); - assertEquals(5, channel.size()); - - fout.close(); - channel.close(); - } + fout.close(); + channel.close(); + } } From 6ae1e85d1361b62f5762129c2664e2fbe822ea25 Mon Sep 17 00:00:00 2001 From: mprevisic Date: Fri, 12 Apr 2019 09:13:59 +0200 Subject: [PATCH 18/26] BAEL-1970 fixed formatting --- .../LogbackTestLogLevelIntegrationTest.java | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java index 5fb40a2f36..af3bafdc2e 100644 --- a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java @@ -22,49 +22,49 @@ import org.springframework.test.context.junit4.SpringRunner; @ActiveProfiles("logback-test") public class LogbackTestLogLevelIntegrationTest { - @Autowired - private TestRestTemplate restTemplate; + @Autowired + private TestRestTemplate restTemplate; - @Rule - public OutputCapture outputCapture = new OutputCapture(); + @Rule + public OutputCapture outputCapture = new OutputCapture(); - private String baseUrl = "/testLogLevel"; + private String baseUrl = "/testLogLevel"; - @Test - public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("DEBUG"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("DEBUG"); + } - @Test - public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); + } - @Test - public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintErrorLogs() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintErrorLogs() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("ERROR"); - assertThatOutputContainsLogForOtherPackages("ERROR"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("ERROR"); + assertThatOutputContainsLogForOtherPackages("ERROR"); + } - private void assertThatOutputContainsLogForOurPackage(String level) { - assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); - } + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } - private void assertThatOutputDoesntContainLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); - } + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } - private void assertThatOutputContainsLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); - } + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } } From 03a369d153874f5f89ef613691e500e8c83d4ad3 Mon Sep 17 00:00:00 2001 From: mprevisic Date: Sat, 13 Apr 2019 10:36:17 +0200 Subject: [PATCH 19/26] BAEL-1970 fixed logback base.xml path --- spring-boot-testing/src/test/resources/logback-multiprofile.xml | 2 +- spring-boot-testing/src/test/resources/logback-test.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-testing/src/test/resources/logback-multiprofile.xml b/spring-boot-testing/src/test/resources/logback-multiprofile.xml index 09ca6e4cac..be790234f2 100644 --- a/spring-boot-testing/src/test/resources/logback-multiprofile.xml +++ b/spring-boot-testing/src/test/resources/logback-multiprofile.xml @@ -1,5 +1,5 @@ - + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n diff --git a/spring-boot-testing/src/test/resources/logback-test.xml b/spring-boot-testing/src/test/resources/logback-test.xml index 312af7c2bf..0528aa88f3 100644 --- a/spring-boot-testing/src/test/resources/logback-test.xml +++ b/spring-boot-testing/src/test/resources/logback-test.xml @@ -1,5 +1,5 @@ - + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n From 431670601afc51c2ed84cc58def8dff95bf99651 Mon Sep 17 00:00:00 2001 From: Sushant Date: Sun, 14 Apr 2019 12:21:51 +0300 Subject: [PATCH 20/26] Created new module guava-collections-set --- guava-collections-set/.gitignore | 13 +++++++ guava-collections-set/pom.xml | 37 +++++++++++++++++++ .../baeldung/guava/GuavaMultiSetUnitTest.java | 2 +- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 guava-collections-set/.gitignore create mode 100644 guava-collections-set/pom.xml rename {guava-collections => guava-collections-set}/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java (99%) diff --git a/guava-collections-set/.gitignore b/guava-collections-set/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/guava-collections-set/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/guava-collections-set/pom.xml b/guava-collections-set/pom.xml new file mode 100644 index 0000000000..46dcae492d --- /dev/null +++ b/guava-collections-set/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + com.baeldung + guava-collections-set + 0.1.0-SNAPSHOT + guava-collections-set + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + guava-collections-set + + + + + 27.1-jre + + 3.6.1 + + + diff --git a/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java b/guava-collections-set/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java similarity index 99% rename from guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java rename to guava-collections-set/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java index 47411031b9..e74db29881 100644 --- a/guava-collections/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java +++ b/guava-collections-set/src/test/java/org/baeldung/guava/GuavaMultiSetUnitTest.java @@ -89,4 +89,4 @@ public class GuavaMultiSetUnitTest { bookStore.put("Potter", -1); assertThat(bookStore.containsKey("Potter")).isTrue(); } -} +} \ No newline at end of file From 15cb0aebfcec2893324b79b41d2c8d6ff667a652 Mon Sep 17 00:00:00 2001 From: Ganesh Pagade Date: Sun, 14 Apr 2019 17:42:51 +0530 Subject: [PATCH 21/26] fix spring boot project --- vaadin/pom.xml | 22 +- .../com/baeldung/introduction/VaadinUI.java | 281 ------------------ 2 files changed, 3 insertions(+), 300 deletions(-) delete mode 100644 vaadin/src/main/java/com/baeldung/introduction/VaadinUI.java diff --git a/vaadin/pom.xml b/vaadin/pom.xml index d24b3dff1b..145a6af293 100644 --- a/vaadin/pom.xml +++ b/vaadin/pom.xml @@ -33,22 +33,6 @@ javax.servlet-api provided - - com.vaadin - vaadin-server - - - com.vaadin - vaadin-push - - - com.vaadin - vaadin-client-compiled - - - com.vaadin - vaadin-themes - org.springframework.boot @@ -183,9 +167,9 @@ 3.0.1 - 7.7.10 - 8.0.6 - 10.0.1 + 10.0.11 + 10.0.11 + 10.0.11 9.3.9.v20160517 UTF-8 1.8 diff --git a/vaadin/src/main/java/com/baeldung/introduction/VaadinUI.java b/vaadin/src/main/java/com/baeldung/introduction/VaadinUI.java deleted file mode 100644 index 1b3733ad74..0000000000 --- a/vaadin/src/main/java/com/baeldung/introduction/VaadinUI.java +++ /dev/null @@ -1,281 +0,0 @@ -package com.baeldung.introduction; - -import java.time.Instant; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import javax.servlet.annotation.WebServlet; - -import com.vaadin.annotations.Push; -import com.vaadin.annotations.Theme; -import com.vaadin.annotations.VaadinServletConfiguration; -import com.vaadin.data.Validator.InvalidValueException; -import com.vaadin.data.fieldgroup.BeanFieldGroup; -import com.vaadin.data.validator.StringLengthValidator; -import com.vaadin.server.ExternalResource; -import com.vaadin.server.FontAwesome; -import com.vaadin.server.VaadinRequest; -import com.vaadin.server.VaadinServlet; -import com.vaadin.ui.Button; -import com.vaadin.ui.CheckBox; -import com.vaadin.ui.ComboBox; -import com.vaadin.ui.DateField; -import com.vaadin.ui.FormLayout; -import com.vaadin.ui.Grid; -import com.vaadin.ui.GridLayout; -import com.vaadin.ui.HorizontalLayout; -import com.vaadin.ui.InlineDateField; -import com.vaadin.ui.Label; -import com.vaadin.ui.Link; -import com.vaadin.ui.ListSelect; -import com.vaadin.ui.NativeButton; -import com.vaadin.ui.NativeSelect; -import com.vaadin.ui.Panel; -import com.vaadin.ui.PasswordField; -import com.vaadin.ui.RichTextArea; -import com.vaadin.ui.TextArea; -import com.vaadin.ui.TextField; -import com.vaadin.ui.TwinColSelect; -import com.vaadin.ui.UI; -import com.vaadin.ui.VerticalLayout; - -@SuppressWarnings("serial") -@Push -@Theme("mytheme") -public class VaadinUI extends UI { - - private Label currentTime; - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Override - protected void init(VaadinRequest vaadinRequest) { - final VerticalLayout verticalLayout = new VerticalLayout(); - verticalLayout.setSpacing(true); - verticalLayout.setMargin(true); - final GridLayout gridLayout = new GridLayout(3, 2); - gridLayout.setSpacing(true); - gridLayout.setMargin(true); - final HorizontalLayout horizontalLayout = new HorizontalLayout(); - horizontalLayout.setSpacing(true); - horizontalLayout.setMargin(true); - final FormLayout formLayout = new FormLayout(); - formLayout.setSpacing(true); - formLayout.setMargin(true); - final GridLayout buttonLayout = new GridLayout(3, 5); - buttonLayout.setMargin(true); - buttonLayout.setSpacing(true); - - final Label label = new Label(); - label.setId("Label"); - label.setValue("Label Value"); - label.setCaption("Label"); - gridLayout.addComponent(label); - - final Link link = new Link("Baeldung", new ExternalResource("http://www.baeldung.com/")); - link.setId("Link"); - link.setTargetName("_blank"); - gridLayout.addComponent(link); - - final TextField textField = new TextField(); - textField.setId("TextField"); - textField.setCaption("TextField:"); - textField.setValue("TextField Value"); - textField.setIcon(FontAwesome.USER); - gridLayout.addComponent(textField); - - final TextArea textArea = new TextArea(); - textArea.setCaption("TextArea"); - textArea.setId("TextArea"); - textArea.setValue("TextArea Value"); - gridLayout.addComponent(textArea); - - final DateField dateField = new DateField("DateField", new Date(0)); - dateField.setId("DateField"); - gridLayout.addComponent(dateField); - - final PasswordField passwordField = new PasswordField(); - passwordField.setId("PasswordField"); - passwordField.setCaption("PasswordField:"); - passwordField.setValue("password"); - gridLayout.addComponent(passwordField); - - final RichTextArea richTextArea = new RichTextArea(); - richTextArea.setCaption("Rich Text Area"); - richTextArea.setValue("

RichTextArea

"); - richTextArea.setSizeFull(); - - Panel richTextPanel = new Panel(); - richTextPanel.setContent(richTextArea); - - final InlineDateField inlineDateField = new InlineDateField(); - inlineDateField.setValue(new Date(0)); - inlineDateField.setCaption("Inline Date Field"); - horizontalLayout.addComponent(inlineDateField); - - Button normalButton = new Button("Normal Button"); - normalButton.setId("NormalButton"); - normalButton.addClickListener(e -> { - label.setValue("CLICK"); - }); - buttonLayout.addComponent(normalButton); - - Button tinyButton = new Button("Tiny Button"); - tinyButton.addStyleName("tiny"); - buttonLayout.addComponent(tinyButton); - - Button smallButton = new Button("Small Button"); - smallButton.addStyleName("small"); - buttonLayout.addComponent(smallButton); - - Button largeButton = new Button("Large Button"); - largeButton.addStyleName("large"); - buttonLayout.addComponent(largeButton); - - Button hugeButton = new Button("Huge Button"); - hugeButton.addStyleName("huge"); - buttonLayout.addComponent(hugeButton); - - Button disabledButton = new Button("Disabled Button"); - disabledButton.setDescription("This button cannot be clicked"); - disabledButton.setEnabled(false); - buttonLayout.addComponent(disabledButton); - - Button dangerButton = new Button("Danger Button"); - dangerButton.addStyleName("danger"); - buttonLayout.addComponent(dangerButton); - - Button friendlyButton = new Button("Friendly Button"); - friendlyButton.addStyleName("friendly"); - buttonLayout.addComponent(friendlyButton); - - Button primaryButton = new Button("Primary Button"); - primaryButton.addStyleName("primary"); - buttonLayout.addComponent(primaryButton); - - NativeButton nativeButton = new NativeButton("Native Button"); - buttonLayout.addComponent(nativeButton); - - Button iconButton = new Button("Icon Button"); - iconButton.setIcon(FontAwesome.ALIGN_LEFT); - buttonLayout.addComponent(iconButton); - - Button borderlessButton = new Button("BorderLess Button"); - borderlessButton.addStyleName("borderless"); - buttonLayout.addComponent(borderlessButton); - - Button linkButton = new Button("Link Button"); - linkButton.addStyleName("link"); - buttonLayout.addComponent(linkButton); - - Button quietButton = new Button("Quiet Button"); - quietButton.addStyleName("quiet"); - buttonLayout.addComponent(quietButton); - - horizontalLayout.addComponent(buttonLayout); - - final CheckBox checkbox = new CheckBox("CheckBox"); - checkbox.setValue(true); - checkbox.addValueChangeListener(e -> checkbox.setValue(!checkbox.getValue())); - formLayout.addComponent(checkbox); - - List numbers = new ArrayList(); - numbers.add("One"); - numbers.add("Ten"); - numbers.add("Eleven"); - ComboBox comboBox = new ComboBox("ComboBox"); - comboBox.addItems(numbers); - formLayout.addComponent(comboBox); - - ListSelect listSelect = new ListSelect("ListSelect"); - listSelect.addItems(numbers); - listSelect.setRows(2); - formLayout.addComponent(listSelect); - - NativeSelect nativeSelect = new NativeSelect("NativeSelect"); - nativeSelect.addItems(numbers); - formLayout.addComponent(nativeSelect); - - TwinColSelect twinColSelect = new TwinColSelect("TwinColSelect"); - twinColSelect.addItems(numbers); - - Grid grid = new Grid("Grid"); - grid.setColumns("Column1", "Column2", "Column3"); - grid.addRow("Item1", "Item2", "Item3"); - grid.addRow("Item4", "Item5", "Item6"); - - Panel panel = new Panel("Panel"); - panel.setContent(grid); - panel.setSizeUndefined(); - - Panel serverPushPanel = new Panel("Server Push"); - FormLayout timeLayout = new FormLayout(); - timeLayout.setSpacing(true); - timeLayout.setMargin(true); - currentTime = new Label("No TIME..."); - timeLayout.addComponent(currentTime); - serverPushPanel.setContent(timeLayout); - serverPushPanel.setSizeUndefined(); - ScheduledExecutorService scheduleExecutor = Executors.newScheduledThreadPool(1); - Runnable task = () -> { - currentTime.setValue("Current Time : " + Instant.now()); - }; - scheduleExecutor.scheduleWithFixedDelay(task, 0, 1, TimeUnit.SECONDS); - - FormLayout dataBindingLayout = new FormLayout(); - dataBindingLayout.setSpacing(true); - dataBindingLayout.setMargin(true); - - BindData bindData = new BindData("BindData"); - BeanFieldGroup beanFieldGroup = new BeanFieldGroup(BindData.class); - beanFieldGroup.setItemDataSource(bindData); - TextField bindedTextField = (TextField) beanFieldGroup.buildAndBind("BindName", "bindName"); - bindedTextField.setWidth("250px"); - dataBindingLayout.addComponent(bindedTextField); - - FormLayout validatorLayout = new FormLayout(); - validatorLayout.setSpacing(true); - validatorLayout.setMargin(true); - - HorizontalLayout textValidatorLayout = new HorizontalLayout(); - textValidatorLayout.setSpacing(true); - textValidatorLayout.setMargin(true); - - TextField stringValidator = new TextField(); - stringValidator.setNullSettingAllowed(true); - stringValidator.setNullRepresentation(""); - stringValidator.addValidator(new StringLengthValidator("String must have 2-5 characters lenght", 2, 5, true)); - stringValidator.setValidationVisible(false); - textValidatorLayout.addComponent(stringValidator); - Button buttonStringValidator = new Button("Validate String"); - buttonStringValidator.addClickListener(e -> { - try { - stringValidator.setValidationVisible(false); - stringValidator.validate(); - } catch (InvalidValueException err) { - stringValidator.setValidationVisible(true); - } - }); - textValidatorLayout.addComponent(buttonStringValidator); - - validatorLayout.addComponent(textValidatorLayout); - verticalLayout.addComponent(gridLayout); - verticalLayout.addComponent(richTextPanel); - verticalLayout.addComponent(horizontalLayout); - verticalLayout.addComponent(formLayout); - verticalLayout.addComponent(twinColSelect); - verticalLayout.addComponent(panel); - verticalLayout.addComponent(serverPushPanel); - verticalLayout.addComponent(dataBindingLayout); - verticalLayout.addComponent(validatorLayout); - setContent(verticalLayout); - } - - @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true) - @VaadinServletConfiguration(ui = VaadinUI.class, productionMode = false) - public static class MyUIServlet extends VaadinServlet { - } -} From bcca8479d5d2d7b315a825d1150eae595cac609b Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sun, 14 Apr 2019 22:37:03 +0530 Subject: [PATCH 22/26] [BAEL-14127] - Moved code to jackson-simple --- jackson-simple/.gitignore | 13 ++ jackson-simple/README.md | 14 ++ jackson-simple/pom.xml | 131 ++++++++++++++++++ jackson-simple/src/main/resources/logback.xml | 19 +++ .../jackson/annotation/AliasBean.java | 0 .../jackson/annotation/BeanWithCreator.java | 0 .../annotation/BeanWithCustomAnnotation.java | 0 .../jackson/annotation/BeanWithFilter.java | 0 .../jackson/annotation/BeanWithGetter.java | 0 .../jackson/annotation/BeanWithIgnore.java | 0 .../jackson/annotation/BeanWithInject.java | 0 .../jackson/annotation/ExtendableBean.java | 0 .../baeldung/jackson/annotation/MyBean.java | 0 .../jackson/annotation/PrivateBean.java | 0 .../baeldung/jackson/annotation/RawBean.java | 0 .../jackson/annotation/UnwrappedUser.java | 0 .../annotation/UserWithIgnoreType.java | 0 .../com/baeldung/jackson/annotation/Zoo.java | 0 .../jackson/bidirection/ItemWithIdentity.java | 21 +++ .../jackson/bidirection/ItemWithIgnore.java | 17 +++ .../jackson/bidirection/ItemWithRef.java | 21 +++ .../jackson/bidirection/UserWithIdentity.java | 28 ++++ .../jackson/bidirection/UserWithIgnore.java | 28 ++++ .../jackson/bidirection/UserWithRef.java | 28 ++++ .../jackson/date/CustomDateDeserializer.java | 36 +++++ .../jackson/date/CustomDateSerializer.java | 29 ++++ .../jackson/date/EventWithFormat.java | 29 ++++ .../jackson/date/EventWithSerializer.java | 31 +++++ .../ItemDeserializerOnClass.java | 41 ++++++ .../java/com/baeldung/jackson/dtos/Item.java | 32 +++++ .../jackson/dtos/ItemWithSerializer.java | 36 +++++ .../java/com/baeldung/jackson/dtos/MyDto.java | 54 ++++++++ .../jackson/dtos/MyDtoFieldNameChanged.java | 0 .../jackson/dtos/MyDtoIncludeNonDefault.java | 0 .../jackson/dtos/MyDtoNoAccessors.java | 0 .../MyDtoNoAccessorsAndFieldVisibility.java | 0 .../jackson/dtos/MyDtoWithFilter.java | 0 .../jackson/dtos/MyDtoWithSpecialField.java | 0 .../jackson/dtos/MyMixInForIgnoreType.java | 0 .../java/com/baeldung/jackson/dtos/User.java | 26 ++++ .../jackson/dtos/ignore/MyDtoIgnoreField.java | 0 .../dtos/ignore/MyDtoIgnoreFieldByName.java | 0 .../jackson/dtos/ignore/MyDtoIgnoreNull.java | 0 .../dtos/withEnum/DistanceEnumWithValue.java | 29 ++++ .../jackson/exception/UserWithRoot.java | 18 +++ .../exception/UserWithRootNamespace.java | 0 .../com/baeldung/jackson/jsonview/Item.java | 36 +++++ .../com/baeldung/jackson/jsonview/Views.java | 9 ++ .../objectmapper/CustomCarDeserializer.java | 0 .../objectmapper/CustomCarSerializer.java | 0 .../JavaReadWriteJsonExampleUnitTest.java | 0 ...izationDeserializationFeatureUnitTest.java | 0 .../jackson/objectmapper/dto/Car.java | 0 .../jackson/objectmapper/dto/Request.java | 0 .../jackson/serialization/ItemSerializer.java | 0 .../serialization/ItemSerializerOnClass.java | 32 +++++ .../serialization/MyDtoNullKeySerializer.java | 0 .../test/JacksonAnnotationUnitTest.java | 0 .../JacksonSerializationIgnoreUnitTest.java | 0 .../test/JacksonSerializationUnitTest.java | 0 jackson-simple/src/test/resources/.gitignore | 13 ++ jackson/README.md | 6 - .../baeldung/jackson/test/UnitTestSuite.java | 2 - pom.xml | 2 + 64 files changed, 773 insertions(+), 8 deletions(-) create mode 100644 jackson-simple/.gitignore create mode 100644 jackson-simple/README.md create mode 100644 jackson-simple/pom.xml create mode 100644 jackson-simple/src/main/resources/logback.xml rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/AliasBean.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/BeanWithCreator.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/BeanWithFilter.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/BeanWithGetter.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/BeanWithIgnore.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/BeanWithInject.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/ExtendableBean.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/MyBean.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/PrivateBean.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/RawBean.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/UnwrappedUser.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/annotation/Zoo.java (100%) create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithFormat.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializerOnClass.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/dtos/Item.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/dtos/ItemWithSerializer.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDto.java rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/dtos/MyDtoFieldNameChanged.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/dtos/MyDtoIncludeNonDefault.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessors.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/dtos/MyDtoWithFilter.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/dtos/MyDtoWithSpecialField.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/dtos/MyMixInForIgnoreType.java (100%) create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/dtos/User.java rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreField.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreFieldByName.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreNull.java (100%) create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithValue.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/exception/UserWithRootNamespace.java (100%) create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Item.java create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Views.java rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/objectmapper/dto/Car.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/objectmapper/dto/Request.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java (100%) create mode 100644 jackson-simple/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/test/JacksonAnnotationUnitTest.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java (100%) rename {jackson => jackson-simple}/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java (100%) create mode 100644 jackson-simple/src/test/resources/.gitignore diff --git a/jackson-simple/.gitignore b/jackson-simple/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/jackson-simple/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/jackson-simple/README.md b/jackson-simple/README.md new file mode 100644 index 0000000000..5a43bebb29 --- /dev/null +++ b/jackson-simple/README.md @@ -0,0 +1,14 @@ +========= + +## Jackson Cookbooks and Examples + +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + +### Relevant Articles: +- [Jackson Ignore Properties on Marshalling](http://www.baeldung.com/jackson-ignore-properties-on-serialization) +- [Jackson Unmarshalling json with Unknown Properties](http://www.baeldung.com/jackson-deserialize-json-unknown-properties) +- [Jackson Annotation Examples](http://www.baeldung.com/jackson-annotations) +- [Intro to the Jackson ObjectMapper](http://www.baeldung.com/jackson-object-mapper-tutorial) +- [Ignore Null Fields with Jackson](http://www.baeldung.com/jackson-ignore-null-fields) +- [Jackson – Change Name of Field](http://www.baeldung.com/jackson-name-of-property) \ No newline at end of file diff --git a/jackson-simple/pom.xml b/jackson-simple/pom.xml new file mode 100644 index 0000000000..5023ad87b6 --- /dev/null +++ b/jackson-simple/pom.xml @@ -0,0 +1,131 @@ + + 4.0.0 + jackson-simple + 0.1-SNAPSHOT + jackson-simple + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + + + + + commons-io + commons-io + ${commons-io.version} + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + ${jackson.version} + + + + com.fasterxml.jackson.datatype + jackson-datatype-joda + ${jackson.version} + + + + com.fasterxml.jackson.module + jackson-module-jsonSchema + ${jackson.version} + + + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + ${jackson.version} + + + + joda-time + joda-time + ${joda-time.version} + + + + com.google.code.gson + gson + ${gson.version} + + + + + + io.rest-assured + json-schema-validator + ${rest-assured.version} + test + + + + io.rest-assured + json-path + ${rest-assured.version} + test + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + jackson-simple + + + src/main/resources + true + + + + + + + 3.8 + 2.10 + 2.8.5 + 4.2 + + + 3.1.1 + 3.11.0 + + + diff --git a/jackson-simple/src/main/resources/logback.xml b/jackson-simple/src/main/resources/logback.xml new file mode 100644 index 0000000000..56af2d397e --- /dev/null +++ b/jackson-simple/src/main/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/AliasBean.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/AliasBean.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/AliasBean.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/AliasBean.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithCreator.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithCreator.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithCreator.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithCreator.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithCustomAnnotation.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithFilter.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithFilter.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithFilter.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithFilter.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithGetter.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithGetter.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithGetter.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithGetter.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithIgnore.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithIgnore.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithIgnore.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithIgnore.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithInject.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithInject.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/BeanWithInject.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/BeanWithInject.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/ExtendableBean.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/ExtendableBean.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/ExtendableBean.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/ExtendableBean.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/MyBean.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/MyBean.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/MyBean.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/MyBean.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/PrivateBean.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/PrivateBean.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/PrivateBean.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/PrivateBean.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/RawBean.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/RawBean.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/RawBean.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/RawBean.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/UnwrappedUser.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/UnwrappedUser.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/UnwrappedUser.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/UnwrappedUser.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/UserWithIgnoreType.java diff --git a/jackson/src/test/java/com/baeldung/jackson/annotation/Zoo.java b/jackson-simple/src/test/java/com/baeldung/jackson/annotation/Zoo.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/annotation/Zoo.java rename to jackson-simple/src/test/java/com/baeldung/jackson/annotation/Zoo.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java new file mode 100644 index 0000000000..25de4a8f7a --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIdentity.java @@ -0,0 +1,21 @@ +package com.baeldung.jackson.bidirection; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; + +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") +public class ItemWithIdentity { + public int id; + public String itemName; + public UserWithIdentity owner; + + public ItemWithIdentity() { + super(); + } + + public ItemWithIdentity(final int id, final String itemName, final UserWithIdentity owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java new file mode 100644 index 0000000000..910ccec174 --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithIgnore.java @@ -0,0 +1,17 @@ +package com.baeldung.jackson.bidirection; + +public class ItemWithIgnore { + public int id; + public String itemName; + public UserWithIgnore owner; + + public ItemWithIgnore() { + super(); + } + + public ItemWithIgnore(final int id, final String itemName, final UserWithIgnore owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java new file mode 100644 index 0000000000..0ca8d721e8 --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/ItemWithRef.java @@ -0,0 +1,21 @@ +package com.baeldung.jackson.bidirection; + +import com.fasterxml.jackson.annotation.JsonManagedReference; + +public class ItemWithRef { + public int id; + public String itemName; + + @JsonManagedReference + public UserWithRef owner; + + public ItemWithRef() { + super(); + } + + public ItemWithRef(final int id, final String itemName, final UserWithRef owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java new file mode 100644 index 0000000000..db83a09389 --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIdentity.java @@ -0,0 +1,28 @@ +package com.baeldung.jackson.bidirection; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; + +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") +public class UserWithIdentity { + public int id; + public String name; + public List userItems; + + public UserWithIdentity() { + super(); + } + + public UserWithIdentity(final int id, final String name) { + this.id = id; + this.name = name; + userItems = new ArrayList(); + } + + public void addItem(final ItemWithIdentity item) { + userItems.add(item); + } +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java new file mode 100644 index 0000000000..857a373cc5 --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithIgnore.java @@ -0,0 +1,28 @@ +package com.baeldung.jackson.bidirection; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +public class UserWithIgnore { + public int id; + public String name; + + @JsonIgnore + public List userItems; + + public UserWithIgnore() { + super(); + } + + public UserWithIgnore(final int id, final String name) { + this.id = id; + this.name = name; + userItems = new ArrayList(); + } + + public void addItem(final ItemWithIgnore item) { + userItems.add(item); + } +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java new file mode 100644 index 0000000000..3de03fc651 --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/bidirection/UserWithRef.java @@ -0,0 +1,28 @@ +package com.baeldung.jackson.bidirection; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonBackReference; + +public class UserWithRef { + public int id; + public String name; + + @JsonBackReference + public List userItems; + + public UserWithRef() { + super(); + } + + public UserWithRef(final int id, final String name) { + this.id = id; + this.name = name; + userItems = new ArrayList(); + } + + public void addItem(final ItemWithRef item) { + userItems.add(item); + } +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java b/jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java new file mode 100644 index 0000000000..90c7d9fbac --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java @@ -0,0 +1,36 @@ +package com.baeldung.jackson.date; + +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +public class CustomDateDeserializer extends StdDeserializer { + + private static final long serialVersionUID = -5451717385630622729L; + private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); + + public CustomDateDeserializer() { + this(null); + } + + public CustomDateDeserializer(final Class vc) { + super(vc); + } + + @Override + public Date deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException { + final String date = jsonparser.getText(); + try { + return formatter.parse(date); + } catch (final ParseException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java b/jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java new file mode 100644 index 0000000000..d840e1940f --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java @@ -0,0 +1,29 @@ +package com.baeldung.jackson.date; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class CustomDateSerializer extends StdSerializer { + + private static final long serialVersionUID = -2894356342227378312L; + private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); + + public CustomDateSerializer() { + this(null); + } + + public CustomDateSerializer(final Class t) { + super(t); + } + + @Override + public void serialize(final Date value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException { + gen.writeString(formatter.format(value)); + } +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithFormat.java b/jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithFormat.java new file mode 100644 index 0000000000..607e694cef --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithFormat.java @@ -0,0 +1,29 @@ +package com.baeldung.jackson.date; + +import java.util.Date; + +import com.fasterxml.jackson.annotation.JsonFormat; + +public class EventWithFormat { + public String name; + + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss") + public Date eventDate; + + public EventWithFormat() { + super(); + } + + public EventWithFormat(final String name, final Date eventDate) { + this.name = name; + this.eventDate = eventDate; + } + + public Date getEventDate() { + return eventDate; + } + + public String getName() { + return name; + } +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java b/jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java new file mode 100644 index 0000000000..c359b5c846 --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/date/EventWithSerializer.java @@ -0,0 +1,31 @@ +package com.baeldung.jackson.date; + +import java.util.Date; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +public class EventWithSerializer { + public String name; + + @JsonDeserialize(using = CustomDateDeserializer.class) + @JsonSerialize(using = CustomDateSerializer.class) + public Date eventDate; + + public EventWithSerializer() { + super(); + } + + public EventWithSerializer(final String name, final Date eventDate) { + this.name = name; + this.eventDate = eventDate; + } + + public Date getEventDate() { + return eventDate; + } + + public String getName() { + return name; + } +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializerOnClass.java b/jackson-simple/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializerOnClass.java new file mode 100644 index 0000000000..eaba9a7173 --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializerOnClass.java @@ -0,0 +1,41 @@ +package com.baeldung.jackson.deserialization; + +import java.io.IOException; + +import com.baeldung.jackson.dtos.ItemWithSerializer; +import com.baeldung.jackson.dtos.User; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.fasterxml.jackson.databind.node.IntNode; + +public class ItemDeserializerOnClass extends StdDeserializer { + + private static final long serialVersionUID = 5579141241817332594L; + + public ItemDeserializerOnClass() { + this(null); + } + + public ItemDeserializerOnClass(final Class vc) { + super(vc); + } + + /** + * {"id":1,"itemNr":"theItem","owner":2} + */ + @Override + public ItemWithSerializer deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { + final JsonNode node = jp.getCodec() + .readTree(jp); + final int id = (Integer) ((IntNode) node.get("id")).numberValue(); + final String itemName = node.get("itemName") + .asText(); + final int userId = (Integer) ((IntNode) node.get("owner")).numberValue(); + + return new ItemWithSerializer(id, itemName, new User(userId, null)); + } + +} \ No newline at end of file diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/Item.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/Item.java new file mode 100644 index 0000000000..6fce2bc88e --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/Item.java @@ -0,0 +1,32 @@ +package com.baeldung.jackson.dtos; + +public class Item { + public int id; + public String itemName; + public User owner; + + public Item() { + super(); + } + + public Item(final int id, final String itemName, final User owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } + + // API + + public int getId() { + return id; + } + + public String getItemName() { + return itemName; + } + + public User getOwner() { + return owner; + } + +} \ No newline at end of file diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/ItemWithSerializer.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/ItemWithSerializer.java new file mode 100644 index 0000000000..aea9aa770d --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/ItemWithSerializer.java @@ -0,0 +1,36 @@ +package com.baeldung.jackson.dtos; + +import com.baeldung.jackson.deserialization.ItemDeserializerOnClass; +import com.baeldung.jackson.serialization.ItemSerializerOnClass; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +@JsonSerialize(using = ItemSerializerOnClass.class) +@JsonDeserialize(using = ItemDeserializerOnClass.class) +public class ItemWithSerializer { + public final int id; + public final String itemName; + public final User owner; + + public ItemWithSerializer(final int id, final String itemName, final User owner) { + this.id = id; + this.itemName = itemName; + this.owner = owner; + } + + // API + + public int getId() { + return id; + } + + public String getItemName() { + return itemName; + } + + public User getOwner() { + return owner; + } + +} \ No newline at end of file diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDto.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDto.java new file mode 100644 index 0000000000..49cf07baea --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDto.java @@ -0,0 +1,54 @@ +package com.baeldung.jackson.dtos; + +public class MyDto { + + private String stringValue; + private int intValue; + private boolean booleanValue; + + public MyDto() { + super(); + } + + public MyDto(final String stringValue, final int intValue, final boolean booleanValue) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + } + + // API + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + public int getIntValue() { + return intValue; + } + + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + public boolean isBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(final boolean booleanValue) { + this.booleanValue = booleanValue; + } + + // + + @Override + public String toString() { + return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]"; + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoFieldNameChanged.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoFieldNameChanged.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoFieldNameChanged.java rename to jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoFieldNameChanged.java diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoIncludeNonDefault.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoIncludeNonDefault.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoIncludeNonDefault.java rename to jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoIncludeNonDefault.java diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessors.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessors.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessors.java rename to jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessors.java diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java rename to jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoNoAccessorsAndFieldVisibility.java diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoWithFilter.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoWithFilter.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoWithFilter.java rename to jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoWithFilter.java diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoWithSpecialField.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoWithSpecialField.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/dtos/MyDtoWithSpecialField.java rename to jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyDtoWithSpecialField.java diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/MyMixInForIgnoreType.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyMixInForIgnoreType.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/dtos/MyMixInForIgnoreType.java rename to jackson-simple/src/test/java/com/baeldung/jackson/dtos/MyMixInForIgnoreType.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/User.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/User.java new file mode 100644 index 0000000000..2418e8070d --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/User.java @@ -0,0 +1,26 @@ +package com.baeldung.jackson.dtos; + +public class User { + public int id; + public String name; + + public User() { + super(); + } + + public User(final int id, final String name) { + this.id = id; + this.name = name; + } + + // API + + public int getId() { + return id; + } + + public String getName() { + return name; + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreField.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreField.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreField.java rename to jackson-simple/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreField.java diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreFieldByName.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreFieldByName.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreFieldByName.java rename to jackson-simple/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreFieldByName.java diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreNull.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreNull.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreNull.java rename to jackson-simple/src/test/java/com/baeldung/jackson/dtos/ignore/MyDtoIgnoreNull.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithValue.java b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithValue.java new file mode 100644 index 0000000000..69c476d8a5 --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/dtos/withEnum/DistanceEnumWithValue.java @@ -0,0 +1,29 @@ +package com.baeldung.jackson.dtos.withEnum; + +import com.fasterxml.jackson.annotation.JsonValue; + +public enum DistanceEnumWithValue { + KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001); + + private String unit; + private final double meters; + + private DistanceEnumWithValue(String unit, double meters) { + this.unit = unit; + this.meters = meters; + } + + @JsonValue + public double getMeters() { + return meters; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + +} \ No newline at end of file diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java b/jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java new file mode 100644 index 0000000000..d879c16e6a --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRoot.java @@ -0,0 +1,18 @@ +package com.baeldung.jackson.exception; + +import com.fasterxml.jackson.annotation.JsonRootName; + +@JsonRootName(value = "user") +public class UserWithRoot { + public int id; + public String name; + + public UserWithRoot() { + super(); + } + + public UserWithRoot(final int id, final String name) { + this.id = id; + this.name = name; + } +} diff --git a/jackson/src/test/java/com/baeldung/jackson/exception/UserWithRootNamespace.java b/jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRootNamespace.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/exception/UserWithRootNamespace.java rename to jackson-simple/src/test/java/com/baeldung/jackson/exception/UserWithRootNamespace.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Item.java b/jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Item.java new file mode 100644 index 0000000000..26d20d4847 --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Item.java @@ -0,0 +1,36 @@ +package com.baeldung.jackson.jsonview; + +import com.fasterxml.jackson.annotation.JsonView; + +public class Item { + @JsonView(Views.Public.class) + public int id; + + @JsonView(Views.Public.class) + public String itemName; + + @JsonView(Views.Internal.class) + public String ownerName; + + public Item() { + super(); + } + + public Item(final int id, final String itemName, final String ownerName) { + this.id = id; + this.itemName = itemName; + this.ownerName = ownerName; + } + + public int getId() { + return id; + } + + public String getItemName() { + return itemName; + } + + public String getOwnerName() { + return ownerName; + } +} diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Views.java b/jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Views.java new file mode 100644 index 0000000000..65950b7f9f --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/jsonview/Views.java @@ -0,0 +1,9 @@ +package com.baeldung.jackson.jsonview; + +public class Views { + public static class Public { + } + + public static class Internal extends Public { + } +} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java b/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java rename to jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java b/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java rename to jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java b/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java rename to jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java b/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java rename to jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/SerializationDeserializationFeatureUnitTest.java diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/dto/Car.java b/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/dto/Car.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/objectmapper/dto/Car.java rename to jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/dto/Car.java diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/dto/Request.java b/jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/dto/Request.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/objectmapper/dto/Request.java rename to jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/dto/Request.java diff --git a/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java b/jackson-simple/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java rename to jackson-simple/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java diff --git a/jackson-simple/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java b/jackson-simple/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java new file mode 100644 index 0000000000..1fdf44e17c --- /dev/null +++ b/jackson-simple/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java @@ -0,0 +1,32 @@ +package com.baeldung.jackson.serialization; + +import java.io.IOException; + +import com.baeldung.jackson.dtos.ItemWithSerializer; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class ItemSerializerOnClass extends StdSerializer { + + private static final long serialVersionUID = -1760959597313610409L; + + public ItemSerializerOnClass() { + this(null); + } + + public ItemSerializerOnClass(final Class t) { + super(t); + } + + @Override + public final void serialize(final ItemWithSerializer value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { + jgen.writeStartObject(); + jgen.writeNumberField("id", value.id); + jgen.writeStringField("itemName", value.itemName); + jgen.writeNumberField("owner", value.owner.id); + jgen.writeEndObject(); + } + +} \ No newline at end of file diff --git a/jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java b/jackson-simple/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java rename to jackson-simple/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java diff --git a/jackson/src/test/java/com/baeldung/jackson/test/JacksonAnnotationUnitTest.java b/jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonAnnotationUnitTest.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/test/JacksonAnnotationUnitTest.java rename to jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonAnnotationUnitTest.java diff --git a/jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java b/jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java rename to jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java diff --git a/jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java b/jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java similarity index 100% rename from jackson/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java rename to jackson-simple/src/test/java/com/baeldung/jackson/test/JacksonSerializationUnitTest.java diff --git a/jackson-simple/src/test/resources/.gitignore b/jackson-simple/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/jackson-simple/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/jackson/README.md b/jackson/README.md index eeb8f1b874..794ddc04d9 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -6,9 +6,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: -- [Jackson Ignore Properties on Marshalling](http://www.baeldung.com/jackson-ignore-properties-on-serialization) - [Jackson – Unmarshall to Collection/Array](http://www.baeldung.com/jackson-collection-array) -- [Jackson Unmarshalling json with Unknown Properties](http://www.baeldung.com/jackson-deserialize-json-unknown-properties) - [Jackson – Custom Serializer](http://www.baeldung.com/jackson-custom-serialization) - [Getting Started with Custom Deserialization in Jackson](http://www.baeldung.com/jackson-deserialization) - [Jackson Exceptions – Problems and Solutions](http://www.baeldung.com/jackson-exception) @@ -17,10 +15,8 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Jackson JSON Tutorial](http://www.baeldung.com/jackson) - [Jackson – Working with Maps and nulls](http://www.baeldung.com/jackson-map-null-values-or-null-key) - [Jackson – Decide What Fields Get Serialized/Deserialized](http://www.baeldung.com/jackson-field-serializable-deserializable-or-not) -- [Jackson Annotation Examples](http://www.baeldung.com/jackson-annotations) - [Working with Tree Model Nodes in Jackson](http://www.baeldung.com/jackson-json-node-tree-model) - [Jackson vs Gson](http://www.baeldung.com/jackson-vs-gson) -- [Intro to the Jackson ObjectMapper](http://www.baeldung.com/jackson-object-mapper-tutorial) - [XML Serialization and Deserialization with Jackson](http://www.baeldung.com/jackson-xml-serialization-and-deserialization) - [More Jackson Annotations](http://www.baeldung.com/jackson-advanced-annotations) - [Inheritance with Jackson](http://www.baeldung.com/jackson-inheritance) @@ -31,9 +27,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Jackson – JsonMappingException (No serializer found for class)](http://www.baeldung.com/jackson-jsonmappingexception) - [How To Serialize Enums as JSON Objects with Jackson](http://www.baeldung.com/jackson-serialize-enums) - [Jackson – Marshall String to JsonNode](http://www.baeldung.com/jackson-json-to-jsonnode) -- [Ignore Null Fields with Jackson](http://www.baeldung.com/jackson-ignore-null-fields) - [Jackson – Unmarshall to Collection/Array](http://www.baeldung.com/jackson-collection-array) -- [Jackson – Change Name of Field](http://www.baeldung.com/jackson-name-of-property) - [Serialize Only Fields that meet a Custom Criteria with Jackson](http://www.baeldung.com/jackson-serialize-field-custom-criteria) - [Mapping Nested Values with Jackson](http://www.baeldung.com/jackson-nested-values) - [Convert XML to JSON Using Jackson](https://www.baeldung.com/jackson-convert-xml-json) diff --git a/jackson/src/test/java/com/baeldung/jackson/test/UnitTestSuite.java b/jackson/src/test/java/com/baeldung/jackson/test/UnitTestSuite.java index 6be2f29baa..e783c67f5b 100644 --- a/jackson/src/test/java/com/baeldung/jackson/test/UnitTestSuite.java +++ b/jackson/src/test/java/com/baeldung/jackson/test/UnitTestSuite.java @@ -12,8 +12,6 @@ import org.junit.runners.Suite; ,JacksonDeserializationUnitTest.class ,JacksonDeserializationUnitTest.class ,JacksonPrettyPrintUnitTest.class - ,JacksonSerializationIgnoreUnitTest.class - ,JacksonSerializationUnitTest.class ,SandboxUnitTest.class ,JacksonFieldUnitTest.class }) // @formatter:on diff --git a/pom.xml b/pom.xml index d57fa1694e..0559985fca 100644 --- a/pom.xml +++ b/pom.xml @@ -443,6 +443,7 @@ jackson jackson-2 + jackson-simple java-collections-conversions java-collections-maps java-collections-maps-2 @@ -1101,6 +1102,7 @@ jackson jackson-2 + jackson-simple java-collections-conversions java-collections-maps java-collections-maps-2 From 6b00fadcb24e7916148d432c7bdc656c23978e88 Mon Sep 17 00:00:00 2001 From: Dave Crane Date: Tue, 16 Apr 2019 14:01:33 +0100 Subject: [PATCH 23/26] moved spring core to spring 5 parent, all tests still pass --- spring-core/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-core/pom.xml b/spring-core/pom.xml index d348d742e7..814addecdd 100644 --- a/spring-core/pom.xml +++ b/spring-core/pom.xml @@ -10,9 +10,9 @@ com.baeldung - parent-spring-4 + parent-spring-5 0.0.1-SNAPSHOT - ../parent-spring-4 + ../parent-spring-5 From e8d15ceadcffe4f2f92b831eacb5083132c51bbe Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Wed, 17 Apr 2019 21:51:12 +0300 Subject: [PATCH 24/26] Update README.md --- jackson-simple/README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/jackson-simple/README.md b/jackson-simple/README.md index 5a43bebb29..be647e22d5 100644 --- a/jackson-simple/README.md +++ b/jackson-simple/README.md @@ -1,6 +1,5 @@ ========= - -## Jackson Cookbooks and Examples +### Jackson Articles that are also part of the e-book ###The Course The "REST With Spring" Classes: http://bit.ly/restwithspring @@ -11,4 +10,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Jackson Annotation Examples](http://www.baeldung.com/jackson-annotations) - [Intro to the Jackson ObjectMapper](http://www.baeldung.com/jackson-object-mapper-tutorial) - [Ignore Null Fields with Jackson](http://www.baeldung.com/jackson-ignore-null-fields) -- [Jackson – Change Name of Field](http://www.baeldung.com/jackson-name-of-property) \ No newline at end of file +- [Jackson – Change Name of Field](http://www.baeldung.com/jackson-name-of-property) From 2b0d0665ba7b6fb770da96d9ae71263e4ed191f6 Mon Sep 17 00:00:00 2001 From: Seun Matt Date: Thu, 18 Apr 2019 03:29:57 +0100 Subject: [PATCH 25/26] updated the example code with data.sql (#6749) * added example code for BAEL-2083 * updated example code for BAEL-2083 * added example code for BAEL-2849 * updated the example code with data.sql --- .../src/main/resources/data.sql | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 persistence-modules/spring-boot-h2/spring-boot-h2-database/src/main/resources/data.sql diff --git a/persistence-modules/spring-boot-h2/spring-boot-h2-database/src/main/resources/data.sql b/persistence-modules/spring-boot-h2/spring-boot-h2-database/src/main/resources/data.sql new file mode 100644 index 0000000000..2d7b446005 --- /dev/null +++ b/persistence-modules/spring-boot-h2/spring-boot-h2-database/src/main/resources/data.sql @@ -0,0 +1,13 @@ +DROP TABLE IF EXISTS billionaires; + +CREATE TABLE billionaires ( + id INT AUTO_INCREMENT PRIMARY KEY, + first_name VARCHAR(250) NOT NULL, + last_name VARCHAR(250) NOT NULL, + career VARCHAR(250) DEFAULT NULL +); + +INSERT INTO billionaires (first_name, last_name, career) VALUES +('Aliko', 'Dangote', 'Billionaire Industrialist'), +('Bill', 'Gates', 'Billionaire Tech Entrepreneur'), +('Folrunsho', 'Alakija', 'Billionaire Oil Magnate'); \ No newline at end of file From 275a4acaeaba3037913aaaaa9126dda12aa3100d Mon Sep 17 00:00:00 2001 From: Kumar Chandrakant Date: Thu, 18 Apr 2019 11:13:09 +0530 Subject: [PATCH 26/26] Adding source files for article BAEL-1932 (#6746) --- pom.xml | 5 +- spring-security-kerberos/README.md | 10 +++ spring-security-kerberos/pom.xml | 61 +++++++++++++ .../main/java/org/baeldung/Application.java | 13 +++ .../baeldung/config/WebSecurityConfig.java | 87 +++++++++++++++++++ .../security/DummyUserDetailsService.java | 16 ++++ 6 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 spring-security-kerberos/README.md create mode 100644 spring-security-kerberos/pom.xml create mode 100644 spring-security-kerberos/src/main/java/org/baeldung/Application.java create mode 100644 spring-security-kerberos/src/main/java/org/baeldung/config/WebSecurityConfig.java create mode 100644 spring-security-kerberos/src/main/java/org/baeldung/security/DummyUserDetailsService.java diff --git a/pom.xml b/pom.xml index 79dab80c74..b102b5cd30 100644 --- a/pom.xml +++ b/pom.xml @@ -381,7 +381,7 @@ core-java-8 core-java-8-2 - core-java-lambdas + core-java-lambdas core-java-arrays @@ -542,6 +542,7 @@ tensorflow-java spring-boot-flowable + spring-security-kerberos @@ -769,6 +770,7 @@ tensorflow-java spring-boot-flowable + spring-security-kerberos @@ -913,6 +915,7 @@ persistence-modules/spring-hibernate-5 spring-boot-flowable + spring-security-kerberos diff --git a/spring-security-kerberos/README.md b/spring-security-kerberos/README.md new file mode 100644 index 0000000000..0338c2058c --- /dev/null +++ b/spring-security-kerberos/README.md @@ -0,0 +1,10 @@ +## @PreFilter and @PostFilter annotations + +### Build the Project ### + +``` +mvn clean install +``` + +### Relevant Articles: +- [Spring Security – Kerberos](http://www.baeldung.com/xxxxxx) diff --git a/spring-security-kerberos/pom.xml b/spring-security-kerberos/pom.xml new file mode 100644 index 0000000000..35c4ba4926 --- /dev/null +++ b/spring-security-kerberos/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + com.baeldung + spring-security-kerberos + 0.1-SNAPSHOT + spring-security-kerberos + war + + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-1 + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-security + + + + org.springframework.security.kerberos + spring-security-kerberos-core + 1.0.1.RELEASE + + + org.springframework.security.kerberos + spring-security-kerberos-web + 1.0.1.RELEASE + + + org.springframework.security.kerberos + spring-security-kerberos-client + 1.0.1.RELEASE + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + + + + + + org.apache.maven.plugins + maven-war-plugin + + + + diff --git a/spring-security-kerberos/src/main/java/org/baeldung/Application.java b/spring-security-kerberos/src/main/java/org/baeldung/Application.java new file mode 100644 index 0000000000..39c2b51356 --- /dev/null +++ b/spring-security-kerberos/src/main/java/org/baeldung/Application.java @@ -0,0 +1,13 @@ +package org.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/spring-security-kerberos/src/main/java/org/baeldung/config/WebSecurityConfig.java b/spring-security-kerberos/src/main/java/org/baeldung/config/WebSecurityConfig.java new file mode 100644 index 0000000000..49a1cf0a8e --- /dev/null +++ b/spring-security-kerberos/src/main/java/org/baeldung/config/WebSecurityConfig.java @@ -0,0 +1,87 @@ +package org.baeldung.config; + +import org.baeldung.security.DummyUserDetailsService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.FileSystemResource; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.kerberos.authentication.KerberosAuthenticationProvider; +import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider; +import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosClient; +import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator; +import org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter; +import org.springframework.security.kerberos.web.authentication.SpnegoEntryPoint; +import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; + +@Configuration +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.authorizeRequests() + .anyRequest() + .authenticated() + .and() + .addFilterBefore(spnegoAuthenticationProcessingFilter(authenticationManagerBean()), BasicAuthenticationFilter.class); + } + + @Override + @Bean + public AuthenticationManager authenticationManagerBean() throws Exception { + return super.authenticationManagerBean(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.authenticationProvider(kerberosAuthenticationProvider()) + .authenticationProvider(kerberosServiceAuthenticationProvider()); + } + + @Bean + public KerberosAuthenticationProvider kerberosAuthenticationProvider() { + KerberosAuthenticationProvider provider = new KerberosAuthenticationProvider(); + SunJaasKerberosClient client = new SunJaasKerberosClient(); + client.setDebug(true); + provider.setKerberosClient(client); + provider.setUserDetailsService(dummyUserDetailsService()); + return provider; + } + + @Bean + public SpnegoEntryPoint spnegoEntryPoint() { + return new SpnegoEntryPoint("/login"); + } + + @Bean + public SpnegoAuthenticationProcessingFilter spnegoAuthenticationProcessingFilter(AuthenticationManager authenticationManager) { + SpnegoAuthenticationProcessingFilter filter = new SpnegoAuthenticationProcessingFilter(); + filter.setAuthenticationManager(authenticationManager); + return filter; + } + + @Bean + public KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider() { + KerberosServiceAuthenticationProvider provider = new KerberosServiceAuthenticationProvider(); + provider.setTicketValidator(sunJaasKerberosTicketValidator()); + provider.setUserDetailsService(dummyUserDetailsService()); + return provider; + } + + @Bean + public SunJaasKerberosTicketValidator sunJaasKerberosTicketValidator() { + SunJaasKerberosTicketValidator ticketValidator = new SunJaasKerberosTicketValidator(); + ticketValidator.setServicePrincipal("HTTP/demo.kerberos.bealdung.com@baeldung.com"); + ticketValidator.setKeyTabLocation(new FileSystemResource("baeldung.keytab")); + ticketValidator.setDebug(true); + return ticketValidator; + } + + @Bean + public DummyUserDetailsService dummyUserDetailsService() { + return new DummyUserDetailsService(); + } + +} \ No newline at end of file diff --git a/spring-security-kerberos/src/main/java/org/baeldung/security/DummyUserDetailsService.java b/spring-security-kerberos/src/main/java/org/baeldung/security/DummyUserDetailsService.java new file mode 100644 index 0000000000..10d71fca8f --- /dev/null +++ b/spring-security-kerberos/src/main/java/org/baeldung/security/DummyUserDetailsService.java @@ -0,0 +1,16 @@ +package org.baeldung.security; + +import org.springframework.security.core.authority.AuthorityUtils; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; + +public class DummyUserDetailsService implements UserDetailsService { + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + return new User(username, "notUsed", true, true, true, true, AuthorityUtils.createAuthorityList("ROLE_USER")); + } + +} \ No newline at end of file