INT-4498: StreamUtils -> FileCopyUtils

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

* Use `FileCopyUtils` instead of `StreamUtils` in test-cases when we deal
with `InputStream` s and files resources.
* Fix reflection-based tests with AOP to use an `ArrayList` access
instead of `LinkedList` since an optimization in SF:
6d6aa72e8f

**Cherry-pick to 5.0.x and 4.3.x**
This commit is contained in:
Artem Bilan
2018-07-09 12:41:45 -04:00
parent 8eac5b449c
commit a1f016a94c
8 changed files with 28 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2018 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.
@@ -82,7 +82,7 @@ public class DefaultOutboundChannelAdapterParserTests {
assertEquals(Boolean.FALSE, TestUtils.getPropertyValue(adapter, "autoStartup")); assertEquals(Boolean.FALSE, TestUtils.getPropertyValue(adapter, "autoStartup"));
MessageHandler handler = TestUtils.getPropertyValue(adapter, "handler", MessageHandler.class); MessageHandler handler = TestUtils.getPropertyValue(adapter, "handler", MessageHandler.class);
assertTrue(AopUtils.isAopProxy(handler)); assertTrue(AopUtils.isAopProxy(handler));
assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors.first.item.advice"), assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors[0].advice"),
Matchers.instanceOf(RequestHandlerRetryAdvice.class)); Matchers.instanceOf(RequestHandlerRetryAdvice.class));
handler.handleMessage(new GenericMessage<>("foo")); handler.handleMessage(new GenericMessage<>("foo"));

View File

