001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.saaj;
020:
021: import java.io.IOException;
022: import java.net.URL;
023:
024: import javax.xml.soap.SOAPConnection;
025: import javax.xml.soap.SOAPConnectionFactory;
026: import javax.xml.soap.SOAPException;
027: import javax.xml.soap.SOAPMessage;
028:
029: import junit.framework.TestCase;
030:
031: import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
032: import org.apache.commons.httpclient.HttpClient;
033: import org.apache.commons.httpclient.HttpException;
034: import org.apache.commons.httpclient.HttpStatus;
035: import org.apache.commons.httpclient.methods.GetMethod;
036: import org.apache.commons.httpclient.params.HttpMethodParams;
037:
038: /**
039: *
040: */
041: public class SOAPConnectionTest extends TestCase {
042: public void testClose() {
043: try {
044: SOAPConnection sCon = SOAPConnectionFactory.newInstance()
045: .createConnection();
046: sCon.close();
047: } catch (SOAPException e) {
048: fail("Unexpected Exception " + e);
049: }
050: }
051:
052: public void testCloseTwice() {
053: SOAPConnectionFactory soapConnectionFactory = null;
054: try {
055: soapConnectionFactory = SOAPConnectionFactory.newInstance();
056: } catch (SOAPException e) {
057: fail("Unexpected Exception " + e);
058: }
059:
060: SOAPConnection sCon = null;
061: try {
062: sCon = soapConnectionFactory.createConnection();
063: sCon.close();
064: } catch (SOAPException e) {
065: fail("Unexpected Exception " + e);
066: }
067:
068: try {
069: sCon.close();
070: fail("Expected Exception did not occur");
071: } catch (SOAPException e) {
072: assertTrue(true);
073: }
074: }
075:
076: public void testCallOnCloseConnection() {
077: SOAPConnectionFactory soapConnectionFactory = null;
078: try {
079: soapConnectionFactory = SOAPConnectionFactory.newInstance();
080: } catch (SOAPException e) {
081: fail("Unexpected Exception " + e);
082: }
083:
084: SOAPConnection sCon = null;
085: try {
086: sCon = soapConnectionFactory.createConnection();
087: sCon.close();
088: } catch (SOAPException e) {
089: fail("Unexpected Exception " + e);
090: }
091:
092: try {
093: sCon.call(null, new Object());
094: fail("Expected Exception did not occur");
095: } catch (SOAPException e) {
096: assertTrue(true);
097: }
098: }
099:
100: public void testGet() {
101: if (isNetworkedResourceAvailable("http://java.sun.com/index.html")) {
102: try {
103: SOAPConnectionFactory sf = new SOAPConnectionFactoryImpl();
104: SOAPConnection con = sf.createConnection();
105: //Create a valid non webservice endpoint for invoking HTTP-GET
106: URL urlEndpoint = new URL("http", "java.sun.com", 80,
107: "/index.html");
108: //invoking HTTP-GET with a valid non webservice endpoint should throw a SOAPException
109: SOAPMessage reply = con.get(urlEndpoint);
110: } catch (Exception e) {
111: assertTrue(e instanceof SOAPException);
112: }
113: } else {
114: //If resource is not available online, do a mock test
115: assertTrue(true);
116: }
117: }
118:
119: private boolean isNetworkedResourceAvailable(String url) {
120: HttpClient client = new HttpClient();
121: GetMethod method = new GetMethod(url);
122: client.getHttpConnectionManager().getParams()
123: .setConnectionTimeout(1000);
124: method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
125: new DefaultHttpMethodRetryHandler(1, false));
126:
127: try {
128: int statusCode = client.executeMethod(method);
129: if (statusCode != HttpStatus.SC_OK) {
130: return false;
131: }
132:
133: } catch (HttpException e) {
134: e.printStackTrace();
135: return false;
136: } catch (IOException e) {
137: e.printStackTrace();
138: return false;
139: } finally {
140: method.releaseConnection();
141: }
142: return true;
143: }
144: }
|