Updated the names of the projects

This commit is contained in:
Ben Hale
2008-05-20 21:41:01 +00:00
parent 37f8d925c8
commit 6696064dd0
531 changed files with 48 additions and 48 deletions

View File

@@ -0,0 +1,114 @@
/*
* 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.event;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class ApplicationEventSourceTests {
@Test
public void testAnyApplicationEventSentByDefault() {
MessageChannel channel = new QueueChannel();
ApplicationEventSource adapter = new ApplicationEventSource(channel);
Message<?> message1 = channel.receive(0);
assertNull(message1);
adapter.onApplicationEvent(new TestApplicationEvent1());
adapter.onApplicationEvent(new TestApplicationEvent2());
Message<?> message2 = channel.receive(20);
assertNotNull(message2);
assertEquals("event1", ((ApplicationEvent) message2.getPayload()).getSource());
Message<?> message3 = channel.receive(20);
assertNotNull(message3);
assertEquals("event2", ((ApplicationEvent) message3.getPayload()).getSource());
}
@Test
public void testOnlyConfiguredEventTypesAreSent() {
MessageChannel channel = new QueueChannel();
ApplicationEventSource adapter = new ApplicationEventSource(channel);
List<Class<? extends ApplicationEvent>> eventTypes = new ArrayList<Class<? extends ApplicationEvent>>();
eventTypes.add(TestApplicationEvent1.class);
adapter.setEventTypes(eventTypes);
Message<?> message1 = channel.receive(0);
assertNull(message1);
adapter.onApplicationEvent(new TestApplicationEvent1());
adapter.onApplicationEvent(new TestApplicationEvent2());
Message<?> message2 = channel.receive(20);
assertNotNull(message2);
assertEquals("event1", ((ApplicationEvent) message2.getPayload()).getSource());
Message<?> message3 = channel.receive(0);
assertNull(message3);
}
@Test
public void testApplicationContextEvents() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationEventSourceTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("channel");
Message<?> refreshedEventMessage = channel.receive(0);
assertNotNull(refreshedEventMessage);
assertEquals(ContextRefreshedEvent.class, refreshedEventMessage.getPayload().getClass());
context.start();
Message<?> startedEventMessage = channel.receive(0);
assertNotNull(startedEventMessage);
assertEquals(ContextStartedEvent.class, startedEventMessage.getPayload().getClass());
context.stop();
Message<?> stoppedEventMessage = channel.receive(0);
assertNotNull(stoppedEventMessage);
assertEquals(ContextStoppedEvent.class, stoppedEventMessage.getPayload().getClass());
context.close();
Message<?> closedEventMessage = channel.receive(0);
assertNotNull(closedEventMessage);
assertEquals(ContextClosedEvent.class, closedEventMessage.getPayload().getClass());
}
private static class TestApplicationEvent1 extends ApplicationEvent {
public TestApplicationEvent1() {
super("event1");
}
}
private static class TestApplicationEvent2 extends ApplicationEvent {
public TestApplicationEvent2() {
super("event2");
}
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2002-2007 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.event;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.scheduling.Subscription;
/**
* @author Mark Fisher
*/
public class ApplicationEventTargetTests {
@Test
public void testSendingEvent() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
ApplicationEventPublisher publisher = new ApplicationEventPublisher() {
public void publishEvent(ApplicationEvent event) {
latch.countDown();
}
};
MessageChannel channel = new QueueChannel();
ApplicationEventTarget adapter = new ApplicationEventTarget();
adapter.setApplicationEventPublisher(publisher);
MessageBus bus = new MessageBus();
bus.registerChannel("channel", channel);
bus.registerTarget("adapter", adapter, new Subscription(channel));
bus.start();
assertEquals(1, latch.getCount());
channel.send(new StringMessage("123", "testing"));
latch.await(100, TimeUnit.MILLISECONDS);
assertEquals(0, latch.getCount());
bus.stop();
}
}

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="bus" class="org.springframework.integration.bus.MessageBus"/>
<bean id="channel" class="org.springframework.integration.channel.QueueChannel"/>
<bean id="source" class="org.springframework.integration.adapter.event.ApplicationEventSource">
<constructor-arg ref="channel"/>
</bean>
</beans>

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2002-2007 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.file;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class DefaultFileNameGeneratorTests {
@Test
public void testWithFileNamePropertyProvided() {
Message<String> message = new GenericMessage<String>("123", "testing");
message.getHeader().setProperty(FileNameGenerator.FILENAME_PROPERTY_KEY, "foo.bar");
FileNameGenerator generator = new DefaultFileNameGenerator();
String filename = generator.generateFileName(message);
assertEquals("foo.bar", filename);
}
@Test
public void testWithoutFileNamePropertyProvided() {
Message<String> message = new GenericMessage<String>("123", "testing");
FileNameGenerator generator = new DefaultFileNameGenerator();
String filename = generator.generateFileName(message);
assertTrue(filename.startsWith("123-"));
assertTrue(filename.endsWith(".msg"));
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.file.config;
import static org.junit.Assert.assertEquals;
import java.io.File;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.file.FileSource;
/**
* @author Mark Fisher
*/
public class FileSourceParserTests {
@Test
public void testFileSource() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileSourceParserTests.xml", this.getClass());
FileSource fileSource = (FileSource) context.getBean("fileSource");
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(fileSource);
File directory = (File) sourceAccessor.getPropertyValue("directory");
assertEquals(System.getProperty("java.io.tmpdir"), directory.getAbsolutePath());
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.file.config;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.file.FileTarget;
import org.springframework.integration.message.Target;
/**
* @author Mark Fisher
*/
public class FileTargetParserTests {
@Test
public void testFileTarget() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileTargetParserTests.xml", this.getClass());
Target target = (Target) context.getBean("target");
assertEquals(FileTarget.class, target.getClass());
}
}

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<si:file-source id="fileSource" directory="${java.io.tmpdir}"/>
<context:property-placeholder/>
</beans>

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<si:message-bus/>
<si:channel id="testChannel"/>
<si:file-target id="target" directory="${java.io.tmpdir}"/>
<context:property-placeholder/>
</beans>

View File

@@ -0,0 +1,177 @@
/*
* 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.ftp;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* @author Marius Bogoevici
*/
public class DirectoryContentManagerTests {
private DirectoryContentManager directoryContentManager;
@Before
public void setUp() {
directoryContentManager = new DirectoryContentManager();
}
@Test
public void testInitialization() {
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
Map<String, FileInfo> remoteSnapshot = generateInitialSnapshot();
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertEquals(3, directoryContentManager.getBacklog().size());
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("a.txt"));
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("b.txt"));
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("c.txt"));
}
@Test
public void testFullProcessingInOneStep() {
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
Map<String, FileInfo> remoteSnapshot = generateInitialSnapshot();
directoryContentManager.processSnapshot(remoteSnapshot);
directoryContentManager.fileProcessed("a.txt");
directoryContentManager.fileProcessed("b.txt");
directoryContentManager.fileProcessed("c.txt");
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
}
@Test
public void testFullProcessingInTwoSteps() {
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
Map<String, FileInfo> remoteSnapshot = generateInitialSnapshot();
directoryContentManager.processSnapshot(remoteSnapshot);
directoryContentManager.fileProcessed("a.txt");
directoryContentManager.fileProcessed("b.txt");
Assert.assertEquals(1, directoryContentManager.getBacklog().size());
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("c.txt"));
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertEquals(1, directoryContentManager.getBacklog().size());
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("c.txt"));
directoryContentManager.fileProcessed("c.txt");
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
}
@Test
public void testOneFileChangedSize() {
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
Map<String, FileInfo> remoteSnapshot = generateInitialSnapshot();
directoryContentManager.processSnapshot(remoteSnapshot);
directoryContentManager.fileProcessed("a.txt");
directoryContentManager.fileProcessed("b.txt");
Assert.assertEquals(1, directoryContentManager.getBacklog().size());
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("c.txt"));
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertEquals(1, directoryContentManager.getBacklog().size());
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("c.txt"));
directoryContentManager.fileProcessed("c.txt");
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
directoryContentManager.processSnapshot(remoteSnapshot);
remoteSnapshot.put("c.txt", new FileInfo("c.txt", 1001, 112));
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertEquals(1, directoryContentManager.getBacklog().size());
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("c.txt"));
}
@Test
public void testOneFileChangedDate() {
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
Map<String, FileInfo> remoteSnapshot = generateInitialSnapshot();
directoryContentManager.processSnapshot(remoteSnapshot);
directoryContentManager.fileProcessed("a.txt");
directoryContentManager.fileProcessed("b.txt");
Assert.assertEquals(1, directoryContentManager.getBacklog().size());
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("c.txt"));
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertEquals(1, directoryContentManager.getBacklog().size());
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("c.txt"));
directoryContentManager.fileProcessed("c.txt");
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
directoryContentManager.processSnapshot(remoteSnapshot);
remoteSnapshot.put("c.txt", new FileInfo("c.txt", 1011, 102));
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertEquals(1, directoryContentManager.getBacklog().size());
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("c.txt"));
}
@Test
public void testOneFileAdded() {
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
Map<String, FileInfo> remoteSnapshot = generateInitialSnapshot();
directoryContentManager.processSnapshot(remoteSnapshot);
directoryContentManager.fileProcessed("a.txt");
directoryContentManager.fileProcessed("b.txt");
directoryContentManager.fileProcessed("c.txt");
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
directoryContentManager.processSnapshot(remoteSnapshot);
remoteSnapshot.put("d.txt", new FileInfo("d.txt", 1003, 103));
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertEquals(1, directoryContentManager.getBacklog().size());
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("d.txt"));
}
@Test
public void testOneFileRemoved() {
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
Map<String, FileInfo> remoteSnapshot = generateInitialSnapshot();
directoryContentManager.processSnapshot(remoteSnapshot);
directoryContentManager.fileProcessed("a.txt");
directoryContentManager.fileProcessed("b.txt");
directoryContentManager.fileProcessed("c.txt");
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
directoryContentManager.processSnapshot(remoteSnapshot);
remoteSnapshot.remove("c.txt");
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
}
@Test
public void testOneFileRemovedBeforeBeingProcessedInTheNextStep() {
Assert.assertTrue(directoryContentManager.getBacklog().isEmpty());
Map<String, FileInfo> remoteSnapshot = generateInitialSnapshot();
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertTrue(directoryContentManager.getBacklog().containsKey("c.txt"));
remoteSnapshot.remove("c.txt");
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertEquals(2, directoryContentManager.getBacklog().size());
directoryContentManager.processSnapshot(remoteSnapshot);
Assert.assertEquals(2, directoryContentManager.getBacklog().size());
}
private static Map<String, FileInfo> generateInitialSnapshot() {
Map<String, FileInfo> remoteSnapshot = new HashMap<String, FileInfo>();
remoteSnapshot.put("a.txt", new FileInfo("a.txt", 1000, 100));
remoteSnapshot.put("b.txt", new FileInfo("b.txt", 1001, 101));
remoteSnapshot.put("c.txt", new FileInfo("c.txt", 1002, 102));
return remoteSnapshot;
}
}

