INT-2932 TCP Connection Events - Doc and Polishing

INT-2932
- Reference Documentation

INT-2941
- Add getter for Throwable
- Add auto-startup to Parser, Endpoint (already in schema)
- Add phase to schema, Parser

(Schema changes are mostly indentation because the element now
extends SmartLifecycleType).
This commit is contained in:
Gary Russell
2013-02-20 17:18:42 -05:00
committed by Gunnar Hillert
parent 8b476b2d76
commit d059abf4d1
10 changed files with 133 additions and 62 deletions

View File

@@ -419,7 +419,13 @@
</constructor-arg>
</bean>
<ip:tcp-connection-event-inbound-channel-adapter id="eventAdapter" channel="nullChannel"
<ip:tcp-connection-event-inbound-channel-adapter id="eventAdapter" channel="eventChannel"
auto-startup="false" phase="23" error-channel="eventErrors"
event-types="org.springframework.integration.ip.config.ParserUnitTests$EventSubclass1, org.springframework.integration.ip.config.ParserUnitTests$EventSubclass2"/>
<int:channel id="eventChannel">
<int:queue />
</int:channel>
<int:channel id="eventErrors" />
</beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import java.util.Iterator;
import java.util.Set;
@@ -41,6 +42,7 @@ import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.endpoint.EventDrivenConsumer;
@@ -54,6 +56,7 @@ import org.springframework.integration.ip.tcp.connection.DefaultTcpNetSSLSocketF
import org.springframework.integration.ip.tcp.connection.DefaultTcpNioSSLConnectionSupport;
import org.springframework.integration.ip.tcp.connection.DefaultTcpSSLContextSupport;
import org.springframework.integration.ip.tcp.connection.TcpConnectionEvent;
import org.springframework.integration.ip.tcp.connection.TcpConnectionEvent.TcpConnectionEventType;
import org.springframework.integration.ip.tcp.connection.TcpConnectionEventListeningMessageProducer;
import org.springframework.integration.ip.tcp.connection.TcpConnectionSupport;
import org.springframework.integration.ip.tcp.connection.TcpMessageMapper;
@@ -262,6 +265,9 @@ public class ParserUnitTests {
@Autowired
TcpConnectionEventListeningMessageProducer eventAdapter;
@Autowired
QueueChannel eventChannel;
private static volatile int adviceCalled;
@Test
@@ -649,12 +655,28 @@ public class ParserUnitTests {
assertSame(socketSupport, dfa.getPropertyValue("tcpSocketSupport"));
}
@SuppressWarnings("unchecked")
@Test
public void testEventAdapter() {
Set<?> eventTypes = TestUtils.getPropertyValue(this.eventAdapter, "eventTypes", Set.class);
assertEquals(2, eventTypes.size());
assertTrue(eventTypes.contains(EventSubclass1.class));
assertTrue(eventTypes.contains(EventSubclass2.class));
assertFalse(TestUtils.getPropertyValue(this.eventAdapter, "autoStartup", Boolean.class));
assertEquals(23, TestUtils.getPropertyValue(this.eventAdapter, "phase"));
assertEquals("eventErrors", TestUtils.getPropertyValue(this.eventAdapter, "errorChannel",
DirectChannel.class).getComponentName());
TcpConnectionSupport connection = mock(TcpConnectionSupport.class);
TcpConnectionEvent event = new TcpConnectionEvent(connection, TcpConnectionEventType.OPEN, "foo");
this.eventAdapter.setEventTypes(new Class[] {TcpConnectionEvent.class});
this.eventAdapter.onApplicationEvent(event);
assertNull(this.eventChannel.receive(0));
this.eventAdapter.start();
this.eventAdapter.onApplicationEvent(event);
Message<?> eventMessage = this.eventChannel.receive(0);
assertNotNull(eventMessage);
assertSame(event, eventMessage.getPayload());
}
public static class FooAdvice extends AbstractRequestHandlerAdvice {

View File

@@ -17,6 +17,7 @@ package org.springframework.integration.ip.tcp.connection;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
@@ -42,18 +43,19 @@ public class ConnectionEventTests {
@Test
public void test() throws Exception {
Socket socket = mock(Socket.class);
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
final AtomicReference<TcpConnectionEvent> theEvent = new AtomicReference<TcpConnectionEvent>();
TcpNetConnection conn = new TcpNetConnection(socket, false, false, new ApplicationEventPublisher() {
public void publishEvent(ApplicationEvent event) {
theEvent.set(event);
theEvent.set((TcpConnectionEvent) event);
}
}, "foo");
assertNotNull(theEvent.get());
assertEquals("TcpConnectionEvent [type=OPEN, factory=foo, connectionId=" + conn.getConnectionId() + "]", theEvent.get().toString());
@SuppressWarnings("unchecked")
Serializer<Object> serializer = mock(Serializer.class);
doThrow(new RuntimeException("foo")).when(serializer).serialize(Mockito.any(Object.class), Mockito.any(OutputStream.class));
RuntimeException toBeThrown = new RuntimeException("foo");
doThrow(toBeThrown).when(serializer).serialize(Mockito.any(Object.class), Mockito.any(OutputStream.class));
conn.setMapper(new TcpMessageMapper());
conn.setSerializer(serializer);
try {
@@ -64,6 +66,8 @@ public class ConnectionEventTests {
assertNotNull(theEvent.get());
assertEquals("TcpConnectionEvent [type=EXCEPTION, factory=foo, connectionId=" + conn.getConnectionId() +
", Exception=java.lang.RuntimeException: foo]", theEvent.get().toString());
assertNotNull(theEvent.get().getThrowable());
assertSame(toBeThrown, theEvent.get().getThrowable());
conn.close();
assertNotNull(theEvent.get());
assertEquals("TcpConnectionEvent [type=CLOSE, factory=foo, connectionId=" + conn.getConnectionId() + "]", theEvent.get().toString());

View File

@@ -38,6 +38,7 @@ public class TcpConnectionEventListenerTests {
QueueChannel outputChannel = new QueueChannel();
eventProducer.setOutputChannel(outputChannel);
eventProducer.afterPropertiesSet();
eventProducer.start();
TcpConnectionSupport connection = Mockito.mock(TcpConnectionSupport.class);
TcpConnectionEvent event1 = new TcpConnectionEvent(connection, TcpConnectionEventType.OPEN, "foo");
eventProducer.onApplicationEvent(event1);
@@ -66,6 +67,7 @@ public class TcpConnectionEventListenerTests {
eventProducer.setOutputChannel(outputChannel);
eventProducer.setEventTypes(new Class[] {FooEvent.class, BarEvent.class});
eventProducer.afterPropertiesSet();
eventProducer.start();
TcpConnectionSupport connection = Mockito.mock(TcpConnectionSupport.class);
TcpConnectionEvent event1 = new TcpConnectionEvent(connection, TcpConnectionEventType.OPEN, "foo");
eventProducer.onApplicationEvent(event1);