001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: TestHelper.java,v 1.2 2007/07/16 16:42:02 ofung Exp $
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059:
060: /**
061: *
062: * @author Edwin Goei
063: */package util;
064:
065: import java.io.*;
066:
067: import javax.xml.soap.SOAPMessage;
068: import javax.xml.soap.SOAPPart;
069: import javax.xml.transform.*;
070: import javax.xml.transform.stream.StreamResult;
071:
072: public class TestHelper {
073: private static final TestHelper INSTANCE = new TestHelper();
074:
075: /** Debug level. A value of 0 means don't write any output. */
076: private int debug;
077:
078: /**
079: * OutputStream for debug output. We need this b/c SAAJ writeTo method
080: * takes OutputStream-s and not PrintWriter-s.
081: */
082: private OutputStream ostream = System.err;
083:
084: /** PrintWriter for debug output */
085: private PrintWriter pw = new PrintWriter(ostream, true);
086:
087: public static TestHelper getInstance() {
088: return INSTANCE;
089: }
090:
091: private TestHelper() {
092: try {
093: debug = Integer.parseInt(System.getProperty("saaj.debug"));
094: } catch (NumberFormatException x) {
095: // assert(no such property defined or bad string)
096: debug = 0;
097: }
098: }
099:
100: /**
101: * @param dataId data resource identifier
102: * @return an InputStream to the data
103: * @throws Exception if resource is not found
104: */
105: public InputStream getInputStream(String dataId) throws Exception {
106: InputStream is = getClass().getResourceAsStream(
107: "/resources/" + dataId);
108: if (is == null) {
109: throw new Exception("Resource not found: " + dataId);
110: }
111: return is;
112: }
113:
114: /**
115: * @return true if debug level is > 0
116: */
117: public boolean isDebug() {
118: return debug > 0;
119: }
120:
121: public void println(String s) {
122: if (debug == 0) {
123: return;
124: }
125: pw.println(s);
126: }
127:
128: public void println() {
129: if (debug == 0) {
130: return;
131: }
132: pw.println();
133: }
134:
135: public void print(String s) {
136: if (debug == 0) {
137: return;
138: }
139: pw.print(s);
140: }
141:
142: /**
143: * @param msg the SOAPMessage whose envelope to dump out
144: * @throws Exception
145: */
146: public void dumpEnvelope(SOAPMessage msg) throws Exception {
147: if (debug == 0) {
148: return;
149: }
150: SOAPPart sp = msg.getSOAPPart();
151: Source source = sp.getContent();
152: TransformerFactory tf = new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl();
153: Transformer xform = tf.newTransformer();
154: println("==== TestHelper.dumpEnvelope(...) Start ====");
155: xform.transform(source, new StreamResult(pw));
156: println();
157: println("==== TestHelper.dumpEnvelope(...) End ====");
158: }
159:
160: /**
161: * Output similar to SOAPMessage.writeTo().
162: *
163: * @param msg the SOAPMessage to dump out
164: * @throws Exception
165: */
166: public void writeTo(SOAPMessage msg) throws Exception {
167: if (debug == 0) {
168: return;
169: }
170: println("==== TestHelper.writeTo(...) Start ====");
171: msg.writeTo(ostream);
172: println();
173: println("==== TestHelper.writeTo(...) End ====");
174: }
175: }
|