View File

@@ -0,0 +1,48 @@
/*
* 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.ftp.config;
import static org.junit.Assert.assertEquals;
import java.io.File;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.ftp.FtpSource;
/**
* @author Mark Fisher
*/
public class FtpSourceParserTests {
@Test
public void testFtpSourceAdapterParser() {
ApplicationContext context = new ClassPathXmlApplicationContext("ftpSourceParserTests.xml", this.getClass());
FtpSource ftpSource = (FtpSource) context.getBean("ftpSource");
DirectFieldAccessor accessor = new DirectFieldAccessor(ftpSource);
assertEquals("testHost", accessor.getPropertyValue("host"));
assertEquals(2121, accessor.getPropertyValue("port"));
assertEquals(new File("/local"), accessor.getPropertyValue("localWorkingDirectory"));
assertEquals("/remote", accessor.getPropertyValue("remoteWorkingDirectory"));
assertEquals("testUser", accessor.getPropertyValue("username"));
assertEquals("testPassword", accessor.getPropertyValue("password"));
}
}

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:ftp-source id="ftpSource"
host="testHost"
port="2121"
local-working-directory="/local"
remote-working-directory="/remote"
username="testUser"
password="testPassword"/>
</beans>

View File

@@ -0,0 +1,108 @@
/*
* 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.httpinvoker;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.concurrent.Executors;
import org.junit.Test;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.remoting.support.RemoteInvocation;
import org.springframework.remoting.support.RemoteInvocationResult;
/**
* @author Mark Fisher
*/
public class HttpInvokerSourceAdapterTests {
@Test
public void testRequestOnly() throws Exception {
MessageChannel channel = new QueueChannel();
HttpInvokerSourceAdapter adapter = new HttpInvokerSourceAdapter(channel);
adapter.setExpectReply(false);
adapter.afterPropertiesSet();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(createRequestContent(new StringMessage("test")));
adapter.handleRequest(request, response);
Message<?> message = channel.receive(500);
assertNotNull(message);
assertEquals("test", message.getPayload());
}
@Test
public void testRequestExpectingReply() throws Exception {
final MessageChannel channel = new QueueChannel();
Executors.newSingleThreadExecutor().execute(new Runnable() {
public void run() {
Message<?> message = channel.receive();
MessageChannel replyChannel = (MessageChannel) message.getHeader().getReturnAddress();
replyChannel.send(new StringMessage(message.getPayload().toString().toUpperCase()));
}
});
HttpInvokerSourceAdapter adapter = new HttpInvokerSourceAdapter(channel);
adapter.setExpectReply(true);
adapter.afterPropertiesSet();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(createRequestContent(new StringMessage("test")));
adapter.handleRequest(request, response);
Message<?> reply = extractMessageFromResponse(response);
assertEquals("TEST", reply.getPayload());
}
private static byte[] createRequestContent(Message<?> message) throws IOException {
RemoteInvocation invocation = new RemoteInvocation(
"handle", new Class[] { Message.class }, new Object[] { message });
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
ObjectOutputStream oos = new ObjectOutputStream(baos);
try {
oos.writeObject(invocation);
oos.flush();
}
finally {
oos.close();
}
return baos.toByteArray();
}
private static Message<?> extractMessageFromResponse(MockHttpServletResponse response) throws IOException, ClassNotFoundException {
byte[] responseContent = response.getContentAsByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(responseContent);
ObjectInputStream ois = new ObjectInputStream(bais);
RemoteInvocationResult remoteResult = (RemoteInvocationResult) ois.readObject();
Object resultValue = remoteResult.getValue();
assertTrue(resultValue instanceof Message);
return (Message<?>) resultValue;
}
}

View File

@@ -0,0 +1,83 @@
/*
* 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.httpinvoker.config;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.httpinvoker.HttpInvokerSourceAdapter;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.RequestReplyTemplate;
/**
* @author Mark Fisher
*/
public class HttpInvokerSourceAdapterParserTests {
@Test
public void testAdapterWithDefaults() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"httpInvokerSourceAdapterParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
HttpInvokerSourceAdapter adapter = (HttpInvokerSourceAdapter) context.getBean("adapterWithDefaults");
DirectFieldAccessor accessor = new DirectFieldAccessor(adapter);
assertEquals(channel, accessor.getPropertyValue("requestChannel"));
assertEquals(true, accessor.getPropertyValue("expectReply"));
RequestReplyTemplate template = (RequestReplyTemplate)
accessor.getPropertyValue("requestReplyTemplate");
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
assertEquals(-1L, templateAccessor.getPropertyValue("requestTimeout"));
assertEquals(-1L, templateAccessor.getPropertyValue("replyTimeout"));
}
@Test
public void testAdapterWithName() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"httpInvokerSourceAdapterParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
HttpInvokerSourceAdapter adapter = (HttpInvokerSourceAdapter) context.getBean("/adapter/with/name");
DirectFieldAccessor accessor = new DirectFieldAccessor(adapter);
assertEquals(channel, accessor.getPropertyValue("requestChannel"));
assertEquals(true, accessor.getPropertyValue("expectReply"));
RequestReplyTemplate template = (RequestReplyTemplate)
accessor.getPropertyValue("requestReplyTemplate");
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
assertEquals(-1L, templateAccessor.getPropertyValue("requestTimeout"));
assertEquals(-1L, templateAccessor.getPropertyValue("replyTimeout"));
}
@Test
public void testAdapterWithCustomProperties() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"httpInvokerSourceAdapterParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
HttpInvokerSourceAdapter adapter = (HttpInvokerSourceAdapter) context.getBean("adapterWithCustomProperties");
DirectFieldAccessor accessor = new DirectFieldAccessor(adapter);
assertEquals(channel, accessor.getPropertyValue("requestChannel"));
assertEquals(false, accessor.getPropertyValue("expectReply"));
RequestReplyTemplate template = (RequestReplyTemplate)
accessor.getPropertyValue("requestReplyTemplate");
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
assertEquals(123L, templateAccessor.getPropertyValue("requestTimeout"));
assertEquals(456L, templateAccessor.getPropertyValue("replyTimeout"));
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.httpinvoker.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.httpinvoker.HttpInvokerTargetAdapter;
import org.springframework.integration.endpoint.HandlerEndpoint;
/**
* @author Mark Fisher
*/
public class HttpInvokerTargetAdapterParserTests {
@Test
public void testHttpInvokerTargetAdapter() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"httpInvokerTargetAdapterParserTests.xml", this.getClass());
HandlerEndpoint endpoint = (HandlerEndpoint) context.getBean("adapter");
assertNotNull(endpoint);
assertEquals(HttpInvokerTargetAdapter.class, endpoint.getHandler().getClass());
assertEquals("testChannel", endpoint.getSubscription().getChannelName());
}
}

View File

@@ -0,0 +1,22 @@
<?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">
<message-bus/>
<channel id="testChannel"/>
<httpinvoker-source id="adapterWithDefaults" request-channel="testChannel"/>
<httpinvoker-source name="/adapter/with/name" request-channel="testChannel"/>
<httpinvoker-source id="adapterWithCustomProperties"
request-channel="testChannel" request-timeout="123"
expect-reply="false" reply-timeout="456"/>
</beans:beans>

View File

@@ -0,0 +1,16 @@
<?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">
<message-bus/>
<channel id="testChannel"/>
<httpinvoker-target id="adapter" channel="testChannel" url="http://localhost:8080/test"/>
</beans:beans>

View File

