From 00d83f90709108e88c16ade58dbeb9899f7067dc Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 17 Feb 2017 18:26:23 +0100 Subject: [PATCH 1/6] BAEL-602 create JOOL module, one test case --- jooq/pom.xml | 46 +++++++++++++++++++ jooq/src/test/java/com/baeldung/JOOLTest.java | 29 ++++++++++++ pom.xml | 1 + 3 files changed, 76 insertions(+) create mode 100644 jooq/pom.xml create mode 100644 jooq/src/test/java/com/baeldung/JOOLTest.java diff --git a/jooq/pom.xml b/jooq/pom.xml new file mode 100644 index 0000000000..ef287d0292 --- /dev/null +++ b/jooq/pom.xml @@ -0,0 +1,46 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + jooq + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + org.jooq + jool + ${jool.version} + + + junit + junit + ${junit.version} + test + + + + + 0.9.12 + 4.12 + + + + \ No newline at end of file diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java new file mode 100644 index 0000000000..aa729a3edf --- /dev/null +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -0,0 +1,29 @@ +package com.baeldung; + + +import org.jooq.lambda.Seq; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static junit.framework.Assert.assertTrue; +import static junit.framework.TestCase.assertEquals; + +public class JOOLTest { + @Test + public void givenSeq_whenCheckContains_shouldReturnTrue() { + List concat = Seq.of(1, 2, 3).concat(Seq.of(4, 5, 6)).toList(); + + assertEquals(concat, Arrays.asList(1, 2, 3, 4, 5, 6)); + + + assertTrue(Seq.of(1, 2, 3, 4).contains(2)); + + + assertTrue(Seq.of(1, 2, 3, 4).containsAll(2, 3)); + + + assertTrue(Seq.of(1, 2, 3, 4).containsAny(2, 5)); + } +} diff --git a/pom.xml b/pom.xml index ecb07e987b..42ded9de0d 100644 --- a/pom.xml +++ b/pom.xml @@ -193,6 +193,7 @@ struts2 apache-velocity + jooq From 8d4b69671a164f224c1bb7593973a9eca1b2e19c Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Sat, 18 Feb 2017 13:59:08 +0100 Subject: [PATCH 2/6] BAEL-602 more examples of JOOL --- jooq/src/test/java/com/baeldung/JOOLTest.java | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java index aa729a3edf..4f5cb3741a 100644 --- a/jooq/src/test/java/com/baeldung/JOOLTest.java +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -5,10 +5,13 @@ import org.jooq.lambda.Seq; import org.junit.Test; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static junit.framework.Assert.assertTrue; import static junit.framework.TestCase.assertEquals; +import static org.jooq.lambda.tuple.Tuple.tuple; public class JOOLTest { @Test @@ -26,4 +29,123 @@ public class JOOLTest { assertTrue(Seq.of(1, 2, 3, 4).containsAny(2, 5)); } + + @Test + public void givenSeq_whenJoin_shouldHaveElementsFromBothSeq() { + assertEquals( + Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(), + Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B")) + ); + + + assertEquals( + Seq.of(1, 2, 4).innerJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(), + Arrays.asList(tuple(1, 1), tuple(2, 2)) + ); + + + assertEquals( + Seq.of(1, 2, 4).leftOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(), + Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(4, null)) + ); + + assertEquals( + Seq.of(1, 2, 4).rightOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(), + Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(null, 3)) + ); + } + + @Test + public void givenSeq_whenManipulateSeq_seqShouldHaveNewElementsInIt() { + assertEquals( + Seq.of(1, 2, 3).cycle().limit(9).toList(), + Arrays.asList(1, 2, 3, 1, 2, 3, 1, 2, 3) + ); + + assertEquals( + Seq.of(1, 2, 3).duplicate().map((first, second) -> tuple(first.toList(), second.toList())), + tuple(Arrays.asList(1, 2, 3), Arrays.asList(1, 2, 3)) + ); + + assertEquals( + Seq.of(1, 2, 3, 4).intersperse(0).toList(), + Arrays.asList(1, 0, 2, 0, 3, 0, 4) + ); + + assertEquals( + Seq.of(1, 2, 3, 4, 5).shuffle().toList().size(), + 5 + ); + + assertEquals( + Seq.of(1, 2, 3, 4).partition(i -> i % 2 != 0).map((first, second) -> tuple(first.toList(), second.toList())), + tuple(Arrays.asList(1, 3), Arrays.asList(2, 4)) + + ); + + assertEquals( + Seq.of(1, 2, 3, 4).reverse().toList(), + Arrays.asList(4, 3, 2, 1) + ); + } + + @Test + public void givenSeq_whenGroupByAndFold_shouldReturnProperSeq() { + + Map> expectedAfterGroupBy = new HashMap<>(); + expectedAfterGroupBy.put(1, Arrays.asList(1, 3)); + expectedAfterGroupBy.put(0, Arrays.asList(2, 4)); + + assertEquals( + Seq.of(1, 2, 3, 4).groupBy(i -> i % 2), + expectedAfterGroupBy + ); + + + assertEquals( + Seq.of("a", "b", "c").foldLeft("!", (u, t) -> u + t), + "!abc" + ); + + + assertEquals( + Seq.of("a", "b", "c").foldRight("!", (t, u) -> t + u), + "abc!" + ); + } + + @Test + public void givenSeq_whenUsingSeqWhile_shouldBehaveAsWhileLoop() { + + assertEquals( + Seq.of(1, 2, 3, 4, 5).skipWhile(i -> i < 3).toList(), + Arrays.asList(3, 4, 5) + ); + + assertEquals( + Seq.of(1, 2, 3, 4, 5).skipUntil(i -> i == 3).toList(), + Arrays.asList(3, 4, 5) + ); + } + + @Test + public void givenSeq_whenZip_shouldHaveZippedSeq() { + + assertEquals( + Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c")).toList(), + Arrays.asList(tuple(1, "a"), tuple(2, "b"), tuple(3, "c")) + ); + + // ("1:a", "2:b", "3:c") + assertEquals( + Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c"), (x, y) -> x + ":" + y).toList(), + Arrays.asList("1:a", "2:b", "3:c") + ); + + + assertEquals( + Seq.of("a", "b", "c").zipWithIndex().toList(), + Arrays.asList(tuple("a", 0L), tuple("b", 1L), tuple("c", 2L)) + ); + } } From 306eb4ee96036d762c6611899953fa9afc02322c Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Sat, 18 Feb 2017 14:06:08 +0100 Subject: [PATCH 3/6] BAEL-602 example of using Unchecked --- jooq/src/test/java/com/baeldung/JOOLTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java index 4f5cb3741a..487af9bddb 100644 --- a/jooq/src/test/java/com/baeldung/JOOLTest.java +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -2,12 +2,14 @@ package com.baeldung; import org.jooq.lambda.Seq; +import org.jooq.lambda.Unchecked; import org.junit.Test; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import static junit.framework.Assert.assertTrue; import static junit.framework.TestCase.assertEquals; @@ -148,4 +150,44 @@ public class JOOLTest { Arrays.asList(tuple("a", 0L), tuple("b", 1L), tuple("c", 2L)) ); } + + + public Integer methodThatThrowsChecked(String arg) throws Exception { + return arg.length(); + } + + @Test + public void givenOperationThatThrowsCheckedException_whenExecuteAndNeedToWrapCheckedIntoUnchecked_shouldPass() { + //when + List collect = Arrays.asList("a", "b", "c").stream().map(elem -> { + try { + return methodThatThrowsChecked(elem); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + }).collect(Collectors.toList()); + + //then + assertEquals( + collect, + Arrays.asList(1, 1, 1) + ); + } + + + @Test + public void givenOperationThatThrowsCheckedException_whenExecuteUsingUncheckedFuction_shouldPass() { + //when + List collect = Arrays.asList("a", "b", "c").stream() + .map(Unchecked.function(elem -> methodThatThrowsChecked(elem))) + .collect(Collectors.toList()); + + //then + assertEquals( + collect, + Arrays.asList(1, 1, 1) + ); + } + } From e35c452ad1ac8ce4fd6bbf5f182cdf952e6bf02e Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Sat, 18 Feb 2017 22:40:15 +0100 Subject: [PATCH 4/6] BAEL-602 add example of join using only Stream API --- jooq/src/test/java/com/baeldung/JOOLTest.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java index 487af9bddb..64ad3b316d 100644 --- a/jooq/src/test/java/com/baeldung/JOOLTest.java +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -10,6 +10,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.Stream; import static junit.framework.Assert.assertTrue; import static junit.framework.TestCase.assertEquals; @@ -32,14 +33,22 @@ public class JOOLTest { assertTrue(Seq.of(1, 2, 3, 4).containsAny(2, 5)); } + @Test + public void givenStreams_whenJoin_shouldHaveElementsFromTwoStreams() { + //given + Stream left = Arrays.asList(1, 2, 4).stream(); + Stream right = Arrays.asList(1, 2, 3).stream(); + + //when + List rightCollected = right.collect(Collectors.toList()); + List collect = left.filter(rightCollected::contains).collect(Collectors.toList()); + + //then + assertEquals(collect, Arrays.asList(1, 2)); + } + @Test public void givenSeq_whenJoin_shouldHaveElementsFromBothSeq() { - assertEquals( - Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(), - Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B")) - ); - - assertEquals( Seq.of(1, 2, 4).innerJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(), Arrays.asList(tuple(1, 1), tuple(2, 2)) @@ -55,6 +64,11 @@ public class JOOLTest { Seq.of(1, 2, 4).rightOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(), Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(null, 3)) ); + + assertEquals( + Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(), + Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B")) + ); } @Test From 08ced4054783518d34038059f831fe902ca32e20 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 20 Feb 2017 10:13:25 +0100 Subject: [PATCH 5/6] BAEL-602 remove unnecessary comment --- jooq/src/test/java/com/baeldung/JOOLTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java index 64ad3b316d..5560e7ea14 100644 --- a/jooq/src/test/java/com/baeldung/JOOLTest.java +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -94,8 +94,8 @@ public class JOOLTest { ); assertEquals( - Seq.of(1, 2, 3, 4).partition(i -> i % 2 != 0).map((first, second) -> tuple(first.toList(), second.toList())), - tuple(Arrays.asList(1, 3), Arrays.asList(2, 4)) + Seq.of(1, 2, 3, 4).partition(i -> i > 2).map((first, second) -> tuple(first.toList(), second.toList())), + tuple(Arrays.asList(3, 4), Arrays.asList(1, 2)) ); @@ -152,7 +152,6 @@ public class JOOLTest { Arrays.asList(tuple(1, "a"), tuple(2, "b"), tuple(3, "c")) ); - // ("1:a", "2:b", "3:c") assertEquals( Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c"), (x, y) -> x + ":" + y).toList(), Arrays.asList("1:a", "2:b", "3:c") From 0fcae56196e02170ff873b4aaec672c519442639 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Thu, 23 Feb 2017 16:03:49 +0100 Subject: [PATCH 6/6] BAEL-602 use Stream.of --- jooq/src/test/java/com/baeldung/JOOLTest.java | 8 ++++---- pom.xml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java index 5560e7ea14..18fca1f67a 100644 --- a/jooq/src/test/java/com/baeldung/JOOLTest.java +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -36,8 +36,8 @@ public class JOOLTest { @Test public void givenStreams_whenJoin_shouldHaveElementsFromTwoStreams() { //given - Stream left = Arrays.asList(1, 2, 4).stream(); - Stream right = Arrays.asList(1, 2, 3).stream(); + Stream left = Stream.of(1, 2, 4); + Stream right = Stream.of(1, 2, 3); //when List rightCollected = right.collect(Collectors.toList()); @@ -172,7 +172,7 @@ public class JOOLTest { @Test public void givenOperationThatThrowsCheckedException_whenExecuteAndNeedToWrapCheckedIntoUnchecked_shouldPass() { //when - List collect = Arrays.asList("a", "b", "c").stream().map(elem -> { + List collect = Stream.of("a", "b", "c").map(elem -> { try { return methodThatThrowsChecked(elem); } catch (Exception e) { @@ -192,7 +192,7 @@ public class JOOLTest { @Test public void givenOperationThatThrowsCheckedException_whenExecuteUsingUncheckedFuction_shouldPass() { //when - List collect = Arrays.asList("a", "b", "c").stream() + List collect = Stream.of("a", "b", "c") .map(Unchecked.function(elem -> methodThatThrowsChecked(elem))) .collect(Collectors.toList()); diff --git a/pom.xml b/pom.xml index 42ded9de0d..7694f29d3a 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,7 @@ jaxb jee7 jjwt + jooq jpa-storedprocedure jsf json-path @@ -193,7 +194,6 @@ struts2 apache-velocity - jooq