KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > dna > impl > SAXConfigurationHandlerTestCase


1 /*
2  * Copyright (C) The DNA Group. All rights reserved.
3  *
4  * This software is published under the terms of the DNA
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.dna.impl;
9
10 import junit.framework.TestCase;
11
12 import org.codehaus.dna.Configuration;
13 import org.codehaus.dna.impl.SAXConfigurationHandler;
14 import org.xml.sax.SAXException JavaDoc;
15 import org.xml.sax.SAXParseException JavaDoc;
16 import org.xml.sax.helpers.AttributesImpl JavaDoc;
17
18 public class SAXConfigurationHandlerTestCase
19     extends TestCase
20 {
21     public void testGetLocationWithNullLocator()
22         throws Exception JavaDoc
23     {
24         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
25         final String JavaDoc location = handler.getLocationDescription();
26         assertEquals( "location", "", location );
27     }
28
29     public void testGetLocationWithNullSystemId()
30         throws Exception JavaDoc
31     {
32         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
33         handler.setDocumentLocator( new MockLocator( null ) );
34         final String JavaDoc location = handler.getLocationDescription();
35         assertEquals( "location", "", location );
36     }
37
38     public void testGetLocationWithNonNullSystemId()
39         throws Exception JavaDoc
40     {
41         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
42         handler.setDocumentLocator( new MockLocator( "file.xml" ) );
43         final String JavaDoc location = handler.getLocationDescription();
44         assertEquals( "location", "file.xml", location );
45     }
46
47     public void testGetLocationWithLineSet()
48         throws Exception JavaDoc
49     {
50         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
51         final MockLocator locator = new MockLocator( "file.xml" );
52         locator.setLineNumber( 23 );
53         handler.setDocumentLocator( locator );
54         final String JavaDoc location = handler.getLocationDescription();
55         assertEquals( "location", "file.xml:23", location );
56     }
57
58     public void testGetLocationWithColSet()
59         throws Exception JavaDoc
60     {
61         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
62         final MockLocator locator = new MockLocator( "file.xml" );
63         locator.setLineNumber( 23 );
64         locator.setColumnNumber( 15 );
65         handler.setDocumentLocator( locator );
66         final String JavaDoc location = handler.getLocationDescription();
67         assertEquals( "location", "file.xml:23:15", location );
68     }
69
70     public void testGetLocationWithColSetButLineNotSet()
71         throws Exception JavaDoc
72     {
73         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
74         final MockLocator locator = new MockLocator( "file.xml" );
75         locator.setColumnNumber( 15 );
76         handler.setDocumentLocator( locator );
77         final String JavaDoc location = handler.getLocationDescription();
78         assertEquals( "location", "file.xml", location );
79     }
80
81     public void testWarningRethrowsException()
82         throws Exception JavaDoc
83     {
84         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
85         final SAXParseException JavaDoc spe = new SAXParseException JavaDoc( "", null );
86         try
87         {
88             handler.warning( spe );
89         }
90         catch( final SAXException JavaDoc se )
91         {
92             assertEquals( spe, se );
93             return;
94         }
95         fail( "Expected exception to be thrown" );
96     }
97
98     public void testErrorRethrowsException()
99         throws Exception JavaDoc
100     {
101         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
102         final SAXParseException JavaDoc spe = new SAXParseException JavaDoc( "", null );
103         try
104         {
105             handler.error( spe );
106         }
107         catch( final SAXException JavaDoc se )
108         {
109             assertEquals( spe, se );
110             return;
111         }
112         fail( "Expected exception to be thrown" );
113     }
114
115     public void testFatalRethrowsException()
116         throws Exception JavaDoc
117     {
118         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
119         final SAXParseException JavaDoc spe = new SAXParseException JavaDoc( "", null );
120         try
121         {
122             handler.fatalError( spe );
123         }
124         catch( final SAXException JavaDoc se )
125         {
126             assertEquals( spe, se );
127             return;
128         }
129         fail( "Expected exception to be thrown" );
130     }
131
132     public void testCreateSimpleConfiguration()
133         throws Exception JavaDoc
134     {
135         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
136         final String JavaDoc qName = "myElement";
137         handler.startElement( "", "", qName, new AttributesImpl JavaDoc() );
138         handler.endElement( "", "", qName );
139         final Configuration configuration = handler.getConfiguration();
140         assertEquals( "configuration.name", qName, configuration.getName() );
141         assertEquals( "configuration.location", "", configuration.getLocation() );
142         assertEquals( "configuration.path", "", configuration.getPath() );
143     }
144
145     public void testCreateConfigurationWithValue()
146         throws Exception JavaDoc
147     {
148         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
149         final String JavaDoc qName = "myElement";
150         final String JavaDoc value = "value";
151         handler.startElement( "", "", qName, new AttributesImpl JavaDoc() );
152         handler.characters( value.toCharArray(), 0, value.length() );
153         handler.endElement( "", "", qName );
154         final Configuration configuration = handler.getConfiguration();
155         assertEquals( "configuration.name", qName, configuration.getName() );
156         assertEquals( "configuration.location", "", configuration.getLocation() );
157         assertEquals( "configuration.path", "", configuration.getPath() );
158         assertEquals( "configuration.value", value, configuration.getValue() );
159     }
160
161     public void testCreateConfigurationWithValueThatIsIntercepted()
162         throws Exception JavaDoc
163     {
164         final SAXConfigurationHandler handler = new MockSAXConfigurationHandler();
165         final String JavaDoc qName = "myElement";
166         final String JavaDoc value = "value";
167         handler.startElement( "", "", qName, new AttributesImpl JavaDoc() );
168         handler.characters( value.toCharArray(), 0, value.length() );
169         handler.endElement( "", "", qName );
170         final Configuration configuration = handler.getConfiguration();
171         assertEquals( "configuration.name", qName, configuration.getName() );
172         assertEquals( "configuration.location", "", configuration.getLocation() );
173         assertEquals( "configuration.path", "", configuration.getPath() );
174         assertEquals( "configuration.value", MockSAXConfigurationHandler.REPLACEMENT,
175                       configuration.getValue() );
176     }
177
178     public void testCreateConfigurationWithValueInMultipleFragments()
179         throws Exception JavaDoc
180     {
181         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
182         final String JavaDoc qName = "myElement";
183         final String JavaDoc value = "value";
184         handler.startElement( "", "", qName, new AttributesImpl JavaDoc() );
185         handler.characters( value.toCharArray(), 0, value.length() );
186         handler.characters( value.toCharArray(), 0, value.length() );
187         handler.endElement( "", "", qName );
188         final Configuration configuration = handler.getConfiguration();
189         assertEquals( "configuration.name", qName, configuration.getName() );
190         assertEquals( "configuration.location", "", configuration.getLocation() );
191         assertEquals( "configuration.path", "", configuration.getPath() );
192         assertEquals( "configuration.value",
193                       value + value,
194                       configuration.getValue() );
195     }
196
197     public void testCreateConfigurationWithChildElement()
198         throws Exception JavaDoc
199     {
200         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
201         final String JavaDoc qName = "myElement";
202         final String JavaDoc childName = "myChild";
203         handler.startElement( "", "", qName, new AttributesImpl JavaDoc() );
204         handler.startElement( "", "", childName, new AttributesImpl JavaDoc() );
205         handler.endElement( "", "", childName );
206         handler.endElement( "", "", qName );
207
208         final Configuration configuration = handler.getConfiguration();
209         assertEquals( "configuration.name", qName, configuration.getName() );
210         assertEquals( "configuration.location", "", configuration.getLocation() );
211         assertEquals( "configuration.path", "", configuration.getPath() );
212         final Configuration[] children = configuration.getChildren();
213         assertEquals( "children.length", 1, children.length );
214         assertEquals( "children[ 0 ].name", childName, children[ 0 ].getName() );
215         assertEquals( "children[ 0 ].location", "", children[ 0 ].getLocation() );
216         assertEquals( "children[ 0 ].path", qName, children[ 0 ].getPath() );
217     }
218
219     public void testCreateConfigurationWithDeepChildElements()
220         throws Exception JavaDoc
221     {
222         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
223         final String JavaDoc qName = "myElement";
224         final String JavaDoc childName = "myChild";
225         final String JavaDoc grandChildName = "myGrandChild";
226         handler.startElement( "", "", qName, new AttributesImpl JavaDoc() );
227         handler.startElement( "", "", childName, new AttributesImpl JavaDoc() );
228         handler.startElement( "", "", grandChildName, new AttributesImpl JavaDoc() );
229         handler.endElement( "", "", grandChildName );
230         handler.endElement( "", "", childName );
231         handler.endElement( "", "", qName );
232
233         final Configuration configuration = handler.getConfiguration();
234         assertEquals( "configuration.name", qName, configuration.getName() );
235         assertEquals( "configuration.location", "", configuration.getLocation() );
236         assertEquals( "configuration.path", "", configuration.getPath() );
237         final Configuration[] children = configuration.getChildren();
238         assertEquals( "children.length", 1, children.length );
239         assertEquals( "children[ 0 ].name", childName, children[ 0 ].getName() );
240         assertEquals( "children[ 0 ].location", "", children[ 0 ].getLocation() );
241         assertEquals( "children[ 0 ].path", qName, children[ 0 ].getPath() );
242         final Configuration[] grandChildren = children[ 0 ].getChildren();
243         assertEquals( "grandChildren.length", 1, grandChildren.length );
244         assertEquals( "grandChildren[ 0 ].name", grandChildName, grandChildren[ 0 ].getName() );
245         assertEquals( "grandChildren[ 0 ].location", "", grandChildren[ 0 ].getLocation() );
246         assertEquals( "grandChildren[ 0 ].path", "myElement/myChild", grandChildren[ 0 ].getPath() );
247     }
248
249
250     public void testCreateConfigurationWithChildElementContaingContent()
251         throws Exception JavaDoc
252     {
253         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
254         final String JavaDoc qName = "myElement";
255         final String JavaDoc childName = "myChild";
256         final String JavaDoc value = "value";
257         handler.startElement( "", "", qName, new AttributesImpl JavaDoc() );
258         handler.startElement( "", "", childName, new AttributesImpl JavaDoc() );
259         handler.characters( value.toCharArray(), 0, value.length() );
260         handler.endElement( "", "", childName );
261         handler.endElement( "", "", qName );
262
263         final Configuration configuration = handler.getConfiguration();
264         assertEquals( "configuration.name", qName, configuration.getName() );
265         assertEquals( "configuration.location", "", configuration.getLocation() );
266         assertEquals( "configuration.path", "", configuration.getPath() );
267         final Configuration[] children = configuration.getChildren();
268         assertEquals( "children.length", 1, children.length );
269         assertEquals( "children[ 0 ].name", childName, children[ 0 ].getName() );
270         assertEquals( "children[ 0 ].location", "", children[ 0 ].getLocation() );
271         assertEquals( "children[ 0 ].path", qName, children[ 0 ].getPath() );
272         assertEquals( "children[ 0 ].value", value, children[ 0 ].getValue() );
273     }
274
275     public void testCreateConfigurationWithMixedContent()
276         throws Exception JavaDoc
277     {
278         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
279         final String JavaDoc qName = "myElement";
280         final String JavaDoc childName = "myChild";
281         final String JavaDoc value = "value";
282         handler.startElement( "", "", qName, new AttributesImpl JavaDoc() );
283         handler.characters( value.toCharArray(), 0, value.length() );
284         handler.startElement( "", "", childName, new AttributesImpl JavaDoc() );
285         handler.endElement( "", "", childName );
286         try
287         {
288             handler.endElement( "", "", qName );
289         }
290         catch( SAXException JavaDoc e )
291         {
292             return;
293         }
294         fail( "Expected to fail handling sax events as mixed content" );
295     }
296
297     public void testClearHandler()
298         throws Exception JavaDoc
299     {
300         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
301         //TODO: This is a really bad unit test - should test internal state
302
handler.clear();
303     }
304
305     public void testCreateConfigurationContainingEmptySeparator()
306         throws Exception JavaDoc
307     {
308         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
309         final String JavaDoc qName = "myElement";
310         final String JavaDoc value = " \n \t";
311         handler.startElement( "", "", qName, new AttributesImpl JavaDoc() );
312         handler.characters( value.toCharArray(), 0, value.length() );
313         handler.endElement( "", "", qName );
314         final Configuration configuration = handler.getConfiguration();
315         assertEquals( "configuration.name", qName, configuration.getName() );
316         assertEquals( "configuration.location", "", configuration.getLocation() );
317         assertEquals( "configuration.path", "", configuration.getPath() );
318         assertEquals( "configuration.value", null, configuration.getValue( null ) );
319     }
320
321     public void testCreateConfigurationWithAttributes()
322         throws Exception JavaDoc
323     {
324         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
325         final String JavaDoc qName = "myElement";
326         final AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
327         attributes.addAttribute( "", "", "key", "CDATA", "value" );
328         handler.startElement( "", "", qName, attributes );
329         handler.endElement( "", "", qName );
330         final Configuration configuration = handler.getConfiguration();
331         assertEquals( "configuration.name", qName, configuration.getName() );
332         assertEquals( "configuration.location", "", configuration.getLocation() );
333         assertEquals( "configuration.path", "", configuration.getPath() );
334         final String JavaDoc[] names = configuration.getAttributeNames();
335         assertEquals( "names.length", 1, names.length );
336         assertEquals( "names[0]", "key", names[ 0 ] );
337         assertEquals( "configuration.getAttribute( names[ 0 ] )",
338                       "value", configuration.getAttribute( names[ 0 ] ) );
339     }
340
341     public void testCreateConfigurationWithAttributesWithInterception()
342         throws Exception JavaDoc
343     {
344         final SAXConfigurationHandler handler = new MockSAXConfigurationHandler();
345         final String JavaDoc qName = "myElement";
346         final AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
347         attributes.addAttribute( "", "", "key", "CDATA", "value" );
348         handler.startElement( "", "", qName, attributes );
349         handler.endElement( "", "", qName );
350         final Configuration configuration = handler.getConfiguration();
351         assertEquals( "configuration.name", qName, configuration.getName() );
352         assertEquals( "configuration.location", "", configuration.getLocation() );
353         assertEquals( "configuration.path", "", configuration.getPath() );
354         final String JavaDoc[] names = configuration.getAttributeNames();
355         assertEquals( "names.length", 1, names.length );
356         assertEquals( "names[0]", "key", names[ 0 ] );
357         assertEquals( "configuration.getAttribute( names[ 0 ] )",
358                       MockSAXConfigurationHandler.REPLACEMENT,
359                       configuration.getAttribute( names[ 0 ] ) );
360     }
361 }
362
Popular Tags