001: /* Copyright 2003, 2004 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package nu.xom.tests;
023:
024: import java.io.PrintWriter;
025: import java.io.StringWriter;
026:
027: import nu.xom.CycleException;
028: import nu.xom.IllegalAddException;
029: import nu.xom.IllegalDataException;
030: import nu.xom.IllegalNameException;
031: import nu.xom.IllegalTargetException;
032: import nu.xom.MalformedURIException;
033: import nu.xom.MultipleParentException;
034: import nu.xom.NamespaceConflictException;
035: import nu.xom.NoSuchAttributeException;
036: import nu.xom.NoSuchChildException;
037: import nu.xom.XMLException;
038:
039: /**
040: * <p>
041: * This class provides unit tests for the <code>XMLException</code>
042: * class.
043: * </p>
044: *
045: * @author Elliotte Rusty Harold
046: * @version 1.1b2
047: *
048: */
049: public class XMLExceptionTest extends XOMTestCase {
050:
051: private XMLException ex;
052: private Exception cause;
053: private String message = "testing 1-2-3";
054:
055: public XMLExceptionTest(String name) {
056: super (name);
057: }
058:
059: protected void setUp() {
060: ex = new XMLException("message");
061: cause = new Exception();
062: }
063:
064: public void testConstructor() {
065: XMLException ex = new XMLException(message, cause);
066: assertEquals(message, ex.getMessage());
067: assertEquals(cause, ex.getCause());
068: }
069:
070: public void testPrintStackTrace() {
071: XMLException ex = new XMLException(message, cause);
072: StringWriter out = new StringWriter();
073: PrintWriter pw = new PrintWriter(out);
074: ex.printStackTrace(pw);
075: pw.close();
076: assertTrue(out.toString().indexOf("Caused by: ") > 0);
077: }
078:
079: public void testMalformedURIExceptionConstructor() {
080: XMLException ex = new MalformedURIException(message, cause);
081: assertEquals(message, ex.getMessage());
082: assertEquals(cause, ex.getCause());
083: }
084:
085: public void testValidityExceptionConstructor() {
086: XMLException ex = new MalformedURIException(message, cause);
087: assertEquals(message, ex.getMessage());
088: assertEquals(cause, ex.getCause());
089: }
090:
091: public void testNamespaceConflictExceptionConstructor() {
092: XMLException ex = new NamespaceConflictException(message, cause);
093: assertEquals(message, ex.getMessage());
094: assertEquals(cause, ex.getCause());
095: }
096:
097: public void testMultipleParentExceptionConstructor() {
098: XMLException ex = new MultipleParentException(message, cause);
099: assertEquals(message, ex.getMessage());
100: assertEquals(cause, ex.getCause());
101: }
102:
103: public void testNoSuchAttributeExceptionConstructor() {
104: XMLException ex = new NoSuchAttributeException(message, cause);
105: assertEquals(message, ex.getMessage());
106: assertEquals(cause, ex.getCause());
107: }
108:
109: public void testNoSuchChildExceptionConstructor() {
110: XMLException ex = new NoSuchChildException(message, cause);
111: assertEquals(message, ex.getMessage());
112: assertEquals(cause, ex.getCause());
113: }
114:
115: public void testCycleExceptionConstructor() {
116: XMLException ex = new CycleException(message, cause);
117: assertEquals(message, ex.getMessage());
118: assertEquals(cause, ex.getCause());
119: }
120:
121: public void testIllegalNameExceptionConstructor() {
122: XMLException ex = new IllegalNameException(message, cause);
123: assertEquals(message, ex.getMessage());
124: assertEquals(cause, ex.getCause());
125: }
126:
127: public void testIllegalTargetExceptionConstructor() {
128: XMLException ex = new IllegalTargetException(message, cause);
129: assertEquals(message, ex.getMessage());
130: assertEquals(cause, ex.getCause());
131: }
132:
133: public void testIllegalAddExceptionConstructor() {
134: XMLException ex = new IllegalAddException(message, cause);
135: assertEquals(message, ex.getMessage());
136: assertEquals(cause, ex.getCause());
137: }
138:
139: public void testIllegalDataExceptionConstructor() {
140: XMLException ex = new IllegalDataException(message, cause);
141: assertEquals(message, ex.getMessage());
142: assertEquals(cause, ex.getCause());
143: }
144:
145: public void testInitCause() {
146:
147: assertNull(ex.getCause());
148: ex.initCause(cause);
149: assertEquals(cause, ex.getCause());
150:
151: try {
152: ex.initCause(null);
153: fail("Reinitialized cause over null");
154: } catch (IllegalStateException result) {
155: // success
156: }
157:
158: try {
159: ex.initCause(new Exception());
160: fail("Reinitialized cause over null");
161: } catch (IllegalStateException result) {
162: // success
163: }
164:
165: }
166:
167: public void testNullInitCause() {
168:
169: ex = new XMLException(null, null);
170: assertNull(ex.getCause());
171:
172: try {
173: ex.initCause(new Exception());
174: fail("Reinitialized cause over null");
175: } catch (IllegalStateException result) {
176: // success
177: }
178:
179: try {
180: ex.initCause(null);
181: fail("Reinitialized cause over null");
182: } catch (IllegalStateException result) {
183: // success
184: }
185:
186: }
187:
188: public void testSelfCause() {
189:
190: try {
191: ex.initCause(ex);
192: fail("Allowed self-causation");
193: } catch (IllegalArgumentException result) {
194: // success
195: }
196:
197: }
198:
199: public void testGetMessage() {
200: Exception ex = new XMLException("testing");
201: assertEquals("testing", ex.getMessage());
202: }
203:
204: }
|