|
| 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 | +} |
0 commit comments