Migrated the stream-based adapters from the "adapters" module to "org.springframework.integration.stream" (INT-375).
This commit is contained in:
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.adapter.stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ByteStreamSourceTests {
|
||||
|
||||
@Test
|
||||
public void testEndOfStream() {
|
||||
byte[] bytes = new byte[] {1,2,3};
|
||||
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
|
||||
ByteStreamSource source = new ByteStreamSource(stream);
|
||||
Message<?> message1 = source.receive();
|
||||
byte[] payload = (byte[]) message1.getPayload();
|
||||
assertEquals(3, payload.length);
|
||||
assertEquals(1, payload[0]);
|
||||
assertEquals(2, payload[1]);
|
||||
assertEquals(3, payload[2]);
|
||||
Message<?> message2 = source.receive();
|
||||
assertNull(message2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteArrayIsTruncated() {
|
||||
byte[] bytes = new byte[] {0,1,2,3,4,5};
|
||||
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
|
||||
ByteStreamSource source = new ByteStreamSource(stream);
|
||||
source.setBytesPerMessage(4);
|
||||
Message<?> message1 = source.receive();
|
||||
assertEquals(4, ((byte[]) message1.getPayload()).length);
|
||||
Message<?> message2 = source.receive();
|
||||
assertEquals(2, ((byte[]) message2.getPayload()).length);
|
||||
Message<?> message3 = source.receive();
|
||||
assertNull(message3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteArrayIsNotTruncated() {
|
||||
byte[] bytes = new byte[] {0,1,2,3,4,5};
|
||||
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
|
||||
ByteStreamSource source = new ByteStreamSource(stream);
|
||||
source.setBytesPerMessage(4);
|
||||
source.setShouldTruncate(false);
|
||||
Message<?> message1 = source.receive();
|
||||
assertEquals(4, ((byte[]) message1.getPayload()).length);
|
||||
Message<?> message2 = source.receive();
|
||||
assertEquals(4, ((byte[]) message2.getPayload()).length);
|
||||
assertEquals(4, ((byte[]) message2.getPayload())[0]);
|
||||
assertEquals(5, ((byte[]) message2.getPayload())[1]);
|
||||
assertEquals(0, ((byte[]) message2.getPayload())[2]);
|
||||
assertEquals(0, ((byte[]) message2.getPayload())[3]);
|
||||
Message<?> message3 = source.receive();
|
||||
assertNull(message3);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,203 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.adapter.stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.endpoint.ChannelPoller;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.scheduling.PollingSchedule;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ByteStreamTargetTests {
|
||||
|
||||
private QueueChannel channel;
|
||||
|
||||
private ChannelPoller poller;
|
||||
|
||||
|
||||
@Before
|
||||
public void initialize() {
|
||||
this.channel = new QueueChannel(10);
|
||||
this.poller = new ChannelPoller(channel, new PollingSchedule(0));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSingleByteArray() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
target.onMessage(new GenericMessage<byte[]>(new byte[] {1,2,3}));
|
||||
byte[] result = stream.toByteArray();
|
||||
assertEquals(3, result.length);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(2, result[1]);
|
||||
assertEquals(3, result[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleString() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
target.onMessage(new StringMessage("foo"));
|
||||
byte[] result = stream.toByteArray();
|
||||
assertEquals(3, result.length);
|
||||
assertEquals("foo", new String(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesPerTaskSameAsMessageCount() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(3);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result = stream.toByteArray();
|
||||
assertEquals(9, result.length);
|
||||
assertEquals(1, result[0]);
|
||||
assertEquals(9, result[8]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesPerTaskLessThanMessageCount() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result = stream.toByteArray();
|
||||
assertEquals(6, result.length);
|
||||
assertEquals(1, result[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesPerTaskExceedsMessageCount() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(5);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result = stream.toByteArray();
|
||||
assertEquals(9, result.length);
|
||||
assertEquals(1, result[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesLessThanMessageCountWithMultipleDispatches() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result1 = stream.toByteArray();
|
||||
assertEquals(6, result1.length);
|
||||
assertEquals(1, result1[0]);
|
||||
poller.run();
|
||||
byte[] result2 = stream.toByteArray();
|
||||
assertEquals(9, result2.length);
|
||||
assertEquals(1, result2[0]);
|
||||
assertEquals(7, result2[6]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesExceedsMessageCountWithMultipleDispatches() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(5);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result1 = stream.toByteArray();
|
||||
assertEquals(9, result1.length);
|
||||
assertEquals(1, result1[0]);
|
||||
poller.run();
|
||||
byte[] result2 = stream.toByteArray();
|
||||
assertEquals(9, result2.length);
|
||||
assertEquals(1, result2[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamResetBetweenDispatches() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result1 = stream.toByteArray();
|
||||
assertEquals(6, result1.length);
|
||||
stream.reset();
|
||||
poller.run();
|
||||
byte[] result2 = stream.toByteArray();
|
||||
assertEquals(3, result2.length);
|
||||
assertEquals(7, result2[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamWriteBetweenDispatches() throws IOException {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ByteStreamTarget target = new ByteStreamTarget(stream);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {1,2,3}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {4,5,6}), 0);
|
||||
channel.send(new GenericMessage<byte[]>(new byte[] {7,8,9}), 0);
|
||||
poller.run();
|
||||
byte[] result1 = stream.toByteArray();
|
||||
assertEquals(6, result1.length);
|
||||
stream.write(new byte[] {123});
|
||||
stream.flush();
|
||||
poller.run();
|
||||
byte[] result2 = stream.toByteArray();
|
||||
assertEquals(10, result2.length);
|
||||
assertEquals(1, result2[0]);
|
||||
assertEquals(123, result2[6]);
|
||||
assertEquals(7, result2[7]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.adapter.stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class CharacterStreamSourceTests {
|
||||
|
||||
@Test
|
||||
public void testEndOfStream() {
|
||||
StringReader reader = new StringReader("test");
|
||||
CharacterStreamSource source = new CharacterStreamSource(reader);
|
||||
Message<?> message1 = source.receive();
|
||||
assertEquals("test", message1.getPayload());
|
||||
Message<?> message2 = source.receive();
|
||||
assertNull(message2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.adapter.stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.endpoint.ChannelPoller;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.scheduling.PollingSchedule;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class CharacterStreamTargetTests {
|
||||
|
||||
private QueueChannel channel;
|
||||
|
||||
private ChannelPoller poller;
|
||||
|
||||
|
||||
@Before
|
||||
public void initialize() {
|
||||
this.channel = new QueueChannel(10);
|
||||
this.poller = new ChannelPoller(channel, new PollingSchedule(0));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSingleString() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
target.onMessage(new StringMessage("foo"));
|
||||
assertEquals("foo", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoStringsAndNoNewLinesByDefault() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
poller.subscribe(target);
|
||||
poller.setMaxMessagesPerPoll(1);
|
||||
channel.send(new StringMessage("foo"), 0);
|
||||
channel.send(new StringMessage("bar"), 0);
|
||||
poller.run();
|
||||
assertEquals("foo", writer.toString());
|
||||
poller.run();
|
||||
assertEquals("foobar", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoStringsWithNewLines() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
target.setShouldAppendNewLine(true);
|
||||
poller.subscribe(target);
|
||||
poller.setMaxMessagesPerPoll(1);
|
||||
channel.send(new StringMessage("foo"), 0);
|
||||
channel.send(new StringMessage("bar"), 0);
|
||||
poller.run();
|
||||
String newLine = System.getProperty("line.separator");
|
||||
assertEquals("foo" + newLine, writer.toString());
|
||||
poller.run();
|
||||
assertEquals("foo" + newLine + "bar" + newLine, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesPerTaskSameAsMessageCount() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.subscribe(target);
|
||||
channel.send(new StringMessage("foo"), 0);
|
||||
channel.send(new StringMessage("bar"), 0);
|
||||
poller.run();
|
||||
assertEquals("foobar", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxMessagesPerTaskExceedsMessageCountWithAppendedNewLines() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
poller.setMaxMessagesPerPoll(10);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.subscribe(target);
|
||||
target.setShouldAppendNewLine(true);
|
||||
channel.send(new StringMessage("foo"), 0);
|
||||
channel.send(new StringMessage("bar"), 0);
|
||||
poller.run();
|
||||
String newLine = System.getProperty("line.separator");
|
||||
assertEquals("foo" + newLine + "bar" + newLine, writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleNonStringObject() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
poller.subscribe(target);
|
||||
poller.setMaxMessagesPerPoll(1);
|
||||
TestObject testObject = new TestObject("foo");
|
||||
channel.send(new GenericMessage<TestObject>(testObject));
|
||||
poller.run();
|
||||
assertEquals("foo", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoNonStringObjectWithOutNewLines() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.subscribe(target);
|
||||
TestObject testObject1 = new TestObject("foo");
|
||||
TestObject testObject2 = new TestObject("bar");
|
||||
channel.send(new GenericMessage<TestObject>(testObject1), 0);
|
||||
channel.send(new GenericMessage<TestObject>(testObject2), 0);
|
||||
poller.run();
|
||||
assertEquals("foobar", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoNonStringObjectWithNewLines() {
|
||||
StringWriter writer = new StringWriter();
|
||||
CharacterStreamTarget target = new CharacterStreamTarget(writer);
|
||||
target.setShouldAppendNewLine(true);
|
||||
poller.setReceiveTimeout(0);
|
||||
poller.setMaxMessagesPerPoll(2);
|
||||
poller.subscribe(target);
|
||||
TestObject testObject1 = new TestObject("foo");
|
||||
TestObject testObject2 = new TestObject("bar");
|
||||
channel.send(new GenericMessage<TestObject>(testObject1), 0);
|
||||
channel.send(new GenericMessage<TestObject>(testObject2), 0);
|
||||
poller.run();
|
||||
String newLine = System.getProperty("line.separator");
|
||||
assertEquals("foo" + newLine + "bar" + newLine, writer.toString());
|
||||
}
|
||||
|
||||
|
||||
private static class TestObject {
|
||||
|
||||
private String text;
|
||||
|
||||
TestObject(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.text;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.adapter.stream.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.adapter.stream.CharacterStreamSource;
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ConsoleSourceParserTests {
|
||||
|
||||
@Before
|
||||
public void writeTestInput() {
|
||||
ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes());
|
||||
System.setIn(stream);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleSourceWithDefaultCharset() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleSourceParserTests.xml", ConsoleSourceParserTests.class);
|
||||
CharacterStreamSource source =
|
||||
(CharacterStreamSource) context.getBean("sourceWithDefaultCharset");
|
||||
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(source);
|
||||
Reader bufferedReader = (Reader) sourceAccessor.getPropertyValue("reader");
|
||||
assertEquals(BufferedReader.class, bufferedReader.getClass());
|
||||
DirectFieldAccessor bufferedReaderAccessor = new DirectFieldAccessor(bufferedReader);
|
||||
Reader reader = (Reader) bufferedReaderAccessor.getPropertyValue("in");
|
||||
assertEquals(InputStreamReader.class, reader.getClass());
|
||||
Charset readerCharset = Charset.forName(((InputStreamReader) reader).getEncoding());
|
||||
assertEquals(Charset.defaultCharset(), readerCharset);
|
||||
Message<?> message = source.receive();
|
||||
assertNotNull(message);
|
||||
assertEquals("foo", message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleSourceWithProvidedCharset() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleSourceParserTests.xml", ConsoleSourceParserTests.class);
|
||||
CharacterStreamSource source =
|
||||
(CharacterStreamSource) context.getBean("sourceWithProvidedCharset");
|
||||
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(source);
|
||||
Reader bufferedReader = (Reader) sourceAccessor.getPropertyValue("reader");
|
||||
assertEquals(BufferedReader.class, bufferedReader.getClass());
|
||||
DirectFieldAccessor bufferedReaderAccessor = new DirectFieldAccessor(bufferedReader);
|
||||
Reader reader = (Reader) bufferedReaderAccessor.getPropertyValue("in");
|
||||
assertEquals(InputStreamReader.class, reader.getClass());
|
||||
Charset readerCharset = Charset.forName(((InputStreamReader) reader).getEncoding());
|
||||
assertEquals(Charset.forName("UTF-8"), readerCharset);
|
||||
Message<?> message = source.receive();
|
||||
assertNotNull(message);
|
||||
assertEquals("foo", message.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleSourceWithInvalidCharset() {
|
||||
BeanCreationException beanCreationException = null;
|
||||
try {
|
||||
new ClassPathXmlApplicationContext(
|
||||
"invalidConsoleSourceParserTests.xml", ConsoleSourceParserTests.class);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
beanCreationException = e;
|
||||
}
|
||||
Throwable parentCause = beanCreationException.getCause().getCause();
|
||||
assertEquals(ConfigurationException.class, parentCause.getClass());
|
||||
Throwable configurationExceptionCause = ((ConfigurationException) parentCause).getCause();
|
||||
assertEquals(UnsupportedEncodingException.class, configurationExceptionCause.getClass());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.adapter.stream.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.adapter.stream.CharacterStreamTarget;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class ConsoleTargetParserTests {
|
||||
|
||||
private final ByteArrayOutputStream err = new ByteArrayOutputStream();
|
||||
|
||||
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
|
||||
@Before
|
||||
public void setupStreams() {
|
||||
System.setErr(new PrintStream(this.err));
|
||||
System.setOut(new PrintStream(this.out));
|
||||
}
|
||||
|
||||
private void resetStreams() {
|
||||
this.err.reset();
|
||||
this.out.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleTargetWithDefaultCharset() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleTargetParserTests.xml", ConsoleTargetParserTests.class);
|
||||
CharacterStreamTarget target =
|
||||
(CharacterStreamTarget) context.getBean("targetWithDefaultCharset");
|
||||
DirectFieldAccessor targetAccessor = new DirectFieldAccessor(target);
|
||||
Writer bufferedWriter = (Writer) targetAccessor.getPropertyValue("writer");
|
||||
assertEquals(BufferedWriter.class, bufferedWriter.getClass());
|
||||
DirectFieldAccessor bufferedWriterAccessor = new DirectFieldAccessor(bufferedWriter);
|
||||
Writer writer = (Writer) bufferedWriterAccessor.getPropertyValue("out");
|
||||
assertEquals(OutputStreamWriter.class, writer.getClass());
|
||||
Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding());
|
||||
assertEquals(Charset.defaultCharset(), writerCharset);
|
||||
this.resetStreams();
|
||||
target.onMessage(new StringMessage("foo"));
|
||||
assertEquals("foo", out.toString());
|
||||
assertEquals("", err.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleTargetWithProvidedCharset() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleTargetParserTests.xml", ConsoleTargetParserTests.class);
|
||||
CharacterStreamTarget target =
|
||||
(CharacterStreamTarget) context.getBean("targetWithProvidedCharset");
|
||||
DirectFieldAccessor targetAccessor = new DirectFieldAccessor(target);
|
||||
Writer bufferedWriter = (Writer) targetAccessor.getPropertyValue("writer");
|
||||
assertEquals(BufferedWriter.class, bufferedWriter.getClass());
|
||||
DirectFieldAccessor bufferedWriterAccessor = new DirectFieldAccessor(bufferedWriter);
|
||||
Writer writer = (Writer) bufferedWriterAccessor.getPropertyValue("out");
|
||||
assertEquals(OutputStreamWriter.class, writer.getClass());
|
||||
Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding());
|
||||
assertEquals(Charset.forName("UTF-8"), writerCharset);
|
||||
this.resetStreams();
|
||||
target.onMessage(new StringMessage("bar"));
|
||||
assertEquals("bar", out.toString());
|
||||
assertEquals("", err.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConsoleTargetWithInvalidCharset() {
|
||||
BeanCreationException beanCreationException = null;
|
||||
try {
|
||||
new ClassPathXmlApplicationContext(
|
||||
"invalidConsoleTargetParserTests.xml", ConsoleTargetParserTests.class);
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
beanCreationException = e;
|
||||
}
|
||||
Throwable parentCause = beanCreationException.getCause().getCause();
|
||||
assertEquals(ConfigurationException.class, parentCause.getClass());
|
||||
Throwable configurationExceptionCause = ((ConfigurationException) parentCause).getCause();
|
||||
assertEquals(UnsupportedEncodingException.class, configurationExceptionCause.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorTarget() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleTargetParserTests.xml", ConsoleTargetParserTests.class);
|
||||
CharacterStreamTarget target =
|
||||
(CharacterStreamTarget) context.getBean("stderrTarget");
|
||||
DirectFieldAccessor targetAccessor = new DirectFieldAccessor(target);
|
||||
Writer bufferedWriter = (Writer) targetAccessor.getPropertyValue("writer");
|
||||
assertEquals(BufferedWriter.class, bufferedWriter.getClass());
|
||||
DirectFieldAccessor bufferedWriterAccessor = new DirectFieldAccessor(bufferedWriter);
|
||||
Writer writer = (Writer) bufferedWriterAccessor.getPropertyValue("out");
|
||||
assertEquals(OutputStreamWriter.class, writer.getClass());
|
||||
Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding());
|
||||
assertEquals(Charset.defaultCharset(), writerCharset);
|
||||
this.resetStreams();
|
||||
target.onMessage(new StringMessage("bad"));
|
||||
assertEquals("", out.toString());
|
||||
assertEquals("bad", err.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppendNewLine() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"consoleTargetParserTests.xml", ConsoleTargetParserTests.class);
|
||||
CharacterStreamTarget target =
|
||||
(CharacterStreamTarget) context.getBean("newlineTarget");
|
||||
DirectFieldAccessor targetAccessor = new DirectFieldAccessor(target);
|
||||
Writer bufferedWriter = (Writer) targetAccessor.getPropertyValue("writer");
|
||||
assertEquals(BufferedWriter.class, bufferedWriter.getClass());
|
||||
DirectFieldAccessor bufferedWriterAccessor = new DirectFieldAccessor(bufferedWriter);
|
||||
Writer writer = (Writer) bufferedWriterAccessor.getPropertyValue("out");
|
||||
assertEquals(OutputStreamWriter.class, writer.getClass());
|
||||
Charset writerCharset = Charset.forName(((OutputStreamWriter) writer).getEncoding());
|
||||
assertEquals(Charset.defaultCharset(), writerCharset);
|
||||
this.resetStreams();
|
||||
target.onMessage(new StringMessage("foo"));
|
||||
assertEquals("foo\n", out.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
|
||||
|
||||
<console-source id="sourceWithDefaultCharset"/>
|
||||
|
||||
<console-source id="sourceWithProvidedCharset" charset="UTF-8"/>
|
||||
|
||||
</beans:beans>
|
||||
@@ -1,20 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
|
||||
|
||||
<channel id="testChannel"/>
|
||||
|
||||
<stdout-channel-adapter id="targetWithDefaultCharset" channel="testChannel"/>
|
||||
|
||||
<stdout-channel-adapter id="targetWithProvidedCharset" charset="UTF-8" channel="testChannel"/>
|
||||
|
||||
<stderr-channel-adapter id="stderrTarget" channel="testChannel"/>
|
||||
|
||||
<stdout-channel-adapter id="newlineTarget" append-newline="true" channel="testChannel"/>
|
||||
|
||||
</beans:beans>
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
|
||||
|
||||
<console-source id="sourceWithInvalidCharset" charset="invalid-charset-name"/>
|
||||
|
||||
</beans:beans>
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
|
||||
|
||||
<stdout-channel-adapter id="targetWithInvalidCharset" charset="invalid-charset-name"/>
|
||||
|
||||
</beans:beans>
|
||||
Reference in New Issue
Block a user