INT-1785 Add Raw Single-Shot (De)Serializer

This commit is contained in:
Gary Russell
2011-02-09 14:54:50 -05:00
parent bece4a1c1b
commit 97617355ee
6 changed files with 212 additions and 17 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory;
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.TcpConnection;
import org.springframework.integration.ip.tcp.serializer.ByteArrayRawSerializer;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
@@ -80,4 +81,20 @@ public class ConnectionToConnectionTests {
assertEquals("Test", new String((byte[]) message.getPayload()));
}
@Test
public void testConnectRaw() throws Exception {
ByteArrayRawSerializer serializer = new ByteArrayRawSerializer();
client.setSerializer(serializer);
server.setDeserializer(serializer);
TcpConnection connection = client.getConnection();
connection.send(MessageBuilder.withPayload("Test").build());
Message<?> message = serverSideChannel.receive(10000);
MessageHistory history = MessageHistory.read(message);
//org.springframework.integration.test.util.TestUtils
Properties componentHistoryRecord = TestUtils.locateComponentInHistory(history, "looper", 0);
assertNotNull(componentHistoryRecord);
assertTrue(componentHistoryRecord.get("type").equals("ip:tcp-inbound-gateway"));
assertNotNull(message);
assertEquals("Test", new String((byte[]) message.getPayload()));
}
}

View File

@@ -90,6 +90,21 @@ public class DeserializationTests {
server.close();
}
@Test
public void testReadRaw() throws Exception {
int port = SocketTestUtils.findAvailableServerSocket();
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
server.setSoTimeout(10000);
SocketTestUtils.testSendRaw(port);
Socket socket = server.accept();
socket.setSoTimeout(5000);
ByteArrayRawSerializer serializer = new ByteArrayRawSerializer();
byte[] out = serializer.deserialize(socket.getInputStream());
assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING,
new String(out));
server.close();
}
@Test
public void testReadSerialized() throws Exception {
int port = SocketTestUtils.findAvailableServerSocket();

View File

@@ -138,6 +138,39 @@ public class SerializationTests {
server.close();
}
@Test
public void testWriteRaw() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();
final String testString = "abcdef";
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
server.setSoTimeout(10000);
Thread t = new Thread(new Runnable() {
public void run() {
try {
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
ByteBuffer buffer = ByteBuffer.allocate(testString.length());
buffer.put(testString.getBytes());
ByteArrayRawSerializer serializer = new ByteArrayRawSerializer();
serializer.serialize(buffer.array(), socket.getOutputStream());
socket.close();
Thread.sleep(1000000000L);
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.setDaemon(true);
t.start();
Socket socket = server.accept();
socket.setSoTimeout(5000);
InputStream is = socket.getInputStream();
byte[] buff = new byte[testString.length() + 1];
readFully(is, buff);
assertEquals(testString, new String(buff, 0, testString.length()));
assertEquals(-1, buff[testString.length()]);
server.close();
}
@Test
public void testWriteSerialized() throws Exception {
final int port = SocketTestUtils.findAvailableServerSocket();

View File

@@ -252,6 +252,26 @@ public class SocketTestUtils {
thread.start();
}
/**
* Sends a single message in two chunks and then closes the socket.
*/
public static void testSendRaw(final int port) {
Thread thread = new Thread(new Runnable() {
public void run() {
try {
Socket socket = new Socket(InetAddress.getByName("localhost"), port);
OutputStream outputStream = socket.getOutputStream();
outputStream.write(TEST_STRING.getBytes());
outputStream.write(TEST_STRING.getBytes());
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.setDaemon(true);
thread.start();
}
/**
* Sends two serialized objects over the same socket.
* @param port