Skip to content

Commit 93162b9

Browse files
authored
Merge pull request eugenp#1202 from tomekl007/BAEL-602
BAEL-602 jool
2 parents a6f2451 + 0fcae56 commit 93162b9

3 files changed

Lines changed: 253 additions & 0 deletions

File tree

‎jooq/pom.xml‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>parent-modules</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>jooq</artifactId>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<configuration>
19+
<source>1.8</source>
20+
<target>1.8</target>
21+
</configuration>
22+
</plugin>
23+
</plugins>
24+
</build>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.jooq</groupId>
29+
<artifactId>jool</artifactId>
30+
<version>${jool.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>${junit.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
<properties>
41+
<jool.version>0.9.12</jool.version>
42+
<junit.version>4.12</junit.version>
43+
</properties>
44+
45+
46+
</project>
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
package com.baeldung;
2+
3+
4+
import org.jooq.lambda.Seq;
5+
import org.jooq.lambda.Unchecked;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
14+
15+
import static junit.framework.Assert.assertTrue;
16+
import static junit.framework.TestCase.assertEquals;
17+
import static org.jooq.lambda.tuple.Tuple.tuple;
18+
19+
public class JOOLTest {
20+
@Test
21+
public void givenSeq_whenCheckContains_shouldReturnTrue() {
22+
List<Integer> concat = Seq.of(1, 2, 3).concat(Seq.of(4, 5, 6)).toList();
23+
24+
assertEquals(concat, Arrays.asList(1, 2, 3, 4, 5, 6));
25+
26+
27+
assertTrue(Seq.of(1, 2, 3, 4).contains(2));
28+
29+
30+
assertTrue(Seq.of(1, 2, 3, 4).containsAll(2, 3));
31+
32+
33+
assertTrue(Seq.of(1, 2, 3, 4).containsAny(2, 5));
34+
}
35+
36+
@Test
37+
public void givenStreams_whenJoin_shouldHaveElementsFromTwoStreams() {
38+
//given
39+
Stream<Integer> left = Stream.of(1, 2, 4);
40+
Stream<Integer> right = Stream.of(1, 2, 3);
41+
42+
//when
43+
List<Integer> rightCollected = right.collect(Collectors.toList());
44+
List<Integer> collect = left.filter(rightCollected::contains).collect(Collectors.toList());
45+
46+
//then
47+
assertEquals(collect, Arrays.asList(1, 2));
48+
}
49+
50+
@Test
51+
public void givenSeq_whenJoin_shouldHaveElementsFromBothSeq() {
52+
assertEquals(
53+
Seq.of(1, 2, 4).innerJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(),
54+
Arrays.asList(tuple(1, 1), tuple(2, 2))
55+
);
56+
57+
58+
assertEquals(
59+
Seq.of(1, 2, 4).leftOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(),
60+
Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(4, null))
61+
);
62+
63+
assertEquals(
64+
Seq.of(1, 2, 4).rightOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(),
65+
Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(null, 3))
66+
);
67+
68+
assertEquals(
69+
Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(),
70+
Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B"))
71+
);
72+
}
73+
74+
@Test
75+
public void givenSeq_whenManipulateSeq_seqShouldHaveNewElementsInIt() {
76+
assertEquals(
77+
Seq.of(1, 2, 3).cycle().limit(9).toList(),
78+
Arrays.asList(1, 2, 3, 1, 2, 3, 1, 2, 3)
79+
);
80+
81+
assertEquals(
82+
Seq.of(1, 2, 3).duplicate().map((first, second) -> tuple(first.toList(), second.toList())),
83+
tuple(Arrays.asList(1, 2, 3), Arrays.asList(1, 2, 3))
84+
);
85+
86+
assertEquals(
87+
Seq.of(1, 2, 3, 4).intersperse(0).toList(),
88+
Arrays.asList(1, 0, 2, 0, 3, 0, 4)
89+
);
90+
91+
assertEquals(
92+
Seq.of(1, 2, 3, 4, 5).shuffle().toList().size(),
93+
5
94+
);
95+
96+
assertEquals(
97+
Seq.of(1, 2, 3, 4).partition(i -> i > 2).map((first, second) -> tuple(first.toList(), second.toList())),
98+
tuple(Arrays.asList(3, 4), Arrays.asList(1, 2))
99+
100+
);
101+
102+
assertEquals(
103+
Seq.of(1, 2, 3, 4).reverse().toList(),
104+
Arrays.asList(4, 3, 2, 1)
105+
);
106+
}
107+
108+
@Test
109+
public void givenSeq_whenGroupByAndFold_shouldReturnProperSeq() {
110+
111+
Map<Integer, List<Integer>> expectedAfterGroupBy = new HashMap<>();
112+
expectedAfterGroupBy.put(1, Arrays.asList(1, 3));
113+
expectedAfterGroupBy.put(0, Arrays.asList(2, 4));
114+
115+
assertEquals(
116+
Seq.of(1, 2, 3, 4).groupBy(i -> i % 2),
117+
expectedAfterGroupBy
118+
);
119+
120+
121+
assertEquals(
122+
Seq.of("a", "b", "c").foldLeft("!", (u, t) -> u + t),
123+
"!abc"
124+
);
125+
126+
127+
assertEquals(
128+
Seq.of("a", "b", "c").foldRight("!", (t, u) -> t + u),
129+
"abc!"
130+
);
131+
}
132+
133+
@Test
134+
public void givenSeq_whenUsingSeqWhile_shouldBehaveAsWhileLoop() {
135+
136+
assertEquals(
137+
Seq.of(1, 2, 3, 4, 5).skipWhile(i -> i < 3).toList(),
138+
Arrays.asList(3, 4, 5)
139+
);
140+
141+
assertEquals(
142+
Seq.of(1, 2, 3, 4, 5).skipUntil(i -> i == 3).toList(),
143+
Arrays.asList(3, 4, 5)
144+
);
145+
}
146+
147+
@Test
148+
public void givenSeq_whenZip_shouldHaveZippedSeq() {
149+
150+
assertEquals(
151+
Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c")).toList(),
152+
Arrays.asList(tuple(1, "a"), tuple(2, "b"), tuple(3, "c"))
153+
);
154+
155+
assertEquals(
156+
Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c"), (x, y) -> x + ":" + y).toList(),
157+
Arrays.asList("1:a", "2:b", "3:c")
158+
);
159+
160+
161+
assertEquals(
162+
Seq.of("a", "b", "c").zipWithIndex().toList(),
163+
Arrays.asList(tuple("a", 0L), tuple("b", 1L), tuple("c", 2L))
164+
);
165+
}
166+
167+
168+
public Integer methodThatThrowsChecked(String arg) throws Exception {
169+
return arg.length();
170+
}
171+
172+
@Test
173+
public void givenOperationThatThrowsCheckedException_whenExecuteAndNeedToWrapCheckedIntoUnchecked_shouldPass() {
174+
//when
175+
List<Integer> collect = Stream.of("a", "b", "c").map(elem -> {
176+
try {
177+
return methodThatThrowsChecked(elem);
178+
} catch (Exception e) {
179+
e.printStackTrace();
180+
throw new RuntimeException(e);
181+
}
182+
}).collect(Collectors.toList());
183+
184+
//then
185+
assertEquals(
186+
collect,
187+
Arrays.asList(1, 1, 1)
188+
);
189+
}
190+
191+
192+
@Test
193+
public void givenOperationThatThrowsCheckedException_whenExecuteUsingUncheckedFuction_shouldPass() {
194+
//when
195+
List<Integer> collect = Stream.of("a", "b", "c")
196+
.map(Unchecked.function(elem -> methodThatThrowsChecked(elem)))
197+
.collect(Collectors.toList());
198+
199+
//then
200+
assertEquals(
201+
collect,
202+
Arrays.asList(1, 1, 1)
203+
);
204+
}
205+
206+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<module>jaxb</module>
6767
<module>jee7</module>
6868
<module>jjwt</module>
69+
<module>jooq</module>
6970
<module>jpa-storedprocedure</module>
7071
<module>jsf</module>
7172
<module>json-path</module>

0 commit comments

Comments
 (0)