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:
020: package org.apache.axis2.engine;
021:
022: import junit.framework.TestCase;
023: import org.apache.axiom.soap.SOAP12Constants;
024: import org.apache.axis2.addressing.AddressingConstants;
025: import org.apache.axis2.addressing.EndpointReference;
026: import org.apache.axis2.client.Options;
027: import org.apache.axis2.context.ConfigurationContext;
028: import org.apache.axis2.description.TransportInDescription;
029: import org.apache.axis2.description.TransportOutDescription;
030: import org.apache.axis2.transport.http.CommonsHTTPTransportSender;
031: import org.apache.axis2.transport.http.SimpleHTTPServer;
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import javax.xml.namespace.QName;
036: import java.io.File;
037: import java.io.FileInputStream;
038: import java.io.FileOutputStream;
039: import java.io.ObjectInputStream;
040: import java.io.ObjectOutputStream;
041:
042: public class OptionsSaveTest extends TestCase {
043: protected static final Log log = LogFactory
044: .getLog(OptionsSaveTest.class);
045:
046: private transient QName serviceName = new QName("NullService");
047: private transient QName operationName = new QName("DummyOp");
048:
049: private String testArg = null;
050:
051: public OptionsSaveTest(String arg0) {
052: super (arg0);
053: testArg = new String(arg0);
054:
055: initAll();
056: }
057:
058: protected void initAll() {
059: }
060:
061: protected void setUp() throws Exception {
062: //org.apache.log4j.BasicConfigurator.configure();
063: }
064:
065: public void testSaveAndRestore() throws Exception {
066: File theFile = null;
067: String theFilename = null;
068: boolean saved = false;
069: boolean restored = false;
070: boolean done = false;
071: boolean comparesOk = false;
072:
073: AxisConfiguration axisConfiguration = new AxisConfiguration();
074: ConfigurationContext configurationContext = new ConfigurationContext(
075: axisConfiguration);
076:
077: log
078: .debug("OptionsSaveTest:testSaveAndRestore(): BEGIN ---------------");
079:
080: // ---------------------------------------------------------
081: // setup an options object to use
082: // ---------------------------------------------------------
083: Options options = new Options();
084:
085: options
086: .setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
087: options.setExceptionToBeThrownOnSOAPFault(true);
088: options.setTimeOutInMilliSeconds(5000L);
089: options.setUseSeparateListener(false);
090: options.setAction("SoapAction");
091: options.setFaultTo(new EndpointReference(
092: "http://ws.apache.org/axis2/faultTo"));
093: options.setFrom(new EndpointReference(
094: "http://ws.apache.org/axis2/from"));
095: options.setTo(new EndpointReference(
096: "http://ws.apache.org/axis2/to"));
097: options.setReplyTo(new EndpointReference(
098: AddressingConstants.Final.WSA_ANONYMOUS_URL));
099:
100: TransportOutDescription transportOut = new TransportOutDescription(
101: "null");
102: TransportOutDescription transportOut2 = new TransportOutDescription(
103: "happy");
104: TransportOutDescription transportOut3 = new TransportOutDescription(
105: "golucky");
106: transportOut.setSender(new CommonsHTTPTransportSender());
107: transportOut2.setSender(new CommonsHTTPTransportSender());
108: transportOut3.setSender(new CommonsHTTPTransportSender());
109: options.setTransportOut(transportOut);
110: axisConfiguration.addTransportOut(transportOut3);
111: axisConfiguration.addTransportOut(transportOut2);
112: axisConfiguration.addTransportOut(transportOut);
113:
114: TransportInDescription transportIn = new TransportInDescription(
115: "null");
116: TransportInDescription transportIn2 = new TransportInDescription(
117: "always");
118: TransportInDescription transportIn3 = new TransportInDescription(
119: "thebest");
120: transportIn.setReceiver(new SimpleHTTPServer());
121: transportIn2.setReceiver(new SimpleHTTPServer());
122: transportIn3.setReceiver(new SimpleHTTPServer());
123: options.setTransportIn(transportIn);
124: axisConfiguration.addTransportIn(transportIn2);
125: axisConfiguration.addTransportIn(transportIn);
126: axisConfiguration.addTransportIn(transportIn3);
127:
128: options.setMessageId("msgId012345");
129:
130: options.setProperty("key01", "value01");
131: options.setProperty("key02", "value02");
132: options.setProperty("key03", "value03");
133: options.setProperty("key04", "value04");
134: options.setProperty("key05", "value05");
135: options.setProperty("key06", "value06");
136: options.setProperty("key07", "value07");
137: options.setProperty("key08", "value08");
138: options.setProperty("key09", "value09");
139: options.setProperty("key10", "value10");
140:
141: // TODO: setup a parent
142:
143: // ---------------------------------------------------------
144: // setup a temporary file to use
145: // ---------------------------------------------------------
146: try {
147: theFile = File.createTempFile("optionsSave", null);
148: theFilename = theFile.getName();
149: log
150: .debug("OptionsSaveTest:testSaveAndRestore(): temp file = ["
151: + theFilename + "]");
152: } catch (Exception ex) {
153: log
154: .debug("OptionsSaveTest:testSaveAndRestore(): error creating temp file = ["
155: + ex.getMessage() + "]");
156: theFile = null;
157: }
158:
159: if (theFile != null) {
160: // ---------------------------------------------------------
161: // save to the temporary file
162: // ---------------------------------------------------------
163: try {
164: // setup an output stream to a physical file
165: FileOutputStream outStream = new FileOutputStream(
166: theFile);
167:
168: // attach a stream capable of writing objects to the
169: // stream connected to the file
170: ObjectOutputStream outObjStream = new ObjectOutputStream(
171: outStream);
172:
173: // try to save the message context
174: log
175: .debug("OptionsSaveTest:testSaveAndRestore(): saving .....");
176: saved = false;
177: outObjStream.writeObject(options);
178:
179: // close out the streams
180: outObjStream.flush();
181: outObjStream.close();
182: outStream.flush();
183: outStream.close();
184:
185: saved = true;
186: log
187: .debug("OptionsSaveTest:testSaveAndRestore(): ....save operation completed.....");
188:
189: long filesize = theFile.length();
190: log
191: .debug("OptionsSaveTest:testSaveAndRestore(): file size after save ["
192: + filesize
193: + "] temp file = ["
194: + theFilename + "]");
195: } catch (Exception ex2) {
196: if (saved != true) {
197: log
198: .debug("OptionsSaveTest:testSaveAndRestore(): error during save ["
199: + ex2.getClass().getName()
200: + " : "
201: + ex2.getMessage() + "]");
202: ex2.printStackTrace();
203: } else {
204: log
205: .debug("OptionsSaveTest:testSaveAndRestore(): error during restore ["
206: + ex2.getClass().getName()
207: + " : "
208: + ex2.getMessage() + "]");
209: ex2.printStackTrace();
210: }
211: }
212:
213: assertTrue(saved);
214:
215: // ---------------------------------------------------------
216: // restore from the temporary file
217: // ---------------------------------------------------------
218: try {
219: // setup an input stream to the file
220: FileInputStream inStream = new FileInputStream(theFile);
221:
222: // attach a stream capable of reading objects from the
223: // stream connected to the file
224: ObjectInputStream inObjStream = new ObjectInputStream(
225: inStream);
226:
227: // try to restore the options
228: log
229: .debug("OptionsSaveTest:testSaveAndRestore(): restoring .....");
230: restored = false;
231: Options options_restored = (Options) inObjStream
232: .readObject();
233: inObjStream.close();
234: inStream.close();
235:
236: options_restored.activate(configurationContext);
237:
238: restored = true;
239: log
240: .debug("OptionsSaveTest:testSaveAndRestore(): ....restored operation completed.....");
241:
242: comparesOk = options_restored.isEquivalent(options);
243: log
244: .debug("OptionsSaveTest:testSaveAndRestore(): Options equivalency ["
245: + comparesOk + "]");
246: } catch (Exception ex2) {
247: log
248: .debug("OptionsSaveTest:testSaveAndRestore(): error during restore ["
249: + ex2.getClass().getName()
250: + " : "
251: + ex2.getMessage() + "]");
252: ex2.printStackTrace();
253: }
254:
255: assertTrue(restored);
256:
257: assertTrue(comparesOk);
258:
259: // if the save/restore of the object succeeded,
260: // then don't keep the temporary file around
261: boolean removeTmpFile = saved && restored && comparesOk;
262: if (removeTmpFile) {
263: try {
264: theFile.delete();
265: } catch (Exception e) {
266: // just absorb it
267: }
268: }
269:
270: // indicate that the temp file was created ok
271: done = true;
272: }
273:
274: // this is false when there are problems with the temporary file
275: assertTrue(done);
276:
277: log
278: .debug("OptionsSaveTest:testSaveAndRestore(): END ---------------");
279: }
280:
281: }
|