From 597602a47833af962dae2c32f5acc50ad6d9f6d2 Mon Sep 17 00:00:00 2001 From: Marcos Lopez Gonzalez Date: Thu, 13 Sep 2018 22:37:40 +0200 Subject: [PATCH 01/14] BAEL-2171 - java heap dumps --- .../java/com/baeldung/heapdump/HeapDump.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/heapdump/HeapDump.java diff --git a/core-java/src/main/java/com/baeldung/heapdump/HeapDump.java b/core-java/src/main/java/com/baeldung/heapdump/HeapDump.java new file mode 100644 index 0000000000..1619bc43ae --- /dev/null +++ b/core-java/src/main/java/com/baeldung/heapdump/HeapDump.java @@ -0,0 +1,23 @@ +package com.baeldung.heapdump; + +import com.sun.management.HotSpotDiagnosticMXBean; + +import javax.management.MBeanServer; +import java.io.IOException; +import java.lang.management.ManagementFactory; + +public class HeapDump { + + public static void dumpHeap(String filePath, boolean live) throws IOException { + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + HotSpotDiagnosticMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy( + server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class); + mxBean.dumpHeap(filePath, live); + } + + public static void main(String[] args) throws IOException { + final String file = "/tmp/dump.hprof"; + + dumpHeap(file, true); + } +} From 21ed59cb25fffdc12e99ddacbca35097c06643b1 Mon Sep 17 00:00:00 2001 From: Satyam Date: Sat, 15 Sep 2018 22:12:47 +0530 Subject: [PATCH 02/14] Initial commit- BAEL 2158 --- algorithms/roundUpToHundred/.gitignore | 1 + .../src/com/java/src/RoundUpToHundred.java | 22 +++++++++++++++++++ .../com/java/src/RoundUpToHundredTest.java | 18 +++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 algorithms/roundUpToHundred/.gitignore create mode 100644 algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java create mode 100644 algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java diff --git a/algorithms/roundUpToHundred/.gitignore b/algorithms/roundUpToHundred/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/algorithms/roundUpToHundred/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java new file mode 100644 index 0000000000..fe3868570c --- /dev/null +++ b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java @@ -0,0 +1,22 @@ +package com.java.src; + +import java.util.Scanner; + +public class RoundUpToHundred { + + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + double input = scanner.nextDouble(); + scanner.close(); + + RoundUpToHundred.round(input); + } + + static int round(double input) { + + int i = (int) Math.round(input); + return ((i + 99) / 100) * 100; + }; + +} diff --git a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java new file mode 100644 index 0000000000..457e762e09 --- /dev/null +++ b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java @@ -0,0 +1,18 @@ +package com.java.src; + +import static org.junit.Assert.assertEquals; + +public class RoundUpToHundredTest { + public void roundupTest() { + + assertEquals("Rounded up to hundred", 100, + RoundUpToHundred.round(99)); + + assertEquals("Rounded down to two hundred ", 200, + RoundUpToHundred.round(200.2)); + + assertEquals("Returns same rounded value", 300, + RoundUpToHundred.round(300)); + + } +} From f7b41ba79c76ec704c03e611f3537c419ea8448d Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sun, 16 Sep 2018 23:45:42 +0530 Subject: [PATCH 03/14] [BAEL-8977] - Added junit to read file into string through IOUtils --- .../com/baeldung/file/FileOperationsManualTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java b/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java index 7968967679..6a020f5eae 100644 --- a/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java +++ b/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java @@ -1,6 +1,7 @@ package com.baeldung.file; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.hamcrest.CoreMatchers; import org.hamcrest.Matchers; import org.junit.Test; @@ -120,4 +121,14 @@ public class FileOperationsManualTest { return resultStringBuilder.toString(); } + + @Test + public void givenFileName_whenUsingIOUtils_thenFileData() throws IOException { + String expectedData = "This is a content of the file"; + + FileInputStream fis = new FileInputStream("src/test/resources/fileToRead.txt"); + String data = IOUtils.toString(fis, "UTF-8"); + + assertEquals(expectedData, data.trim()); + } } \ No newline at end of file From 6391285dd231cde7eea20ddd156adca870841ad4 Mon Sep 17 00:00:00 2001 From: Satyam Date: Mon, 17 Sep 2018 02:10:06 +0530 Subject: [PATCH 04/14] Added Math.ceil method to round up. --- .../src/com/java/src/RoundUpToHundred.java | 22 +++++++++---------- .../com/java/src/RoundUpToHundredTest.java | 20 +++++++---------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java index fe3868570c..3536b6a633 100644 --- a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java +++ b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java @@ -4,19 +4,17 @@ import java.util.Scanner; public class RoundUpToHundred { - public static void main(String[] args) { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + double input = scanner.nextDouble(); + scanner.close(); - Scanner scanner = new Scanner(System.in); - double input = scanner.nextDouble(); - scanner.close(); + RoundUpToHundred.round(input); + } - RoundUpToHundred.round(input); - } - - static int round(double input) { - - int i = (int) Math.round(input); - return ((i + 99) / 100) * 100; - }; + static int round(double input) { + int i = (int) Math.ceil(input); + return ((i + 99) / 100) * 100; + }; } diff --git a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java index 457e762e09..a13aa1f8cc 100644 --- a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java +++ b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java @@ -2,17 +2,13 @@ package com.java.src; import static org.junit.Assert.assertEquals; +import org.junit.Test; + public class RoundUpToHundredTest { - public void roundupTest() { - - assertEquals("Rounded up to hundred", 100, - RoundUpToHundred.round(99)); - - assertEquals("Rounded down to two hundred ", 200, - RoundUpToHundred.round(200.2)); - - assertEquals("Returns same rounded value", 300, - RoundUpToHundred.round(300)); - - } + @Test + public void givenInput_whenRoundedUp_thenTrue() { + assertEquals("Rounded up to hundred", 100, RoundUpToHundred.round(99)); + assertEquals("Rounded up to three hundred ", 300, RoundUpToHundred.round(200.2)); + assertEquals("Returns same rounded value", 400, RoundUpToHundred.round(400)); + } } From 14b6193fff46d3527365f8f1ca2fefe98ebdd5eb Mon Sep 17 00:00:00 2001 From: Satyam Date: Mon, 17 Sep 2018 19:02:35 +0530 Subject: [PATCH 05/14] Changed return type of static method to long --- .../roundUpToHundred/src/com/java/src/RoundUpToHundred.java | 4 ++-- .../src/com/java/src/RoundUpToHundredTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java index 3536b6a633..f1e8c6b653 100644 --- a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java +++ b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java @@ -12,8 +12,8 @@ public class RoundUpToHundred { RoundUpToHundred.round(input); } - static int round(double input) { - int i = (int) Math.ceil(input); + static long round(double input) { + long i = (int) Math.ceil(input); return ((i + 99) / 100) * 100; }; diff --git a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java index a13aa1f8cc..f35a9a249f 100644 --- a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java +++ b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java @@ -6,7 +6,7 @@ import org.junit.Test; public class RoundUpToHundredTest { @Test - public void givenInput_whenRoundedUp_thenTrue() { + public void givenInput_whenRound_thenRoundUpToTheNearestHundred() { assertEquals("Rounded up to hundred", 100, RoundUpToHundred.round(99)); assertEquals("Rounded up to three hundred ", 300, RoundUpToHundred.round(200.2)); assertEquals("Returns same rounded value", 400, RoundUpToHundred.round(400)); From a48f3851763c5aef7958ecd15d66e8ce607e6720 Mon Sep 17 00:00:00 2001 From: Marcos Lopez Gonzalez Date: Mon, 17 Sep 2018 18:13:09 +0200 Subject: [PATCH 06/14] File creation for dump changed --- core-java/src/main/java/com/baeldung/heapdump/HeapDump.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core-java/src/main/java/com/baeldung/heapdump/HeapDump.java b/core-java/src/main/java/com/baeldung/heapdump/HeapDump.java index 1619bc43ae..8cce20de8d 100644 --- a/core-java/src/main/java/com/baeldung/heapdump/HeapDump.java +++ b/core-java/src/main/java/com/baeldung/heapdump/HeapDump.java @@ -3,8 +3,10 @@ package com.baeldung.heapdump; import com.sun.management.HotSpotDiagnosticMXBean; import javax.management.MBeanServer; + import java.io.IOException; import java.lang.management.ManagementFactory; +import java.nio.file.Paths; public class HeapDump { @@ -16,7 +18,7 @@ public class HeapDump { } public static void main(String[] args) throws IOException { - final String file = "/tmp/dump.hprof"; + String file = Paths.get("dump.hprof").toFile().getPath(); dumpHeap(file, true); } From e9e971d38441d4f216dbf2e0747d3a680d192d75 Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 17 Sep 2018 23:47:09 +0300 Subject: [PATCH 07/14] fix nested loop --- .../src/main/java/org/baeldung/ip/web/MainController.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/web/MainController.java b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/web/MainController.java index f90c64a031..940194c421 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/ip/web/MainController.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/ip/web/MainController.java @@ -28,8 +28,9 @@ public class MainController { public void getFilters() { FilterChainProxy filterChainProxy = (FilterChainProxy) springSecurityFilterChain; List list = filterChainProxy.getFilterChains(); - list.forEach(chain -> chain.getFilters() - .forEach(filter -> System.out.println(filter.getClass()))); + list.stream() + .flatMap(chain -> chain.getFilters().stream()) + .forEach(filter -> System.out.println(filter.getClass())); } @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") From 38af85f7730315a41e204d65e7357c38395854c1 Mon Sep 17 00:00:00 2001 From: Eric Martin Date: Mon, 17 Sep 2018 21:55:14 -0500 Subject: [PATCH 08/14] Update RoundUpToHundred.java --- .../roundUpToHundred/src/com/java/src/RoundUpToHundred.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java index f1e8c6b653..f5024c227d 100644 --- a/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java +++ b/algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java @@ -13,7 +13,7 @@ public class RoundUpToHundred { } static long round(double input) { - long i = (int) Math.ceil(input); + long i = (long) Math.ceil(input); return ((i + 99) / 100) * 100; }; From 87c568052efdf650c4bbcabde1982a60ffee0860 Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Tue, 18 Sep 2018 21:16:26 +0530 Subject: [PATCH 09/14] BAEL-8961 Fix surefire configs of spring-4, jasyptdemo, spring-boot logging and mvc, auth projects - Test classes renaming as per standards - Surefire config fixes, pom standardization - Added packaging pom at spring-cloud-security module --- ....java => CustomJasyptIntegrationTest.java} | 2 +- ....java => JasyptSimpleIntegrationTest.java} | 2 +- ... => JasyptWithStarterIntegrationTest.java} | 2 +- spring-cloud/pom.xml | 1 + spring-cloud/spring-cloud-security/README.md | 29 ------------------- .../spring-cloud-security/auth-client/pom.xml | 14 +++------ ...va => Springoath2ApplicationUnitTest.java} | 2 +- .../auth-resource/pom.xml | 14 +++------ ... => PersonserviceApplicationUnitTest.java} | 2 +- .../spring-cloud-security/auth-server/pom.xml | 10 +++---- spring-cloud/spring-cloud-security/pom.xml | 23 +++++++++++++++ 11 files changed, 41 insertions(+), 60 deletions(-) rename spring-boot-jasypt/src/test/java/com/baeldung/jasypt/{CustomJasyptTest.java => CustomJasyptIntegrationTest.java} (94%) rename spring-boot-jasypt/src/test/java/com/baeldung/jasypt/{JasyptSimpleTest.java => JasyptSimpleIntegrationTest.java} (95%) rename spring-boot-jasypt/src/test/java/com/baeldung/jasypt/{JasyptWithStarterTest.java => JasyptWithStarterIntegrationTest.java} (95%) delete mode 100644 spring-cloud/spring-cloud-security/README.md rename spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/{Springoath2ApplicationTests.java => Springoath2ApplicationUnitTest.java} (88%) rename spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/{PersonserviceApplicationTests.java => PersonserviceApplicationUnitTest.java} (87%) create mode 100644 spring-cloud/spring-cloud-security/pom.xml diff --git a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptTest.java b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptIntegrationTest.java similarity index 94% rename from spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptTest.java rename to spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptIntegrationTest.java index 58c2dc7bb2..c24cfe6efa 100644 --- a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptTest.java +++ b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/CustomJasyptIntegrationTest.java @@ -14,7 +14,7 @@ import com.baeldung.jasypt.Main; @RunWith(SpringRunner.class) @SpringBootTest(classes = {Main.class}) -public class CustomJasyptTest { +public class CustomJasyptIntegrationTest { @Autowired ApplicationContext appCtx; diff --git a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleTest.java b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleIntegrationTest.java similarity index 95% rename from spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleTest.java rename to spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleIntegrationTest.java index f9b66b5044..e8dda73b4a 100644 --- a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleTest.java +++ b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptSimpleIntegrationTest.java @@ -13,7 +13,7 @@ import com.baeldung.jasypt.simple.PropertyServiceForJasyptSimple; @RunWith(SpringRunner.class) @SpringBootTest -public class JasyptSimpleTest { +public class JasyptSimpleIntegrationTest { @Autowired ApplicationContext appCtx; diff --git a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterTest.java b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterIntegrationTest.java similarity index 95% rename from spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterTest.java rename to spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterIntegrationTest.java index d246c21036..5f5d409ab9 100644 --- a/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterTest.java +++ b/spring-boot-jasypt/src/test/java/com/baeldung/jasypt/JasyptWithStarterIntegrationTest.java @@ -14,7 +14,7 @@ import com.baeldung.jasypt.starter.PropertyServiceForJasyptStarter; @RunWith(SpringRunner.class) @SpringBootTest -public class JasyptWithStarterTest { +public class JasyptWithStarterIntegrationTest { @Autowired ApplicationContext appCtx; diff --git a/spring-cloud/pom.xml b/spring-cloud/pom.xml index 376d8099ed..d0095e9a8a 100644 --- a/spring-cloud/pom.xml +++ b/spring-cloud/pom.xml @@ -35,6 +35,7 @@ spring-cloud-archaius spring-cloud-functions spring-cloud-vault + spring-cloud-security diff --git a/spring-cloud/spring-cloud-security/README.md b/spring-cloud/spring-cloud-security/README.md deleted file mode 100644 index 39af52c077..0000000000 --- a/spring-cloud/spring-cloud-security/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# README # - -This README would normally document whatever steps are necessary to get your application up and running. - -### What is this repository for? ### - -* Quick summary -* Version -* [Learn Markdown](https://bitbucket.org/tutorials/markdowndemo) - -### How do I get set up? ### - -* Summary of set up -* Configuration -* Dependencies -* Database configuration -* How to run tests -* Deployment instructions - -### Contribution guidelines ### - -* Writing tests -* Code review -* Other guidelines - -### Who do I talk to? ### - -* Repo owner or admin -* Other community or team contact \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/auth-client/pom.xml b/spring-cloud/spring-cloud-security/auth-client/pom.xml index f2d6308374..340c276c2d 100644 --- a/spring-cloud/spring-cloud-security/auth-client/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-client/pom.xml @@ -2,18 +2,15 @@ 4.0.0 - com.baeldung auth-client - 0.0.1-SNAPSHOT jar auth-client - Demo project for Spring Boot - + Spring Cloud Security APP Client Module + - parent-boot-1 + spring-cloud-security com.baeldung - 0.0.1-SNAPSHOT - ../../../parent-boot-1 + 1.0.0-SNAPSHOT @@ -88,9 +85,6 @@ - UTF-8 - UTF-8 - 1.8 2.1.0 Dalston.SR4 diff --git a/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java b/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationUnitTest.java similarity index 88% rename from spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java rename to spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationUnitTest.java index ca89575ee0..b112146dca 100644 --- a/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java +++ b/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationUnitTest.java @@ -9,7 +9,7 @@ import com.baeldung.CloudSite; @RunWith(SpringRunner.class) @SpringBootTest(classes = CloudSite.class) -public class Springoath2ApplicationTests { +public class Springoath2ApplicationUnitTest { @Test public void contextLoads() { diff --git a/spring-cloud/spring-cloud-security/auth-resource/pom.xml b/spring-cloud/spring-cloud-security/auth-resource/pom.xml index 0115259108..09474b2a4d 100644 --- a/spring-cloud/spring-cloud-security/auth-resource/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-resource/pom.xml @@ -3,21 +3,18 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung auth-resource - 0.0.1-SNAPSHOT jar auth-resource - Demo project for Spring Boot + Spring Cloud Security APP Resource Module - parent-boot-1 + spring-cloud-security com.baeldung - 0.0.1-SNAPSHOT - ../../../parent-boot-1 + 1.0.0-SNAPSHOT - + org.springframework.security.oauth @@ -60,9 +57,6 @@ - UTF-8 - UTF-8 - 1.8 Edgware.RELEASE diff --git a/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java b/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationUnitTest.java similarity index 87% rename from spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java rename to spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationUnitTest.java index e0fe7006d9..fb078d5c7c 100644 --- a/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java +++ b/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationUnitTest.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest -public class PersonserviceApplicationTests { +public class PersonserviceApplicationUnitTest { @Test public void contextLoads() { diff --git a/spring-cloud/spring-cloud-security/auth-server/pom.xml b/spring-cloud/spring-cloud-security/auth-server/pom.xml index b4367935e3..92f92808f6 100644 --- a/spring-cloud/spring-cloud-security/auth-server/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-server/pom.xml @@ -2,15 +2,13 @@ 4.0.0 - com.baeldung auth-server - 0.0.1-SNAPSHOT - + Spring Cloud Security APP Server Module + - parent-boot-1 + spring-cloud-security com.baeldung - 0.0.1-SNAPSHOT - ../../../parent-boot-1 + 1.0.0-SNAPSHOT diff --git a/spring-cloud/spring-cloud-security/pom.xml b/spring-cloud/spring-cloud-security/pom.xml new file mode 100644 index 0000000000..1cf8751548 --- /dev/null +++ b/spring-cloud/spring-cloud-security/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + spring-cloud-security + pom + 1.0.0-SNAPSHOT + spring-cloud-security + + + parent-boot-1 + com.baeldung + 0.0.1-SNAPSHOT + ../../parent-boot-1 + + + + auth-client + auth-resource + auth-server + + + From c67e8e817c74b3a03e5e7be894e346437d7b1d47 Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Wed, 19 Sep 2018 09:03:00 +0530 Subject: [PATCH 10/14] BAEL-8961 Fix surefire configs of spring-4, jasyptdemo, spring-boot logging and mvc, auth projects - Renamed spring-cloud-security tests to *IntegrationTest as contextLoad was tested in these tests. --- ...UnitTest.java => Springoath2ApplicationIntegrationTest.java} | 2 +- ...itTest.java => PersonserviceApplicationIntegrationTest.java} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/{Springoath2ApplicationUnitTest.java => Springoath2ApplicationIntegrationTest.java} (87%) rename spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/{PersonserviceApplicationUnitTest.java => PersonserviceApplicationIntegrationTest.java} (85%) diff --git a/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationUnitTest.java b/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationIntegrationTest.java similarity index 87% rename from spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationUnitTest.java rename to spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationIntegrationTest.java index b112146dca..37cff095db 100644 --- a/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationUnitTest.java +++ b/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationIntegrationTest.java @@ -9,7 +9,7 @@ import com.baeldung.CloudSite; @RunWith(SpringRunner.class) @SpringBootTest(classes = CloudSite.class) -public class Springoath2ApplicationUnitTest { +public class Springoath2ApplicationIntegrationTest { @Test public void contextLoads() { diff --git a/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationUnitTest.java b/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationIntegrationTest.java similarity index 85% rename from spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationUnitTest.java rename to spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationIntegrationTest.java index fb078d5c7c..3caa06ba4d 100644 --- a/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationUnitTest.java +++ b/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationIntegrationTest.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest -public class PersonserviceApplicationUnitTest { +public class PersonserviceApplicationIntegrationTest { @Test public void contextLoads() { From 4be74d00d11cc976d2f58c56ff571152a516f892 Mon Sep 17 00:00:00 2001 From: RanjeetKaur17 Date: Wed, 19 Sep 2018 10:38:11 +0400 Subject: [PATCH 11/14] Changes to directly create ZoneOffset using offset value rather using ZoneRules. --- .../baeldung/zoneddatetime/OffsetDateTimeExample.java | 8 ++------ .../com/baeldung/zoneddatetime/OffsetTimeExample.java | 9 ++------- .../zoneddatetime/OffsetDateTimeExampleUnitTest.java | 11 ++++------- .../zoneddatetime/OffsetTimeExampleUnitTest.java | 11 ++++------- .../zoneddatetime/ZoneDateTimeExampleUnitTest.java | 7 +++++-- 5 files changed, 17 insertions(+), 29 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetDateTimeExample.java b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetDateTimeExample.java index a22de0db18..fb92eb8d0d 100644 --- a/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetDateTimeExample.java +++ b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetDateTimeExample.java @@ -1,16 +1,12 @@ package com.baeldung.zoneddatetime; -import java.time.LocalDateTime; import java.time.OffsetDateTime; -import java.time.ZoneId; import java.time.ZoneOffset; public class OffsetDateTimeExample { - public OffsetDateTime getCurrentTimeByZoneOffset(String region) { - LocalDateTime now = LocalDateTime.now(); - ZoneId zone = ZoneId.of(region); - ZoneOffset zoneOffSet= zone.getRules().getOffset(now); + public OffsetDateTime getCurrentTimeByZoneOffset(String offset) { + ZoneOffset zoneOffSet= ZoneOffset.of(offset); OffsetDateTime date = OffsetDateTime.now(zoneOffSet); return date; } diff --git a/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetTimeExample.java b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetTimeExample.java index 7d926c0562..58e2d4d5ad 100644 --- a/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetTimeExample.java +++ b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetTimeExample.java @@ -1,17 +1,12 @@ package com.baeldung.zoneddatetime; -import java.time.LocalDateTime; import java.time.OffsetTime; -import java.time.ZoneId; import java.time.ZoneOffset; public class OffsetTimeExample { - public OffsetTime getCurrentTimeByZoneOffset(String region) { - LocalDateTime now = LocalDateTime.now(); - ZoneId zone = ZoneId.of(region); - ZoneOffset zoneOffSet = zone.getRules() - .getOffset(now); + public OffsetTime getCurrentTimeByZoneOffset(String offset) { + ZoneOffset zoneOffSet = ZoneOffset.of(offset); OffsetTime time = OffsetTime.now(zoneOffSet); return time; } diff --git a/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetDateTimeExampleUnitTest.java b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetDateTimeExampleUnitTest.java index c60f6967f9..a08d3737cd 100644 --- a/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetDateTimeExampleUnitTest.java +++ b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetDateTimeExampleUnitTest.java @@ -2,9 +2,8 @@ package com.baeldung.zoneddatetime; import static org.junit.Assert.assertTrue; -import java.time.LocalDateTime; import java.time.OffsetDateTime; -import java.time.ZoneId; +import java.time.ZoneOffset; import org.junit.Test; @@ -14,12 +13,10 @@ public class OffsetDateTimeExampleUnitTest { @Test public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() { - String zone = "Europe/Berlin"; - OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(zone); + String offset = "+02:00"; + OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(offset); assertTrue(time.getOffset() - .equals(ZoneId.of(zone) - .getRules() - .getOffset(LocalDateTime.now()))); + .equals(ZoneOffset.of(offset))); } } diff --git a/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetTimeExampleUnitTest.java b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetTimeExampleUnitTest.java index 0e1206dc5b..488f934179 100644 --- a/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetTimeExampleUnitTest.java +++ b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetTimeExampleUnitTest.java @@ -2,9 +2,8 @@ package com.baeldung.zoneddatetime; import static org.junit.Assert.assertTrue; -import java.time.LocalDateTime; import java.time.OffsetTime; -import java.time.ZoneId; +import java.time.ZoneOffset; import org.junit.Test; @@ -14,12 +13,10 @@ public class OffsetTimeExampleUnitTest { @Test public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() { - String zone = "Europe/Berlin"; - OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(zone); + String offset = "+02:00"; + OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(offset); assertTrue(time.getOffset() - .equals(ZoneId.of(zone) - .getRules() - .getOffset(LocalDateTime.now()))); + .equals(ZoneOffset.of(offset))); } } diff --git a/core-java/src/test/java/com/baeldung/zoneddatetime/ZoneDateTimeExampleUnitTest.java b/core-java/src/test/java/com/baeldung/zoneddatetime/ZoneDateTimeExampleUnitTest.java index 7f4f9cd17c..e78ff3e3fd 100644 --- a/core-java/src/test/java/com/baeldung/zoneddatetime/ZoneDateTimeExampleUnitTest.java +++ b/core-java/src/test/java/com/baeldung/zoneddatetime/ZoneDateTimeExampleUnitTest.java @@ -15,16 +15,19 @@ public class ZoneDateTimeExampleUnitTest { public void givenZone_whenGetCurrentTime_thenResultHasZone() { String zone = "Europe/Berlin"; ZonedDateTime time = zoneDateTimeExample.getCurrentTimeByZoneId(zone); + assertTrue(time.getZone() .equals(ZoneId.of(zone))); } - + @Test public void givenZones_whenConvertDateByZone_thenGetConstantDiff() { String sourceZone = "Europe/Berlin"; String destZone = "Asia/Tokyo"; ZonedDateTime sourceDate = zoneDateTimeExample.getCurrentTimeByZoneId(sourceZone); ZonedDateTime destDate = zoneDateTimeExample.convertZonedDateTime(sourceDate, destZone); - assertTrue(sourceDate.toInstant().compareTo(destDate.toInstant()) == 0); + + assertTrue(sourceDate.toInstant() + .compareTo(destDate.toInstant()) == 0); } } From a5acc10bac168e6f8ae9a7c9416b738640b8521b Mon Sep 17 00:00:00 2001 From: rozagerardo Date: Wed, 19 Sep 2018 11:16:10 -0300 Subject: [PATCH 12/14] [BAEL-1626] testing-modules | A Quick Guide to @TestPropertySource - move to existing submodule (#5292) * *added tests using testpropertysource * fix: using property for version in pom file * fix, added dependency with compile scope to build successfully on mvn clean install * * moved testpropertysource tests from spring-context-tests to spring-tests submodule * deleted submodule created for this article, since it was moved --- .../spring-context-testing/pom.xml | 25 ------------------- .../ClassUsingProperty.java | 0 ...aultTestPropertySourceIntegrationTest.java | 0 ...tionTestPropertySourceIntegrationTest.java | 0 ...tiesTestPropertySourceIntegrationTest.java | 0 ...stPropertySourceIntegrationTest.properties | 0 .../test/resources/other-location.properties | 0 7 files changed, 25 deletions(-) delete mode 100644 testing-modules/spring-context-testing/pom.xml rename testing-modules/{spring-context-testing => spring-testing}/src/main/java/com/baeldung/testpropertysource/ClassUsingProperty.java (100%) rename testing-modules/{spring-context-testing => spring-testing}/src/test/java/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.java (100%) rename testing-modules/{spring-context-testing => spring-testing}/src/test/java/com/baeldung/testpropertysource/LocationTestPropertySourceIntegrationTest.java (100%) rename testing-modules/{spring-context-testing => spring-testing}/src/test/java/com/baeldung/testpropertysource/PropertiesTestPropertySourceIntegrationTest.java (100%) rename testing-modules/{spring-context-testing => spring-testing}/src/test/resources/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.properties (100%) rename testing-modules/{spring-context-testing => spring-testing}/src/test/resources/other-location.properties (100%) diff --git a/testing-modules/spring-context-testing/pom.xml b/testing-modules/spring-context-testing/pom.xml deleted file mode 100644 index 148192d6c5..0000000000 --- a/testing-modules/spring-context-testing/pom.xml +++ /dev/null @@ -1,25 +0,0 @@ - - 4.0.0 - com.baeldung - spring-context-testing - 0.0.1-SNAPSHOT - - - - org.springframework.boot - spring-boot-starter - ${spring.boot.starter.version} - - - org.springframework.boot - spring-boot-starter-test - test - ${spring.boot.starter.version} - - - - - 2.0.4.RELEASE - - diff --git a/testing-modules/spring-context-testing/src/main/java/com/baeldung/testpropertysource/ClassUsingProperty.java b/testing-modules/spring-testing/src/main/java/com/baeldung/testpropertysource/ClassUsingProperty.java similarity index 100% rename from testing-modules/spring-context-testing/src/main/java/com/baeldung/testpropertysource/ClassUsingProperty.java rename to testing-modules/spring-testing/src/main/java/com/baeldung/testpropertysource/ClassUsingProperty.java diff --git a/testing-modules/spring-context-testing/src/test/java/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.java similarity index 100% rename from testing-modules/spring-context-testing/src/test/java/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.java rename to testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.java diff --git a/testing-modules/spring-context-testing/src/test/java/com/baeldung/testpropertysource/LocationTestPropertySourceIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/LocationTestPropertySourceIntegrationTest.java similarity index 100% rename from testing-modules/spring-context-testing/src/test/java/com/baeldung/testpropertysource/LocationTestPropertySourceIntegrationTest.java rename to testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/LocationTestPropertySourceIntegrationTest.java diff --git a/testing-modules/spring-context-testing/src/test/java/com/baeldung/testpropertysource/PropertiesTestPropertySourceIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/PropertiesTestPropertySourceIntegrationTest.java similarity index 100% rename from testing-modules/spring-context-testing/src/test/java/com/baeldung/testpropertysource/PropertiesTestPropertySourceIntegrationTest.java rename to testing-modules/spring-testing/src/test/java/com/baeldung/testpropertysource/PropertiesTestPropertySourceIntegrationTest.java diff --git a/testing-modules/spring-context-testing/src/test/resources/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.properties b/testing-modules/spring-testing/src/test/resources/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.properties similarity index 100% rename from testing-modules/spring-context-testing/src/test/resources/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.properties rename to testing-modules/spring-testing/src/test/resources/com/baeldung/testpropertysource/DefaultTestPropertySourceIntegrationTest.properties diff --git a/testing-modules/spring-context-testing/src/test/resources/other-location.properties b/testing-modules/spring-testing/src/test/resources/other-location.properties similarity index 100% rename from testing-modules/spring-context-testing/src/test/resources/other-location.properties rename to testing-modules/spring-testing/src/test/resources/other-location.properties From 4d2780379d6921f7f1a8749684872452f021b591 Mon Sep 17 00:00:00 2001 From: Sam Millington Date: Wed, 19 Sep 2018 17:29:04 +0100 Subject: [PATCH 13/14] Password Hashing, PBKDF2 and SHA-512 Issue: BAEL-2164 --- .../passwordhashing/PBKDF2Hasher.java | 149 ++++++++++++++++++ .../passwordhashing/SHA512Hasher.java | 35 ++++ .../passwordhashing/SimplePBKDF2Hasher.java | 18 +++ .../passwordhashing/PBKDF2HasherUnitTest.java | 41 +++++ .../passwordhashing/SHA512HasherUnitTest.java | 70 ++++++++ 5 files changed, 313 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/passwordhashing/PBKDF2Hasher.java create mode 100644 core-java/src/main/java/com/baeldung/passwordhashing/SHA512Hasher.java create mode 100644 core-java/src/main/java/com/baeldung/passwordhashing/SimplePBKDF2Hasher.java create mode 100644 core-java/src/test/java/com/baeldung/passwordhashing/PBKDF2HasherUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/passwordhashing/SHA512HasherUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/passwordhashing/PBKDF2Hasher.java b/core-java/src/main/java/com/baeldung/passwordhashing/PBKDF2Hasher.java new file mode 100644 index 0000000000..e2259e4249 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/passwordhashing/PBKDF2Hasher.java @@ -0,0 +1,149 @@ +package com.baeldung.passwordhashing; + +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.KeySpec; +import java.util.Arrays; +import java.util.Base64; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; + +/** + * Hash passwords for storage, and test passwords against password tokens. + * + * Instances of this class can be used concurrently by multiple threads. + * + * @author erickson + * @see StackOverflow + */ +public final class PBKDF2Hasher +{ + + /** + * Each token produced by this class uses this identifier as a prefix. + */ + public static final String ID = "$31$"; + + /** + * The minimum recommended cost, used by default + */ + public static final int DEFAULT_COST = 16; + + private static final String ALGORITHM = "PBKDF2WithHmacSHA1"; + + private static final int SIZE = 128; + + private static final Pattern layout = Pattern.compile("\\$31\\$(\\d\\d?)\\$(.{43})"); + + private final SecureRandom random; + + private final int cost; + + public PBKDF2Hasher() + { + this(DEFAULT_COST); + } + + /** + * Create a password manager with a specified cost + * + * @param cost the exponential computational cost of hashing a password, 0 to 30 + */ + public PBKDF2Hasher(int cost) + { + iterations(cost); /* Validate cost */ + this.cost = cost; + this.random = new SecureRandom(); + } + + private static int iterations(int cost) + { + if ((cost < 0) || (cost > 30)) + throw new IllegalArgumentException("cost: " + cost); + return 1 << cost; + } + + /** + * Hash a password for storage. + * + * @return a secure authentication token to be stored for later authentication + */ + public String hash(char[] password) + { + byte[] salt = new byte[SIZE / 8]; + random.nextBytes(salt); + byte[] dk = pbkdf2(password, salt, 1 << cost); + byte[] hash = new byte[salt.length + dk.length]; + System.arraycopy(salt, 0, hash, 0, salt.length); + System.arraycopy(dk, 0, hash, salt.length, dk.length); + Base64.Encoder enc = Base64.getUrlEncoder().withoutPadding(); + return ID + cost + '$' + enc.encodeToString(hash); + } + + /** + * Authenticate with a password and a stored password token. + * + * @return true if the password and token match + */ + public boolean checkPassword(char[] password, String token) + { + Matcher m = layout.matcher(token); + if (!m.matches()) + throw new IllegalArgumentException("Invalid token format"); + int iterations = iterations(Integer.parseInt(m.group(1))); + byte[] hash = Base64.getUrlDecoder().decode(m.group(2)); + byte[] salt = Arrays.copyOfRange(hash, 0, SIZE / 8); + byte[] check = pbkdf2(password, salt, iterations); + int zero = 0; + for (int idx = 0; idx < check.length; ++idx) + zero |= hash[salt.length + idx] ^ check[idx]; + return zero == 0; + } + + private static byte[] pbkdf2(char[] password, byte[] salt, int iterations) + { + KeySpec spec = new PBEKeySpec(password, salt, iterations, SIZE); + try { + SecretKeyFactory f = SecretKeyFactory.getInstance(ALGORITHM); + return f.generateSecret(spec).getEncoded(); + } + catch (NoSuchAlgorithmException ex) { + throw new IllegalStateException("Missing algorithm: " + ALGORITHM, ex); + } + catch (InvalidKeySpecException ex) { + throw new IllegalStateException("Invalid SecretKeyFactory", ex); + } + } + + /** + * Hash a password in an immutable {@code String}. + * + *

Passwords should be stored in a {@code char[]} so that it can be filled + * with zeros after use instead of lingering on the heap and elsewhere. + * + * @deprecated Use {@link #hash(char[])} instead + */ + @Deprecated + public String hash(String password) + { + return hash(password.toCharArray()); + } + + /** + * Authenticate with a password in an immutable {@code String} and a stored + * password token. + * + * @deprecated Use {@link #checkPassword(char[],String)} instead. + * @see #hash(String) + */ + @Deprecated + public boolean checkPassword(String password, String token) + { + return checkPassword(password.toCharArray(), token); + } + +} diff --git a/core-java/src/main/java/com/baeldung/passwordhashing/SHA512Hasher.java b/core-java/src/main/java/com/baeldung/passwordhashing/SHA512Hasher.java new file mode 100644 index 0000000000..4f5337f963 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/passwordhashing/SHA512Hasher.java @@ -0,0 +1,35 @@ +package com.baeldung.passwordhashing; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + + +/** A really simple SHA_512 Encryption example. + * + */ +public class SHA512Hasher { + + public String hash(String passwordToHash, byte[] salt){ + String generatedPassword = null; + try { + MessageDigest md = MessageDigest.getInstance("SHA-512"); + md.update(salt); + byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8)); + StringBuilder sb = new StringBuilder(); + for(int i=0; i< bytes.length ;i++){ + sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); + } + generatedPassword = sb.toString(); + } + catch (NoSuchAlgorithmException e){ + e.printStackTrace(); + } + return generatedPassword; + } + + public boolean checkPassword(String hash, String attempt, byte[] salt){ + String generatedHash = hash(attempt, salt); + return hash.equals(generatedHash); + } +} diff --git a/core-java/src/main/java/com/baeldung/passwordhashing/SimplePBKDF2Hasher.java b/core-java/src/main/java/com/baeldung/passwordhashing/SimplePBKDF2Hasher.java new file mode 100644 index 0000000000..36c9b65070 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/passwordhashing/SimplePBKDF2Hasher.java @@ -0,0 +1,18 @@ +package com.baeldung.passwordhashing; + +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; +import java.security.spec.KeySpec; + +/** A really simple SimplePBKDF2 Encryption example. + * + */ +public class SimplePBKDF2Hasher { + + public static String hashSimple(String password, byte[] salt) throws Exception{ + KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128); + SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); + byte[] hash = f.generateSecret(spec).getEncoded(); + return String.valueOf(hash); + } +} diff --git a/core-java/src/test/java/com/baeldung/passwordhashing/PBKDF2HasherUnitTest.java b/core-java/src/test/java/com/baeldung/passwordhashing/PBKDF2HasherUnitTest.java new file mode 100644 index 0000000000..8e90725c77 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/passwordhashing/PBKDF2HasherUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.passwordhashing; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + + +public class PBKDF2HasherUnitTest { + + private PBKDF2Hasher mPBKDF2Hasher; + + @Before + public void setUp() throws Exception { + mPBKDF2Hasher = new PBKDF2Hasher(); + } + + @Test + public void givenCorrectMessageAndHash_whenAuthenticated_checkAuthenticationSucceeds() throws Exception { + String message1 = "password123"; + + String hash1 = mPBKDF2Hasher.hash(message1.toCharArray()); + + assertTrue(mPBKDF2Hasher.checkPassword(message1.toCharArray(), hash1)); + + } + + @Test + public void givenWrongMessage_whenAuthenticated_checkAuthenticationFails() throws Exception { + String message1 = "password123"; + + String hash1 = mPBKDF2Hasher.hash(message1.toCharArray()); + + String wrongPasswordAttempt = "IamWrong"; + + assertFalse(mPBKDF2Hasher.checkPassword(wrongPasswordAttempt.toCharArray(), hash1)); + + } + + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/passwordhashing/SHA512HasherUnitTest.java b/core-java/src/test/java/com/baeldung/passwordhashing/SHA512HasherUnitTest.java new file mode 100644 index 0000000000..3acfb0ba9d --- /dev/null +++ b/core-java/src/test/java/com/baeldung/passwordhashing/SHA512HasherUnitTest.java @@ -0,0 +1,70 @@ +package com.baeldung.passwordhashing; + +import org.junit.Before; +import org.junit.Test; + +import java.security.SecureRandom; + +import static org.junit.Assert.*; + +/** + * Created by PhysicsSam on 06-Sep-18. + */ +public class SHA512HasherUnitTest { + + private SHA512Hasher hasher; + private SecureRandom secureRandom; + + @Before + public void setUp() throws Exception { + hasher = new SHA512Hasher(); + secureRandom = new SecureRandom(); + } + + @Test + public void givenSamePasswordAndSalt_whenHashed_checkResultingHashesAreEqual() throws Exception { + + byte[] salt = new byte[16]; + secureRandom.nextBytes(salt); + + String hash1 = hasher.hash("password", salt); + String hash2 = hasher.hash("password", salt); + + assertEquals(hash1, hash2); + + } + + @Test + public void givenSamePasswordAndDifferentSalt_whenHashed_checkResultingHashesNotEqual() throws Exception { + + byte[] salt = new byte[16]; + secureRandom.nextBytes(salt); + String hash1 = hasher.hash("password", salt); + //generate a second salt + byte[] secondSalt = new byte[16]; + String hash2 = hasher.hash("password", secondSalt); + + assertNotEquals(hash1, hash2); + + } + + @Test + public void givenPredefinedHash_whenCorrectAttemptGiven_checkAuthenticationSucceeds() throws Exception { + byte[] salt = new byte[16]; + secureRandom.nextBytes(salt); + + String originalHash = hasher.hash("password123", salt); + + assertTrue(hasher.checkPassword(originalHash, "password123", salt)); + } + + @Test + public void givenPredefinedHash_whenIncorrectAttemptGiven_checkAuthenticationFails() throws Exception { + byte[] salt = new byte[16]; + secureRandom.nextBytes(salt); + + String originalHash = hasher.hash("password123", salt); + + assertFalse(hasher.checkPassword(originalHash, "password124", salt)); + } +} \ No newline at end of file From 6a09ba47e8581972a7e81eb45e4239c9b5c6b037 Mon Sep 17 00:00:00 2001 From: Saikat <41847480+psychsane@users.noreply.github.com> Date: Wed, 19 Sep 2018 22:26:11 +0530 Subject: [PATCH 14/14] Check String is not empty Issue: BAEL-2128 --- java-strings/pom.xml | 6 +++ .../baeldung/string/StringEmptyUnitTest.java | 51 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 java-strings/src/test/java/com/baeldung/string/StringEmptyUnitTest.java diff --git a/java-strings/pom.xml b/java-strings/pom.xml index 2afe18f07a..29e992a2ce 100644 --- a/java-strings/pom.xml +++ b/java-strings/pom.xml @@ -52,6 +52,11 @@ icu4j ${icu4j.version} + + com.google.guava + guava + ${guava.version} + com.vdurmont @@ -92,6 +97,7 @@ 3.6.1 1.19 61.1 + 26.0-jre \ No newline at end of file diff --git a/java-strings/src/test/java/com/baeldung/string/StringEmptyUnitTest.java b/java-strings/src/test/java/com/baeldung/string/StringEmptyUnitTest.java new file mode 100644 index 0000000000..17b13f89de --- /dev/null +++ b/java-strings/src/test/java/com/baeldung/string/StringEmptyUnitTest.java @@ -0,0 +1,51 @@ +package com.baeldung.string; + +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString; +import static org.hamcrest.text.IsEmptyString.isEmptyString; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import org.apache.commons.lang3.StringUtils; +import org.assertj.core.api.Assertions; +import org.junit.Test; + +import com.google.common.base.Strings; + +public class StringEmptyUnitTest { + + private String text = "baeldung"; + + @Test + public void givenAString_whenCheckedForEmptyUsingJunit_shouldAssertSuccessfully() { + assertTrue(!text.isEmpty()); + assertFalse(text.isEmpty()); + assertNotEquals("", text); + assertNotSame("", text); + } + + @Test + public void givenAString_whenCheckedForEmptyUsingHamcrest_shouldAssertSuccessfully() { + assertThat(text, not(isEmptyString())); + assertThat(text, not(isEmptyOrNullString())); + } + + @Test + public void givenAString_whenCheckedForEmptyUsingCommonsLang_shouldAssertSuccessfully() { + assertTrue(StringUtils.isNotBlank(text)); + } + + @Test + public void givenAString_whenCheckedForEmptyUsingAssertJ_shouldAssertSuccessfully() { + Assertions.assertThat(text).isNotEmpty(); + } + + @Test + public void givenAString_whenCheckedForEmptyUsingGuava_shouldAssertSuccessfully() { + assertFalse(Strings.isNullOrEmpty(text)); + } + +}