KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > HttpAddressingTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.http;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLConnection JavaDoc;
24
25 import javax.jbi.messaging.ExchangeStatus;
26 import javax.jbi.messaging.InOut;
27 import javax.xml.namespace.QName JavaDoc;
28 import javax.xml.transform.stream.StreamSource JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.servicemix.client.DefaultServiceMixClient;
33 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
34 import org.apache.servicemix.jbi.util.FileUtil;
35 import org.apache.servicemix.tck.SpringTestSupport;
36 import org.springframework.context.support.AbstractXmlApplicationContext;
37 import org.w3c.dom.Node JavaDoc;
38 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
39
40 public class HttpAddressingTest extends SpringTestSupport {
41
42     private static Log logger = LogFactory.getLog(HttpAddressingTest.class);
43
44     public void testOk() throws Exception JavaDoc {
45         DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
46         InOut me = client.createInOutExchange();
47         me.setService(new QName JavaDoc("http://test", "MyProviderService"));
48         InputStream JavaDoc fis = getClass().getResourceAsStream("addressing-request.xml");
49         me.getInMessage().setContent(new StreamSource JavaDoc(fis));
50         client.sendSync(me);
51         if (me.getStatus() == ExchangeStatus.ERROR) {
52             if (me.getError() != null) {
53                 throw me.getError();
54             } else {
55                 fail("Received ERROR status");
56             }
57         } else if (me.getFault() != null) {
58             fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
59         } else {
60             Node JavaDoc node = new SourceTransformer().toDOMNode(me.getOutMessage());
61             logger.info(new SourceTransformer().toString(node));
62             assertEquals("myid", textValueOfXPath(node, "//*[local-name()='RelatesTo']"));
63             assertNotNull(textValueOfXPath(node, "//*[local-name()='MessageID']"));
64         }
65     }
66     
67     public void testOkFromUrl() throws Exception JavaDoc {
68         URLConnection JavaDoc connection = new URL JavaDoc("http://localhost:8192/Service/").openConnection();
69         connection.setDoOutput(true);
70         connection.setDoInput(true);
71         OutputStream JavaDoc os = connection.getOutputStream();
72         // Post the request file.
73
InputStream JavaDoc fis = getClass().getResourceAsStream("addressing-request.xml");
74         FileUtil.copyInputStream(fis, os);
75         // Read the response.
76
InputStream JavaDoc is = connection.getInputStream();
77         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
78         FileUtil.copyInputStream(is, baos);
79         System.err.println(baos.toString());
80     }
81     
82     public void testBad() throws Exception JavaDoc {
83         /*
84          * Disable this test until http://jira.codehaus.org/browse/JETTY-99 or
85          * http://issues.apache.org/activemq/browse/SM-541
86          *
87         // This test is bit weird, because the http consumer is not soap
88         // so it will just forward the HTTP error
89         //
90         // TODO: note that WSA based faults are not created
91         //
92         DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
93         InOut me = client.createInOutExchange();
94         me.setService(new QName("http://test", "MyProviderService"));
95         InputStream fis = getClass().getResourceAsStream("bad-addressing-request.xml");
96         me.getInMessage().setContent(new StreamSource(fis));
97         client.sendSync(me);
98         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
99         assertNotNull(me.getFault());
100         logger.info(new SourceTransformer().toString(me.getFault().getContent()));
101         */

102     }
103     
104     protected AbstractXmlApplicationContext createBeanFactory() {
105         return new ClassPathXmlApplicationContext("org/apache/servicemix/http/addressing.xml");
106     }
107     
108 }
109
Popular Tags