001: /* Copyright 2005 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.ByteArrayOutputStream;
025: import java.io.IOException;
026: import java.io.ObjectOutputStream;
027:
028: import nu.xom.Element;
029: import nu.xom.XPathException;
030: import nu.xom.XPathTypeException;
031:
032: /**
033: * <p>
034: * Unit tests for the <code>XPathException</code> class.
035: * </p>
036: *
037: * @author Elliotte Rusty Harold
038: * @version 1.1b4
039: *
040: */
041: public class XPathExceptionTest extends XOMTestCase {
042:
043: private XPathException ex;
044: private Exception cause;
045:
046: public XPathExceptionTest(String name) {
047: super (name);
048: }
049:
050: protected void setUp() {
051: ex = new XPathException("message");
052: cause = new Exception();
053: }
054:
055: public void testConstructor() {
056: String message = "testing 1-2-3";
057: XPathException ex = new XPathException(message, cause);
058: assertEquals(message, ex.getMessage());
059: assertEquals(cause, ex.getCause());
060: }
061:
062: public void testInitCause() {
063:
064: assertNull(ex.getCause());
065: ex.initCause(cause);
066: assertEquals(cause, ex.getCause());
067:
068: try {
069: ex.initCause(null);
070: fail("Reinitialized cause over null");
071: } catch (IllegalStateException success) {
072: assertNotNull(success.getMessage());
073: }
074:
075: try {
076: ex.initCause(new Exception());
077: fail("Reinitialized cause over null");
078: } catch (IllegalStateException success) {
079: assertNotNull(success.getMessage());
080: }
081:
082: }
083:
084: public void testNullInitCause() {
085:
086: XPathException ex = new XPathException(null, null);
087: assertNull(ex.getCause());
088:
089: try {
090: ex.initCause(new Exception());
091: fail("Reinitialized cause over null");
092: } catch (IllegalStateException success) {
093: assertNotNull(success.getMessage());
094: }
095:
096: try {
097: ex.initCause(null);
098: fail("Reinitialized cause over null");
099: } catch (IllegalStateException success) {
100: assertNotNull(success.getMessage());
101: }
102:
103: }
104:
105: public void testSelfCause() {
106:
107: try {
108: ex.initCause(ex);
109: fail("Allowed self-causation");
110: } catch (IllegalArgumentException success) {
111: assertNotNull(success.getMessage());
112: }
113:
114: }
115:
116: public void testGetMessage() {
117: Exception ex = new XPathException("testing");
118: assertEquals("testing", ex.getMessage());
119: }
120:
121: public void testGetXPathExpression() {
122:
123: Element parent = new Element("Test");
124:
125: try {
126: parent.query("This is not an XPath expression");
127: fail("Allowed malformed query");
128: } catch (XPathException success) {
129: assertEquals("This is not an XPath expression", success
130: .getXPath());
131: }
132:
133: }
134:
135: public void testSerializeXPathTypeException() throws IOException {
136:
137: Element parent = new Element("Test");
138: Element child = new Element("child");
139: parent.appendChild(child);
140:
141: try {
142: parent.query("count(*)");
143: fail("Allowed query to return number");
144: } catch (XPathTypeException success) {
145: ByteArrayOutputStream out = new ByteArrayOutputStream();
146: ObjectOutputStream oout = new ObjectOutputStream(out);
147: oout.writeObject(success);
148: oout.close();
149: }
150:
151: }
152:
153: }
|