|
6 | 6 | package org.hibernate.reactive;
|
7 | 7 |
|
8 | 8 | import java.util.ArrayList;
|
9 |
| -import java.util.Arrays; |
10 | 9 | import java.util.List;
|
11 | 10 | import java.util.concurrent.CompletionStage;
|
12 | 11 | import java.util.stream.IntStream;
|
13 | 12 |
|
14 |
| -import org.hibernate.reactive.util.impl.CompletionStages; |
15 | 13 |
|
| 14 | +import static java.util.Arrays.asList; |
| 15 | +import static java.util.Arrays.stream; |
| 16 | +import static org.hibernate.reactive.util.impl.CompletionStages.*; |
16 | 17 | import static org.hibernate.reactive.util.impl.CompletionStages.total;
|
17 | 18 | import static org.hibernate.reactive.util.impl.CompletionStages.loop;
|
18 | 19 | import static org.hibernate.reactive.util.impl.CompletionStages.loopWithoutTrampoline;
|
|
23 | 24 | import io.vertx.ext.unit.Async;
|
24 | 25 | import io.vertx.ext.unit.TestContext;
|
25 | 26 | import io.vertx.ext.unit.junit.VertxUnitRunner;
|
26 |
| -import org.assertj.core.api.Assertions; |
| 27 | + |
27 | 28 | import static org.assertj.core.api.Assertions.assertThat;
|
28 | 29 |
|
29 | 30 | @RunWith(VertxUnitRunner.class)
|
30 | 31 | public class CompletionStagesTest {
|
31 | 32 |
|
32 |
| - protected static void test(TestContext context, CompletionStage<?> cs) { |
33 |
| - // this will be added to TestContext in the next vert.x release |
34 |
| - Async async = context.async(); |
35 |
| - cs.whenComplete( (res, err) -> { |
36 |
| - if ( err != null ) { |
37 |
| - context.fail( err ); |
38 |
| - } |
39 |
| - else { |
40 |
| - async.complete(); |
41 |
| - } |
42 |
| - } ); |
43 |
| - } |
44 |
| - |
45 | 33 | @Test
|
46 | 34 | public void testTotalWithIntegers(TestContext context) {
|
47 | 35 | int startInt = 0;
|
48 | 36 | int endInt = 11;
|
| 37 | + int expectedTotal = IntStream.range( startInt, endInt ).sum(); |
49 | 38 |
|
50 |
| - // create a value of 'index + 2' and add it to the total. Result should be 77 |
51 |
| - test( context, |
52 |
| - total( startInt, endInt, index -> CompletionStages.completedFuture( index + 2 ) ) |
53 |
| - .thenAccept( total -> Assertions.assertThat( total ).isEqualTo( 77 ) ) |
54 |
| - ); |
| 39 | + test( context, total( startInt, endInt, index -> completedFuture( index ) ) |
| 40 | + .thenAccept( total -> assertThat( total ).isEqualTo( expectedTotal ) ) ); |
55 | 41 | }
|
56 | 42 |
|
57 | 43 | @Test
|
58 | 44 | public void testTotalWithIterator(TestContext context) {
|
59 |
| - List<Object> entries = Arrays.asList( "a", "b", "c", "d", "e" ); |
60 |
| - int incrementAddedPerEntry = 3; |
| 45 | + int increment = 3; |
| 46 | + Object[] entries = { "a", "b", "c", "d", "e" }; |
61 | 47 | List<Object> looped = new ArrayList<>();
|
62 | 48 |
|
63 |
| - test( context, |
64 |
| - total( |
65 |
| - entries.iterator(), |
66 |
| - entry -> CompletionStages.voidFuture() |
67 |
| - .thenAccept( v -> looped.add( entry ) ) |
68 |
| - .thenApply( v -> incrementAddedPerEntry ) |
69 |
| - ) |
70 |
| - .thenAccept( total -> { |
71 |
| - assertThat( total ).isEqualTo( entries.size() * incrementAddedPerEntry ); |
72 |
| - assertThat( looped ).containsExactly( entries.toArray( new Object[entries.size()] ) ); |
73 |
| - } ) |
| 49 | + test( context, total( asList( entries ).iterator(), entry -> voidFuture() |
| 50 | + .thenAccept( v -> looped.add( entry ) ) |
| 51 | + .thenApply( v -> increment ) ) |
| 52 | + .thenAccept( total -> { |
| 53 | + assertThat( total ).isEqualTo( entries.length * increment ); |
| 54 | + assertThat( looped ).containsExactly( entries ); |
| 55 | + } ) |
74 | 56 | );
|
75 | 57 | }
|
76 | 58 |
|
77 | 59 | @Test
|
78 | 60 | public void testTotalWithArray(TestContext context) {
|
79 |
| - String[] entries = { "a", "b", "c", "d", "e" }; |
80 |
| - int incrementAddedPerEntry = 2; |
| 61 | + int increment = 2; |
| 62 | + Object[] entries = { "a", "b", "c", "d", "e" }; |
81 | 63 | List<Object> looped = new ArrayList<>();
|
82 | 64 |
|
83 |
| - test( context, |
84 |
| - total( entries, entry -> CompletionStages.voidFuture() |
85 |
| - .thenAccept( v -> looped.add( entry ) ) |
86 |
| - .thenApply( v -> incrementAddedPerEntry ) ) |
87 |
| - .thenAccept( total -> { |
88 |
| - assertThat( total ).isEqualTo( entries.length * incrementAddedPerEntry ); |
89 |
| - assertThat( looped ).containsExactly( entries ); |
90 |
| - } ) |
| 65 | + test( context, total( entries, entry -> voidFuture() |
| 66 | + .thenAccept( v -> looped.add( entry ) ) |
| 67 | + .thenApply( v -> increment ) ) |
| 68 | + .thenAccept( total -> { |
| 69 | + assertThat( total ).isEqualTo( entries.length * increment ); |
| 70 | + assertThat( looped ).containsExactly( entries ); |
| 71 | + } ) |
91 | 72 | );
|
92 | 73 | }
|
93 | 74 |
|
94 | 75 | @Test
|
95 | 76 | public void testLoopOnArray(TestContext context) {
|
96 |
| - String[] entries = { "a", "b", "c", "d", "e" }; |
97 |
| - List<String> looped = new ArrayList<>(); |
| 77 | + Object[] entries = { "a", "b", "c", "d", "e" }; |
| 78 | + List<Object> looped = new ArrayList<>(); |
98 | 79 |
|
99 |
| - test( context, |
100 |
| - loop( entries, entry -> CompletionStages.completedFuture( looped.add( entry ) ) ) |
101 |
| - .thenAccept( count -> assertThat( looped ) |
102 |
| - .containsExactly( entries ) ) |
103 |
| - ); |
| 80 | + test( context, loop( entries, entry -> completedFuture( looped.add( entry ) ) ) |
| 81 | + .thenAccept( v -> assertThat( looped ).containsExactly( entries ) ) ); |
104 | 82 | }
|
105 | 83 |
|
106 | 84 | @Test
|
107 | 85 | public void testLoopOnIterator(TestContext context) {
|
108 |
| - List<Object> entries = Arrays.asList( "a", "b", "c", "d", "e" ); |
| 86 | + Object[] entries = { "a", "b", "c", "d", "e" }; |
109 | 87 | List<Object> looped = new ArrayList<>();
|
110 | 88 |
|
111 |
| - test( context, |
112 |
| - loop( |
113 |
| - entries.iterator(), |
114 |
| - entry -> CompletionStages.completedFuture( looped.add( entry ) ) |
115 |
| - ) |
116 |
| - .thenAccept( count -> assertThat( looped ) |
117 |
| - .containsExactly( entries.toArray( new Object[entries.size()] ) ) ) |
118 |
| - ); |
| 89 | + test( context, loop( asList( entries ).iterator(), entry -> completedFuture( looped.add( entry ) ) ) |
| 90 | + .thenAccept( v -> assertThat( looped ).containsExactly( entries ) ) ); |
119 | 91 | }
|
120 | 92 |
|
121 | 93 | @Test
|
122 | 94 | public void testLoopOnIterable(TestContext context) {
|
123 |
| - List<String> entries = Arrays.asList( "a", "b", "c", "d", "e" ); |
124 |
| - List<String> looped = new ArrayList<>(); |
125 |
| - |
126 |
| - test( context, |
127 |
| - loop( |
128 |
| - entries, |
129 |
| - entry -> CompletionStages.completedFuture( looped.add( entry ) ) |
130 |
| - ).thenAccept( count -> assertThat( looped ) |
131 |
| - .containsExactly( entries.toArray( new String[entries.size()] ) ) ) |
132 |
| - ); |
| 95 | + Object[] entries = { "a", "b", "c", "d", "e" }; |
| 96 | + List<Object> looped = new ArrayList<>(); |
| 97 | + |
| 98 | + test( context, loop( asList( entries ), entry -> completedFuture( looped.add( entry ) ) ) |
| 99 | + .thenAccept( count -> assertThat( looped ).containsExactly( entries ) ) ); |
133 | 100 | }
|
134 | 101 |
|
135 | 102 | @Test
|
136 | 103 | public void testLoopOnStream(TestContext context) {
|
137 |
| - String[] entries = { "a", "b", "c", "d", "e" }; |
138 |
| - List<String> looped = new ArrayList<>(); |
139 |
| - |
140 |
| - test( context, |
141 |
| - loop( |
142 |
| - Arrays.stream( entries ), |
143 |
| - entry -> CompletionStages.completedFuture( looped.add( entry ) ) |
144 |
| - ) |
145 |
| - .thenAccept( count -> assertThat( looped.toArray( new String[looped.size()] ) ) |
146 |
| - .containsExactly( entries ) ) |
147 |
| - ); |
| 104 | + Object[] entries = { "a", "b", "c", "d", "e" }; |
| 105 | + List<Object> looped = new ArrayList<>(); |
| 106 | + |
| 107 | + test( context, loop( stream( entries ), entry -> completedFuture( looped.add( entry ) ) ) |
| 108 | + .thenAccept( count -> assertThat( looped ).containsExactly( entries ) ) ); |
148 | 109 | }
|
149 | 110 |
|
150 | 111 | @Test
|
151 | 112 | public void testLoopOnIntStream(TestContext context) {
|
152 |
| - List<Object> entries = Arrays.asList( "a", "b", "c", "d", "e" ); |
| 113 | + Object[] entries = { "a", "b", "c", "d", "e" }; |
153 | 114 | List<Object> looped = new ArrayList<>();
|
154 | 115 |
|
155 |
| - test( context, |
156 |
| - loop( |
157 |
| - IntStream.range( 0, entries.size() ), |
158 |
| - index -> CompletionStages.voidFuture( looped.add( entries.get( index ) ) ) |
159 |
| - ).thenAccept( count -> assertThat( looped ) |
160 |
| - .containsExactly( entries.toArray( new Object[entries.size()] ) ) ) |
| 116 | + test( context, loop( |
| 117 | + IntStream.range( 0, entries.length ), |
| 118 | + index -> voidFuture( looped.add( entries[index] ) ) |
| 119 | + ).thenAccept( count -> assertThat( looped ).containsExactly( entries ) ) |
161 | 120 | );
|
162 | 121 | }
|
163 | 122 |
|
164 | 123 | @Test
|
165 | 124 | public void testLoopOnIntStreamWithoutTrampoline(TestContext context) {
|
166 |
| - List<Object> entries = Arrays.asList( "a", "b", "c", "d", "e" ); |
| 125 | + Object[] entries = { "a", "b", "c", "d", "e" }; |
167 | 126 | List<Object> looped = new ArrayList<>();
|
168 | 127 |
|
169 |
| - test( context, |
170 |
| - loopWithoutTrampoline( |
171 |
| - IntStream.range( 0, entries.size() ), |
172 |
| - index -> CompletionStages.voidFuture( looped.add( entries.get( index ) ) ) |
173 |
| - ).thenAccept( count -> assertThat( looped ) |
174 |
| - .containsExactly( entries.toArray( new Object[entries.size()] ) ) ) |
| 128 | + test( context, loopWithoutTrampoline( |
| 129 | + IntStream.range( 0, entries.length ), |
| 130 | + index -> voidFuture( looped.add( entries[index] ) ) |
| 131 | + ).thenAccept( count -> assertThat( looped ).containsExactly( entries ) ) |
175 | 132 | );
|
176 | 133 | }
|
177 | 134 |
|
178 | 135 | @Test
|
179 | 136 | public void testLoopWithIteratorWithoutTrampoline(TestContext context) {
|
180 |
| - List<Object> entries = Arrays.asList( "a", "b", "c", "d", "e" ); |
| 137 | + Object[] entries = { "a", "b", "c", "d", "e" }; |
181 | 138 | List<Object> looped = new ArrayList<>();
|
182 | 139 |
|
183 |
| - test( context, |
184 |
| - loopWithoutTrampoline( |
185 |
| - entries.iterator(), |
186 |
| - entry -> CompletionStages.voidFuture( looped.add( entry ) ) |
187 |
| - ).thenAccept( count -> assertThat( looped ) |
188 |
| - .containsExactly( entries.toArray( new Object[entries.size()] ) ) ) |
| 140 | + test( context, loopWithoutTrampoline( |
| 141 | + asList( entries ).iterator(), |
| 142 | + entry -> voidFuture( looped.add( entry ) ) |
| 143 | + ).thenAccept( count -> assertThat( looped ).containsExactly( entries ) ) |
189 | 144 | );
|
190 | 145 | }
|
| 146 | + |
| 147 | + private static void test(TestContext context, CompletionStage<?> cs) { |
| 148 | + Async async = context.async(); |
| 149 | + cs.whenComplete( (res, err) -> { |
| 150 | + if ( err != null ) { |
| 151 | + context.fail( err ); |
| 152 | + } |
| 153 | + else { |
| 154 | + async.complete(); |
| 155 | + } |
| 156 | + } ); |
| 157 | + } |
191 | 158 | }
|
0 commit comments