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

« back to all changes in this revision

Viewing changes to src/nu/xom/tests/XMLExceptionTest.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 2003, 2004 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.PrintWriter;
 
25
import java.io.StringWriter;
 
26
 
 
27
import nu.xom.CycleException;
 
28
import nu.xom.IllegalAddException;
 
29
import nu.xom.IllegalDataException;
 
30
import nu.xom.IllegalNameException;
 
31
import nu.xom.IllegalTargetException;
 
32
import nu.xom.MalformedURIException;
 
33
import nu.xom.MultipleParentException;
 
34
import nu.xom.NamespaceConflictException;
 
35
import nu.xom.NoSuchAttributeException;
 
36
import nu.xom.NoSuchChildException;
 
37
import nu.xom.XMLException;
 
38
 
 
39
/**
 
40
 * <p>
 
41
 *   This class provides unit tests for the <code>XMLException</code>
 
42
 *   class.
 
43
 * </p>
 
44
 * 
 
45
 * @author Elliotte Rusty Harold
 
46
 * @version 1.1b2
 
47
 *
 
48
 */
 
49
public class XMLExceptionTest extends XOMTestCase {
 
50
    
 
51
    private XMLException ex;
 
52
    private Exception cause;
 
53
    private String message = "testing 1-2-3";
 
54
    
 
55
    public XMLExceptionTest(String name) {
 
56
        super(name);
 
57
    }
 
58
 
 
59
    
 
60
    protected void setUp() {
 
61
        ex = new XMLException("message");
 
62
        cause = new Exception();
 
63
    }
 
64
 
 
65
    
 
66
    public void testConstructor() {
 
67
        XMLException ex = new XMLException(message, cause);
 
68
        assertEquals(message, ex.getMessage());
 
69
        assertEquals(cause, ex.getCause()); 
 
70
    }
 
71
    
 
72
    
 
73
    public void testPrintStackTrace() {
 
74
        XMLException ex = new XMLException(message, cause);
 
75
        StringWriter out = new StringWriter();
 
76
        PrintWriter pw = new PrintWriter(out);
 
77
        ex.printStackTrace(pw);
 
78
        pw.close();
 
79
        assertTrue(out.toString().indexOf("Caused by: ") > 0);
 
80
    }
 
81
    
 
82
    
 
83
    public void testMalformedURIExceptionConstructor() {
 
84
        XMLException ex = new MalformedURIException(message, cause);
 
85
        assertEquals(message, ex.getMessage());
 
86
        assertEquals(cause, ex.getCause()); 
 
87
    }
 
88
    
 
89
    
 
90
    public void testValidityExceptionConstructor() {
 
91
        XMLException ex = new MalformedURIException(message, cause);
 
92
        assertEquals(message, ex.getMessage());
 
93
        assertEquals(cause, ex.getCause()); 
 
94
    }
 
95
    
 
96
    
 
97
    public void testNamespaceConflictExceptionConstructor() {
 
98
        XMLException ex = new NamespaceConflictException(message, cause);
 
99
        assertEquals(message, ex.getMessage());
 
100
        assertEquals(cause, ex.getCause()); 
 
101
    }
 
102
    
 
103
    
 
104
    public void testMultipleParentExceptionConstructor() {
 
105
        XMLException ex = new MultipleParentException(message, cause);
 
106
        assertEquals(message, ex.getMessage());
 
107
        assertEquals(cause, ex.getCause()); 
 
108
    }
 
109
    
 
110
    
 
111
    public void testNoSuchAttributeExceptionConstructor() {
 
112
        XMLException ex = new NoSuchAttributeException(message, cause);
 
113
        assertEquals(message, ex.getMessage());
 
114
        assertEquals(cause, ex.getCause()); 
 
115
    }
 
116
    
 
117
    
 
118
    public void testNoSuchChildExceptionConstructor() {
 
119
        XMLException ex = new NoSuchChildException(message, cause);
 
120
        assertEquals(message, ex.getMessage());
 
121
        assertEquals(cause, ex.getCause()); 
 
122
    }
 
123
    
 
124
    
 
125
    public void testCycleExceptionConstructor() {
 
126
        XMLException ex = new CycleException(message, cause);
 
127
        assertEquals(message, ex.getMessage());
 
128
        assertEquals(cause, ex.getCause()); 
 
129
    }
 
130
    
 
131
    
 
132
    public void testIllegalNameExceptionConstructor() {
 
133
        XMLException ex = new IllegalNameException(message, cause);
 
134
        assertEquals(message, ex.getMessage());
 
135
        assertEquals(cause, ex.getCause()); 
 
136
    }
 
137
    
 
138
    
 
139
    public void testIllegalTargetExceptionConstructor() {
 
140
        XMLException ex = new IllegalTargetException(message, cause);
 
141
        assertEquals(message, ex.getMessage());
 
142
        assertEquals(cause, ex.getCause()); 
 
143
    }
 
144
    
 
145
    
 
146
    public void testIllegalAddExceptionConstructor() {
 
147
        XMLException ex = new IllegalAddException(message, cause);
 
148
        assertEquals(message, ex.getMessage());
 
149
        assertEquals(cause, ex.getCause()); 
 
150
    }
 
151
    
 
152
    
 
153
    public void testIllegalDataExceptionConstructor() {
 
154
        XMLException ex = new IllegalDataException(message, cause);
 
155
        assertEquals(message, ex.getMessage());
 
156
        assertEquals(cause, ex.getCause()); 
 
157
    }
 
158
    
 
159
    public void testInitCause() {
 
160
        
 
161
        assertNull(ex.getCause());
 
162
        ex.initCause(cause);
 
163
        assertEquals(cause, ex.getCause());
 
164
        
 
165
        try {
 
166
            ex.initCause(null);   
 
167
            fail("Reinitialized cause over null");   
 
168
        }
 
169
        catch (IllegalStateException result) {
 
170
            // success   
 
171
        }
 
172
        
 
173
        try {
 
174
            ex.initCause(new Exception());   
 
175
            fail("Reinitialized cause over null");   
 
176
        }
 
177
        catch (IllegalStateException result) {
 
178
            // success   
 
179
        }
 
180
        
 
181
    }
 
182
 
 
183
 
 
184
    public void testNullInitCause() {
 
185
        
 
186
        ex = new XMLException(null, null);
 
187
        assertNull(ex.getCause());
 
188
        
 
189
        try {
 
190
            ex.initCause(new Exception());
 
191
            fail("Reinitialized cause over null");   
 
192
        }
 
193
        catch (IllegalStateException result) {
 
194
            // success   
 
195
        }
 
196
 
 
197
        try {
 
198
            ex.initCause(null);   
 
199
            fail("Reinitialized cause over null");   
 
200
        }
 
201
        catch (IllegalStateException result) {
 
202
            // success   
 
203
        }
 
204
        
 
205
    }
 
206
 
 
207
    
 
208
    public void testSelfCause() {
 
209
        
 
210
        try {
 
211
            ex.initCause(ex);   
 
212
            fail("Allowed self-causation");   
 
213
        }
 
214
        catch (IllegalArgumentException result) {
 
215
            // success   
 
216
        }
 
217
        
 
218
    }
 
219
 
 
220
    
 
221
    public void testGetMessage() {      
 
222
        Exception ex = new XMLException("testing");
 
223
        assertEquals("testing", ex.getMessage());
 
224
    }
 
225
 
 
226
    
 
227
}