Browse Source

Replace anonymous inner classes with lambdas in tests

Closes gh-24808
pull/24815/head
Qimiao Chen 6 years ago committed by GitHub
parent
commit
7c831d2ef4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      spring-context/src/test/java/org/springframework/context/support/DefaultLifecycleProcessorTests.java
  2. 9
      spring-context/src/test/java/org/springframework/scheduling/annotation/EnableSchedulingTests.java
  3. 53
      spring-core/src/test/java/org/springframework/util/concurrent/SettableListenableFutureTests.java
  4. 124
      spring-jdbc/src/test/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilderTests.java
  5. 9
      spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandlerIntegrationTests.java
  6. 16
      spring-web/src/test/java/org/springframework/web/context/request/async/DeferredResultTests.java
  7. 9
      spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerTimeoutTests.java

23
spring-context/src/test/java/org/springframework/context/support/DefaultLifecycleProcessorTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -657,18 +657,15 @@ public class DefaultLifecycleProcessorTests { @@ -657,18 +657,15 @@ public class DefaultLifecycleProcessorTests {
// invocation order in the 'stoppedBeans' list
stop();
final int delay = this.shutdownDelay;
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(delay);
}
catch (InterruptedException e) {
// ignore
}
finally {
callback.run();
}
new Thread(() -> {
try {
Thread.sleep(delay);
}
catch (InterruptedException e) {
// ignore
}
finally {
callback.run();
}
}).start();
}

9
spring-context/src/test/java/org/springframework/scheduling/annotation/EnableSchedulingTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -422,12 +422,7 @@ public class EnableSchedulingTests { @@ -422,12 +422,7 @@ public class EnableSchedulingTests {
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler());
taskRegistrar.addFixedRateTask(new IntervalTask(
new Runnable() {
@Override
public void run() {
worker().executedByThread = Thread.currentThread().getName();
}
},
() -> worker().executedByThread = Thread.currentThread().getName(),
10, 0));
}
}

