Skip to content
This repository was archived by the owner on Aug 12, 2024. It is now read-only.

Commit e36fed1

Browse files
authored
Make tests compatible with jdk 8 (#51)
* Make tests compatible with jdk 8 * Remove unnecessary exceptions
1 parent d0b3d89 commit e36fed1

10 files changed

Lines changed: 62 additions & 27 deletions

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<guava.version>28.2-jre</guava.version>
16+
<maven.compiler.source>8</maven.compiler.source>
17+
<maven.compiler.target>8</maven.compiler.target>
1618
</properties>
1719

1820
<scm>

src/test/java/com/spotify/dns/AbstractChangeNotifierTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import static org.mockito.Mockito.verify;
2727

2828
import java.util.Set;
29+
30+
import com.google.common.collect.Sets;
2931
import org.junit.Before;
3032
import org.junit.Rule;
3133
import org.junit.Test;
@@ -51,10 +53,10 @@ public class AbstractChangeNotifierTest {
5153
public void setUp() {
5254
MockitoAnnotations.initMocks(this);
5355

54-
sut = new AbstractChangeNotifier<>() {
56+
sut = new AbstractChangeNotifier<String>() {
5557
@Override
5658
public Set<String> current() {
57-
return Set.of("foo", "bar");
59+
return Sets.newHashSet("foo", "bar");
5860
}
5961

6062
@Override

src/test/java/com/spotify/dns/AggregatingChangeNotifierTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@
2121
import static org.mockito.Mockito.verify;
2222
import static org.mockito.Mockito.verifyNoMoreInteractions;
2323

24+
import java.util.Arrays;
2425
import java.util.List;
2526
import java.util.Set;
27+
28+
import com.google.common.collect.Sets;
2629
import org.junit.Test;
2730

2831
public class AggregatingChangeNotifierTest {
2932
@Test
3033
public void testEmptySet() {
3134
MyNotifier childNotifier = new MyNotifier();
32-
AggregatingChangeNotifier<String> notifier = new AggregatingChangeNotifier<>(List.of(childNotifier));
35+
AggregatingChangeNotifier<String> notifier = new AggregatingChangeNotifier<>(Arrays.asList(childNotifier));
3336

3437
ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class);
3538
notifier.setListener(listener, false);
3639

3740
verify(listener, never()).onChange(any(ChangeNotifier.ChangeNotification.class));
3841

39-
childNotifier.set(Set.of());
42+
childNotifier.set(Sets.newHashSet());
4043
verifyNoMoreInteractions(listener);
4144

4245
}

src/test/java/com/spotify/dns/DnsLookupPerformanceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.Ignore;
44
import org.junit.Test;
55

6+
import java.util.Arrays;
67
import java.util.List;
78
import java.util.concurrent.CompletableFuture;
89
import java.util.concurrent.CountDownLatch;
@@ -29,7 +30,7 @@ public class DnsLookupPerformanceTest {
2930
public void runTest() throws InterruptedException {
3031
int numThreads = 3;
3132
final ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
32-
List<String> records = List.of(
33+
List<String> records = Arrays.asList(
3334
"_spotify-noop._http.services.gew1.spotify.net.",
3435
"_spotify-noop._http.services.guc3.spotify.net.",
3536
"_spotify-noop._http.services.gae2.spotify.net.",

src/test/java/com/spotify/dns/DnsSrvResolversIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.spotify.dns.statistics.DnsReporter;
3333
import com.spotify.dns.statistics.DnsTimingContext;
3434
import java.util.ArrayList;
35+
import java.util.Arrays;
3536
import java.util.Collections;
3637
import java.util.List;
3738
import java.util.Set;
@@ -121,7 +122,7 @@ public void shouldReturnResultsUsingSpecifiedServers() throws Exception {
121122
final String server = new SimpleResolver().getAddress().getHostName();
122123
final DnsSrvResolver resolver = DnsSrvResolvers
123124
.newBuilder()
124-
.servers(List.of(server))
125+
.servers(Arrays.asList(server))
125126
.build();
126127
assertThat(resolver.resolve("_spotify-client._tcp.spotify.com").isEmpty(), is(false));
127128
assertThat(resolver.resolveAsync("_spotify-client._tcp.spotify.com").toCompletableFuture().get().isEmpty(), is(false));

src/test/java/com/spotify/dns/DnsSrvWatchersTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.hamcrest.Matchers.contains;
55
import static org.hamcrest.Matchers.is;
66

7+
import java.util.Arrays;
78
import java.util.List;
89
import java.util.Set;
910
import java.util.concurrent.CompletableFuture;
@@ -80,7 +81,7 @@ public FakeResolver(String fqdn, LookupResult result) {
8081
@Override
8182
public List<LookupResult> resolve(String fqdn) {
8283
if (this.fqdn.equals(fqdn)) {
83-
return List.of(result);
84+
return Arrays.asList(result);
8485
} else {
8586
return null;
8687
}
@@ -89,9 +90,9 @@ public List<LookupResult> resolve(String fqdn) {
8990
@Override
9091
public CompletionStage<List<LookupResult>> resolveAsync(String fqdn) {
9192
if (this.fqdn.equals(fqdn)) {
92-
return CompletableFuture.completedFuture(List.of(result));
93+
return CompletableFuture.completedFuture(Arrays.asList(result));
9394
} else {
94-
return CompletableFuture.failedFuture(new DnsException(this.fqdn + " != " + fqdn));
95+
return DnsTestUtil.failedFuture(new DnsException(this.fqdn + " != " + fqdn));
9596
}
9697
}
9798
}

src/test/java/com/spotify/dns/DnsTestUtil.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.spotify.dns;
1818

1919
import java.util.List;
20+
import java.util.concurrent.CompletableFuture;
2021
import java.util.stream.Collectors;
2122
import java.util.stream.Stream;
2223

@@ -29,4 +30,22 @@ static List<LookupResult> nodes(String... nodeNames) {
2930
.map(input -> LookupResult.create(input, 8080, 1, 2, 999))
3031
.collect(Collectors.toList());
3132
}
33+
34+
/**
35+
* method to replace CompletableFuture.failedFuture() from Java 9 in Java 8
36+
*/
37+
static CompletableFuture<List<LookupResult>> failedFuture(Exception ex) {
38+
CompletableFuture<List<LookupResult>> future = new CompletableFuture<>();
39+
future.completeExceptionally(ex);
40+
return future;
41+
}
42+
43+
/**
44+
* method to replace CompletableFuture.failedFuture() from Java 9 in Java 8
45+
*/
46+
static CompletableFuture<List<LookupResult>> failedFuture(Error error) {
47+
CompletableFuture<List<LookupResult>> future = new CompletableFuture<>();
48+
future.completeExceptionally(error);
49+
return future;
50+
}
3251
}

src/test/java/com/spotify/dns/MeteredDnsSrvResolverTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ public void shouldReportRuntimeException() throws Exception {
131131

132132
@Test
133133
public void shouldReportRuntimeExceptionAsync() throws Exception {
134-
when(delegate.resolveAsync(FQDN)).thenReturn(CompletableFuture.failedFuture((RUNTIME_EXCEPTION)));
134+
CompletableFuture<List<LookupResult>> future = new CompletableFuture<>();
135+
future.completeExceptionally(RUNTIME_EXCEPTION);
136+
when(delegate.resolveAsync(FQDN)).thenReturn(future);
135137

136138
try {
137139
resolver.resolveAsync(FQDN).toCompletableFuture().get();
@@ -161,7 +163,7 @@ public void shouldNotReportError() throws Exception {
161163

162164
@Test
163165
public void shouldNotReportErrorAsync() throws Exception {
164-
when(delegate.resolveAsync(FQDN)).thenReturn(CompletableFuture.failedFuture(ERROR));
166+
when(delegate.resolveAsync(FQDN)).thenReturn(DnsTestUtil.failedFuture(ERROR));
165167

166168
try {
167169
resolver.resolveAsync(FQDN).toCompletableFuture().get();

src/test/java/com/spotify/dns/RetainingDnsSrvResolverTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void shouldRetainDataOnFailure() {
125125
public void shouldRetainDataOnFailureAsync() throws ExecutionException, InterruptedException {
126126
when(delegate.resolveAsync(FQDN))
127127
.thenReturn(CompletableFuture.completedFuture(nodes1))
128-
.thenReturn(CompletableFuture.failedFuture(new DnsException("expected")));
128+
.thenReturn(DnsTestUtil.failedFuture(new DnsException("expected")));
129129

130130
resolver.resolveAsync(FQDN).toCompletableFuture().get();
131131

@@ -145,7 +145,7 @@ public void shouldThrowOnFailureAndNoDataAvailable() {
145145
@Test
146146
public void shouldThrowOnFailureAndNoDataAvailableAsync() throws ExecutionException, InterruptedException {
147147
DnsException cause = new DnsException("expected");
148-
when(delegate.resolveAsync(FQDN)).thenReturn(CompletableFuture.failedFuture(cause));
148+
when(delegate.resolveAsync(FQDN)).thenReturn(DnsTestUtil.failedFuture(cause));
149149

150150
thrown.expect(ExecutionException.class);
151151
thrown.expectCause(is(cause));
@@ -186,7 +186,7 @@ public void shouldNotStoreEmptyResultsAsync() throws ExecutionException, Interru
186186
DnsException cause = new DnsException("expected");
187187
when(delegate.resolveAsync(FQDN))
188188
.thenReturn(CompletableFuture.completedFuture(nodes()))
189-
.thenReturn(CompletableFuture.failedFuture(cause));
189+
.thenReturn(DnsTestUtil.failedFuture(cause));
190190

191191
resolver.resolveAsync(FQDN).toCompletableFuture().get();
192192

@@ -246,7 +246,7 @@ public void shouldNotRetainPastEndOfRetentionOnExceptionAsync() throws Exception
246246
DnsException expected = new DnsException("expected");
247247
when(delegate.resolveAsync(FQDN))
248248
.thenReturn(CompletableFuture.completedFuture(nodes("aresult")))
249-
.thenReturn(CompletableFuture.failedFuture(expected));
249+
.thenReturn(DnsTestUtil.failedFuture(expected));
250250

251251
resolver.resolveAsync(FQDN).toCompletableFuture().get();
252252

src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.spotify.dns;
1818

19-
import static java.util.List.of;
2019
import static org.hamcrest.CoreMatchers.is;
2120
import static org.hamcrest.Matchers.containsInAnyOrder;
2221
import static org.junit.Assert.assertThat;
@@ -29,8 +28,10 @@
2928
import static org.mockito.Mockito.verifyNoMoreInteractions;
3029
import static org.mockito.Mockito.when;
3130

31+
import java.util.Arrays;
3232
import java.util.List;
3333
import java.util.concurrent.CompletableFuture;
34+
import java.util.concurrent.ExecutionException;
3435
import java.util.function.Function;
3536
import org.junit.Before;
3637
import org.junit.Test;
@@ -62,9 +63,10 @@ public void shouldCallListenerOnChange() {
6263

6364
LookupResult result1 = result("host", 1234);
6465
LookupResult result2 = result("host", 4321);
65-
when(resolver.resolve(FQDN)).thenReturn(of(result1), of(result1, result2));
66+
when(resolver.resolve(FQDN)).thenReturn(Arrays.asList(result1), Arrays.asList(result1, result2));
6667
when(resolver.resolveAsync(FQDN))
67-
.thenReturn(CompletableFuture.completedFuture(of(result1)), CompletableFuture.completedFuture(of(result1, result2)));
68+
.thenReturn(CompletableFuture.completedFuture(Arrays.asList(result1)),
69+
CompletableFuture.completedFuture(Arrays.asList(result1, result2)));
6870

6971
sut.run();
7072
sut.run();
@@ -96,9 +98,9 @@ public void shouldCallListenerOnSet() {
9698

9799
LookupResult result = result("host", 1234);
98100
when(resolver.resolve(FQDN))
99-
.thenReturn(of(result));
101+
.thenReturn(Arrays.asList(result));
100102
when(resolver.resolveAsync(FQDN))
101-
.thenReturn(CompletableFuture.completedFuture(of(result)));
103+
.thenReturn(CompletableFuture.completedFuture(Arrays.asList(result)));
102104

103105
sut.run();
104106
sut.setListener(listener, true);
@@ -122,9 +124,10 @@ public void shouldReturnImmutableSets() {
122124
LookupResult result1 = result("host", 1234);
123125
LookupResult result2 = result("host", 4321);
124126
when(resolver.resolve(FQDN))
125-
.thenReturn(of(result1), of(result1, result2));
127+
.thenReturn(Arrays.asList(result1), Arrays.asList(result1, result2));
126128
when(resolver.resolveAsync(FQDN))
127-
.thenReturn(CompletableFuture.completedFuture(of(result1)), CompletableFuture.completedFuture(of(result1, result2)));
129+
.thenReturn(CompletableFuture.completedFuture(Arrays.asList(result1)),
130+
CompletableFuture.completedFuture(Arrays.asList(result1, result2)));
128131

129132
sut.run();
130133
sut.setListener(listener, true);
@@ -158,9 +161,10 @@ public void shouldOnlyChangeIfTransformedValuesChange() {
158161
LookupResult result1 = result("host", 1234);
159162
LookupResult result2 = result("host", 4321);
160163
when(resolver.resolve(FQDN))
161-
.thenReturn(of(result1), of(result1, result2));
164+
.thenReturn(Arrays.asList(result1), Arrays.asList(result1, result2));
162165
when(resolver.resolveAsync(FQDN))
163-
.thenReturn(CompletableFuture.completedFuture(of(result1)), CompletableFuture.completedFuture(of(result1, result2)));
166+
.thenReturn(CompletableFuture.completedFuture(Arrays.asList(result1)),
167+
CompletableFuture.completedFuture(Arrays.asList(result1, result2)));
164168

165169
sut.run();
166170
sut.run();
@@ -197,12 +201,12 @@ public void shouldDoSomethingWithNulls() {
197201
ChangeNotifier.Listener<String> listener = mock(ChangeNotifier.Listener.class);
198202

199203
when(resolver.resolve(FQDN))
200-
.thenReturn(of(
204+
.thenReturn(Arrays.asList(
201205
result("host1", 1234),
202206
result("host2", 1234),
203207
result("host3", 1234)));
204208
when(resolver.resolveAsync(FQDN))
205-
.thenReturn(CompletableFuture.completedFuture(of(
209+
.thenReturn(CompletableFuture.completedFuture(Arrays.asList(
206210
result("host1", 1234),
207211
result("host2", 1234),
208212
result("host3", 1234))));
@@ -228,7 +232,7 @@ public void shouldCallErrorHandlerOnResolveErrors() {
228232
when(resolver.resolve(FQDN))
229233
.thenThrow(exception);
230234
when(resolver.resolveAsync(FQDN))
231-
.thenReturn(CompletableFuture.failedFuture(exception));
235+
.thenReturn(DnsTestUtil.failedFuture(exception));
232236

233237
sut.setListener(listener, false);
234238
sut.run();

0 commit comments

Comments
 (0)