@@ -0,0 +1,229 @@
/*
* 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.jms;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import javax.jms.Destination;
import javax.jms.JMSException;
import org.junit.Test;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class DefaultJmsHeaderMapperTests {
@Test
public void testJmsReplyToMappedFromHeader() throws JMSException {
StringMessage message = new StringMessage("test");
Destination replyTo = new Destination() {};
message.getHeader().setAttribute(JmsAttributeKeys.REPLY_TO, replyTo);
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage();
mapper.mapFromMessageHeader(message.getHeader(), jmsMessage);
assertNotNull(jmsMessage.getJMSReplyTo());
assertSame(replyTo, jmsMessage.getJMSReplyTo());
}
@Test
public void testJmsReplyToIgnoredIfIncorrectType() throws JMSException {
StringMessage message = new StringMessage("test");
message.getHeader().setAttribute(JmsAttributeKeys.REPLY_TO, "not-a-destination");
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage();
mapper.mapFromMessageHeader(message.getHeader(), jmsMessage);
assertNull(jmsMessage.getJMSReplyTo());
}
@Test
public void testJmsCorrelationIdMappedFromHeader() throws JMSException {
StringMessage message = new StringMessage("test");
String jmsCorrelationId = "ABC-123";
message.getHeader().setAttribute(JmsAttributeKeys.CORRELATION_ID, jmsCorrelationId);
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage();
mapper.mapFromMessageHeader(message.getHeader(), jmsMessage);
assertNotNull(jmsMessage.getJMSCorrelationID());
assertEquals(jmsCorrelationId, jmsMessage.getJMSCorrelationID());
}
@Test
public void testJmsCorrelationIdIgnoredIfIncorrectType() throws JMSException {
StringMessage message = new StringMessage("test");
message.getHeader().setAttribute(JmsAttributeKeys.CORRELATION_ID, new Integer(123));
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage();
mapper.mapFromMessageHeader(message.getHeader(), jmsMessage);
assertNull(jmsMessage.getJMSCorrelationID());
}
@Test
public void testJmsTypeMappedFromHeader() throws JMSException {
StringMessage message = new StringMessage("test");
String jmsType = "testing";
message.getHeader().setAttribute(JmsAttributeKeys.TYPE, jmsType);
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage();
mapper.mapFromMessageHeader(message.getHeader(), jmsMessage);
assertNotNull(jmsMessage.getJMSType());
assertEquals(jmsType, jmsMessage.getJMSType());
}
@Test
public void testJmsTypeIgnoredIfIncorrectType() throws JMSException {
StringMessage message = new StringMessage("test");
message.getHeader().setAttribute(JmsAttributeKeys.TYPE, new Integer(123));
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage();
mapper.mapFromMessageHeader(message.getHeader(), jmsMessage);
assertNull(jmsMessage.getJMSType());
}
@Test
public void testUserDefinedPropertyMappedFromHeader() throws JMSException {
StringMessage message = new StringMessage("test");
message.getHeader().setAttribute(JmsAttributeKeys.USER_DEFINED_ATTRIBUTE_PREFIX + "foo", new Integer(123));
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage();
mapper.mapFromMessageHeader(message.getHeader(), jmsMessage);
Object value = jmsMessage.getObjectProperty("foo");
assertNotNull(value);
assertEquals(Integer.class, value.getClass());
assertEquals(123, ((Integer) value).intValue());
}
@Test
public void testUserDefinedPropertyWithUnsupportedType() throws JMSException {
StringMessage message = new StringMessage("test");
Destination destination = new Destination() {};
message.getHeader().setAttribute(JmsAttributeKeys.USER_DEFINED_ATTRIBUTE_PREFIX + "destination", destination);
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage();
mapper.mapFromMessageHeader(message.getHeader(), jmsMessage);
Object value = jmsMessage.getObjectProperty("foo");
assertNull(value);
}
@Test
public void testJmsReplyToMappedToHeader() throws JMSException {
StringMessage message = new StringMessage("test");
Destination replyTo = new Destination() {};
javax.jms.Message jmsMessage = new StubTextMessage();
jmsMessage.setJMSReplyTo(replyTo);
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
mapper.mapToMessageHeader(jmsMessage, message.getHeader());
Object attrib = message.getHeader().getAttribute(JmsAttributeKeys.REPLY_TO);
assertNotNull(attrib);
assertSame(replyTo, attrib);
}
@Test
public void testJmsCorrelationIdMappedToHeader() throws JMSException {
StringMessage message = new StringMessage("test");
String correlationId = "ABC-123";
javax.jms.Message jmsMessage = new StubTextMessage();
jmsMessage.setJMSCorrelationID(correlationId);
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
mapper.mapToMessageHeader(jmsMessage, message.getHeader());
Object attrib = message.getHeader().getAttribute(JmsAttributeKeys.CORRELATION_ID);
assertNotNull(attrib);
assertSame(correlationId, attrib);
}
@Test
public void testJmsTypeMappedToHeader() throws JMSException {
StringMessage message = new StringMessage("test");
String type = "testing";
javax.jms.Message jmsMessage = new StubTextMessage();
jmsMessage.setJMSType(type);
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
mapper.mapToMessageHeader(jmsMessage, message.getHeader());
Object attrib = message.getHeader().getAttribute(JmsAttributeKeys.TYPE);
assertNotNull(attrib);
assertSame(type, attrib);
}
@Test
public void testUserDefinedPropertyMappedToHeader() throws JMSException {
StringMessage message = new StringMessage("test");
javax.jms.Message jmsMessage = new StubTextMessage();
jmsMessage.setIntProperty("foo", 123);
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
mapper.mapToMessageHeader(jmsMessage, message.getHeader());
Object attrib = message.getHeader().getAttribute(JmsAttributeKeys.USER_DEFINED_ATTRIBUTE_PREFIX + "foo");
assertNotNull(attrib);
assertEquals(Integer.class, attrib.getClass());
assertEquals(123, ((Integer) attrib).intValue());
}
@Test
public void testJMSExceptionIsNotFatal() throws JMSException {
StringMessage message = new StringMessage("test");
message.getHeader().setAttribute(JmsAttributeKeys.USER_DEFINED_ATTRIBUTE_PREFIX + "foo", new Integer(123));
message.getHeader().setAttribute(JmsAttributeKeys.USER_DEFINED_ATTRIBUTE_PREFIX + "bad", new Integer(456));
message.getHeader().setAttribute(JmsAttributeKeys.USER_DEFINED_ATTRIBUTE_PREFIX + "bar", new Integer(789));
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage() {
@Override
public void setObjectProperty(String name, Object value) throws JMSException {
if (name.equals("bad")) {
throw new JMSException("illegal property");
}
super.setObjectProperty(name, value);
}
};
mapper.mapFromMessageHeader(message.getHeader(), jmsMessage);
Object foo = jmsMessage.getObjectProperty("foo");
assertNotNull(foo);
Object bar = jmsMessage.getObjectProperty("bar");
assertNotNull(bar);
Object bad = jmsMessage.getObjectProperty("bad");
assertNull(bad);
}
@Test
public void testIllegalArgumentExceptionIsNotFatal() throws JMSException {
StringMessage message = new StringMessage("test");
message.getHeader().setAttribute(JmsAttributeKeys.USER_DEFINED_ATTRIBUTE_PREFIX + "foo", new Integer(123));
message.getHeader().setAttribute(JmsAttributeKeys.USER_DEFINED_ATTRIBUTE_PREFIX + "bad", new Integer(456));
message.getHeader().setAttribute(JmsAttributeKeys.USER_DEFINED_ATTRIBUTE_PREFIX + "bar", new Integer(789));
DefaultJmsHeaderMapper mapper = new DefaultJmsHeaderMapper();
javax.jms.Message jmsMessage = new StubTextMessage() {
@Override
public void setObjectProperty(String name, Object value) throws JMSException {
if (name.equals("bad")) {
throw new IllegalArgumentException("illegal property");
}
super.setObjectProperty(name, value);
}
};
mapper.mapFromMessageHeader(message.getHeader(), jmsMessage);
Object foo = jmsMessage.getObjectProperty("foo");
assertNotNull(foo);
Object bar = jmsMessage.getObjectProperty("bar");
assertNotNull(bar);
Object bad = jmsMessage.getObjectProperty("bad");
assertNull(bad);
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2002-2007 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.jms;
import javax.jms.Connection;
import javax.jms.ConnectionConsumer;
import javax.jms.ConnectionMetaData;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.ServerSessionPool;
import javax.jms.Session;
import javax.jms.Topic;
/**
* @author Mark Fisher
*/
public class StubConnection implements Connection {
private String messageText;
public StubConnection(String messageText) {
this.messageText = messageText;
}
public void close() throws JMSException {
}
public ConnectionConsumer createConnectionConsumer(Destination destination, String messageSelector,
ServerSessionPool sessionPool, int maxMessages) throws JMSException {
return null;
}
public ConnectionConsumer createDurableConnectionConsumer(Topic topic, String subscriptionName,
String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException {
return null;
}
public Session createSession(boolean transacted, int acknowledgeMode) throws JMSException {
return new StubSession(this.messageText);
}
public String getClientID() throws JMSException {
return null;
}
public ExceptionListener getExceptionListener() throws JMSException {
return null;
}
public ConnectionMetaData getMetaData() throws JMSException {
return null;
}
public void setClientID(String clientID) throws JMSException {
}
public void setExceptionListener(ExceptionListener listener) throws JMSException {
}
public void start() throws JMSException {
}
public void stop() throws JMSException {
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2002-2007 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.jms;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
/**
* @author Mark Fisher
*/
public class StubConsumer implements MessageConsumer {
private String messageText;
public StubConsumer(String messageText) {
this.messageText = messageText;
}
public void close() throws JMSException {
}
public MessageListener getMessageListener() throws JMSException {
return null;
}
public String getMessageSelector() throws JMSException {
return null;
}
public Message receive() throws JMSException {
StubTextMessage message = new StubTextMessage();
message.setText(this.messageText);
return message;
}
public Message receive(long timeout) throws JMSException {
return this.receive();
}
public Message receiveNoWait() throws JMSException {
return this.receive();
}
public void setMessageListener(MessageListener listener) throws JMSException {
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2002-2007 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.jms;
import javax.jms.Destination;
/**
* @author Mark Fisher
*/
public class StubDestination implements Destination {
}

View File

@@ -0,0 +1,168 @@
/*
* Copyright 2002-2007 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.jms;
import java.io.Serializable;
import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TemporaryQueue;
import javax.jms.TemporaryTopic;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;
/**
* @author Mark Fisher
*/
public class StubSession implements Session {
private String messageText;
public StubSession(String messageText) {
this.messageText = messageText;
}
public void close() throws JMSException {
}
public void commit() throws JMSException {
}
public QueueBrowser createBrowser(Queue queue) throws JMSException {
return null;
}
public QueueBrowser createBrowser(Queue queue, String messageSelector) throws JMSException {
return null;
}
public BytesMessage createBytesMessage() throws JMSException {
return null;
}
public MessageConsumer createConsumer(Destination destination) throws JMSException {
return new StubConsumer(this.messageText);
}
public MessageConsumer createConsumer(Destination destination, String messageSelector) throws JMSException {
return new StubConsumer(this.messageText);
}
public MessageConsumer createConsumer(Destination destination, String messageSelector, boolean NoLocal)
throws JMSException {
return new StubConsumer(this.messageText);
}
public TopicSubscriber createDurableSubscriber(Topic topic, String name) throws JMSException {
return null;
}
public TopicSubscriber createDurableSubscriber(Topic topic, String name, String messageSelector, boolean noLocal)
throws JMSException {
return null;
}
public MapMessage createMapMessage() throws JMSException {
return null;
}
public Message createMessage() throws JMSException {
return null;
}
public ObjectMessage createObjectMessage() throws JMSException {
return null;
}
public ObjectMessage createObjectMessage(Serializable object) throws JMSException {
return null;
}
public MessageProducer createProducer(Destination destination) throws JMSException {
return null;
}
public Queue createQueue(String queueName) throws JMSException {
return null;
}
public StreamMessage createStreamMessage() throws JMSException {
return null;
}
public TemporaryQueue createTemporaryQueue() throws JMSException {
return null;
}
public TemporaryTopic createTemporaryTopic() throws JMSException {
return null;
}
public TextMessage createTextMessage() throws JMSException {
return null;
}
public TextMessage createTextMessage(String text) throws JMSException {
return null;
}
public Topic createTopic(String topicName) throws JMSException {
return null;
}
public int getAcknowledgeMode() throws JMSException {
return 0;
}
public MessageListener getMessageListener() throws JMSException {
return null;
}
public boolean getTransacted() throws JMSException {
return false;
}
public void recover() throws JMSException {
}
public void rollback() throws JMSException {
}
public void run() {
}
public void setMessageListener(MessageListener listener) throws JMSException {
}
public void unsubscribe(String name) throws JMSException {
}
}

View File

@@ -0,0 +1,216 @@
/*
* 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.jms;
import java.util.Enumeration;
import java.util.concurrent.ConcurrentHashMap;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
public class StubTextMessage implements TextMessage {
private String text;
private Destination replyTo;
private String correlationID;
private String type;
private ConcurrentHashMap<String, Object> properties = new ConcurrentHashMap<String, Object>();
public String getText() throws JMSException {
return this.text;
}
public void setText(String text) throws JMSException {
this.text = text;
}
public void acknowledge() throws JMSException {
}
public void clearBody() throws JMSException {
}
public void clearProperties() throws JMSException {
}
public boolean getBooleanProperty(String name) throws JMSException {
return false;
}
public byte getByteProperty(String name) throws JMSException {
return 0;
}
public double getDoubleProperty(String name) throws JMSException {
return 0;
}
public float getFloatProperty(String name) throws JMSException {
return 0;
}
public int getIntProperty(String name) throws JMSException {
return 0;
}
public String getJMSCorrelationID() throws JMSException {
return this.correlationID;
}
public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
return null;
}
public int getJMSDeliveryMode() throws JMSException {
return 0;
}
public Destination getJMSDestination() throws JMSException {
return null;
}
public long getJMSExpiration() throws JMSException {
return 0;
}
public String getJMSMessageID() throws JMSException {
return null;
}
public int getJMSPriority() throws JMSException {
return 0;
}
public boolean getJMSRedelivered() throws JMSException {
return false;
}
public Destination getJMSReplyTo() throws JMSException {
return this.replyTo;
}
public long getJMSTimestamp() throws JMSException {
return 0;
}
public String getJMSType() throws JMSException {
return this.type;
}
public long getLongProperty(String name) throws JMSException {
return 0;
}
public Object getObjectProperty(String name) throws JMSException {
return this.properties.get(name);
}
public Enumeration getPropertyNames() throws JMSException {
return this.properties.keys();
}
public short getShortProperty(String name) throws JMSException {
return 0;
}
public String getStringProperty(String name) throws JMSException {
return null;
}
public boolean propertyExists(String name) throws JMSException {
return this.properties.containsKey(name);
}
public void setBooleanProperty(String name, boolean value) throws JMSException {
this.properties.put(name, value);
}
public void setByteProperty(String name, byte value) throws JMSException {
this.properties.put(name, value);
}
public void setDoubleProperty(String name, double value) throws JMSException {
this.properties.put(name, value);
}
public void setFloatProperty(String name, float value) throws JMSException {
this.properties.put(name, value);
}
public void setIntProperty(String name, int value) throws JMSException {
this.properties.put(name, value);
}
public void setJMSCorrelationID(String correlationID) throws JMSException {
this.correlationID = correlationID;
}
public void setJMSCorrelationIDAsBytes(byte[] correlationID) throws JMSException {
}
public void setJMSDeliveryMode(int deliveryMode) throws JMSException {
}
public void setJMSDestination(Destination destination) throws JMSException {
}
public void setJMSExpiration(long expiration) throws JMSException {
}
public void setJMSMessageID(String id) throws JMSException {
}
public void setJMSPriority(int priority) throws JMSException {
}
public void setJMSRedelivered(boolean redelivered) throws JMSException {
}
public void setJMSReplyTo(Destination replyTo) throws JMSException {
this.replyTo = replyTo;
}
public void setJMSTimestamp(long timestamp) throws JMSException {
}
public void setJMSType(String type) throws JMSException {
this.type = type;
}
public void setLongProperty(String name, long value) throws JMSException {
this.properties.put(name, value);
}
public void setObjectProperty(String name, Object value) throws JMSException {
this.properties.put(name, value);
}
public void setShortProperty(String name, short value) throws JMSException {
this.properties.put(name, value);
}
public void setStringProperty(String name, String value) throws JMSException {
this.properties.put(name, value);
}
}

View File

@@ -0,0 +1,143 @@
/*
* 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.jms.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.jms.JmsGateway;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class JmsGatewayParserTests {
@Test
public void testGatewayWithConnectionFactoryAndDestination() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsGatewayWithConnectionFactoryAndDestination.xml", this.getClass());
MessageChannel channel = new QueueChannel(1);
JmsGateway gateway = (JmsGateway) context.getBean("jmsGateway");
gateway.setRequestChannel(channel);
context.start();
Message<?> message = channel.receive(3000);
assertNotNull("message should not be null", message);
assertEquals("message-driven-test", message.getPayload());
context.stop();
}
@Test
public void testGatewayWithConnectionFactoryAndDestinationName() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsGatewayWithConnectionFactoryAndDestinationName.xml", this.getClass());
MessageChannel channel = new QueueChannel(1);
JmsGateway gateway = (JmsGateway) context.getBean("jmsGateway");
gateway.setRequestChannel(channel);
context.start();
Message<?> message = channel.receive(3000);
assertNotNull("message should not be null", message);
assertEquals("message-driven-test", message.getPayload());
context.stop();
}
@Test
public void testGatewayWithMessageConverter() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsGatewayWithMessageConverter.xml", this.getClass());
MessageChannel channel = new QueueChannel(1);
JmsGateway gateway = (JmsGateway) context.getBean("jmsGateway");
gateway.setRequestChannel(channel);
context.start();
Message<?> message = channel.receive(3000);
assertNotNull("message should not be null", message);
assertEquals("converted-test-message", message.getPayload());
context.stop();
}
@Test
public void testGatewayWithDefaultExpectReply() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsGatewaysWithExpectReplyAttributes.xml", this.getClass());
JmsGateway gateway = (JmsGateway) context.getBean("defaultGateway");
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
assertEquals(Boolean.FALSE, accessor.getPropertyValue("expectReply"));
}
@Test
public void testGatewayExpectingReply() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsGatewaysWithExpectReplyAttributes.xml", this.getClass());
JmsGateway gateway = (JmsGateway) context.getBean("gatewayExpectingReply");
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
assertEquals(Boolean.TRUE, accessor.getPropertyValue("expectReply"));
}
@Test
public void testGatewayNotExpectingReply() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsGatewaysWithExpectReplyAttributes.xml", this.getClass());
JmsGateway gateway = (JmsGateway) context.getBean("gatewayNotExpectingReply");
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
assertEquals(Boolean.FALSE, accessor.getPropertyValue("expectReply"));
}
@Test(expected=BeanDefinitionStoreException.class)
public void testGatewayWithConnectionFactoryOnly() {
try {
new ClassPathXmlApplicationContext("jmsGatewayWithConnectionFactoryOnly.xml", this.getClass());
}
catch (RuntimeException e) {
assertEquals(BeanCreationException.class, e.getCause().getClass());
throw e;
}
}
@Test(expected=BeanDefinitionStoreException.class)
public void testGatewayWithEmptyConnectionFactory() {
try {
new ClassPathXmlApplicationContext("jmsGatewayWithEmptyConnectionFactory.xml", this.getClass());
}
catch (RuntimeException e) {
assertEquals(BeanCreationException.class, e.getCause().getClass());
throw e;
}
}
@Test
public void testGatewayWithDefaultConnectionFactory() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsGatewayWithDefaultConnectionFactory.xml", this.getClass());
MessageChannel channel = new QueueChannel(1);
JmsGateway gateway = (JmsGateway) context.getBean("jmsGateway");
gateway.setRequestChannel(channel);
context.start();
Message<?> message = channel.receive(3000);
assertNotNull("message should not be null", message);
assertEquals("message-driven-test", message.getPayload());
context.stop();
}
}

View File

@@ -0,0 +1,141 @@
/*
* 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.jms.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.jms.JmsSource;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class JmsSourceParserTests {
@Test
public void testSourceWithJmsTemplate() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsSourceWithJmsTemplate.xml", this.getClass());
JmsSource source = (JmsSource) context.getBean("jmsSource");
Message<?> message = source.receive();
assertNotNull("message should not be null", message);
assertEquals("polling-test", message.getPayload());
}
@Test
public void testSourceWithConnectionFactoryAndDestination() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsSourceWithConnectionFactoryAndDestination.xml", this.getClass());
JmsSource source = (JmsSource) context.getBean("jmsSource");
Message<?> message = source.receive();
assertNotNull("message should not be null", message);
assertEquals("polling-test", message.getPayload());
context.stop();
}
@Test
public void testSourceWithConnectionFactoryAndDestinationName() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsSourceWithConnectionFactoryAndDestinationName.xml", this.getClass());
JmsSource source = (JmsSource) context.getBean("jmsSource");
Message<?> message = source.receive();
assertNotNull("message should not be null", message);
assertEquals("polling-test", message.getPayload());
context.stop();
}
@Test(expected=BeanDefinitionStoreException.class)
public void testSourceWithConnectionFactoryOnly() {
try {
new ClassPathXmlApplicationContext("jmsSourceWithConnectionFactoryOnly.xml", this.getClass());
}
catch (RuntimeException e) {
assertEquals(BeanCreationException.class, e.getCause().getClass());
throw e;
}
}
@Test(expected=BeanCreationException.class)
public void testSourceWithDestinationOnly() {
try {
new ClassPathXmlApplicationContext("jmsSourceWithDestinationOnly.xml", this.getClass());
}
catch (RuntimeException e) {
assertEquals(NoSuchBeanDefinitionException.class, e.getCause().getClass());
throw e;
}
}
@Test
public void testSourceWithDestinationAndDefaultConnectionFactory() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsSourceWithDestinationAndDefaultConnectionFactory.xml", this.getClass());
JmsSource source = (JmsSource) context.getBean("jmsSource");
Message<?> message = source.receive();
assertNotNull("message should not be null", message);
assertEquals("polling-test", message.getPayload());
context.stop();
}
@Test(expected=BeanCreationException.class)
public void testSourceWithDestinationNameOnly() {
new ClassPathXmlApplicationContext("jmsSourceWithDestinationNameOnly.xml", this.getClass());
}
@Test
public void testSourceWithDestinationNameAndDefaultConnectionFactory() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsSourceWithDestinationNameAndDefaultConnectionFactory.xml", this.getClass());
JmsSource source = (JmsSource) context.getBean("jmsSource");
Message<?> message = source.receive();
assertNotNull("message should not be null", message);
assertEquals("polling-test", message.getPayload());
}
@Test
public void testSourceWithHeaderMapper() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsSourceWithHeaderMapper.xml", this.getClass());
JmsSource source = (JmsSource) context.getBean("jmsSource");
Message<?> message = source.receive();
assertNotNull("message should not be null", message);
assertEquals("polling-test", message.getPayload());
assertEquals("foo", message.getHeader().getProperty("testProperty"));
assertEquals(new Integer(123), message.getHeader().getAttribute("testAttribute"));
}
@Test
public void testSourceEndpoint() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"jmsSourceEndpoint.xml", this.getClass());
context.start();
MessageChannel channel = (MessageChannel) context.getBean("channel");
Message<?> message = channel.receive(3000);
assertNotNull("message should not be null", message);
assertEquals("polling-test", message.getPayload());
context.stop();
}
}

View File

@@ -0,0 +1,87 @@
/*
* 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.jms.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.MessageHeaderMapper;
import org.springframework.integration.adapter.jms.JmsTarget;
/**
* @author Mark Fisher
*/
public class JmsTargetParserTests {
@Test
public void testTargetWithConnectionFactoryAndDestination() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"targetWithConnectionFactoryAndDestination.xml", this.getClass());
JmsTarget target = (JmsTarget) context.getBean("target");
DirectFieldAccessor accessor = new DirectFieldAccessor(target);
assertNotNull(accessor.getPropertyValue("jmsTemplate"));
}
@Test
public void testTargetWithConnectionFactoryAndDestinationName() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"targetWithConnectionFactoryAndDestinationName.xml", this.getClass());
JmsTarget target = (JmsTarget) context.getBean("target");
DirectFieldAccessor accessor = new DirectFieldAccessor(target);
assertNotNull(accessor.getPropertyValue("jmsTemplate"));
}
@Test
public void testTargetWithDefaultConnectionFactory() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"targetWithDefaultConnectionFactory.xml", this.getClass());
JmsTarget target = (JmsTarget) context.getBean("target");
DirectFieldAccessor accessor = new DirectFieldAccessor(target);
assertNotNull(accessor.getPropertyValue("jmsTemplate"));
}
@Test
@SuppressWarnings("unchecked")
public void testTargetWithHeaderMapper() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"targetWithHeaderMapper.xml", this.getClass());
JmsTarget target = (JmsTarget) context.getBean("target");
DirectFieldAccessor accessor = new DirectFieldAccessor(target);
MessageHeaderMapper headerMapper = (MessageHeaderMapper)
accessor.getPropertyValue("headerMapper");
assertNotNull(headerMapper);
assertEquals(TestMessageHeaderMapper.class, headerMapper.getClass());
}
@Test(expected=BeanDefinitionStoreException.class)
public void testTargetWithEmptyConnectionFactory() {
try {
new ClassPathXmlApplicationContext("targetWithEmptyConnectionFactory.xml", this.getClass());
}
catch (RuntimeException e) {
assertEquals(BeanCreationException.class, e.getCause().getClass());
throw e;
}
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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.jms.config;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
/**
* @author Mark Fisher
*/
public class TestMessageConverter implements MessageConverter {
public Object fromMessage(Message message) throws JMSException, MessageConversionException {
String original = ((TextMessage) message).getText();
return "converted-" + original;
}
public javax.jms.Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
return null;
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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.jms.config;
import javax.jms.Message;
import org.springframework.integration.adapter.MessageHeaderMapper;
import org.springframework.integration.message.MessageHeader;
/**
* @author Mark Fisher
*/
public class TestMessageHeaderMapper implements MessageHeaderMapper<Message> {
public void mapFromMessageHeader(MessageHeader header, Message target) {
}
public void mapToMessageHeader(Message source, MessageHeader header) {
header.setProperty("testProperty", "foo");
header.setAttribute("testAttribute", new Integer(123));
}
}

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:channel id="requestChannel"/>
<si:jms-gateway id="jmsGateway"
connection-factory="testConnectionFactory"
destination="testDestination"
request-channel="requestChannel"/>
<bean id="testConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="message-driven-test"/>
</bean>
</constructor-arg>
</bean>
<bean id="testDestination" class="org.springframework.integration.adapter.jms.StubDestination"/>
</beans>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:message-bus/>
<si:channel id="requestChannel"/>
<si:jms-gateway id="jmsGateway"
connection-factory="testConnectionFactory"
destination-name="testDestinationName"
request-channel="requestChannel"/>
<bean id="testConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="message-driven-test"/>
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:message-bus/>
<si:channel id="requestChannel"/>
<si:jms-gateway id="jmsGateway"
connection-factory="testConnectionFactory"
request-channel="requestChannel"/>
<bean id="testConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="message-driven-test"/>
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:message-bus/>
<si:channel id="requestChannel"/>
<si:jms-gateway id="jmsGateway"
destination-name="testDestinationName"
request-channel="requestChannel"/>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="message-driven-test"/>
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:channel id="requestChannel"/>
<si:jms-gateway id="jmsGateway"
connection-factory=""
destination-name="testDestinationName"
request-channel="requestChannel"/>
</beans>

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:message-bus/>
<si:channel id="requestChannel"/>
<si:jms-gateway id="jmsGateway"
connection-factory="testConnectionFactory"
destination="testDestination"
message-converter="converter"
request-channel="requestChannel"/>
<bean id="converter" class="org.springframework.integration.adapter.jms.config.TestMessageConverter"/>
<bean id="testConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="test-message"/>
</bean>
</constructor-arg>
</bean>
<bean id="testDestination" class="org.springframework.integration.adapter.jms.StubDestination"/>
</beans>

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:message-bus/>
<si:channel id="requestChannel"/>
<si:jms-gateway id="defaultGateway"
destination="testDestination"
request-channel="requestChannel"/>
<si:jms-gateway id="gatewayNotExpectingReply"
destination="testDestination"
request-channel="requestChannel"
expect-reply="false"/>
<si:jms-gateway id="gatewayExpectingReply"
destination="testDestination"
request-channel="requestChannel"
expect-reply="true"/>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="test-message"/>
</bean>
</constructor-arg>
</bean>
<bean id="testDestination" class="org.springframework.integration.adapter.jms.StubDestination"/>
</beans>

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:message-bus/>
<si:channel id="channel"/>
<si:source-endpoint id="endpoint" source="jmsSource" channel="channel">
<si:schedule period="5000"/>
</si:source-endpoint>
<si:jms-source id="jmsSource" jms-template="jmsTemplate"/>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestinationName" value="test"/>
</bean>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="polling-test"/>
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-source id="jmsSource"
connection-factory="testConnectionFactory"
destination="testDestination"/>
<bean id="testConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="polling-test"/>
</bean>
</constructor-arg>
</bean>
<bean id="testDestination" class="org.springframework.integration.adapter.jms.StubDestination"/>
</beans>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-source id="jmsSource"
connection-factory="testConnectionFactory"
destination-name="testDestinationName"/>
<bean id="testConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="polling-test"/>
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-source id="adapter" connection-factory="testConnectionFactory"/>
<bean id="testConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="polling-test"/>
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-source id="jmsSource" destination="testDestination"/>
<bean id="testDestination" class="org.springframework.integration.adapter.jms.StubDestination"/>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="polling-test"/>
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-source id="jmsSource" destination-name="testDestinationName"/>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="polling-test"/>
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-source id="jmsSource" destination-name="testDestinationName"/>
</beans>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-source id="jmsSource" destination="testDestination"/>
<bean id="testDestination" class="org.springframework.integration.adapter.jms.StubDestination"/>
</beans>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-source id="jmsSource" jms-template="jmsTemplate" header-mapper="mapper"/>
<bean id="mapper" class="org.springframework.integration.adapter.jms.config.TestMessageHeaderMapper"/>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestinationName" value="test"/>
</bean>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="polling-test"/>
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-source id="jmsSource" jms-template="jmsTemplate"/>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestinationName" value="test"/>
</bean>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="polling-test"/>
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-target id="target"
connection-factory="testConnectionFactory"
destination="testDestination"/>
<bean id="testConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="target-test"/>
</bean>
</constructor-arg>
</bean>
<bean id="testDestination" class="org.springframework.integration.adapter.jms.StubDestination"/>
</beans>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-target id="target"
connection-factory="testConnectionFactory"
destination-name="queue.test"/>
<bean id="testConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="target-test"/>
</bean>
</constructor-arg>
</bean>
</beans>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-target id="target" destination="testDestination"/>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="target-test"/>
</bean>
</constructor-arg>
</bean>
<bean id="testDestination" class="org.springframework.integration.adapter.jms.StubDestination"/>
</beans>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-target id="target"
connection-factory=""
destination="testDestination"/>
<bean id="testDestination" class="org.springframework.integration.adapter.jms.StubDestination"/>
</beans>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:jms-target id="target" destination="testDestination" header-mapper="mapper"/>
<bean id="mapper" class="org.springframework.integration.adapter.jms.config.TestMessageHeaderMapper"/>
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<constructor-arg>
<bean class="org.springframework.integration.adapter.jms.StubConnection">
<constructor-arg value="target-test"/>
</bean>
</constructor-arg>
</bean>
<bean id="testDestination" class="org.springframework.integration.adapter.jms.StubDestination"/>
</beans>

View File

@@ -0,0 +1,91 @@
/*
* 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.mail;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.DataInputStream;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.internet.MimeMessage;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.adapter.mail.MailTarget;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.StringMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Marius Bogoevici
*/
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/org/springframework/integration/adapter/mail/mailTarget.xml"})
public class MailTargetContextTests {
@Autowired
private MailTarget mailTarget;
@Autowired
private StubJavaMailSender mailSender;
@Before
public void reset() {
this.mailSender.reset();
}
@Test
public void testStringMesssagesWithConfiguration() {
this.mailTarget.send(new StringMessage(MailTestsHelper.MESSAGE_TEXT));
SimpleMailMessage message = MailTestsHelper.createSimpleMailMessage();
assertEquals("no mime message should have been sent",
0, this.mailSender.getSentMimeMessages().size());
assertEquals("only one simple message must be sent",
1, this.mailSender.getSentSimpleMailMessages().size());
assertEquals("message content different from expected",
message, this.mailSender.getSentSimpleMailMessages().get(0));
}
@Test
public void testByteArrayMessage() throws Exception {
byte[] payload = {1, 2, 3};
mailTarget.send(new GenericMessage<byte[]>(payload));
assertEquals("no mime message should have been sent",
1, mailSender.getSentMimeMessages().size());
assertEquals("only one simple message must be sent",
0, mailSender.getSentSimpleMailMessages().size());
byte[] buffer = new byte[1024];
MimeMessage mimeMessage = mailSender.getSentMimeMessages().get(0);
assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart);
int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream()).read(buffer);
assertEquals("buffer size does not match", payload.length, size);
byte[] messageContent = new byte[size];
System.arraycopy(buffer, 0, messageContent, 0, payload.length);
assertArrayEquals("buffer content does not match", payload, messageContent);
assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length);
}
}