@@ -28,8 +28,8 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
@@ -91,7 +91,7 @@ import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StreamUtils; import org.springframework.util.FileCopyUtils;
/** /**
* @author Artem Bilan * @author Artem Bilan
@@ -242,7 +242,7 @@ public class FileTests {
File resultFile = (File) receive.getPayload(); File resultFile = (File) receive.getPayload();
assertThat(resultFile.getAbsolutePath(), assertThat(resultFile.getAbsolutePath(),
endsWith(TestUtils.applySystemFileSeparator("fileWritingFlow/foo.write"))); endsWith(TestUtils.applySystemFileSeparator("fileWritingFlow/foo.write")));
String fileContent = StreamUtils.copyToString(new FileInputStream(resultFile), Charset.defaultCharset()); String fileContent = FileCopyUtils.copyToString(new FileReader(resultFile));
assertEquals(payload, fileContent); assertEquals(payload, fileContent);
if (FileUtils.IS_POSIX) { if (FileUtils.IS_POSIX) {
assertThat(java.nio.file.Files.getPosixFilePermissions(resultFile.toPath()).size(), equalTo(9)); assertThat(java.nio.file.Files.getPosixFilePermissions(resultFile.toPath()).size(), equalTo(9));

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2018 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.
@@ -46,6 +46,7 @@ import org.springframework.test.context.junit4.SpringRunner;
/** /**
* @author Gary Russell * @author Gary Russell
* @author Artem Bilan * @author Artem Bilan
*
* @since 4.0 * @since 4.0
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@@ -93,7 +94,7 @@ public class MqttOutboundChannelAdapterParserTests {
assertSame(((Advised) handler).getTargetSource().getTarget(), this.withConverterHandler); assertSame(((Advised) handler).getTargetSource().getTarget(), this.withConverterHandler);
assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors.first.item.advice"), assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors[0].advice"),
Matchers.instanceOf(RequestHandlerRetryAdvice.class)); Matchers.instanceOf(RequestHandlerRetryAdvice.class));
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 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.
@@ -101,7 +101,7 @@ public class RedisOutboundChannelAdapterParserTests extends RedisAvailableTests
assertTrue(AopUtils.isAopProxy(endpointHandler)); assertTrue(AopUtils.isAopProxy(endpointHandler));
assertThat(TestUtils.getPropertyValue(endpointHandler, "h.advised.advisors.first.item.advice"), assertThat(TestUtils.getPropertyValue(endpointHandler, "h.advised.advisors[0].advice"),
Matchers.instanceOf(RequestHandlerRetryAdvice.class)); Matchers.instanceOf(RequestHandlerRetryAdvice.class));
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2016 the original author or authors. * Copyright 2013-2018 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.
@@ -45,6 +45,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Artem Bilan * @author Artem Bilan
* @author Gary Russell * @author Gary Russell
* @author Rainer Frey * @author Rainer Frey
*
* @since 3.0 * @since 3.0
*/ */
@ContextConfiguration @ContextConfiguration
@@ -88,15 +89,17 @@ public class RedisQueueOutboundChannelAdapterParserTests {
assertSame(((Advised) handler).getTargetSource().getTarget(), this.defaultAdapter); assertSame(((Advised) handler).getTargetSource().getTarget(), this.defaultAdapter);
assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors.first.item.advice"), assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors[0].advice"),
Matchers.instanceOf(RequestHandlerRetryAdvice.class)); Matchers.instanceOf(RequestHandlerRetryAdvice.class));
assertTrue(TestUtils.getPropertyValue(this.defaultAdapter, "leftPush", Boolean.class)); assertTrue(TestUtils.getPropertyValue(this.defaultAdapter, "leftPush", Boolean.class));
} }
@Test @Test
public void testInt3017CustomConfig() { public void testInt3017CustomConfig() {
assertSame(this.customRedisConnectionFactory, TestUtils.getPropertyValue(this.customAdapter, "template.connectionFactory")); assertSame(this.customRedisConnectionFactory,
assertEquals("headers['redis_queue']", TestUtils.getPropertyValue(this.customAdapter, "queueNameExpression", Expression.class).getExpressionString()); TestUtils.getPropertyValue(this.customAdapter, "template.connectionFactory"));
assertEquals("headers['redis_queue']",
TestUtils.getPropertyValue(this.customAdapter, "queueNameExpression.expression"));
assertFalse(TestUtils.getPropertyValue(this.customAdapter, "extractPayload", Boolean.class)); assertFalse(TestUtils.getPropertyValue(this.customAdapter, "extractPayload", Boolean.class));
assertTrue(TestUtils.getPropertyValue(this.customAdapter, "serializerExplicitlySet", Boolean.class)); assertTrue(TestUtils.getPropertyValue(this.customAdapter, "serializerExplicitlySet", Boolean.class));
assertSame(this.serializer, TestUtils.getPropertyValue(this.customAdapter, "serializer")); assertSame(this.serializer, TestUtils.getPropertyValue(this.customAdapter, "serializer"));

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2007-2017 the original author or authors. * Copyright 2007-2018 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.
@@ -75,7 +75,7 @@ public class RedisStoreOutboundChannelAdapterParserTests {
assertSame(((Advised) handler).getTargetSource().getTarget(), withStringTemplate); assertSame(((Advised) handler).getTargetSource().getTarget(), withStringTemplate);
assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors.first.item.advice"), assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors[0].advice"),
Matchers.instanceOf(RequestHandlerRetryAdvice.class)); Matchers.instanceOf(RequestHandlerRetryAdvice.class));
assertEquals("true", TestUtils.getPropertyValue(withStringTemplate, "zsetIncrementScoreExpression", assertEquals("true", TestUtils.getPropertyValue(withStringTemplate, "zsetIncrementScoreExpression",

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2017 the original author or authors. * Copyright 2014-2018 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.
@@ -44,7 +44,7 @@ import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.integration.file.remote.session.Session; import org.springframework.integration.file.remote.session.Session;
import org.springframework.util.Base64Utils; import org.springframework.util.Base64Utils;
import org.springframework.util.StreamUtils; import org.springframework.util.FileCopyUtils;
import com.jcraft.jsch.ChannelSftp.LsEntry; import com.jcraft.jsch.ChannelSftp.LsEntry;
@@ -53,6 +53,7 @@ import com.jcraft.jsch.ChannelSftp.LsEntry;
* @author Gary Russell * @author Gary Russell
* @author David Liu * @author David Liu
* @author Artem Bilan * @author Artem Bilan
*
* @since 4.1 * @since 4.1
* *
*/ */
@@ -115,7 +116,7 @@ public class SftpServerTests {
f.setUser("user"); f.setUser("user");
f.setAllowUnknownKeys(true); f.setAllowUnknownKeys(true);
InputStream stream = new ClassPathResource(privKey).getInputStream(); InputStream stream = new ClassPathResource(privKey).getInputStream();
f.setPrivateKey(new ByteArrayResource(StreamUtils.copyToByteArray(stream))); f.setPrivateKey(new ByteArrayResource(FileCopyUtils.copyToByteArray(stream)));
f.setPrivateKeyPassphrase(passphrase); f.setPrivateKeyPassphrase(passphrase);
Session<LsEntry> session = f.getSession(); Session<LsEntry> session = f.getSession();
doTest(server, session); doTest(server, session);
@@ -127,7 +128,7 @@ public class SftpServerTests {
private PublicKey decodePublicKey(String key) throws Exception { private PublicKey decodePublicKey(String key) throws Exception {
InputStream stream = new ClassPathResource(key).getInputStream(); InputStream stream = new ClassPathResource(key).getInputStream();
byte[] keyBytes = StreamUtils.copyToByteArray(stream); byte[] keyBytes = FileCopyUtils.copyToByteArray(stream);
// strip any newline chars // strip any newline chars
while (keyBytes[keyBytes.length - 1] == 0x0a || keyBytes[keyBytes.length - 1] == 0x0d) { while (keyBytes[keyBytes.length - 1] == 0x0a || keyBytes[keyBytes.length - 1] == 0x0d) {
keyBytes = Arrays.copyOf(keyBytes, keyBytes.length - 1); keyBytes = Arrays.copyOf(keyBytes, keyBytes.length - 1);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 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.
@@ -24,8 +24,8 @@ import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.net.URI; import java.net.URI;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@@ -48,7 +48,7 @@ import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandlingException; import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.PollableChannel; import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage; import org.springframework.messaging.support.GenericMessage;
import org.springframework.util.StreamUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.util.concurrent.SettableListenableFuture; import org.springframework.util.concurrent.SettableListenableFuture;
import org.springframework.ws.WebServiceMessage; import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.WebServiceMessageFactory; import org.springframework.ws.WebServiceMessageFactory;
@@ -195,7 +195,7 @@ public class SimpleWebServiceOutboundGatewayTests {
assertNotNull(myAttachment); assertNotNull(myAttachment);
assertEquals("text/plain", myAttachment.getContentType()); assertEquals("text/plain", myAttachment.getContentType());
assertEquals("my_data", StreamUtils.copyToString(myAttachment.getInputStream(), Charset.forName("UTF-8"))); assertEquals("my_data", FileCopyUtils.copyToString(new InputStreamReader(myAttachment.getInputStream())));
} }
@Test @Test