Skip to content

Commit 920a07d

Browse files
committed
[#487] Refactor CompletionStagesTest
1 parent 7e6e596 commit 920a07d

File tree

1 file changed

+69
-102
lines changed

1 file changed

+69
-102
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/CompletionStagesTest.java

Lines changed: 69 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
package org.hibernate.reactive;
77

88
import java.util.ArrayList;
9-
import java.util.Arrays;
109
import java.util.List;
1110
import java.util.concurrent.CompletionStage;
1211
import java.util.stream.IntStream;
1312

14-
import org.hibernate.reactive.util.impl.CompletionStages;
1513

14+
import static java.util.Arrays.asList;
15+
import static java.util.Arrays.stream;
16+
import static org.hibernate.reactive.util.impl.CompletionStages.*;
1617
import static org.hibernate.reactive.util.impl.CompletionStages.total;
1718
import static org.hibernate.reactive.util.impl.CompletionStages.loop;
1819
import static org.hibernate.reactive.util.impl.CompletionStages.loopWithoutTrampoline;
@@ -23,169 +24,135 @@
2324
import io.vertx.ext.unit.Async;
2425
import io.vertx.ext.unit.TestContext;
2526
import io.vertx.ext.unit.junit.VertxUnitRunner;
26-
import org.assertj.core.api.Assertions;
27+
2728
import static org.assertj.core.api.Assertions.assertThat;
2829

2930
@RunWith(VertxUnitRunner.class)
3031
public class CompletionStagesTest {
3132

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-
4533
@Test
4634
public void testTotalWithIntegers(TestContext context) {
4735
int startInt = 0;
4836
int endInt = 11;
37+
int expectedTotal = IntStream.range( startInt, endInt ).sum();
4938

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 ) ) );
5541
}
5642

5743
@Test
5844
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" };
6147
List<Object> looped = new ArrayList<>();
6248

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+
} )
7456
);
7557
}
7658

7759
@Test
7860
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" };
8163
List<Object> looped = new ArrayList<>();
8264

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+
} )
9172
);
9273
}
9374

9475
@Test
9576
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<>();
9879

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 ) ) );
10482
}
10583

10684
@Test
10785
public void testLoopOnIterator(TestContext context) {
108-
List<Object> entries = Arrays.asList( "a", "b", "c", "d", "e" );
86+
Object[] entries = { "a", "b", "c", "d", "e" };
10987
List<Object> looped = new ArrayList<>();
11088

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 ) ) );
11991
}
12092

12193
@Test
12294
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 ) ) );
133100
}
134101

135102
@Test
136103
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 ) ) );
148109
}
149110

150111
@Test
151112
public void testLoopOnIntStream(TestContext context) {
152-
List<Object> entries = Arrays.asList( "a", "b", "c", "d", "e" );
113+
Object[] entries = { "a", "b", "c", "d", "e" };
153114
List<Object> looped = new ArrayList<>();
154115

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 ) )
161120
);
162121
}
163122

164123
@Test
165124
public void testLoopOnIntStreamWithoutTrampoline(TestContext context) {
166-
List<Object> entries = Arrays.asList( "a", "b", "c", "d", "e" );
125+
Object[] entries = { "a", "b", "c", "d", "e" };
167126
List<Object> looped = new ArrayList<>();
168127

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 ) )
175132
);
176133
}
177134

178135
@Test
179136
public void testLoopWithIteratorWithoutTrampoline(TestContext context) {
180-
List<Object> entries = Arrays.asList( "a", "b", "c", "d", "e" );
137+
Object[] entries = { "a", "b", "c", "d", "e" };
181138
List<Object> looped = new ArrayList<>();
182139

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 ) )
189144
);
190145
}
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+
}
191158
}

0 commit comments

Comments
 (0)