View File

@@ -0,0 +1,117 @@
/*
* 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.mail;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.DataInputStream;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.StringMessage;
import org.springframework.mail.SimpleMailMessage;
/**
* @author Marius Bogoevici
*/
public class MailTargetTests {
private MailTarget mailTarget;
private StubJavaMailSender mailSender;
private StaticMailHeaderGenerator staticMailHeaderGenerator;
@Before
public void setUp() throws Exception {
this.mailSender = new StubJavaMailSender(new MimeMessage((Session) null));
this.staticMailHeaderGenerator = new StaticMailHeaderGenerator();
this.staticMailHeaderGenerator.setBcc(MailTestsHelper.BCC);
this.staticMailHeaderGenerator.setCc(MailTestsHelper.CC);
this.staticMailHeaderGenerator.setFrom(MailTestsHelper.FROM);
this.staticMailHeaderGenerator.setReplyTo(MailTestsHelper.REPLY_TO);
this.staticMailHeaderGenerator.setSubject(MailTestsHelper.SUBJECT);
this.staticMailHeaderGenerator.setTo(MailTestsHelper.TO);
this.mailTarget = new MailTarget(this.mailSender);
this.mailTarget.afterPropertiesSet();
}
@Test
public void testTextMessage() {
this.mailTarget.setHeaderGenerator(this.staticMailHeaderGenerator);
this.mailTarget.send(new StringMessage(MailTestsHelper.MESSAGE_TEXT));
SimpleMailMessage message = MailTestsHelper.createSimpleMailMessage();
assertEquals("no mime message should have been sent",
0, mailSender.getSentMimeMessages().size());
assertEquals("only one simple message must be sent",
1, mailSender.getSentSimpleMailMessages().size());
assertEquals("message content different from expected",
message, mailSender.getSentSimpleMailMessages().get(0));
}
@Test
public void testByteArrayMessage() throws Exception {
this.mailTarget.setHeaderGenerator(this.staticMailHeaderGenerator);
byte[] payload = {1, 2, 3};
this.mailTarget.send(new GenericMessage<byte[]>(payload));
byte[] buffer = new byte[1024];
MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0);
assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart);
int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream()).read(buffer);
assertEquals("buffer size does not match", payload.length, size);
byte[] messageContent = new byte[size];
System.arraycopy(buffer, 0, messageContent, 0, payload.length);
assertArrayEquals("buffer content does not match", payload, messageContent);
assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length);
}
@Test
public void testDefaultMailHeaderGenerator() {
StringMessage message = new StringMessage(MailTestsHelper.MESSAGE_TEXT);
message.getHeader().setAttribute(MailAttributeKeys.SUBJECT, MailTestsHelper.SUBJECT);
message.getHeader().setAttribute(MailAttributeKeys.TO, MailTestsHelper.TO);
message.getHeader().setAttribute(MailAttributeKeys.CC, MailTestsHelper.CC);
message.getHeader().setAttribute(MailAttributeKeys.BCC, MailTestsHelper.BCC);
message.getHeader().setAttribute(MailAttributeKeys.FROM, MailTestsHelper.FROM);
message.getHeader().setAttribute(MailAttributeKeys.REPLY_TO, MailTestsHelper.REPLY_TO);
this.mailTarget.send(message);
SimpleMailMessage mailMessage = MailTestsHelper.createSimpleMailMessage();
assertEquals("no mime message should have been sent",
0, mailSender.getSentMimeMessages().size());
assertEquals("only one simple message must be sent",
1, mailSender.getSentSimpleMailMessages().size());
assertEquals("message content different from expected",
mailMessage, mailSender.getSentSimpleMailMessages().get(0));
}
@After
public void reset() {
this.mailSender.reset();
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2002-2007 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.mail;
import org.springframework.mail.SimpleMailMessage;
/**
* @author Marius Bogoevici
*/
public class MailTestsHelper {
public static final String SUBJECT = "Some subject";
public static final String MESSAGE_TEXT = "Some text";
public static final String[] TO = new String[] {
"toRecipient1@springframework.org", "toRecipient2@springframework.org" };
public static final String[] CC = new String[] {
"ccRecipient1@springframework.org", "ccRecipient2@springframework.org" };
public static final String[] BCC = new String[] {
"bccRecipient1@springframework.org", "bccRecipient2@springframework.org" };
public static final String FROM = "from@springframework.org";
public static final String REPLY_TO = "replyTo@springframework.org";
public static SimpleMailMessage createSimpleMailMessage() {
SimpleMailMessage message = new SimpleMailMessage();
message.setBcc(BCC);
message.setCc(CC);
message.setTo(TO);
message.setSubject(SUBJECT);
message.setReplyTo(REPLY_TO);
message.setFrom(FROM);
message.setText(MESSAGE_TEXT);
return message;
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2002-2007 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.mail;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
/**
* @author Marius Bogoevici
*/
public class StubJavaMailSender implements JavaMailSender {
private MimeMessage uniqueMessage;
private final List<MimeMessage> sentMimeMessages = new ArrayList<MimeMessage>();
private final List<SimpleMailMessage> sentSimpleMailMessages = new ArrayList<SimpleMailMessage>();
public StubJavaMailSender(MimeMessage uniqueMessage) {
this.uniqueMessage = uniqueMessage;
}
public List<MimeMessage> getSentMimeMessages() {
return this.sentMimeMessages;
}
public List<SimpleMailMessage> getSentSimpleMailMessages() {
return this.sentSimpleMailMessages;
}
public MimeMessage createMimeMessage() {
return this.uniqueMessage;
}
public MimeMessage createMimeMessage(InputStream contentStream) throws MailException {
return this.uniqueMessage;
}
public void send(MimeMessage mimeMessage) throws MailException {
this.sentMimeMessages.add(mimeMessage);
}
public void send(MimeMessage[] mimeMessages) throws MailException {
this.sentMimeMessages.addAll(Arrays.asList(mimeMessages));
}
public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException {
throw new UnsupportedOperationException("MimeMessagePreparator not supported");
}
public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException {
throw new UnsupportedOperationException("MimeMessagePreparator not supported");
}
public void send(SimpleMailMessage simpleMessage) throws MailException {
this.sentSimpleMailMessages.add(simpleMessage);
}
public void send(SimpleMailMessage[] simpleMessages) throws MailException {
this.sentSimpleMailMessages.addAll(Arrays.asList(simpleMessages));
}
public void reset() {
this.sentMimeMessages.clear();
this.sentSimpleMailMessages.clear();
}
}

View File

@@ -0,0 +1,76 @@
/*
* 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.mail.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.mail.MailHeaderGenerator;
import org.springframework.integration.adapter.mail.MailTarget;
import org.springframework.integration.message.Message;
import org.springframework.mail.MailMessage;
import org.springframework.mail.MailSender;
/**
* @author Mark Fisher
*/
public class MailTargetParserTests {
@Test
public void testTargetWithMailSenderReference() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"mailTargetParserTests.xml", this.getClass());
MailTarget target = (MailTarget) context.getBean("targetWithMailSenderReference");
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(target);
MailSender mailSender = (MailSender) fieldAccessor.getPropertyValue("mailSender");
assertNotNull(mailSender);
}
@Test
public void testTargetWithHostProperty() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"mailTargetParserTests.xml", this.getClass());
MailTarget target = (MailTarget) context.getBean("targetWithHostProperty");
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(target);
MailSender mailSender = (MailSender) fieldAccessor.getPropertyValue("mailSender");
assertNotNull(mailSender);
}
@Test
public void testTargetWithHeaderGeneratorReference() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"mailTargetParserTests.xml", this.getClass());
MailTarget target = (MailTarget) context.getBean("targetWithHeaderGeneratorReference");
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(target);
MailHeaderGenerator headerGenerator =
(MailHeaderGenerator) fieldAccessor.getPropertyValue("mailHeaderGenerator");
assertEquals(TestHeaderGenerator.class, headerGenerator.getClass());
}
public static class TestHeaderGenerator implements MailHeaderGenerator {
public void populateMailMessageHeader(MailMessage mailMessage, Message<?> message) {
}
}
}

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
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">
<si:mail-target id="targetWithMailSenderReference" mail-sender="mailSender"/>
<si:mail-target id="targetWithHostProperty"
host="somehost" username="someuser" password="somepassword"/>
<bean id="mailSender" class="org.springframework.integration.adapter.mail.StubJavaMailSender">
<constructor-arg>
<bean class="javax.mail.internet.MimeMessage">
<constructor-arg type="javax.mail.Session"><null/></constructor-arg>
</bean>
</constructor-arg>
</bean>
<si:mail-target id="targetWithHeaderGeneratorReference"
mail-sender="mailSender"
header-generator="testHeaderGenerator"/>
<bean id="testHeaderGenerator"
class="org.springframework.integration.adapter.mail.config.MailTargetParserTests$TestHeaderGenerator"/>
</beans>

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="javaMailSender" class="org.springframework.integration.adapter.mail.StubJavaMailSender">
<constructor-arg>
<bean class="javax.mail.internet.MimeMessage">
<constructor-arg type="javax.mail.Session"><null/></constructor-arg>
</bean>
</constructor-arg>
</bean>
<bean id="mailTarget" class="org.springframework.integration.adapter.mail.MailTarget">
<constructor-arg ref="javaMailSender"/>
<property name="headerGenerator">
<bean class="org.springframework.integration.adapter.mail.StaticMailHeaderGenerator">
<property name="subject">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.SUBJECT"/>
</property>
<property name="to">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.TO"/>
</property>
<property name="cc">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.CC"/>
</property>
<property name="bcc">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.BCC"/>
</property>
<property name="from">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.FROM"/>
</property>
<property name="replyTo">
<util:constant static-field="org.springframework.integration.adapter.mail.MailTestsHelper.REPLY_TO"/>
</property>
</bean>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,146 @@
/*
* Copyright 2002-2007 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.rmi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.rmi.RemoteException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageHandlingException;
import org.springframework.integration.message.StringMessage;
import org.springframework.remoting.RemoteLookupFailureException;
import org.springframework.remoting.rmi.RmiServiceExporter;
/**
* @author Mark Fisher
*/
public class RmiTargetAdapterTests {
private final RmiTargetAdapter adapter = new RmiTargetAdapter("rmi://localhost:1099/testRemoteHandler");
@Before
public void createExporter() throws RemoteException {
RmiServiceExporter exporter = new RmiServiceExporter();
exporter.setService(new TestHandler());
exporter.setServiceInterface(MessageHandler.class);
exporter.setServiceName("testRemoteHandler");
exporter.afterPropertiesSet();
}
@Test
public void testSerializablePayload() throws RemoteException {
Message<?> replyMessage = adapter.handle(new StringMessage("test"));
assertNotNull(replyMessage);
assertEquals("TEST", replyMessage.getPayload());
}
@Test
public void testSerializableAttribute() throws RemoteException {
Message<?> requestMessage = new StringMessage("test");
requestMessage.getHeader().setAttribute("testAttribute", "foo");
Message<?> replyMessage = adapter.handle(requestMessage);
assertNotNull(replyMessage);
assertEquals("foo", replyMessage.getHeader().getAttribute("testAttribute"));
}
@Test
public void testProperty() throws RemoteException {
Message<?> requestMessage = new StringMessage("test");
requestMessage.getHeader().setProperty("testProperty", "bar");
Message<?> replyMessage = adapter.handle(requestMessage);
assertNotNull(replyMessage);
assertEquals("bar", replyMessage.getHeader().getProperty("testProperty"));
}
@Test(expected=MessageHandlingException.class)
public void testNonSerializablePayload() throws RemoteException {
NonSerializableTestObject payload = new NonSerializableTestObject();
Message<?> requestMessage = new GenericMessage<NonSerializableTestObject>(payload);
adapter.handle(requestMessage);
}
@Test(expected=MessageHandlingException.class)
public void testNonSerializableAttribute() throws RemoteException {
Message<?> requestMessage = new StringMessage("test");
requestMessage.getHeader().setAttribute("testAttribute", new NonSerializableTestObject());
adapter.handle(requestMessage);
}
@Test
public void testInvalidServiceName() throws RemoteException {
RmiTargetAdapter adapter = new RmiTargetAdapter("rmi://localhost:1099/noSuchService");
boolean exceptionThrown = false;
try {
adapter.handle(new StringMessage("test"));
}
catch (MessageHandlingException e) {
assertEquals(RemoteLookupFailureException.class, e.getCause().getClass());
exceptionThrown = true;
}
assertTrue(exceptionThrown);
}
@Test
public void testInvalidHost() {
RmiTargetAdapter adapter = new RmiTargetAdapter("rmi://noSuchHost:1099/testRemoteHandler");
boolean exceptionThrown = false;
try {
adapter.handle(new StringMessage("test"));
}
catch (MessageHandlingException e) {
assertEquals(RemoteLookupFailureException.class, e.getCause().getClass());
exceptionThrown = true;
}
assertTrue(exceptionThrown);
}
@Test
public void testInvalidUrl() throws RemoteException {
RmiTargetAdapter adapter = new RmiTargetAdapter("invalid");
boolean exceptionThrown = false;
try {
adapter.handle(new StringMessage("test"));
}
catch (MessageHandlingException e) {
assertEquals(RemoteLookupFailureException.class, e.getCause().getClass());
exceptionThrown = true;
}
assertTrue(exceptionThrown);
}
private static class TestHandler implements MessageHandler {
public Message<?> handle(Message<?> message) {
return new GenericMessage<String>(message.getPayload().toString().toUpperCase(), message.getHeader());
}
}
private static class NonSerializableTestObject {
}
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright 2002-2007 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.rmi.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.rmi.RmiSourceAdapter;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.RequestReplyTemplate;
/**
* @author Mark Fisher
*/
public class RmiSourceAdapterParserTests {
@Test
public void testAdapterWithDefaults() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"rmiSourceAdapterParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
RmiSourceAdapter adapter = (RmiSourceAdapter) context.getBean("adapterWithDefaults");
DirectFieldAccessor accessor = new DirectFieldAccessor(adapter);
assertEquals(channel, accessor.getPropertyValue("requestChannel"));
assertEquals(true, accessor.getPropertyValue("expectReply"));
RequestReplyTemplate template = (RequestReplyTemplate)
accessor.getPropertyValue("requestReplyTemplate");
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
assertEquals(-1L, templateAccessor.getPropertyValue("requestTimeout"));
assertEquals(-1L, templateAccessor.getPropertyValue("replyTimeout"));
}
@Test
public void testAdapterWithCustomProperties() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"rmiSourceAdapterParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
RmiSourceAdapter adapter = (RmiSourceAdapter) context.getBean("adapterWithCustomProperties");
DirectFieldAccessor accessor = new DirectFieldAccessor(adapter);
assertEquals(channel, accessor.getPropertyValue("requestChannel"));
assertEquals(false, accessor.getPropertyValue("expectReply"));
RequestReplyTemplate template = (RequestReplyTemplate)
accessor.getPropertyValue("requestReplyTemplate");
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
assertEquals(123L, templateAccessor.getPropertyValue("requestTimeout"));
assertEquals(456L, templateAccessor.getPropertyValue("replyTimeout"));
}
@Test
public void testAdapterWithHost() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"rmiSourceAdapterParserTests.xml", this.getClass());
RmiSourceAdapter adapter = (RmiSourceAdapter) context.getBean("adapterWithHost");
DirectFieldAccessor accessor = new DirectFieldAccessor(adapter);
assertEquals("localhost", accessor.getPropertyValue("registryHost"));
}
@Test
public void testAdapterWithPort() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"rmiSourceAdapterParserTests.xml", this.getClass());
RmiSourceAdapter adapter = (RmiSourceAdapter) context.getBean("adapterWithPort");
DirectFieldAccessor accessor = new DirectFieldAccessor(adapter);
assertEquals(1234, accessor.getPropertyValue("registryPort"));
}
@Test
public void testAdapterWithRemoteInvocationExecutorReference() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"rmiSourceAdapterParserTests.xml", this.getClass());
RmiSourceAdapter adapter = (RmiSourceAdapter) context.getBean("adapterWithExecutorRef");
DirectFieldAccessor accessor = new DirectFieldAccessor(adapter);
Object remoteInvocationExecutor = accessor.getPropertyValue("remoteInvocationExecutor");
assertNotNull(remoteInvocationExecutor);
assertEquals(StubRemoteInvocationExecutor.class, remoteInvocationExecutor.getClass());
}
}

