INT-4219: Test Polishing

JIRA: https://jira.spring.io/browse/INT-4219

- Use `AdditionalAnswers.returnFirstArgument` instead of lambda to return first arg
- Enable CheckStyle `AvoidStaticImport` for tests
- Fix static import violations in tests
This commit is contained in:
Gary Russell
2017-03-05 22:19:01 -05:00
committed by Artem Bilan
parent dd0c426d14
commit 37683f1b80
13 changed files with 70 additions and 52 deletions

View File

@@ -25,6 +25,7 @@ import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.Mockito.mock;
@@ -843,7 +844,7 @@ public class MethodInvokingMessageProcessorTests {
helper = new MessagingMethodInvokerHelper<>(bean,
UseSpelInvokerBean.class.getDeclaredMethod("buz", String.class), false);
ConfigurableListableBeanFactory bf = mock(ConfigurableListableBeanFactory.class);
willAnswer(i -> i.getArgument(0)).given(bf).resolveEmbeddedValue(anyString());
willAnswer(returnsFirstArg()).given(bf).resolveEmbeddedValue(anyString());
helper.setBeanFactory(bf);
try {
helper.process(message);

View File

@@ -45,9 +45,9 @@ public class LockRegistryLeaderInitiatorTests {
private CountDownLatch revoked;
private LockRegistry registry = new DefaultLockRegistry();
private final LockRegistry registry = new DefaultLockRegistry();
private LockRegistryLeaderInitiator initiator =
private final LockRegistryLeaderInitiator initiator =
new LockRegistryLeaderInitiator(this.registry, new DefaultCandidate());
@Before
@@ -90,10 +90,10 @@ public class LockRegistryLeaderInitiatorTests {
CountDownLatch other = new CountDownLatch(1);
another.setLeaderEventPublisher(new CountingPublisher(other));
this.initiator.start();
assertThat(this.granted.await(10, TimeUnit.SECONDS), is(true));
assertThat(this.granted.await(20, TimeUnit.SECONDS), is(true));
another.start();
this.initiator.stop();
assertThat(other.await(10, TimeUnit.SECONDS), is(true));
assertThat(other.await(20, TimeUnit.SECONDS), is(true));
assertThat(another.getContext().isLeader(), is(true));
}

View File

@@ -16,7 +16,6 @@
package org.springframework.integration.ftp.outbound;
import static java.util.regex.Matcher.quoteReplacement;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
@@ -48,6 +47,7 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTPClient;
@@ -165,7 +165,7 @@ public class FtpServerOutboundTests extends FtpTestSupport {
Message<?> result = this.output.receive(1000);
assertNotNull(result);
File localFile = (File) result.getPayload();
assertThat(localFile.getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(localFile.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir.toUpperCase()));
assertPreserved(modified, localFile);
@@ -174,7 +174,7 @@ public class FtpServerOutboundTests extends FtpTestSupport {
result = this.output.receive(1000);
assertNotNull(result);
localFile = (File) result.getPayload();
assertThat(localFile.getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(localFile.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir.toUpperCase()));
}
@@ -207,7 +207,7 @@ public class FtpServerOutboundTests extends FtpTestSupport {
boolean assertedModified = false;
for (File file : localFiles) {
assertThat(file.getPath().replaceAll(quoteReplacement(File.separator), "/"), containsString(dir));
assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), containsString(dir));
if (file.getPath().contains("localTarget1")) {
assertedModified = assertPreserved(modified, file);
}
@@ -223,7 +223,7 @@ public class FtpServerOutboundTests extends FtpTestSupport {
assertThat(localFiles.size(), Matchers.greaterThan(0));
for (File file : localFiles) {
assertThat(file.getPath().replaceAll(quoteReplacement(File.separator), "/"), containsString(dir));
assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"), containsString(dir));
}
}
@@ -262,14 +262,14 @@ public class FtpServerOutboundTests extends FtpTestSupport {
boolean assertedModified = false;
for (File file : localFiles) {
assertThat(file.getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir));
if (file.getPath().contains("localTarget1")) {
assertedModified = assertPreserved(modified, file);
}
}
assertTrue(assertedModified);
assertThat(localFiles.get(2).getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(localFiles.get(2).getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir + "subFtpSource"));
File secondTarget = new File(getTargetLocalDirectory() + File.separator + "ftpSource", "localTarget2.txt");
@@ -325,10 +325,10 @@ public class FtpServerOutboundTests extends FtpTestSupport {
assertEquals(2, localFiles.size());
for (File file : localFiles) {
assertThat(file.getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir));
}
assertThat(localFiles.get(1).getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(localFiles.get(1).getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir + "subFtpSource"));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -17,12 +17,12 @@
package org.springframework.integration.http.config;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;

View File

@@ -21,7 +21,9 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.internal.verification.VerificationModeFactory.atLeastOnce;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.Arrays;
@@ -268,7 +270,7 @@ public class RedisQueueMessageDrivenEndpointTests extends RedisAvailableTests {
assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
Mockito.verify(boundListOperations, atLeastOnce()).rightPush(Mockito.any(byte[].class));
verify(boundListOperations, atLeastOnce()).rightPush(any(byte[].class));
}

View File

@@ -16,7 +16,6 @@
package org.springframework.integration.sftp.outbound;
import static java.util.regex.Matcher.quoteReplacement;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
@@ -40,6 +39,7 @@ import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import org.apache.commons.io.FileUtils;
import org.hamcrest.Matchers;
@@ -141,7 +141,7 @@ public class SftpServerOutboundTests extends SftpTestSupport {
Message<?> result = this.output.receive(1000);
assertNotNull(result);
File localFile = (File) result.getPayload();
assertThat(localFile.getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(localFile.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir.toUpperCase()));
assertPreserved(modified, localFile);
@@ -150,7 +150,7 @@ public class SftpServerOutboundTests extends SftpTestSupport {
result = this.output.receive(1000);
assertNotNull(result);
localFile = (File) result.getPayload();
assertThat(localFile.getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(localFile.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir.toUpperCase()));
Session<?> session2 = this.sessionFactory.getSession();
assertSame(TestUtils.getPropertyValue(session, "targetSession.jschSession"),
@@ -186,7 +186,7 @@ public class SftpServerOutboundTests extends SftpTestSupport {
boolean assertedModified = false;
for (File file : localFiles) {
assertThat(file.getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir));
if (file.getPath().contains("localTarget1")) {
assertedModified = assertPreserved(modified, file);
@@ -203,7 +203,7 @@ public class SftpServerOutboundTests extends SftpTestSupport {
assertThat(localFiles.size(), Matchers.greaterThan(0));
for (File file : localFiles) {
assertThat(file.getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir));
}
}
@@ -223,14 +223,14 @@ public class SftpServerOutboundTests extends SftpTestSupport {
boolean assertedModified = false;
for (File file : localFiles) {
assertThat(file.getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir));
if (file.getPath().contains("localTarget1")) {
assertedModified = assertPreserved(modified, file);
}
}
assertTrue(assertedModified);
assertThat(localFiles.get(2).getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(localFiles.get(2).getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir + "subSftpSource"));
File secondTarget = new File(getTargetLocalDirectory() + File.separator + "sftpSource", "localTarget2.txt");
@@ -285,10 +285,10 @@ public class SftpServerOutboundTests extends SftpTestSupport {
assertEquals(2, localFiles.size());
for (File file : localFiles) {
assertThat(file.getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(file.getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir));
}
assertThat(localFiles.get(1).getPath().replaceAll(quoteReplacement(File.separator), "/"),
assertThat(localFiles.get(1).getPath().replaceAll(Matcher.quoteReplacement(File.separator), "/"),
containsString(dir + "subSftpSource"));
}

View File

@@ -16,8 +16,8 @@
package org.springframework.integration.stomp.client;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2017 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.
@@ -16,8 +16,8 @@
package org.springframework.integration.stomp.inbound;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2017 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.
@@ -16,8 +16,8 @@
package org.springframework.integration.stomp.outbound;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2017 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.
@@ -20,7 +20,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.springframework.integration.xml.xpath.XPathUtils.evaluate;
import java.util.Date;
import java.util.List;
@@ -46,6 +45,7 @@ import org.springframework.xml.xpath.NodeMapper;
/**
* @author Artem Bilan
* @author Gary Russell
* @since 3.0
*/
@ContextConfiguration
@@ -78,19 +78,19 @@ public class XPathTests {
@Test
@SuppressWarnings("unchecked")
public void testXPathUtils() {
Object result = evaluate(XML, "/parent/child/@name");
Object result = XPathUtils.evaluate(XML, "/parent/child/@name");
assertEquals("test", result);
result = evaluate(XML, "/parent/child/@name", "string");
result = XPathUtils.evaluate(XML, "/parent/child/@name", "string");
assertEquals("test", result);
result = evaluate(XML, "/parent/child/@age", "number");
result = XPathUtils.evaluate(XML, "/parent/child/@age", "number");
assertEquals((double) 42, result);
result = evaluate(XML, "/parent/child/@married = 'true'", "boolean");
result = XPathUtils.evaluate(XML, "/parent/child/@married = 'true'", "boolean");
assertEquals(Boolean.TRUE, result);
result = evaluate(XML, "/parent/child", "node");
result = XPathUtils.evaluate(XML, "/parent/child", "node");
assertThat(result, Matchers.instanceOf(Node.class));
Node node = (Node) result;
assertEquals("child", node.getLocalName());
@@ -98,7 +98,8 @@ public class XPathTests {
assertEquals("42", node.getAttributes().getNamedItem("age").getTextContent());
assertEquals("true", node.getAttributes().getNamedItem("married").getTextContent());
result = evaluate("<parent><child name='foo'/><child name='bar'/></parent>", "/parent/child", "node_list");
result = XPathUtils.evaluate("<parent><child name='foo'/><child name='bar'/></parent>", "/parent/child",
"node_list");
assertThat(result, Matchers.instanceOf(List.class));
List<Node> nodeList = (List<Node>) result;
assertEquals(2, nodeList.size());
@@ -109,7 +110,7 @@ public class XPathTests {
assertEquals("child", node2.getLocalName());
assertEquals("bar", node2.getAttributes().getNamedItem("name").getTextContent());
result = evaluate("<parent><child name='foo'/><child name='bar'/></parent>", "/parent/child", "document_list");
result = XPathUtils.evaluate("<parent><child name='foo'/><child name='bar'/></parent>", "/parent/child", "document_list");
assertThat(result, Matchers.instanceOf(List.class));
List<Document> documentList = (List<Document>) result;
assertEquals(2, documentList.size());
@@ -120,11 +121,11 @@ public class XPathTests {
assertEquals("child", document2.getFirstChild().getLocalName());
assertEquals("bar", document2.getFirstChild().getAttributes().getNamedItem("name").getTextContent());
result = evaluate(XML, "/parent/child/@name", new TestNodeMapper());
result = XPathUtils.evaluate(XML, "/parent/child/@name", new TestNodeMapper());
assertEquals("test-mapped", result);
try {
evaluate(new Date(), "/parent/child");
XPathUtils.evaluate(new Date(), "/parent/child");
fail("MessagingException expected.");
}
catch (Exception e) {
@@ -133,7 +134,7 @@ public class XPathTests {
}
try {
evaluate(XML, "/parent/child", "string", "number");
XPathUtils.evaluate(XML, "/parent/child", "string", "number");
fail("MessagingException expected.");
}
catch (Exception e) {
@@ -142,7 +143,7 @@ public class XPathTests {
}
try {
evaluate(XML, "/parent/child", "foo");
XPathUtils.evaluate(XML, "/parent/child", "foo");
fail("MessagingException expected.");
}
catch (Exception e) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -16,7 +16,7 @@
package org.springframework.integration.xmpp.inbound;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
@@ -86,7 +86,7 @@ public class ChatMessageListeningEndpointTests {
.addAsyncStanzaListener(any(StanzaListener.class), isNull());
willAnswer(invocation -> {
packetListSet.remove((StanzaListener) invocation.getArgument(0));
packetListSet.remove(invocation.getArgument(0));
return null;
}).given(connection)
.removeAsyncStanzaListener(any(StanzaListener.class));

View File

@@ -6,7 +6,6 @@
<suppress files="package-info\.java" checks=".*" />
<suppress files="[\\/]test[\\/]" checks="RequireThis" />
<suppress files="[\\/]test[\\/]" checks="FinalClass" />
<suppress files="[\\/]test[\\/]" checks="AvoidStaticImport" />
<suppress files="[\\/]test[\\/]" checks="InnerTypeLast" />
<suppress files="CachingSessionFactory" checks="FinalClass" /> <!-- Tests spy -->
<suppress files="[\\/]test[\\/]" checks="Javadoc*" />

View File

@@ -76,14 +76,29 @@
<module name="AvoidStarImport" />
<module name="AvoidStaticImport">
<property name="excludes"
value="org.assertj.core.api.Assertions.*,
org.junit.Assert.*,
org.junit.Assume.*,
value="org.custommonkey.xmlunit.XMLAssert.*,
org.assertj.core.api.Assertions.*,
org.hamcrest.CoreMatchers.*,
org.hamcrest.Matchers.*,
org.hamcrest.collection.IsCollectionWithSize.*,
org.junit.Assert.*,
org.junit.Assume.*,
org.mockito.Mockito.*,
org.mockito.BDDMockito.*,
org.mockito.AdditionalAnswers.*,
org.mockito.ArgumentMatchers.*,
org.mockito.hamcrest.MockitoHamcrest.*,
org.springframework.integration.gemfire.config.xml.ParserTestUtil.*,
org.springframework.integration.test.matcher.EqualsResultMatcher.*,
org.springframework.integration.test.matcher.EventuallyMatcher.*,
org.springframework.integration.test.matcher.HeaderMatcher.*,
org.springframework.integration.test.matcher.MapContentMatchers.*,
org.springframework.integration.test.matcher.MockitoMessageMatchers.*,
org.springframework.integration.test.matcher.PayloadAndHeaderMatcher.*,
org.springframework.integration.test.matcher.PayloadMatcher.*,
org.springframework.integration.test.util.TestUtils.*,
org.springframework.test.web.client.match.MockRestRequestMatchers.*,
org.springframework.test.web.client.response.MockRestResponseCreators.*,
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*,
org.springframework.test.web.servlet.result.MockMvcResultMatchers.*,
org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*,