KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > http > HttpTest


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.components.http;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLConnection JavaDoc;
26
27 import javax.xml.namespace.QName JavaDoc;
28
29 import org.apache.servicemix.tck.TestSupport;
30 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
31 import org.springframework.context.support.AbstractXmlApplicationContext;
32 import org.w3c.dom.Node JavaDoc;
33
34 /**
35  * @version $Revision: 426415 $
36  */

37 public class HttpTest extends TestSupport {
38
39     protected String JavaDoc quote = "SUNW";
40     
41     protected void setUp() throws Exception JavaDoc {
42         System.setProperty("javax.net.debug", "all");
43         // The following properties will be used by default if not specified in the xml conf
44
/*
45         System.setProperty("javax.net.ssl.trustStore", getResourceFilePath("client.keystore"));
46         System.setProperty("javax.net.ssl.trustStorePassword", "password");
47         System.setProperty("javax.net.ssl.keyStore", getResourceFilePath("server.keystore"));
48         System.setProperty("javax.net.ssl.keyStorePassword", "password");
49         */

50         super.setUp();
51     }
52     
53     String JavaDoc getResourceFilePath(String JavaDoc resource) throws Exception JavaDoc {
54         URL JavaDoc url = getClass().getResource(resource);
55         File JavaDoc f = new File JavaDoc(url.toURI());
56         return f.toString();
57     }
58
59     public void testCurrencyQuotes() throws Exception JavaDoc {
60         QName JavaDoc serviceName = new QName JavaDoc("http://servicemix.org/cheese/", "httpSender");
61         String JavaDoc file = "request.xml";
62
63         Object JavaDoc answer = requestServiceWithFileRequest(serviceName, file);
64         assertTrue("Shoud return a DOM Node: " + answer, answer instanceof Node JavaDoc);
65         Node JavaDoc node = (Node JavaDoc) answer;
66         System.out.println(transformer.toString(node));
67
68         String JavaDoc text = textValueOfXPath(node, "//Result").trim();
69
70         System.out.println("Found price: " + text);
71
72         assertTrue("price text should not be empty", text.length() > 0);
73
74     }
75     
76     public void testWithURLConnection() throws Exception JavaDoc {
77         URLConnection JavaDoc connection = new URL JavaDoc("http://localhost:8912").openConnection();
78         connection.setDoOutput(true);
79         connection.setDoInput(true);
80         OutputStream JavaDoc os = connection.getOutputStream();
81
82         // Post the request file.
83
InputStream JavaDoc fis = getClass().getResourceAsStream("request.xml");
84         int c;
85         while ((c = fis.read()) >= 0) {
86             os.write(c);
87         }
88         os.flush();
89         os.close();
90         fis.close();
91
92         // Read the response.
93
BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(connection.getInputStream()));
94         String JavaDoc inputLine;
95         while ((inputLine = in.readLine()) != null) {
96             System.out.println(inputLine);
97         }
98         in.close();
99     }
100
101     protected AbstractXmlApplicationContext createBeanFactory() {
102         return new ClassPathXmlApplicationContext("org/apache/servicemix/components/http/example.xml");
103     }
104 }
105
Popular Tags