53
spring-core/src/test/java/org/springframework/util/concurrent/SettableListenableFutureTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -229,16 +229,13 @@ class SettableListenableFutureTests { @@ -229,16 +229,13 @@ class SettableListenableFutureTests {
void getWaitsForCompletion() throws ExecutionException, InterruptedException {
final String string = "hello";
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(20L);
settableListenableFuture.set(string);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
new Thread(() -> {
try {
Thread.sleep(20L);
settableListenableFuture.set(string);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}).start();
@ -258,16 +255,13 @@ class SettableListenableFutureTests { @@ -258,16 +255,13 @@ class SettableListenableFutureTests {
void getWithTimeoutWaitsForCompletion() throws ExecutionException, InterruptedException, TimeoutException {
final String string = "hello";
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(20L);
settableListenableFuture.set(string);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
new Thread(() -> {
try {
Thread.sleep(20L);
settableListenableFuture.set(string);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}).start();
@ -347,16 +341,13 @@ class SettableListenableFutureTests { @@ -347,16 +341,13 @@ class SettableListenableFutureTests {
@Test
void cancelStateThrowsExceptionWhenCallingGetWithTimeout() throws ExecutionException, TimeoutException, InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(20L);
settableListenableFuture.cancel(true);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
new Thread(() -> {
try {
Thread.sleep(20L);
settableListenableFuture.cancel(true);
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}).start();

124
spring-jdbc/src/test/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilderTests.java

@ -42,15 +42,11 @@ public class EmbeddedDatabaseBuilderTests { @@ -42,15 +42,11 @@ public class EmbeddedDatabaseBuilderTests {
@Test
public void addDefaultScripts() throws Exception {
doTwice(new Runnable() {
@Override
public void run() {
EmbeddedDatabase db = new EmbeddedDatabaseBuilder()//
.addDefaultScripts()//
.build();
assertDatabaseCreatedAndShutdown(db);
}
doTwice(() -> {
EmbeddedDatabase db = new EmbeddedDatabaseBuilder()//
.addDefaultScripts()//
.build();
assertDatabaseCreatedAndShutdown(db);
});
}
@ -62,105 +58,77 @@ public class EmbeddedDatabaseBuilderTests { @@ -62,105 +58,77 @@ public class EmbeddedDatabaseBuilderTests {
@Test
public void addScript() throws Exception {
doTwice(new Runnable() {
@Override
public void run() {
EmbeddedDatabase db = builder//
.addScript("db-schema.sql")//
.addScript("db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
doTwice(() -> {
EmbeddedDatabase db = builder//
.addScript("db-schema.sql")//
.addScript("db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
});
}
@Test
public void addScripts() throws Exception {
doTwice(new Runnable() {
@Override
public void run() {
EmbeddedDatabase db = builder//
.addScripts("db-schema.sql", "db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
doTwice(() -> {
EmbeddedDatabase db = builder//
.addScripts("db-schema.sql", "db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
});
}
@Test
public void addScriptsWithDefaultCommentPrefix() throws Exception {
doTwice(new Runnable() {
@Override
public void run() {
EmbeddedDatabase db = builder//
.addScripts("db-schema-comments.sql", "db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
doTwice(() -> {
EmbeddedDatabase db = builder//
.addScripts("db-schema-comments.sql", "db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
});
}
@Test
public void addScriptsWithCustomCommentPrefix() throws Exception {
doTwice(new Runnable() {
@Override
public void run() {
EmbeddedDatabase db = builder//
.addScripts("db-schema-custom-comments.sql", "db-test-data.sql")//
.setCommentPrefix("~")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
doTwice(() -> {
EmbeddedDatabase db = builder//
.addScripts("db-schema-custom-comments.sql", "db-test-data.sql")//
.setCommentPrefix("~")//
.build();
assertDatabaseCreatedAndShutdown(db);
});
}
@Test
public void addScriptsWithCustomBlockComments() throws Exception {
doTwice(new Runnable() {
@Override
public void run() {
EmbeddedDatabase db = builder//
.addScripts("db-schema-block-comments.sql", "db-test-data.sql")//
.setBlockCommentStartDelimiter("{*")//
.setBlockCommentEndDelimiter("*}")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
doTwice(() -> {
EmbeddedDatabase db = builder//
.addScripts("db-schema-block-comments.sql", "db-test-data.sql")//
.setBlockCommentStartDelimiter("{*")//
.setBlockCommentEndDelimiter("*}")//
.build();
assertDatabaseCreatedAndShutdown(db);
});
}
@Test
public void setTypeToH2() throws Exception {
doTwice(new Runnable() {
@Override
public void run() {
EmbeddedDatabase db = builder//
.setType(H2)//
.addScripts("db-schema.sql", "db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
doTwice(() -> {
EmbeddedDatabase db = builder//
.setType(H2)//
.addScripts("db-schema.sql", "db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
});
}
@Test
public void setTypeToDerbyAndIgnoreFailedDrops() throws Exception {
doTwice(new Runnable() {
@Override
public void run() {
EmbeddedDatabase db = builder//
.setType(DERBY)//
.ignoreFailedDrops(true)//
.addScripts("db-schema-derby-with-drop.sql", "db-test-data.sql").build();
assertDatabaseCreatedAndShutdown(db);
}
doTwice(() -> {
EmbeddedDatabase db = builder//
.setType(DERBY)//
.ignoreFailedDrops(true)//
.addScripts("db-schema-derby-with-drop.sql", "db-test-data.sql").build();
assertDatabaseCreatedAndShutdown(db);
});
}

9
spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandlerIntegrationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -130,12 +130,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests { @@ -130,12 +130,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
return;
}
final CountDownLatch latch = new CountDownLatch(1);
this.activeMQBroker.addShutdownHook(new Runnable() {
@Override
public void run() {
latch.countDown();
}
});
this.activeMQBroker.addShutdownHook(latch::countDown);
this.activeMQBroker.stop();
assertThat(latch.await(5, TimeUnit.SECONDS)).as("Broker did not stop").isTrue();
logger.debug("Broker stopped");

16
spring-web/src/test/java/org/springframework/web/context/request/async/DeferredResultTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -93,12 +93,7 @@ public class DeferredResultTests { @@ -93,12 +93,7 @@ public class DeferredResultTests {
final StringBuilder sb = new StringBuilder();
DeferredResult<String> result = new DeferredResult<>();
result.onCompletion(new Runnable() {
@Override
public void run() {
sb.append("completion event");
}
});
result.onCompletion(() -> sb.append("completion event"));
result.getInterceptor().afterCompletion(null, null);
@ -114,12 +109,7 @@ public class DeferredResultTests { @@ -114,12 +109,7 @@ public class DeferredResultTests {
DeferredResult<String> result = new DeferredResult<>(null, "timeout result");
result.setResultHandler(handler);
result.onTimeout(new Runnable() {
@Override
public void run() {
sb.append("timeout event");
}
});
result.onTimeout(() -> sb.append("timeout event"));
result.getInterceptor().handleTimeout(null, null);

9
spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerTimeoutTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -215,12 +215,7 @@ public class WebAsyncManagerTimeoutTests { @@ -215,12 +215,7 @@ public class WebAsyncManagerTimeoutTests {
public void startDeferredResultProcessingTimeoutAndResumeThroughCallback() throws Exception {
final DeferredResult<Integer> deferredResult = new DeferredResult<>();
deferredResult.onTimeout(new Runnable() {
@Override
public void run() {
deferredResult.setResult(23);
}
});
deferredResult.onTimeout(() -> deferredResult.setResult(23));
this.asyncManager.startDeferredResultProcessing(deferredResult);

Loading…
Cancel
Save