[04/23] camel git commit: CAMEL-8239 Split jetty8 and jetty9 support into separate modules
http://git-wip-us.apache.org/repos/asf/camel/blob/0d96e56d/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java -- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java new file mode 100644 index 000..7c81101 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java @@ -0,0 +1,115 @@ +/** + * 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.camel.component.jetty; + +import java.io.File; + +import javax.activation.DataHandler; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.RequestEntity; +import org.apache.commons.httpclient.methods.multipart.FilePart; +import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; +import org.apache.commons.httpclient.methods.multipart.Part; +import org.apache.commons.httpclient.methods.multipart.StringPart; +import org.apache.commons.httpclient.params.HttpMethodParams; +import org.junit.Test; + +public class MultiPartFormTest extends BaseJettyTest { +private RequestEntity createMultipartRequestEntity() throws Exception { +File file = new File(src/main/resources/META-INF/NOTICE.txt); + +Part[] parts = {new StringPart(comment, A binary file of some kind), +new FilePart(file.getName(), file)}; + +return new MultipartRequestEntity(parts, new HttpMethodParams()); + +} + +@Test +public void testSendMultiPartForm() throws Exception { +HttpClient httpclient = new HttpClient(); + +PostMethod httppost = new PostMethod(http://localhost:; + getPort() + /test); + +httppost.setRequestEntity(createMultipartRequestEntity()); + +int status = httpclient.executeMethod(httppost); + +assertEquals(Get a wrong response status, 200, status); +String result = httppost.getResponseBodyAsString(); + +assertEquals(Get a wrong result, A binary file of some kind, result); + +} + +@Test +public void testSendMultiPartFormFromCamelHttpComponnent() throws Exception { +String result = template.requestBody(http://localhost:; + getPort() + /test, createMultipartRequestEntity(), String.class); +assertEquals(Get a wrong result, A binary file of some kind, result); +} + +protected RouteBuilder createRouteBuilder() throws Exception { +return new RouteBuilder() { +public void configure() throws Exception { +// START SNIPPET: e1 +// Set the jetty temp directory which store the file for multi +// part form +// camel-jetty will clean up the file after it handled the +// request. +// The option works rightly from Camel 2.4.0 +getContext().getProperties().put(CamelJettyTempDir, target); + +from(jetty://http://localhost:{{port}}/test;).process(new Processor() { + +public void process(Exchange exchange) throws Exception { +Message in = exchange.getIn(); +assertEquals(Get a wrong attachement size, 1, in.getAttachments().size()); +// The file name is attachment id +DataHandler data = in.getAttachment(NOTICE.txt); + +assertNotNull(Should get the DataHandle NOTICE.txt, data); +// This assert is wrong, but the correct content-type +// (application/octet-stream) +// will not be returned until Jetty makes it available - +// currently the content-type +// returned is just the default for FileDataHandler
[04/23] camel git commit: CAMEL-8239 Split jetty8 and jetty9 support into separate modules
http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiThreadedHttpGetTest.java -- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiThreadedHttpGetTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiThreadedHttpGetTest.java new file mode 100644 index 000..d1f77db --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiThreadedHttpGetTest.java @@ -0,0 +1,120 @@ +/** + * 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.camel.component.jetty; + +import java.io.InputStream; +import java.util.List; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.http.HttpComponent; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.commons.httpclient.HttpConnectionManager; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; +import org.junit.Test; + +/** + * @version + */ +public class MultiThreadedHttpGetTest extends BaseJettyTest { + +@Test +public void testHttpGetWithConversion() throws Exception { + +// In this scenario response stream is converted to String +// so the stream has to be read to the end. When this happens +// the associated connection is released automatically. + +String endpointName = seda:withConversion?concurrentConsumers=5; +sendMessagesTo(endpointName, 5); +} + +@Test +public void testHttpGetWithoutConversion() throws Exception { + +// This is needed as by default there are 2 parallel +// connections to some host and there is nothing that +// closes the http connection here. +// Need to set the httpConnectionManager +HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager(); +httpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(5); +context.getComponent(http, HttpComponent.class).setHttpConnectionManager(httpConnectionManager); + + +String endpointName = seda:withoutConversion?concurrentConsumers=5; +sendMessagesTo(endpointName, 5); +} + +@Test +public void testHttpGetWithExplicitStreamClose() throws Exception { + +// We close connections explicitely at the very end of the flow +// (camel doesn't know when the stream is not needed any more) + +MockEndpoint mockEndpoint = resolveMandatoryEndpoint(mock:results, MockEndpoint.class); + +for (int i = 0; i 5; i++) { +mockEndpoint.expectedMessageCount(1); +template.sendBody(seda:withoutConversion?concurrentConsumers=5, null); +mockEndpoint.assertIsSatisfied(); +Object response = mockEndpoint.getReceivedExchanges().get(0).getIn().getBody(); +InputStream responseStream = assertIsInstanceOf(InputStream.class, response); +responseStream.close(); +mockEndpoint.reset(); +} +} + +protected void sendMessagesTo(String endpointName, int count) throws InterruptedException { +MockEndpoint mockEndpoint = resolveMandatoryEndpoint(mock:results, MockEndpoint.class); +mockEndpoint.expectedMessageCount(count); + +for (int i = 0; i count; i++) { +template.sendBody(endpointName, null); +} + +mockEndpoint.assertIsSatisfied(); +ListExchange list = mockEndpoint.getReceivedExchanges(); +for (Exchange exchange : list) { +String body = exchange.getIn().getBody(String.class); + +log.debug(Body: + body); +assertNotNull(Should have a body!, body); +assertTrue(body should contain: html, body.contains(html)); +} +} + +@Override +protected RouteBuilder createRouteBuilder() throws Exception { +return new RouteBuilder() { +public void configure() { +