View File

@@ -0,0 +1,61 @@
/*
* 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.rmi.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.rmi.RmiSourceAdapter;
import org.springframework.integration.adapter.rmi.RmiTargetAdapter;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.HandlerEndpoint;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class RmiTargetAdapterParserTests {
private final QueueChannel testChannel = new QueueChannel();
@Before
public void exportRemoteHandler() throws Exception {
testChannel.setBeanName("testChannel");
RmiSourceAdapter sourceAdapter = new RmiSourceAdapter(testChannel);
sourceAdapter.setExpectReply(false);
sourceAdapter.afterPropertiesSet();
}
@Test
public void testRmiTargetAdapter() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"rmiTargetAdapterParserTests.xml", this.getClass());
HandlerEndpoint endpoint = (HandlerEndpoint) context.getBean("adapter");
assertNotNull(endpoint);
assertEquals(RmiTargetAdapter.class, endpoint.getHandler().getClass());
RmiTargetAdapter adapter = (RmiTargetAdapter) endpoint.getHandler();
adapter.handle(new StringMessage("test"));
assertNotNull(testChannel.receive(500));
}
}

View File

@@ -0,0 +1,26 @@
/*
* 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.rmi.config;
import org.springframework.remoting.support.DefaultRemoteInvocationExecutor;
/**
* @author Mark Fisher
*/
public class StubRemoteInvocationExecutor extends DefaultRemoteInvocationExecutor {
}

