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 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -657,18 +657,15 @@ public class DefaultLifecycleProcessorTests {
// invocation order in the 'stoppedBeans' list // invocation order in the 'stoppedBeans' list
stop(); stop();
final int delay = this.shutdownDelay; final int delay = this.shutdownDelay;
new Thread(new Runnable() { new Thread(() -> {
@Override try {
public void run() { Thread.sleep(delay);
try { }
Thread.sleep(delay); catch (InterruptedException e) {
} // ignore
catch (InterruptedException e) { }
// ignore finally {
} callback.run();
finally {
callback.run();
}
} }
}).start(); }).start();
} }

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -422,12 +422,7 @@ public class EnableSchedulingTests {
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler()); taskRegistrar.setScheduler(taskScheduler());
taskRegistrar.addFixedRateTask(new IntervalTask( taskRegistrar.addFixedRateTask(new IntervalTask(
new Runnable() { () -> worker().executedByThread = Thread.currentThread().getName(),
@Override
public void run() {
worker().executedByThread = Thread.currentThread().getName();
}
},
10, 0)); 10, 0));
} }
} }

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

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

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

@ -42,15 +42,11 @@ public class EmbeddedDatabaseBuilderTests {
@Test @Test
public void addDefaultScripts() throws Exception { public void addDefaultScripts() throws Exception {
doTwice(new Runnable() { doTwice(() -> {
EmbeddedDatabase db = new EmbeddedDatabaseBuilder()//
@Override .addDefaultScripts()//
public void run() { .build();
EmbeddedDatabase db = new EmbeddedDatabaseBuilder()// assertDatabaseCreatedAndShutdown(db);
.addDefaultScripts()//
.build();
assertDatabaseCreatedAndShutdown(db);
}
}); });
} }
@ -62,105 +58,77 @@ public class EmbeddedDatabaseBuilderTests {
@Test @Test
public void addScript() throws Exception { public void addScript() throws Exception {
doTwice(new Runnable() { doTwice(() -> {
EmbeddedDatabase db = builder//
@Override .addScript("db-schema.sql")//
public void run() { .addScript("db-test-data.sql")//
EmbeddedDatabase db = builder// .build();
.addScript("db-schema.sql")// assertDatabaseCreatedAndShutdown(db);
.addScript("db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
}); });
} }
@Test @Test
public void addScripts() throws Exception { public void addScripts() throws Exception {
doTwice(new Runnable() { doTwice(() -> {
EmbeddedDatabase db = builder//
@Override .addScripts("db-schema.sql", "db-test-data.sql")//
public void run() { .build();
EmbeddedDatabase db = builder// assertDatabaseCreatedAndShutdown(db);
.addScripts("db-schema.sql", "db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
}); });
} }
@Test @Test
public void addScriptsWithDefaultCommentPrefix() throws Exception { public void addScriptsWithDefaultCommentPrefix() throws Exception {
doTwice(new Runnable() { doTwice(() -> {
EmbeddedDatabase db = builder//
@Override .addScripts("db-schema-comments.sql", "db-test-data.sql")//
public void run() { .build();
EmbeddedDatabase db = builder// assertDatabaseCreatedAndShutdown(db);
.addScripts("db-schema-comments.sql", "db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
}); });
} }
@Test @Test
public void addScriptsWithCustomCommentPrefix() throws Exception { public void addScriptsWithCustomCommentPrefix() throws Exception {
doTwice(new Runnable() { doTwice(() -> {
EmbeddedDatabase db = builder//
@Override .addScripts("db-schema-custom-comments.sql", "db-test-data.sql")//
public void run() { .setCommentPrefix("~")//
EmbeddedDatabase db = builder// .build();
.addScripts("db-schema-custom-comments.sql", "db-test-data.sql")// assertDatabaseCreatedAndShutdown(db);
.setCommentPrefix("~")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
}); });
} }
@Test @Test
public void addScriptsWithCustomBlockComments() throws Exception { public void addScriptsWithCustomBlockComments() throws Exception {
doTwice(new Runnable() { doTwice(() -> {
EmbeddedDatabase db = builder//
@Override .addScripts("db-schema-block-comments.sql", "db-test-data.sql")//
public void run() { .setBlockCommentStartDelimiter("{*")//
EmbeddedDatabase db = builder// .setBlockCommentEndDelimiter("*}")//
.addScripts("db-schema-block-comments.sql", "db-test-data.sql")// .build();
.setBlockCommentStartDelimiter("{*")// assertDatabaseCreatedAndShutdown(db);
.setBlockCommentEndDelimiter("*}")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
}); });
} }
@Test @Test
public void setTypeToH2() throws Exception { public void setTypeToH2() throws Exception {
doTwice(new Runnable() { doTwice(() -> {
EmbeddedDatabase db = builder//
@Override .setType(H2)//
public void run() { .addScripts("db-schema.sql", "db-test-data.sql")//
EmbeddedDatabase db = builder// .build();
.setType(H2)// assertDatabaseCreatedAndShutdown(db);
.addScripts("db-schema.sql", "db-test-data.sql")//
.build();
assertDatabaseCreatedAndShutdown(db);
}
}); });
} }
@Test @Test
public void setTypeToDerbyAndIgnoreFailedDrops() throws Exception { public void setTypeToDerbyAndIgnoreFailedDrops() throws Exception {
doTwice(new Runnable() { doTwice(() -> {
EmbeddedDatabase db = builder//
@Override .setType(DERBY)//
public void run() { .ignoreFailedDrops(true)//
EmbeddedDatabase db = builder// .addScripts("db-schema-derby-with-drop.sql", "db-test-data.sql").build();
.setType(DERBY)// assertDatabaseCreatedAndShutdown(db);
.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 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -130,12 +130,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
return; return;
} }
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
this.activeMQBroker.addShutdownHook(new Runnable() { this.activeMQBroker.addShutdownHook(latch::countDown);
@Override
public void run() {
latch.countDown();
}
});
this.activeMQBroker.stop(); this.activeMQBroker.stop();
assertThat(latch.await(5, TimeUnit.SECONDS)).as("Broker did not stop").isTrue(); assertThat(latch.await(5, TimeUnit.SECONDS)).as("Broker did not stop").isTrue();
logger.debug("Broker stopped"); logger.debug("Broker stopped");

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

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

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -215,12 +215,7 @@ public class WebAsyncManagerTimeoutTests {
public void startDeferredResultProcessingTimeoutAndResumeThroughCallback() throws Exception { public void startDeferredResultProcessingTimeoutAndResumeThroughCallback() throws Exception {
final DeferredResult<Integer> deferredResult = new DeferredResult<>(); final DeferredResult<Integer> deferredResult = new DeferredResult<>();
deferredResult.onTimeout(new Runnable() { deferredResult.onTimeout(() -> deferredResult.setResult(23));
@Override
public void run() {
deferredResult.setResult(23);
}
});
this.asyncManager.startDeferredResultProcessing(deferredResult); this.asyncManager.startDeferredResultProcessing(deferredResult);

Loading…
Cancel
Save