~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/tests/XPathExceptionTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2005 Elliotte Rusty Harold
 
2
   
 
3
   This library is free software; you can redistribute it and/or modify
 
4
   it under the terms of version 2.1 of the GNU Lesser General Public 
 
5
   License as published by the Free Software Foundation.
 
6
   
 
7
   This library is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 
10
   GNU Lesser General Public License for more details.
 
11
   
 
12
   You should have received a copy of the GNU Lesser General Public
 
13
   License along with this library; if not, write to the 
 
14
   Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 
15
   Boston, MA 02111-1307  USA
 
16
   
 
17
   You can contact Elliotte Rusty Harold by sending e-mail to
 
18
   elharo@metalab.unc.edu. Please include the word "XOM" in the
 
19
   subject line. The XOM home page is located at http://www.xom.nu/
 
20
*/
 
21
 
 
22
package nu.xom.tests;
 
23
 
 
24
import java.io.ByteArrayOutputStream;
 
25
import java.io.IOException;
 
26
import java.io.ObjectOutputStream;
 
27
 
 
28
import nu.xom.Element;
 
29
import nu.xom.XPathException;
 
30
import nu.xom.XPathTypeException;
 
31
 
 
32
/**
 
33
 * <p>
 
34
 * Unit tests for the <code>XPathException</code> class.
 
35
 * </p>
 
36
 * 
 
37
 * @author Elliotte Rusty Harold
 
38
 * @version 1.1b4
 
39
 *
 
40
 */
 
41
public class XPathExceptionTest extends XOMTestCase {
 
42
    
 
43
    
 
44
    private XPathException ex;
 
45
    private Exception cause;
 
46
    
 
47
    
 
48
    public XPathExceptionTest(String name) {
 
49
        super(name);   
 
50
    }
 
51
 
 
52
    
 
53
    protected void setUp() {
 
54
        ex = new XPathException("message");
 
55
        cause = new Exception();
 
56
    }
 
57
    
 
58
    
 
59
    public void testConstructor() {
 
60
        String message = "testing 1-2-3";
 
61
        XPathException ex = new XPathException(message, cause);
 
62
        assertEquals(message, ex.getMessage());
 
63
        assertEquals(cause, ex.getCause()); 
 
64
    }
 
65
 
 
66
    
 
67
    public void testInitCause() {
 
68
        
 
69
        assertNull(ex.getCause());
 
70
        ex.initCause(cause);
 
71
        assertEquals(cause, ex.getCause());
 
72
        
 
73
        try {
 
74
            ex.initCause(null);   
 
75
            fail("Reinitialized cause over null");   
 
76
        }
 
77
        catch (IllegalStateException success) {
 
78
            assertNotNull(success.getMessage()); 
 
79
        }
 
80
        
 
81
        try {
 
82
            ex.initCause(new Exception());   
 
83
            fail("Reinitialized cause over null");   
 
84
        }
 
85
        catch (IllegalStateException success) {
 
86
            assertNotNull(success.getMessage()); 
 
87
        }
 
88
        
 
89
    }
 
90
 
 
91
 
 
92
    public void testNullInitCause() {
 
93
        
 
94
        XPathException ex = new XPathException(null, null);
 
95
        assertNull(ex.getCause());
 
96
        
 
97
        try {
 
98
            ex.initCause(new Exception());
 
99
            fail("Reinitialized cause over null");   
 
100
        }
 
101
        catch (IllegalStateException success) {
 
102
            assertNotNull(success.getMessage());   
 
103
        }
 
104
 
 
105
        try {
 
106
            ex.initCause(null);   
 
107
            fail("Reinitialized cause over null");   
 
108
        }
 
109
        catch (IllegalStateException success) {
 
110
            assertNotNull(success.getMessage());   
 
111
        }
 
112
        
 
113
    }
 
114
 
 
115
    
 
116
    public void testSelfCause() {
 
117
        
 
118
        try {
 
119
            ex.initCause(ex);   
 
120
            fail("Allowed self-causation");   
 
121
        }
 
122
        catch (IllegalArgumentException success) {
 
123
            assertNotNull(success.getMessage());   
 
124
        }
 
125
        
 
126
    }
 
127
 
 
128
    
 
129
    public void testGetMessage() {      
 
130
        Exception ex = new XPathException("testing");
 
131
        assertEquals("testing", ex.getMessage());
 
132
    }
 
133
 
 
134
    public void testGetXPathExpression() {
 
135
        
 
136
        Element parent = new Element("Test");
 
137
        
 
138
        try {
 
139
            parent.query("This is not an XPath expression");
 
140
            fail("Allowed malformed query");
 
141
        }
 
142
        catch (XPathException success) {
 
143
            assertEquals(
 
144
              "This is not an XPath expression", success.getXPath());
 
145
        }  
 
146
        
 
147
    }
 
148
    
 
149
    public void testSerializeXPathTypeException() throws IOException {
 
150
        
 
151
        Element parent = new Element("Test");
 
152
        Element child = new Element("child");
 
153
        parent.appendChild(child);
 
154
        
 
155
        try {
 
156
            parent.query("count(*)");
 
157
            fail("Allowed query to return number");
 
158
        }
 
159
        catch (XPathTypeException success) {
 
160
            ByteArrayOutputStream out = new ByteArrayOutputStream();
 
161
            ObjectOutputStream oout = new ObjectOutputStream(out);
 
162
            oout.writeObject(success);
 
163
            oout.close();
 
164
        }
 
165
        
 
166
    }
 
167
    
 
168
 
 
169
    
 
170
}