View File

@@ -0,0 +1,27 @@
<?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">
<message-bus/>
<channel id="testChannel"/>
<rmi-source id="adapterWithDefaults" request-channel="testChannel"/>
<rmi-source id="adapterWithCustomProperties" request-channel="testChannel"
expect-reply="false" request-timeout="123" reply-timeout="456"/>
<rmi-source id="adapterWithHost" request-channel="testChannel" registry-host="localhost"/>
<rmi-source id="adapterWithPort" request-channel="testChannel" registry-port="1234"/>
<rmi-source id="adapterWithExecutorRef" request-channel="testChannel" remote-invocation-executor="invocationExecutor"/>
<beans:bean id="invocationExecutor" class="org.springframework.integration.adapter.rmi.config.StubRemoteInvocationExecutor"/>
</beans:beans>

View File

@@ -0,0 +1,16 @@
<?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">
<message-bus/>
<channel id="localChannel"/>
<rmi-target id="adapter" local-channel="localChannel" remote-channel="testChannel" host="localhost"/>
</beans:beans>

View File

@@ -0,0 +1,171 @@
/*
* 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.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.SourceEndpoint;
import org.springframework.integration.message.Message;
import org.springframework.integration.scheduling.PollingSchedule;
/**
* @author Mark Fisher
*/
public class ByteStreamSourceTests {
@Test
public void testEndOfStream() {
byte[] bytes = new byte[] {1,2,3};
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
MessageChannel channel = new QueueChannel();
ByteStreamSource source = new ByteStreamSource(stream);
PollingSchedule schedule = new PollingSchedule(1000);
schedule.setInitialDelay(10000);
SourceEndpoint endpoint = new SourceEndpoint(source, channel, schedule);
endpoint.run();
Message<?> message1 = channel.receive(500);
byte[] payload = (byte[]) message1.getPayload();
assertEquals(3, payload.length);
assertEquals(1, payload[0]);
assertEquals(2, payload[1]);
assertEquals(3, payload[2]);
Message<?> message2 = channel.receive(0);
assertNull(message2);
endpoint.run();
Message<?> message3 = channel.receive(0);
assertNull(message3);
}
@Test
public void testEndOfStreamWithMaxMessagesPerTask() throws Exception {
byte[] bytes = new byte[] {0,1,2,3,4,5,6,7};
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
MessageChannel channel = new QueueChannel();
ByteStreamSource source = new ByteStreamSource(stream);
source.setBytesPerMessage(8);
PollingSchedule schedule = new PollingSchedule(1000);
schedule.setInitialDelay(10000);
SourceEndpoint endpoint = new SourceEndpoint(source, channel, schedule);
endpoint.setMaxMessagesPerTask(5);
endpoint.run();
Message<?> message1 = channel.receive(500);
assertEquals(8, ((byte[]) message1.getPayload()).length);
Message<?> message2 = channel.receive(0);
assertNull(message2);
}
@Test
public void testMultipleMessagesWithSingleMessagePerTask() {
byte[] bytes = new byte[] {0,1,2,3,4,5,6,7};
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
MessageChannel channel = new QueueChannel();
ByteStreamSource source = new ByteStreamSource(stream);
source.setBytesPerMessage(4);
PollingSchedule schedule = new PollingSchedule(1000);
schedule.setInitialDelay(10000);
SourceEndpoint endpoint = new SourceEndpoint(source, channel, schedule);
endpoint.setMaxMessagesPerTask(1);
endpoint.run();
Message<?> message1 = channel.receive(0);
byte[] bytes1 = (byte[]) message1.getPayload();
assertEquals(4, bytes1.length);
assertEquals(0, bytes1[0]);
Message<?> message2 = channel.receive(0);
assertNull(message2);
endpoint.run();
Message<?> message3 = channel.receive(0);
byte[] bytes3 = (byte[]) message3.getPayload();
assertEquals(4, bytes3.length);
assertEquals(4, bytes3[0]);
}
@Test
public void testLessThanMaxMessagesAvailable() {
byte[] bytes = new byte[] {0,1,2,3,4,5,6,7};
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
MessageChannel channel = new QueueChannel();
ByteStreamSource source = new ByteStreamSource(stream);
source.setBytesPerMessage(4);
PollingSchedule schedule = new PollingSchedule(1000);
schedule.setInitialDelay(10000);
SourceEndpoint endpoint = new SourceEndpoint(source, channel, schedule);
endpoint.setMaxMessagesPerTask(5);
endpoint.run();
Message<?> message1 = channel.receive(0);
byte[] bytes1 = (byte[]) message1.getPayload();
assertEquals(4, bytes1.length);
assertEquals(0, bytes1[0]);
Message<?> message2 = channel.receive(0);
byte[] bytes2 = (byte[]) message2.getPayload();
assertEquals(4, bytes2.length);
assertEquals(4, bytes2[0]);
Message<?> message3 = channel.receive(0);
assertNull(message3);
}
@Test
public void testByteArrayIsTruncated() {
byte[] bytes = new byte[] {0,1,2,3,4,5};
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
MessageChannel channel = new QueueChannel();
ByteStreamSource source = new ByteStreamSource(stream);
source.setBytesPerMessage(4);
PollingSchedule schedule = new PollingSchedule(1000);
schedule.setInitialDelay(10000);
SourceEndpoint endpoint = new SourceEndpoint(source, channel, schedule);
endpoint.setMaxMessagesPerTask(1);
endpoint.run();
Message<?> message1 = channel.receive(0);
assertEquals(4, ((byte[]) message1.getPayload()).length);
Message<?> message2 = channel.receive(0);
assertNull(message2);
endpoint.run();
Message<?> message3 = channel.receive(0);
assertEquals(2, ((byte[]) message3.getPayload()).length);
}
@Test
public void testByteArrayIsNotTruncated() {
byte[] bytes = new byte[] {0,1,2,3,4,5};
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
MessageChannel channel = new QueueChannel();
ByteStreamSource source = new ByteStreamSource(stream);
source.setBytesPerMessage(4);
source.setShouldTruncate(false);
PollingSchedule schedule = new PollingSchedule(1000);
schedule.setInitialDelay(10000);
SourceEndpoint endpoint = new SourceEndpoint(source, channel, schedule);
endpoint.setMaxMessagesPerTask(1);
endpoint.run();
Message<?> message1 = channel.receive(0);
assertEquals(4, ((byte[]) message1.getPayload()).length);
Message<?> message2 = channel.receive(0);
assertNull(message2);
endpoint.run();
Message<?> message3 = channel.receive(0);
assertEquals(4, ((byte[]) message3.getPayload()).length);
assertEquals(0, ((byte[]) message3.getPayload())[3]);
}
}

