001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestJBIRemoteException.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.common;
030:
031: import java.io.BufferedReader;
032: import java.io.BufferedWriter;
033: import java.io.FileReader;
034: import java.io.InputStream;
035: import java.io.InputStreamReader;
036: import java.io.StringWriter;
037: import java.util.ArrayList;
038: import java.util.Iterator;
039: import java.util.List;
040: import junit.framework.TestCase;
041:
042: /**
043: * test class
044: * @author Sun Microsystems, Inc.
045: */
046: public class TestJBIRemoteException extends TestCase {
047:
048: /**
049: * Creates a new instance of TestMgmtMessage
050: * @param aTestName name
051: */
052: public TestJBIRemoteException(String aTestName) {
053: super (aTestName);
054: }
055:
056: private String getSrcRootDir() {
057: String srcroot = System.getProperty("junit.srcroot");
058: return srcroot;
059: }
060:
061: /**
062: * reads the file to a string
063: * @throws Exception if an unexpected error occurs
064: */
065: protected String readFromResource(String resource) throws Exception {
066: StringWriter strWriter = new StringWriter();
067: BufferedWriter writer = new BufferedWriter(strWriter);
068: BufferedReader reader = new BufferedReader(
069: new InputStreamReader(this .getClass()
070: .getResourceAsStream(resource)));
071: for (String line; (line = reader.readLine()) != null;) {
072: writer.write(line);
073: writer.newLine();
074: }
075: reader.close();
076: writer.close();
077: strWriter.close();
078: return strWriter.getBuffer().toString();
079: }
080:
081: /**
082: * reads the file to a string
083: * @throws Exception if an unexpected error occurs
084: */
085: protected String readFile(String filePath) throws Exception {
086: StringWriter strWriter = new StringWriter();
087: BufferedWriter writer = new BufferedWriter(strWriter);
088: BufferedReader reader = new BufferedReader(new FileReader(
089: filePath));
090: for (String line; (line = reader.readLine()) != null;) {
091: writer.write(line);
092: writer.newLine();
093: }
094: reader.close();
095: writer.close();
096: strWriter.close();
097: return strWriter.getBuffer().toString();
098: }
099:
100: /**
101: * test sucess msg
102: * @throws Exception on error
103: */
104: public void testMessageFormatter() throws Exception {
105: Exception ex1, ex2, ex3, ex4, ex5;
106:
107: ex1 = new JBIRemoteException("This is the Main Message");
108: System.out.println("Main Message Only [\n" + ex1.getMessage()
109: + "\n]\n");
110:
111: ex2 = new JBIRemoteException("Secondary Cause");
112: ex1 = new JBIRemoteException("Main Message", ex2);
113:
114: System.out.println("Main Message with Cause [\n"
115: + ex1.getMessage() + "\n]\n");
116:
117: ex2 = new JBIRemoteException("Secondary Cause");
118: ex1 = new JBIRemoteException(ex2);
119:
120: System.out.println("Only Cause [\n" + ex1.getMessage()
121: + "\n]\n");
122:
123: ex5 = new JBIRemoteException("Fifth Cause");
124: ex4 = new JBIRemoteException("Fourth Cause", ex5);
125: ex3 = new JBIRemoteException(
126: "Third Cause has multiple lines. \nSecond line for third cause",
127: ex4);
128: ex2 = new JBIRemoteException("Secondary Cause", ex3);
129: ex1 = new JBIRemoteException("Main Message", ex2);
130:
131: System.out.println("Main Message with Multiple Cause [\n"
132: + ex1.getMessage() + "\n]\n");
133:
134: ex5 = new JBIRemoteException("Fifth Cause");
135: ex4 = new JBIRemoteException("Fourth Cause", ex5);
136: ex3 = new JBIRemoteException(ex4);
137: ex2 = new JBIRemoteException("Secondary Cause", ex3);
138: ex1 = new JBIRemoteException("Main Message", ex2);
139:
140: System.out
141: .println("Main Message with Multiple Cause and 3rd cause with no message [\n"
142: + ex1.getMessage() + "\n]\n");
143:
144: ex4 = new JBIRemoteException("Fourth Cause");
145: ex3 = new JBIRemoteException(ex4);
146: ex2 = new JBIRemoteException(ex3);
147: ex1 = new JBIRemoteException(ex2);
148:
149: System.out.println("Last Cause Message [\n" + ex1.getMessage()
150: + "\n]\n");
151:
152: ex4 = new Exception("Some Exception");
153: ex3 = new JBIRemoteException(ex4);
154: ex2 = new JBIRemoteException(ex3);
155: ex1 = new JBIRemoteException(ex2);
156:
157: System.out.println("Last 3rd party Cause Message [\n"
158: + ex1.getMessage() + "\n]\n");
159:
160: ex4 = new JBIRemoteException((String) null);
161: ex3 = new Exception("Some Exception", ex4);
162: ex2 = new JBIRemoteException(ex3);
163: ex1 = new JBIRemoteException(ex2);
164:
165: System.out.println("Middle 3rd party Cause Message [\n"
166: + ex1.getMessage() + "\n]\n");
167:
168: ex4 = new JBIRemoteException((String) null);
169: ex3 = new JBIRemoteException(null, ex4);
170: ex2 = new JBIRemoteException(null, ex3);
171: ex1 = new JBIRemoteException("Main Message", ex2);
172:
173: System.out.println("All Null messages [\n" + ex1.getMessage()
174: + "\n]\n");
175:
176: ex3 = new JBIRemoteException(new NullPointerException());
177: ex2 = new JBIRemoteException(null, ex3);
178: ex1 = new JBIRemoteException(ex2);
179:
180: System.out.println("3rd party with no message [\n"
181: + ex1.getMessage() + "\n]\n");
182:
183: ex3 = new JBIRemoteException(new NullPointerException());
184: ex2 = new Exception(ex3);
185: ex1 = new JBIRemoteException(ex2);
186:
187: System.out
188: .println("3rd party with no message in the middle [\n"
189: + ex1.getMessage() + "\n]\n");
190:
191: // // Caused by:
192: // ex5 = new Exception("Fifth Cause");
193: // ex4 = new Exception("Fourth Cause", ex5);
194: // ex3 = new Exception("Third Cause", ex4);
195: // ex2 = new Exception("Secondary Cause", ex3);
196: // ex1 = new Exception("Main Message", ex2);
197: //
198: // // System.out.println("Normal Exception Message : \n" + ex1.getMessage());
199: // System.out.println("Normal Exception Stacktrace : \n");
200: // ex1.printStackTrace();
201:
202: }
203:
204: /**
205: * test sucess msg
206: * @throws Exception on error
207: */
208: public void testNonJbiResultXmlInRemoteException() throws Exception {
209: String nonJbiResultXml = this .readFromResource("test_jbi.xml");
210: Exception ex = new Exception(nonJbiResultXml);
211: JBIRemoteException rEx = new JBIRemoteException(ex);
212:
213: JBIManagementMessage mgmtMsg = rEx
214: .extractJBIManagementMessage();
215:
216: this .assertNull(
217: "Expecting a NULL Mmgmt object from non jbi mgmt xml",
218: mgmtMsg);
219: }
220:
221: /**
222: * main
223: * @param args the command line arguments
224: */
225: public static void main(String[] args) {
226: // TODO code application logic here
227: try {
228: new TestJBIRemoteException("test").testMessageFormatter();
229: } catch (Exception ex) {
230: ex.printStackTrace();
231: }
232: }
233: }
|