http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AbstractComponentTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AbstractComponentTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AbstractComponentTest.java deleted file mode 100644 index f0dd7b0..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AbstractComponentTest.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import org.apache.camel.impl.JndiRegistry; -import org.apache.camel.test.junit4.CamelTestSupport; -import org.apache.servicemix.executors.ExecutorFactory; -import org.apache.servicemix.executors.impl.ExecutorConfig; -import org.apache.servicemix.executors.impl.ExecutorFactoryImpl; -import org.apache.servicemix.nmr.api.Channel; -import org.apache.servicemix.nmr.api.Endpoint; -import org.apache.servicemix.nmr.api.Exchange; -import org.apache.servicemix.nmr.api.event.ExchangeListener; -import org.apache.servicemix.nmr.api.service.ServiceHelper; -import org.apache.servicemix.nmr.core.InternalEndpointWrapper; -import org.apache.servicemix.nmr.core.ServiceMix; - -import java.util.LinkedList; -import java.util.List; - -/** - * Abstract base class for building NMR component unit tests - * - the NMR component is available with URI prefix nmr: - * - a client channel to the NMR can be obtained with the {@link #getChannel()} method - */ -public abstract class AbstractComponentTest extends CamelTestSupport implements ExchangeListener { - - private ServiceMix nmr; - private ServiceMixComponent component; - private Channel channel; - - @Override - public void setUp() throws Exception { - nmr = new ServiceMix(); - nmr.setExecutorFactory(createExecutorFactory()); - nmr.init(); - - nmr.getListenerRegistry().register(this, ServiceHelper.createMap()); - - component = new ServiceMixComponent(); - component.setNmr(nmr); - - super.setUp(); - } - - @Override - public void tearDown() throws Exception { - for (ServiceMixProducer producer : findEndpoints(ServiceMixProducer.class)) { - if (producer.getContinuations().size() > 0) { - // let's wait for a moment to give the last exchanges the time to get Done - Thread.sleep(500); - } - assertEquals("There should be no more pending Camel exchanges in the producer endpoints", - 0, producer.getContinuations().size()); - } - - nmr.shutdown(); - super.tearDown(); - } - - private <E extends Endpoint> List<E> findEndpoints(Class<E> type) { - List<E> result = new LinkedList<E>(); - - for (Endpoint endpoint : nmr.getEndpointRegistry().getServices()) { - if (endpoint instanceof InternalEndpointWrapper) { - InternalEndpointWrapper wrapper = (InternalEndpointWrapper) endpoint; - if (type.isAssignableFrom(wrapper.getEndpoint().getClass())) { - result.add(type.cast(wrapper.getEndpoint())); - } - } - } - return result; - } - - /* - * Create the ExecutorFactory for the unit test - * based on the default configuration used in ServiceMix 4 - */ - protected ExecutorFactory createExecutorFactory() { - ExecutorFactoryImpl factory = new ExecutorFactoryImpl(); - - ExecutorConfig config = factory.getDefaultConfig(); - config.setCorePoolSize(1); - config.setMaximumPoolSize(16); - config.setQueueSize(0); - config.setBypassIfSynchronous(true); - - return factory; - }; - - @Override - protected JndiRegistry createRegistry() throws Exception { - JndiRegistry registry = super.createRegistry(); - registry.bind("nmr", component); - return registry; - } - - /** - * Get a client channel to access the NMR used for testing - * - * @return the client channel - */ - protected Channel getChannel() { - if (channel == null) { - channel = nmr.createChannel(); - } - - return channel; - } - - public void exchangeSent(Exchange exchange) { - // graciously do nothing - } - - public void exchangeDelivered(Exchange exchange) { - // graciously do nothing - } - - public void exchangeFailed(Exchange exchange) { - // graciously do nothing - } -}
http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AttachmentTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AttachmentTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AttachmentTest.java deleted file mode 100644 index e7944bb..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/AttachmentTest.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import java.io.InputStream; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Proxy; -import java.lang.reflect.UndeclaredThrowableException; - -import javax.activation.DataHandler; -import javax.mail.util.ByteArrayDataSource; -import javax.xml.namespace.QName; -import javax.xml.ws.BindingProvider; -import javax.xml.ws.Holder; -import javax.xml.ws.soap.SOAPBinding; - -import org.apache.camel.CamelContext; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.spring.SpringCamelContext; -import org.apache.camel.test.CamelTestSupport; -import org.apache.cxf.Bus; -import org.apache.cxf.BusFactory; -import org.apache.cxf.endpoint.Client; -import org.apache.cxf.endpoint.ClientImpl; -import org.apache.cxf.jaxws.JaxWsClientProxy; -import org.apache.cxf.jaxws.binding.soap.SOAPBindingImpl; -import org.apache.cxf.jaxws.support.JaxWsEndpointImpl; -import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean; -import org.apache.cxf.mime.TestMtom; -import org.apache.cxf.service.Service; -import org.apache.cxf.service.factory.ReflectionServiceFactoryBean; -import org.apache.cxf.service.model.EndpointInfo; -import org.springframework.context.support.AbstractXmlApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class AttachmentTest extends CamelTestSupport { - - protected static final String SERVICE_ADDRESS = "local://smx/attachment"; - protected static final String ROUTER_ADDRESS = "http://localhost:9036/mime-test"; - - protected static final String SERVICE_CLASS = "serviceClass=org.apache.cxf.mime.TestMtom"; - - - - - public static final QName MTOM_PORT = new QName( - "http://cxf.apache.org/mime", "TestMtomPort"); - - public static final QName MTOM_SERVICE = new QName( - "http://cxf.apache.org/mime", "TestMtomService"); - - protected AbstractXmlApplicationContext applicationContext; - - - - - @Override - protected void setUp() throws Exception { - applicationContext = createApplicationContext(); - super.setUp(); - assertNotNull("Should have created a valid spring context", applicationContext); - - startService(); - } - - @Override - protected void tearDown() throws Exception { - if (applicationContext != null) { - applicationContext.destroy(); - } - super.tearDown(); - } - - protected void startService() { - //start a service - Object implementor = new MtomImpl(); - - javax.xml.ws.Endpoint.publish(SERVICE_ADDRESS, implementor); - } - - - - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - public void configure() { - from("cxf:bean:routerEndpoint").to("smx:testEndpoint"); - from("smx:testEndpoint").to("cxf:bean:serviceEndpoint"); - } - }; - } - - @Override - protected CamelContext createCamelContext() throws Exception { - return SpringCamelContext.springCamelContext(applicationContext); - } - - - protected ClassPathXmlApplicationContext createApplicationContext() { - return new ClassPathXmlApplicationContext("org/apache/servicemix/camel/spring/mtom.xml"); - } - - - public void testAttachment() throws Exception { - TestMtom mtomPort = createPort(MTOM_SERVICE, MTOM_PORT, TestMtom.class, - true); - try { - - Holder<DataHandler> param = new Holder<DataHandler>(); - - param.value = new DataHandler(new ByteArrayDataSource("foobar".getBytes(), - "application/octet-stream")); - - Holder<String> name = new Holder<String>("call detail"); - mtomPort.testXop(name, param); - assertEquals("call detailfoobar", - name.value); - assertNotNull(param.value); - InputStream bis = param.value.getDataSource().getInputStream(); - byte b[] = new byte[10]; - bis.read(b, 0, 10); - String attachContent = new String(b); - assertEquals(attachContent, "testfoobar"); - } catch (UndeclaredThrowableException ex) { - throw (Exception) ex.getCause(); - } - - } - - private <T> T createPort(QName serviceName, QName portName, - Class<T> serviceEndpointInterface, boolean enableMTOM) - throws Exception { - Bus bus = BusFactory.getDefaultBus(); - ReflectionServiceFactoryBean serviceFactory = new JaxWsServiceFactoryBean(); - serviceFactory.setBus(bus); - serviceFactory.setServiceName(serviceName); - serviceFactory.setServiceClass(serviceEndpointInterface); - serviceFactory.setWsdlURL(getClass().getResource("/wsdl/mtom_xop.wsdl")); - Service service = serviceFactory.create(); - EndpointInfo ei = service.getEndpointInfo(portName); - JaxWsEndpointImpl jaxwsEndpoint = new JaxWsEndpointImpl(bus, service, - ei); - SOAPBinding jaxWsSoapBinding = new SOAPBindingImpl(ei.getBinding(), jaxwsEndpoint); - jaxWsSoapBinding.setMTOMEnabled(enableMTOM); - - Client client = new ClientImpl(bus, jaxwsEndpoint); - InvocationHandler ih = new JaxWsClientProxy(client, jaxwsEndpoint - .getJaxwsBinding()); - Object obj = Proxy.newProxyInstance(serviceEndpointInterface - .getClassLoader(), new Class[] {serviceEndpointInterface, - BindingProvider.class}, ih); - return serviceEndpointInterface.cast(obj); - } -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CamelAsyncRouteTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CamelAsyncRouteTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CamelAsyncRouteTest.java deleted file mode 100644 index c925038..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CamelAsyncRouteTest.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.servicemix.nmr.api.Status; - -import org.junit.Test; - -/** - * Test case for making sure that the component behaves properly if the Camel route is using - * asynchronous elements (e.g. threads or seda queues) - */ -public class CamelAsyncRouteTest extends AbstractComponentTest { - - private static final String HANDLED_BY_THREAD = "HandledByThread"; - - private static final int COUNT = 1000; - private static final long DELAY = 60000; - - /* Latch to count NMR Done Exchanges */ - private CountDownLatch done; - - @Override - public void setUp() throws Exception { - super.setUp(); - - done = new CountDownLatch(COUNT); - } - - @Test - public void testCamelThreads() throws InterruptedException { - expectDefaultMessageCount("mock:sent"); - expectDefaultMessageCount("mock:threads").whenAnyExchangeReceived(new AssertHandledByCamelThreadProcessor()); - - for (int i = 0 ; i < COUNT ; i++) { - template.asyncSendBody("direct:threads", "Simple message body " + i); - } - - assertMockEndpointsSatisfied(); - - assertTrue("All NMR exchanges should have been marked DONE", - done.await(DELAY, TimeUnit.MILLISECONDS)); - } - - @Test - public void testCamelSeda() throws InterruptedException { - expectDefaultMessageCount("mock:sent"); - expectDefaultMessageCount("mock:seda"); - - for (int i = 0 ; i < COUNT ; i++) { - template.asyncSendBody("seda:seda", "Simple message body " + i); - } - - assertMockEndpointsSatisfied(); - - assertTrue("All NMR exchanges should have been marked DONE", - done.await(DELAY, TimeUnit.MILLISECONDS)); - } - - /* - * Configure the mock endpoint to expect {@value #COUNT} messages to arrive in {@value #DELAY}ms - */ - private MockEndpoint expectDefaultMessageCount(String endpoint) { - final MockEndpoint mock = getMockEndpoint(endpoint); - mock.setResultWaitTime(DELAY); - mock.expectedMessageCount(COUNT); - return mock; - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - from("direct:threads").to("mock:sent").to("nmr:threads"); - from("nmr:threads") - .threads(5) - .process(new Processor() { - public void process(Exchange exchange) throws Exception { - exchange.setProperty(HANDLED_BY_THREAD, Thread.currentThread()); - } - }) - .to("mock:threads"); - - from("seda:seda?concurrentConsumers=10").to("mock:sent").to("nmr:seda"); - from("nmr:seda").to("seda:seda-internal?waitForTaskToComplete=Never"); - from("seda:seda-internal").to("mock:seda"); - - } - }; - } - - @Override - public void exchangeDelivered(org.apache.servicemix.nmr.api.Exchange exchange) { - if (exchange.getStatus().equals(Status.Done)) { - done.countDown(); - } - } - - /* - * Processor to ensure that the exchange has been handled by a Camel thread instead of an NMR thread - */ - private static final class AssertHandledByCamelThreadProcessor implements Processor { - - public void process(Exchange exchange) throws Exception { - Thread thread = exchange.getProperty(HANDLED_BY_THREAD, Thread.class); - assertTrue("processor should have been called from the Camel 'threads' thread pool instead of " + thread.getName(), - thread.getName().contains("Camel") && thread.getName().contains("Thread")); - } - - } -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CamelTryCatchTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CamelTryCatchTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CamelTryCatchTest.java deleted file mode 100644 index 4a77285..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CamelTryCatchTest.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; - -/** - * A very basic NMR test, just testing if the Exchange can flow through the NMR - * from one Camel route to the next one - */ -public class CamelTryCatchTest extends AbstractComponentTest { - - private static final String REQUEST_MESSAGE = "Simple message body"; - - @Test - public void testInOnlyTryCatch() throws InterruptedException { - MockEndpoint mock = getMockEndpoint("mock:caught"); - mock.expectedMessageCount(1); - - template.sendBody("direct:inonly", REQUEST_MESSAGE); - - assertMockEndpointsSatisfied(); - } - - @Test - public void testInOutTryCatch() throws InterruptedException { - MockEndpoint mock = getMockEndpoint("mock:caught"); - mock.expectedMessageCount(1); - - template.requestBody("direct:inout", REQUEST_MESSAGE); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - from("direct:inonly") - .doTry() - .to("nmr:throwsException") - .doCatch(CustomBusinessException.class) - .to("mock:caught"); - - from("direct:inout") - .doTry() - .to("nmr:throwsException") - .doCatch(CustomBusinessException.class) - .to("mock:caught"); - - from("nmr:throwsException") - .errorHandler(noErrorHandler()) - .throwException(new CustomBusinessException()); - } - }; - } - - /* - * Custom business exception for testing purposes - */ - private static final class CustomBusinessException extends Exception { - - } -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CxfMessageTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CxfMessageTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CxfMessageTest.java deleted file mode 100644 index 2528032..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/CxfMessageTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import java.net.URL; - -import javax.xml.namespace.QName; -import javax.xml.ws.Endpoint; -import javax.xml.ws.Holder; - -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.test.CamelSpringTestSupport; -import org.apache.camel.CamelContext; -import org.apache.cxf.endpoint.ServerImpl; -import org.apache.cxf.frontend.ClientProxy; -import org.apache.cxf.interceptor.LoggingInInterceptor; -import org.apache.cxf.interceptor.LoggingOutInterceptor; -import org.apache.servicemix.samples.wsdl_first.Person; -import org.apache.servicemix.samples.wsdl_first.PersonService; -import org.springframework.context.support.ClassPathXmlApplicationContext; - - -public class CxfMessageTest extends CamelSpringTestSupport { - - - private ServerImpl server; - - - @Override - protected void setUp() throws Exception { - super.setUp(); - - startService(); - } - - protected ClassPathXmlApplicationContext createApplicationContext() { - return new ClassPathXmlApplicationContext("org/apache/servicemix/camel/spring/CxfMessageBeans.xml"); - } - - protected void assertValidContext(CamelContext context) { - assertNotNull("No context found!", context); - } - - protected void startService() { - Object implementor = new PersonImpl(); - String address = "http://localhost:19000/PersonService/"; - Endpoint.publish(address, implementor); - } - - @Override - protected void tearDown() throws Exception { - if (server != null) { - server.stop(); - } - super.tearDown(); - } - - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - public void configure() { - //from(routerEndpointURI).to("smx:testEndpoint");// like what do in binding component - //from("smx:testEndpoint").to(serviceEndpointURI);// like what do in se - } - }; - } - - public void testInvokingServiceFromCXFClient() throws Exception { - - URL wsdlURL = getClass().getClassLoader().getResource("person.wsdl"); - - - System.out.println(wsdlURL); - PersonService ss = new PersonService(wsdlURL, new QName("http://servicemix.apache.org/samples/wsdl-first", - "PersonService")); - Person client = ss.getSoap(); - ClientProxy.getClient(client).getOutInterceptors().add(new LoggingOutInterceptor()); - ClientProxy.getClient(client).getInInterceptors().add(new LoggingInInterceptor()); - Holder<String> personId = new Holder<String>(); - personId.value = "world"; - Holder<String> ssn = new Holder<String>(); - Holder<String> name = new Holder<String>(); - client.getPerson(personId, ssn, name); - assertEquals("we should get the right answer from router", "Bonjour", name.value); - } - - - -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ExceptionHandleTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ExceptionHandleTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ExceptionHandleTest.java deleted file mode 100644 index 3a037f9..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ExceptionHandleTest.java +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import javax.naming.Context; -import javax.xml.namespace.QName; -import javax.xml.ws.BindingProvider; -import javax.xml.ws.soap.SOAPBinding; - -import org.apache.camel.CamelContext; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.cxf.transport.CamelTransportFactory; -import org.apache.camel.impl.DefaultCamelContext; -import org.apache.camel.test.CamelTestSupport; -import org.apache.cxf.Bus; -import org.apache.cxf.BusFactory; -import org.apache.cxf.frontend.ClientProxy; -import org.apache.cxf.interceptor.LoggingInInterceptor; -import org.apache.cxf.interceptor.LoggingOutInterceptor; -import org.apache.cxf.message.Message; -import org.apache.cxf.transport.ConduitInitiatorManager; -import org.apache.hello_world_soap_http.BadRecordLitFault; -import org.apache.hello_world_soap_http.Greeter; -import org.apache.hello_world_soap_http.GreeterImpl; -import org.apache.hello_world_soap_http.NoSuchCodeLitFault; -import org.apache.hello_world_soap_http.SOAPService; -import org.apache.servicemix.nmr.api.NMR; -import org.apache.servicemix.nmr.core.ServiceMix; - - -public class ExceptionHandleTest extends CamelTestSupport { - protected static final String ROUTER_ADDRESS = "camel://jetty:http://localhost:19000/SoapContext/SoapPort"; - protected static final String SERVICE_ADDRESS = "local://smx/hello_world"; - protected static final String SERVICE_CLASS = "serviceClass=org.apache.hello_world_soap_http.Greeter"; - private static final String WSDL_LOCATION = "wsdlURL=/wsdl/hello_world.wsdl"; - private static final String SERVICE_NAME = "serviceName={http://apache.org/hello_world_soap_http}SOAPService"; - - private String routerEndpointURI = "cxf://" + ROUTER_ADDRESS + "?" + SERVICE_CLASS - + "&" + WSDL_LOCATION + "&" + SERVICE_NAME + "&dataFormat=POJO&bus=#Bus"; - private String serviceEndpointURI = "cxf://" + SERVICE_ADDRESS + "?" + SERVICE_CLASS - + "&" + WSDL_LOCATION + "&" + SERVICE_NAME + "&dataFormat=POJO&bus=#Bus"; - - private CamelContext camelContext; - private ServiceMixComponent smxComponent; - private NMR nmr; - private javax.xml.ws.Endpoint endpoint; - - @Override - protected void setUp() throws Exception { - super.setUp(); - Object implementor = new GreeterImpl(); - endpoint = javax.xml.ws.Endpoint.publish(SERVICE_ADDRESS, implementor); - } - - @Override - protected void tearDown() throws Exception { - if (camelContext != null) { - camelContext.stop(); - } - if (endpoint != null) { - endpoint.stop(); - } - super.tearDown(); - // Not sure why we need a timeout here - // but if we don't, the jetty server is not fully - // stopped, so the next test fails. - Thread.sleep(5000); - } - - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - public void configure() { - errorHandler(noErrorHandler()); - from(routerEndpointURI).to("smx:testEndpoint");// like what do in binding component - from("smx:testEndpoint").to(serviceEndpointURI);// like what do in se - } - }; - } - - protected CamelContext createCamelContext() throws Exception { - camelContext = new DefaultCamelContext(createJndiContext()); - Bus bus = BusFactory.getDefaultBus(); - CamelTransportFactory camelTransportFactory = (CamelTransportFactory) bus.getExtension(ConduitInitiatorManager.class) - .getConduitInitiator(CamelTransportFactory.TRANSPORT_ID); - camelTransportFactory.setCamelContext(camelContext); - List<String> ids = new ArrayList<String>(); - ids.add(CamelTransportFactory.TRANSPORT_ID); - camelTransportFactory.setTransportIds(ids); - smxComponent = new ServiceMixComponent(); - nmr = new ServiceMix(); - ((ServiceMix)nmr).init(); - smxComponent.setNmr(nmr); - camelContext.addComponent("smx", smxComponent); - return camelContext; - } - - @Override - protected Context createJndiContext() throws Exception { - Context ctx = super.createJndiContext(); - ctx.bind("Bus", BusFactory.getDefaultBus()); - return ctx; //To change body of overridden methods use File | Settings | File Templates. - } - - public void testException() throws Exception { - URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); - assertNotNull(wsdl); - SOAPService service1 = new SOAPService(wsdl, new QName( - "http://apache.org/hello_world_soap_http", "SOAPService")); - QName endpoint = new QName("http://apache.org/hello_world_soap_http", "SoapPort"); - service1.addPort(endpoint, - SOAPBinding.SOAP12HTTP_BINDING, "http://localhost:19000/SoapContext/SoapPort"); - Greeter greeter = service1.getPort(endpoint, Greeter.class); - ClientProxy.getClient(greeter).getInInterceptors().add(new LoggingInInterceptor()); - ClientProxy.getClient(greeter).getOutInterceptors().add(new LoggingOutInterceptor()); - String ret = greeter.sayHi(); - assertEquals(ret, "Bonjour"); - String noSuchCodeFault = "NoSuchCodeLitFault"; - String badRecordFault = "BadRecordLitFault"; - try { - greeter.testDocLitFault(noSuchCodeFault); - fail("Should have thrown NoSuchCodeLitFault exception"); - } catch (NoSuchCodeLitFault nslf) { - assertNotNull(nslf.getFaultInfo()); - assertNotNull(nslf.getFaultInfo().getCode()); - } - - try { - greeter.testDocLitFault(badRecordFault); - fail("Should have thrown BadRecordLitFault exception"); - } catch (BadRecordLitFault brlf) { - BindingProvider bp = (BindingProvider)greeter; - Map<String, Object> responseContext = bp.getResponseContext(); - Integer responseCode = (Integer) responseContext.get(Message.RESPONSE_CODE); - assertEquals(500, responseCode.intValue()); - assertNotNull(brlf.getFaultInfo()); - assertEquals("BadRecordLitFault", brlf.getFaultInfo()); - } - } - - public void testOneway() throws Exception { - URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl"); - assertNotNull(wsdl); - SOAPService service1 = new SOAPService(wsdl, new QName( - "http://apache.org/hello_world_soap_http", "SOAPService")); - QName endpoint = new QName("http://apache.org/hello_world_soap_http", "SoapPort"); - service1.addPort(endpoint, - SOAPBinding.SOAP12HTTP_BINDING, "http://localhost:19000/SoapContext/SoapPort"); - Greeter greeter = service1.getPort(endpoint, Greeter.class); - ClientProxy.getClient(greeter).getInInterceptors().add(new LoggingInInterceptor()); - ClientProxy.getClient(greeter).getOutInterceptors().add(new LoggingOutInterceptor()); - greeter.greetMeOneWay("test oneway"); - // Need to sleep a while as Camel is using Async Engine, - // we need to make sure the camel context is not shutdown rightly. - Thread.sleep(1000); - } - - public void testGetTransportFactoryFromBus() throws Exception { - Bus bus = BusFactory.getDefaultBus(); - assertNotNull(bus.getExtension(ConduitInitiatorManager.class) - .getConduitInitiator(CamelTransportFactory.TRANSPORT_ID)); - } -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/HelloService.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/HelloService.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/HelloService.java deleted file mode 100644 index ebddb7f..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/HelloService.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -public interface HelloService { - String sayHello(); - - void ping(); - - String echo(String text); -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/HelloServiceImpl.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/HelloServiceImpl.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/HelloServiceImpl.java deleted file mode 100644 index 77bd20e..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/HelloServiceImpl.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -public class HelloServiceImpl implements HelloService { - public String echo(String text) { - System.out.println("call for echo with " + text); - return text + " echo"; - } - - public void ping() { - - } - - public String sayHello() { - return "hello"; - } -} - http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/MtomImpl.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/MtomImpl.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/MtomImpl.java deleted file mode 100644 index b231cbc..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/MtomImpl.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import java.io.IOException; -import java.io.InputStream; - -import javax.activation.DataHandler; -import javax.jws.WebService; -import javax.mail.util.ByteArrayDataSource; -import javax.xml.ws.Holder; - -import org.apache.cxf.mime.types.XopStringType; - -@WebService(serviceName = "TestMtomService", - portName = "TestMtomPort", - targetNamespace = "http://cxf.apache.org/mime", - endpointInterface = "org.apache.cxf.mime.TestMtom", - wsdlLocation = "testutils/mtom_xop.wsdl") -public class MtomImpl { - public void testXop(Holder<String> name, Holder<DataHandler> attachinfo) { - - try { - InputStream bis = attachinfo.value.getDataSource().getInputStream(); - byte b[] = new byte[6]; - bis.read(b, 0, 6); - String attachContent = new String(b); - name.value = name.value + attachContent; - - ByteArrayDataSource source = - new ByteArrayDataSource(("test" + attachContent).getBytes(), "application/octet-stream"); - attachinfo.value = new DataHandler(source); - } catch (IOException e) { - e.printStackTrace(); - } - - } - public XopStringType testXopString(XopStringType data) { - return data; - } - -} - http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/MultipleProducersTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/MultipleProducersTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/MultipleProducersTest.java deleted file mode 100644 index fff06e4..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/MultipleProducersTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; - -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -/** - * Test case to ensure that the component can deal with multiple {@link org.apache.servicemix.camel.nmr.ServiceMixProducer} - * instances for the same endpoint name being used concurrently. - */ -public class MultipleProducersTest extends AbstractComponentTest { - - private static final int COUNT = 100; - - @Test - public void testConcurrentlyUsingTheSameProducerName() throws InterruptedException { - getMockEndpoint("mock:handler").expectedMessageCount(2 * COUNT); - - ExecutorService executor = Executors.newFixedThreadPool(10); - - for (int i = 0 ; i < 100 ; i++) { - executor.execute(new Runnable() { - public void run() { - assertEquals("Replying to Guillaume", - template.requestBody("direct:a", "Guillaume")); - } - }); - executor.execute(new Runnable() { - public void run() { - assertEquals("Replying to Chris", - template.requestBody("direct:a", "Chris")); - } - }); - } - - assertMockEndpointsSatisfied(); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - from("direct:a").to("nmr:handler"); - from("direct:b").to("nmr:handler"); - - from("nmr:handler").setBody(simple("Replying to ${body}")).to("mock:handler"); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/PersonImpl.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/PersonImpl.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/PersonImpl.java deleted file mode 100644 index 4676e9a..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/PersonImpl.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import javax.jws.WebService; -import javax.xml.ws.Holder; - -import org.apache.servicemix.samples.wsdl_first.Person; -import org.apache.servicemix.samples.wsdl_first.UnknownPersonFault; - -@WebService(serviceName = "PersonService", - targetNamespace = "http://servicemix.apache.org/samples/wsdl-first", - endpointInterface = "org.apache.servicemix.samples.wsdl_first.Person") -public class PersonImpl implements Person { - - public void getPerson(Holder<String> personId, Holder<String> ssn, - Holder<String> name) throws UnknownPersonFault { - System.out.println("the server is invoked "); - if (personId.value == null || personId.value.length() == 0) { - org.apache.servicemix.samples.wsdl_first.types.UnknownPersonFault - fault = new org.apache.servicemix.samples.wsdl_first.types.UnknownPersonFault(); - fault.setPersonId(personId.value); - throw new UnknownPersonFault(null, fault); - } - name.value = "Bonjour"; - ssn.value = "000-000-0000"; - } - -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/RunAsSubjectTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/RunAsSubjectTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/RunAsSubjectTest.java deleted file mode 100644 index 8d00a33..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/RunAsSubjectTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import java.security.AccessController; - -import javax.security.auth.Subject; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.servicemix.nmr.api.security.UserPrincipal; -import org.junit.Test; - -public class RunAsSubjectTest extends AbstractComponentTest { - - private static final String REQUEST_MESSAGE = "Simple message body"; - - @Test - public void testRunAsSubject() throws InterruptedException { - MockEndpoint mock = getMockEndpoint("mock:caught"); - mock.expectedMessageCount(1); - - template.sendBody("direct:inonly", REQUEST_MESSAGE); - - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - Subject subject = new Subject(); - subject.getPrincipals().add(new UserPrincipal("ffang")); - from("direct:inonly").setHeader(Exchange.AUTHENTICATION).constant(subject). - to("nmr:helloworld"); - - from("nmr:helloworld?runAsSubject=true").process(new SubjectProcessor()); - - } - }; - } - - class SubjectProcessor implements Processor { - - public void process(Exchange exchange) throws Exception { - Subject receivedSubject = - (Subject)exchange.getIn().getHeader(Exchange.AUTHENTICATION); - assertNotNull(receivedSubject); - assertEquals(receivedSubject.getPrincipals().size(), 1); - assertEquals(receivedSubject.getPrincipals().iterator().next().getName(), "ffang"); - Subject onBefalfsubject = Subject.getSubject(AccessController.getContext()); - assertNotNull(onBefalfsubject); - assertEquals(onBefalfsubject, receivedSubject); - - } - - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SecuritySubjectTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SecuritySubjectTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SecuritySubjectTest.java deleted file mode 100644 index 6e9c40e..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SecuritySubjectTest.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import org.apache.camel.Exchange; -import org.apache.camel.ExchangePattern; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; - -import javax.security.auth.Subject; -import java.security.Principal; - -/** - * Test case to ensure the Camel NMR component is capable of conveying security information provided by Camel - */ -public class SecuritySubjectTest extends AbstractComponentTest { - - private static final String REQUEST_MESSAGE = "Simple message body"; - private static final String RESPONSE_MESSAGE = "Simple message reply"; - - private static final Principal CLARK_KENT = new PrincipalImpl("Clark Kent"); - private static final Principal SUPERMAN = new PrincipalImpl("Superman"); - - /* - * Test case for conveying security subject information in an InOnly MEP - */ - @Test - public void testInOnlyWithSecuritySubject() throws Exception { - Subject subject = createSubject(CLARK_KENT); - - MockEndpoint mock = getMockEndpoint("mock:simple"); - mock.expectedBodiesReceived(REQUEST_MESSAGE); - - template.sendBodyAndHeader("direct:simple", REQUEST_MESSAGE, - Exchange.AUTHENTICATION, subject); - - assertMockEndpointsSatisfied(); - } - - /* - * Test case for conveying security subject information in an InOut MEP - */ - @Test - public void testInOutWithSecuritySubject() throws Exception { - final Subject subject = createSubject(CLARK_KENT); - - Exchange result = template.request("direct:simple", new Processor() { - public void process(Exchange exchange) throws Exception { - exchange.getIn().setBody(REQUEST_MESSAGE); - exchange.getIn().setHeader(Exchange.AUTHENTICATION, subject); - } - }); - - assertSecuritySubject(SUPERMAN, result.getOut().getHeader(Exchange.AUTHENTICATION, Subject.class)); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - from("direct:simple").to("nmr:simple"); - from("nmr:simple").process(assertSecuritySubject()).to("mock:simple"); - } - }; - } - - /* - * Build a simple {@link Processor} to ensure our exchange contains the correct security information - */ - private Processor assertSecuritySubject() { - return new Processor() { - public void process(Exchange exchange) throws Exception { - Subject subject = exchange.getIn().getHeader(Exchange.AUTHENTICATION, Subject.class); - assertSecuritySubject(CLARK_KENT, subject); - - if (ExchangePattern.InOut.equals(exchange.getPattern())) { - // wow, Clark Kent is Superman, who would have thought that? - exchange.getOut().copyFrom(exchange.getIn()); - exchange.getOut().setBody(RESPONSE_MESSAGE); - exchange.getOut().setHeader(Exchange.AUTHENTICATION, createSubject(SUPERMAN)); - } - } - }; - } - - @Override - public void exchangeSent(org.apache.servicemix.nmr.api.Exchange exchange) { - super.exchangeSent(exchange); - - // let's check the subject inside the NMR as well - if (exchange.getIn(false) != null) { - assertSecuritySubject(CLARK_KENT, exchange.getIn().getSecuritySubject()); - } - if (exchange.getOut(false) != null) { - assertSecuritySubject(SUPERMAN, exchange.getOut().getSecuritySubject()); - } - } - - /* - * Ensure that the Subject is valid and matches the principal - */ - private void assertSecuritySubject(Principal expected, Subject subject) { - assertNotNull(subject); - assertTrue("Subject should have contained " + expected, subject.getPrincipals().contains(expected)); - } - - /* - * Create a new Subject, containing the provided principal information - */ - private Subject createSubject(Principal principal) { - final Subject subject = new Subject(); - subject.getPrincipals().add(principal); - return subject; - } - - /* - * Simple {@link Principal} implementation used for testing - */ - private static final class PrincipalImpl implements Principal { - - private final String name; - - public PrincipalImpl(String name) { - super(); - this.name = name; - } - - public String getName() { - return name; - } - - public String toString() { - return String.format("Principal [%s]", name); - } - } -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixBindingTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixBindingTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixBindingTest.java deleted file mode 100644 index 9a8258a..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixBindingTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import org.apache.camel.impl.DefaultCamelContext; -import org.apache.servicemix.nmr.api.Exchange; -import org.apache.servicemix.nmr.api.Pattern; -import org.junit.Test; - -/** - * Test cases for {@link org.apache.servicemix.camel.nmr.ServiceMixBinding} - */ -public class ServiceMixBindingTest extends AbstractComponentTest { - - private static final String KEY = "test.key"; - private static final String VALUE = "test.value"; - private static final String MESSAGE = "Message content"; - - private ServiceMixBinding binding = new ServiceMixBinding(); - - @Test - public void testToCamelAndBackToNmr() { - Exchange nmr = getChannel().createExchange(Pattern.InOnly); - nmr.setProperty(KEY, VALUE); - nmr.getIn().setBody(MESSAGE); - nmr.getIn().setHeader(KEY, VALUE); - - org.apache.camel.Exchange camel = - binding.populateCamelExchangeFromNmrExchange(new DefaultCamelContext(), nmr); - - assertEquals(VALUE, camel.getProperty(KEY)); - assertEquals(VALUE, camel.getIn().getHeader(KEY)); - assertEquals(MESSAGE, camel.getIn().getBody()); - assertEquals("NMR Exchange should be available on the Camel Exchange", - nmr, camel.getProperty(ServiceMixBinding.NMR_EXCHANGE)); - - assertSame(nmr, binding.extractNmrExchange(camel)); - assertNull("NMR Exchange should have been removed from the Camel Exchange", - camel.getProperty(ServiceMixBinding.NMR_EXCHANGE)); - - - } - -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixComponentTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixComponentTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixComponentTest.java deleted file mode 100644 index 91ed657..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ServiceMixComponentTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import org.apache.camel.CamelContext; -import org.apache.camel.Producer; -import org.apache.camel.impl.DefaultCamelContext; -import org.apache.camel.impl.SynchronousDelegateProducer; -import org.junit.Test; - -/** - * Test cases for {@link ServiceMixComponent} and the correct handling of the nmr: URIs - */ -public class ServiceMixComponentTest extends AbstractComponentTest { - - private final CamelContext context = new DefaultCamelContext(); - - @Test - public void testSimpleUri() { - ServiceMixEndpoint endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test"); - assertNotNull(endpoint); - } - - @Test - public void testSyncOperation() throws Exception { - ServiceMixEndpoint endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test?synchronous=true"); - assertNotNull(endpoint); - Producer producer = endpoint.createProducer(); - assertTrue("It should be the instance of ", producer instanceof SynchronousDelegateProducer); - } - - @Test - public void testUriRunAsSubject() { - ServiceMixEndpoint endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test"); - assertNotNull(endpoint); - assertFalse(endpoint.isRunAsSubject()); - - endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test?runAsSubject=false"); - assertNotNull(endpoint); - assertFalse(endpoint.isRunAsSubject()); - - endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test?runAsSubject=true"); - assertNotNull(endpoint); - assertTrue(endpoint.isRunAsSubject()); - } - - @Test - public void testUriTimeOut() { - ServiceMixEndpoint endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test"); - assertNotNull(endpoint); - assertEquals(new Long(0), endpoint.getTimeOut()); - endpoint = (ServiceMixEndpoint) context.getEndpoint("nmr:Test?timeout=3000"); - assertNotNull(endpoint); - assertEquals(new Long(3000), endpoint.getTimeOut()); - } - -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ShouldRunSynchronouslyTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ShouldRunSynchronouslyTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ShouldRunSynchronouslyTest.java deleted file mode 100644 index f32fc71..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/ShouldRunSynchronouslyTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; - -/** - * Test case for the ?synchronous=true setting on a camel consumer endpoint - */ -public class ShouldRunSynchronouslyTest extends AbstractComponentTest { - - private static final String HANDLED_BY_THREAD = "HandledByThread"; - - @Test - public void testProcessingOnSameThread() throws InterruptedException { - MockEndpoint mock = getMockEndpoint("mock:simple"); - mock.expectedBodiesReceived("Simple message body"); - - template.sendBody("direct:simple", "Simple message body"); - - assertMockEndpointsSatisfied(); - - Thread thread = mock.getExchanges().get(0).getProperty(HANDLED_BY_THREAD, Thread.class); - assertNotNull(thread); - assertEquals("No thread context switching should have occurred", - Thread.currentThread(), thread); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - from("direct:simple").to("nmr:simple"); - from("nmr:simple?synchronous=true").process(new Processor() { - - public void process(Exchange exchange) throws Exception { - exchange.setProperty(HANDLED_BY_THREAD, Thread.currentThread()); - } - - }).to("mock:simple"); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SimpleNmrTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SimpleNmrTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SimpleNmrTest.java deleted file mode 100644 index d36adb6..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SimpleNmrTest.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; - -/** - * A very basic NMR test, just testing if the Exchange can flow through the NMR - * from one Camel route to the next one - */ -public class SimpleNmrTest extends AbstractComponentTest { - - private static final String REQUEST_MESSAGE = "Simple message body"; - private static final String RESPONSE_MESSAGE = "Simple message reply"; - - @Test - public void testSimpleInOnly() throws InterruptedException { - MockEndpoint mock = getMockEndpoint("mock:simple"); - mock.expectedBodiesReceived(REQUEST_MESSAGE); - - template.sendBody("direct:simple", REQUEST_MESSAGE); - - assertMockEndpointsSatisfied(); - } - - @Test - public void testSimpleInOnlyWithMultipleHops() throws Exception { - MockEndpoint mock = getMockEndpoint("mock:hops"); - mock.expectedBodiesReceived(REQUEST_MESSAGE); - - template.sendBody("direct:hops", REQUEST_MESSAGE); - - assertMockEndpointsSatisfied(); - } - - @Test - public void testSimpleInOut() throws InterruptedException { - MockEndpoint mock = getMockEndpoint("mock:simple"); - mock.expectedBodiesReceived(REQUEST_MESSAGE); - - final String response = template.requestBody("direct:simple", REQUEST_MESSAGE, String.class); - - assertMockEndpointsSatisfied(); - assertEquals("Receiving back the reply set by the second route", - RESPONSE_MESSAGE, response); - } - - @Test - public void testSimpleInOutWithMultipleHops() throws InterruptedException { - MockEndpoint mock = getMockEndpoint("mock:hops"); - mock.expectedBodiesReceived(REQUEST_MESSAGE); - - final String response = template.requestBody("direct:hops", REQUEST_MESSAGE, String.class); - - assertMockEndpointsSatisfied(); - assertEquals("Receiving back the reply set by the second route", - RESPONSE_MESSAGE, response); - } - - @Test - public void testSimpleInvalidEndpoint() throws InterruptedException { - Exchange exchange = template.send("direct:error", new Processor() { - public void process(Exchange exchange) throws Exception { - exchange.getIn().setBody(REQUEST_MESSAGE); - } - }); - - assertTrue("Sending to an invalid NMR endpoint should have failed", exchange.isFailed()); - } - - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - from("direct:simple").to("nmr:simple"); - from("nmr:simple").to("mock:simple").setBody(constant(RESPONSE_MESSAGE)); - - from("direct:hops").to("nmr:hop1"); - from("nmr:hop1").to("nmr:hop2"); - from("nmr:hop2").to("mock:hops").setBody(constant(RESPONSE_MESSAGE)); - - from("direct:error").to("nmr:invalid-endpoint-name"); - } - }; - } -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SmxToCxfSpringTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SmxToCxfSpringTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SmxToCxfSpringTest.java deleted file mode 100644 index cf729f3..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SmxToCxfSpringTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import org.apache.camel.CamelContext; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.spring.SpringCamelContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class SmxToCxfSpringTest extends SmxToCxfTest { - - @Override - protected void setUp() throws Exception { - super.setUp(); - assertNotNull("Should have created a valid spring context", applicationContext); - - - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } - - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - public void configure() { - from("cxf:bean:routerEndpoint").to("smx:bean:testEndpoint"); - from("smx:bean:testEndpoint").to("cxf:bean:serviceEndpoint"); - } - }; - } - - @Override - protected CamelContext createCamelContext() throws Exception { - return SpringCamelContext.springCamelContext(applicationContext); - } - - - protected ClassPathXmlApplicationContext createApplicationContext() { - return new ClassPathXmlApplicationContext("org/apache/servicemix/camel/spring/EndpointBeans.xml"); - } -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SmxToCxfTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SmxToCxfTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SmxToCxfTest.java deleted file mode 100644 index e2684e2..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/SmxToCxfTest.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.test.CamelSpringTestSupport; -import org.apache.camel.CamelContext; -import org.apache.cxf.Bus; -import org.apache.cxf.BusFactory; -import org.apache.cxf.bus.CXFBusFactory; -import org.apache.cxf.endpoint.ServerImpl; -import org.apache.cxf.frontend.ClientFactoryBean; -import org.apache.cxf.frontend.ClientProxyFactoryBean; -import org.apache.cxf.frontend.ServerFactoryBean; -import org.springframework.beans.BeansException; -import org.springframework.context.support.ClassPathXmlApplicationContext; - - -public class SmxToCxfTest extends CamelSpringTestSupport { - - private static final String BUS_BEAN_NAME = "Bus"; - protected static final String ROUTER_ADDRESS = "http://localhost:19000/router"; - protected static final String SERVICE_ADDRESS = "local://smx/helloworld"; - protected static final String SERVICE_CLASS = "serviceClass=org.apache.servicemix.camel.nmr.HelloService"; - - private String routerEndpointURI = - String.format("cxf://%s?%s&dataFormat=POJO&setDefaultBus=true&bus=#%s", ROUTER_ADDRESS, SERVICE_CLASS, BUS_BEAN_NAME); - - private String serviceEndpointURI = - String.format("cxf://%s?%s&dataFormat=POJO&setDefaultBus=true&bus=#%s", SERVICE_ADDRESS, SERVICE_CLASS, BUS_BEAN_NAME); - - private ServerImpl server; - private Bus bus; - - @Override - protected void setUp() throws Exception { - bus = CXFBusFactory.getDefaultBus(); - - super.setUp(); - - startService(); - } - - protected ClassPathXmlApplicationContext createApplicationContext() { - return new ClassPathXmlApplicationContext("org/apache/servicemix/camel/spring/DummyBean.xml") { - @Override - public <T> T getBean(String name, Class<T> requiredType) throws BeansException { - if (BUS_BEAN_NAME.equals(name)) { - return requiredType.cast(bus); - } - return super.getBean(name, requiredType); //To change body of overridden methods use File | Settings | File Templates. - } - }; - } - - protected void assertValidContext(CamelContext context) { - assertNotNull("No context found!", context); - } - - protected void startService() { - //start a service - ServerFactoryBean svrBean = new ServerFactoryBean(); - - svrBean.setAddress(SERVICE_ADDRESS); - svrBean.setServiceClass(HelloService.class); - svrBean.setServiceBean(new HelloServiceImpl()); - svrBean.setBus(CXFBusFactory.getDefaultBus()); - - server = (ServerImpl)svrBean.create(); - server.start(); - } - - @Override - protected void tearDown() throws Exception { - if (server != null) { - server.stop(); - } - super.tearDown(); - } - - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - public void configure() { - from(routerEndpointURI).to("smx:testEndpoint");// like what do in binding component - from("smx:testEndpoint").to(serviceEndpointURI);// like what do in se - } - }; - } - - public void testInvokingServiceFromCXFClient() throws Exception { - Bus bus = BusFactory.getDefaultBus(); - - ClientProxyFactoryBean proxyFactory = new ClientProxyFactoryBean(); - ClientFactoryBean clientBean = proxyFactory.getClientFactoryBean(); - clientBean.setAddress(ROUTER_ADDRESS); - clientBean.setServiceClass(HelloService.class); - clientBean.setBus(bus); - - HelloService client = (HelloService) proxyFactory.create(); - String result = client.echo("hello world"); - assertEquals("we should get the right answer from router", "hello world echo", result); - } - - - -} http://git-wip-us.apache.org/repos/asf/servicemix-features/blob/d358d2ea/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/TimeoutTest.java ---------------------------------------------------------------------- diff --git a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/TimeoutTest.java b/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/TimeoutTest.java deleted file mode 100644 index 00a0d76..0000000 --- a/camel/servicemix-camel/src/test/java/org/apache/servicemix/camel/nmr/TimeoutTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.servicemix.camel.nmr; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.servicemix.nmr.api.AbortedException; -import org.junit.Test; - -/** - * A basic test to ensure that the 'timeout' property on the endpoint works fine. - */ -public class TimeoutTest extends AbstractComponentTest { - - private static final String SLOW_MESSAGE = "Take the slow route, please!"; - private static final String FAST_MESSAGE = "Get me there as quickly as you can!"; - private static final String RESPONSE_MESSAGE = "You've arrived at your destination!"; - - private static final Long TIMEOUT = 1000l; - - @Test - public void testFastInOutWithTimeout() throws InterruptedException { - MockEndpoint mock = getMockEndpoint("mock:timeout"); - mock.expectedMessageCount(1); - - Exchange result = template.request("direct:timeout", new Processor() { - - public void process(Exchange exchange) throws Exception { - exchange.getIn().setBody(FAST_MESSAGE); - } - - }); - - assertMockEndpointsSatisfied(); - assertFalse("Exchange got finished successfully", result.isFailed()); - assertEquals("Response message got set", RESPONSE_MESSAGE, result.getOut().getBody()); - } - - @Test - public void testSlowInOutWithTimeout() throws InterruptedException { - MockEndpoint mock = getMockEndpoint("mock:timeout"); - mock.expectedMessageCount(0); - - Exchange result = template.request("direct:timeout", new Processor() { - - public void process(Exchange exchange) throws Exception { - exchange.getIn().setBody(SLOW_MESSAGE); - } - - }); - - assertTrue("Exchange got finished successfully", result.isFailed()); - assertFalse("Response message not set", result.hasOut()); - assertTrue("TimeoutException was thrown", result.getException() instanceof AbortedException); - } - - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - from("direct:timeout") - .choice() - .when(simple("${body} contains slow")).to("nmr:slow-route?timeout=" + TIMEOUT) - .otherwise().to("nmr:fast-route?timeout=" + TIMEOUT) - .end() - .to("mock:timeout"); - - from("nmr:fast-route").process(new ResponseProcessor()); - from("nmr:slow-route").delay(2 * TIMEOUT).process(new ResponseProcessor()); - } - }; - } - - private final class ResponseProcessor implements Processor { - - public void process(Exchange exchange) throws Exception { - exchange.getOut().setBody(RESPONSE_MESSAGE); - } - } -}