View File

@@ -0,0 +1,212 @@
/*
* 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.Test;
import org.springframework.integration.channel.DispatcherPolicy;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.dispatcher.DefaultPollingDispatcher;
import org.springframework.integration.dispatcher.PollingDispatcherTask;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class ByteStreamTargetTests {
@Test
public void testSingleByteArray() {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ByteStreamTarget target = new ByteStreamTarget(stream);
target.send(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.send(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);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setMaxMessagesPerTask(3);
QueueChannel channel = new QueueChannel(5, dispatcherPolicy);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().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);
task.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);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setMaxMessagesPerTask(2);
QueueChannel channel = new QueueChannel(5, dispatcherPolicy);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().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);
task.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);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setMaxMessagesPerTask(5);
dispatcherPolicy.setReceiveTimeout(0);
QueueChannel channel = new QueueChannel(5, dispatcherPolicy);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().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);
task.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);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setMaxMessagesPerTask(2);
dispatcherPolicy.setReceiveTimeout(0);
QueueChannel channel = new QueueChannel(5, dispatcherPolicy);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().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);
task.run();
byte[] result1 = stream.toByteArray();
assertEquals(6, result1.length);
assertEquals(1, result1[0]);
task.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);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setMaxMessagesPerTask(5);
dispatcherPolicy.setReceiveTimeout(0);
QueueChannel channel = new QueueChannel(5, dispatcherPolicy);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().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);
task.run();
byte[] result1 = stream.toByteArray();
assertEquals(9, result1.length);
assertEquals(1, result1[0]);
task.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);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setMaxMessagesPerTask(2);
dispatcherPolicy.setReceiveTimeout(0);
QueueChannel channel = new QueueChannel(5, dispatcherPolicy);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().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);
task.run();
byte[] result1 = stream.toByteArray();
assertEquals(6, result1.length);
stream.reset();
task.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);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setMaxMessagesPerTask(2);
dispatcherPolicy.setReceiveTimeout(0);
QueueChannel channel = new QueueChannel(5, dispatcherPolicy);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().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);
task.run();
byte[] result1 = stream.toByteArray();
assertEquals(6, result1.length);
stream.write(new byte[] {123});
stream.flush();
task.run();
byte[] result2 = stream.toByteArray();
assertEquals(10, result2.length);
assertEquals(1, result2[0]);
assertEquals(123, result2[6]);
assertEquals(7, result2[7]);
}
}

View File

@@ -0,0 +1,110 @@
/*
* 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.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.SourceEndpoint;
import org.springframework.integration.message.Message;
import org.springframework.integration.scheduling.PollingSchedule;
/**
* @author Mark Fisher
*/
public class CharacterStreamSourceTests {
@Test
public void testEndOfStream() {
StringReader reader = new StringReader("test");
MessageChannel channel = new QueueChannel();
CharacterStreamSource source = new CharacterStreamSource(reader);
PollingSchedule schedule = new PollingSchedule(1000);
schedule.setInitialDelay(10000);
SourceEndpoint endpoint = new SourceEndpoint(source, channel, schedule);
endpoint.run();
Message<?> message1 = channel.receive(0);
assertEquals("test", message1.getPayload());
Message<?> message2 = channel.receive(0);
assertNull(message2);
endpoint.run();
Message<?> message3 = channel.receive(0);
assertNull(message3);
}
@Test
public void testEndOfStreamWithMaxMessagesPerTask() {
StringReader reader = new StringReader("test");
MessageChannel channel = new QueueChannel();
CharacterStreamSource source = new CharacterStreamSource(reader);
PollingSchedule schedule = new PollingSchedule(1000);
schedule.setInitialDelay(10000);
SourceEndpoint endpoint = new SourceEndpoint(source, channel, schedule);
endpoint.setMaxMessagesPerTask(5);
endpoint.run();
Message<?> message1 = channel.receive(0);
assertEquals("test", message1.getPayload());
Message<?> message2 = channel.receive(0);
assertNull(message2);
}
@Test
public void testMultipleLinesWithSingleMessagePerTask() {
String s = "test1" + System.getProperty("line.separator") + "test2";
StringReader reader = new StringReader(s);
MessageChannel channel = new QueueChannel();
CharacterStreamSource source = new CharacterStreamSource(reader);
PollingSchedule schedule = new PollingSchedule(1000);
schedule.setInitialDelay(10000);
SourceEndpoint endpoint = new SourceEndpoint(source, channel, schedule);
endpoint.setMaxMessagesPerTask(1);
endpoint.run();
Message<?> message1 = channel.receive(0);
assertEquals("test1", message1.getPayload());
Message<?> message2 = channel.receive(0);
assertNull(message2);
endpoint.run();
Message<?> message3 = channel.receive(0);
assertEquals("test2", message3.getPayload());
}
@Test
public void testLessThanMaxMessagesAvailable() {
String s = "test1" + System.getProperty("line.separator") + "test2";
StringReader reader = new StringReader(s);
MessageChannel channel = new QueueChannel();
CharacterStreamSource source = new CharacterStreamSource(reader);
PollingSchedule schedule = new PollingSchedule(1000);
schedule.setInitialDelay(5000);
SourceEndpoint endpoint = new SourceEndpoint(source, channel, schedule);
endpoint.setMaxMessagesPerTask(5);
endpoint.run();
Message<?> message1 = channel.receive(500);
assertEquals("test1", message1.getPayload());
Message<?> message2 = channel.receive(500);
assertEquals("test2", message2.getPayload());
Message<?> message3 = channel.receive(0);
assertNull(message3);
}
}

View File

@@ -0,0 +1,176 @@
/*
* 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.Test;
import org.springframework.integration.channel.DispatcherPolicy;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.dispatcher.DefaultPollingDispatcher;
import org.springframework.integration.dispatcher.PollingDispatcherTask;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class CharacterStreamTargetTests {
@Test
public void testSingleString() {
StringWriter writer = new StringWriter();
CharacterStreamTarget target = new CharacterStreamTarget(writer);
target.send(new StringMessage("foo"));
assertEquals("foo", writer.toString());
}
@Test
public void testTwoStringsAndNoNewLinesByDefault() {
MessageChannel channel = new QueueChannel();
StringWriter writer = new StringWriter();
CharacterStreamTarget target = new CharacterStreamTarget(writer);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().subscribe(target);
channel.send(new StringMessage("foo"), 0);
channel.send(new StringMessage("bar"), 0);
task.run();
assertEquals("foo", writer.toString());
task.run();
assertEquals("foobar", writer.toString());
}
@Test
public void testTwoStringsWithNewLines() {
MessageChannel channel = new QueueChannel();
StringWriter writer = new StringWriter();
CharacterStreamTarget target = new CharacterStreamTarget(writer);
target.setShouldAppendNewLine(true);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().subscribe(target);
channel.send(new StringMessage("foo"), 0);
channel.send(new StringMessage("bar"), 0);
task.run();
String newLine = System.getProperty("line.separator");
assertEquals("foo" + newLine, writer.toString());
task.run();
assertEquals("foo" + newLine + "bar" + newLine, writer.toString());
}
@Test
public void testMaxMessagesPerTaskSameAsMessageCount() {
StringWriter writer = new StringWriter();
CharacterStreamTarget target = new CharacterStreamTarget(writer);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setMaxMessagesPerTask(2);
QueueChannel channel = new QueueChannel(5, dispatcherPolicy);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().subscribe(target);
channel.send(new StringMessage("foo"), 0);
channel.send(new StringMessage("bar"), 0);
task.run();
assertEquals("foobar", writer.toString());
}
@Test
public void testMaxMessagesPerTaskExceedsMessageCountWithAppendedNewLines() {
StringWriter writer = new StringWriter();
CharacterStreamTarget target = new CharacterStreamTarget(writer);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setMaxMessagesPerTask(10);
dispatcherPolicy.setReceiveTimeout(0);
QueueChannel channel = new QueueChannel(5, dispatcherPolicy);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().subscribe(target);
target.setShouldAppendNewLine(true);
channel.send(new StringMessage("foo"), 0);
channel.send(new StringMessage("bar"), 0);
task.run();
String newLine = System.getProperty("line.separator");
assertEquals("foo" + newLine + "bar" + newLine, writer.toString());
}
@Test
public void testSingleNonStringObject() {
MessageChannel channel = new QueueChannel();
StringWriter writer = new StringWriter();
CharacterStreamTarget target = new CharacterStreamTarget(writer);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().subscribe(target);
TestObject testObject = new TestObject("foo");
channel.send(new GenericMessage<TestObject>(testObject));
task.run();
assertEquals("foo", writer.toString());
}
@Test
public void testTwoNonStringObjectWithOutNewLines() {
StringWriter writer = new StringWriter();
CharacterStreamTarget target = new CharacterStreamTarget(writer);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setReceiveTimeout(0);
dispatcherPolicy.setMaxMessagesPerTask(2);
QueueChannel channel = new QueueChannel(5, dispatcherPolicy);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().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);
task.run();
assertEquals("foobar", writer.toString());
}
@Test
public void testTwoNonStringObjectWithNewLines() {
StringWriter writer = new StringWriter();
CharacterStreamTarget target = new CharacterStreamTarget(writer);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setReceiveTimeout(0);
dispatcherPolicy.setMaxMessagesPerTask(2);
QueueChannel channel = new QueueChannel(5, dispatcherPolicy);
target.setShouldAppendNewLine(true);
PollingDispatcherTask task = new PollingDispatcherTask(new DefaultPollingDispatcher(channel), null);
task.getDispatcher().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);
task.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;
}
}
}

View File

@@ -0,0 +1,104 @@
/*
* 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());
}
}

View File

@@ -0,0 +1,155 @@
/*
* 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.send(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.send(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.send(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.send(new StringMessage("foo"));
assertEquals("foo\n", out.toString());
}
}

View File

@@ -0,0 +1,14 @@
<?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>

View File

@@ -0,0 +1,18 @@
<?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-target id="targetWithDefaultCharset"/>
<console-target id="targetWithProvidedCharset" charset="UTF-8"/>
<console-target id="stderrTarget" error="true"/>
<console-target id="newlineTarget" append-newline="true"/>
</beans:beans>

View File

@@ -0,0 +1,12 @@
<?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>

View File

@@ -0,0 +1,12 @@
<?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-target id="targetWithInvalidCharset" charset="invalid-charset-name"/>
</beans:beans>