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;
15  import org.xml.sax.SAXParseException;
16  import org.xml.sax.helpers.AttributesImpl;
17  
18  public class SAXConfigurationHandlerTestCase
19      extends TestCase
20  {
21      public void testGetLocationWithNullLocator()
22          throws Exception
23      {
24          final SAXConfigurationHandler handler = new SAXConfigurationHandler();
25          final String location = handler.getLocationDescription();
26          assertEquals( "location", "", location );
27      }
28  
29      public void testGetLocationWithNullSystemId()
30          throws Exception
31      {
32          final SAXConfigurationHandler handler = new SAXConfigurationHandler();
33          handler.setDocumentLocator( new MockLocator( null ) );
34          final String location = handler.getLocationDescription();
35          assertEquals( "location", "", location );
36      }
37  
38      public void testGetLocationWithNonNullSystemId()
39          throws Exception
40      {
41          final SAXConfigurationHandler handler = new SAXConfigurationHandler();
42          handler.setDocumentLocator( new MockLocator( "file.xml" ) );
43          final String location = handler.getLocationDescription();
44          assertEquals( "location", "file.xml", location );
45      }
46  
47      public void testGetLocationWithLineSet()
48          throws Exception
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 location = handler.getLocationDescription();
55          assertEquals( "location", "file.xml:23", location );
56      }
57  
58      public void testGetLocationWithColSet()
59          throws Exception
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 location = handler.getLocationDescription();
67          assertEquals( "location", "file.xml:23:15", location );
68      }
69  
70      public void testGetLocationWithColSetButLineNotSet()
71          throws Exception
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 location = handler.getLocationDescription();
78          assertEquals( "location", "file.xml", location );
79      }
80  
81      public void testWarningRethrowsException()
82          throws Exception
83      {
84          final SAXConfigurationHandler handler = new SAXConfigurationHandler();
85          final SAXParseException spe = new SAXParseException( "", null );
86          try
87          {
88              handler.warning( spe );
89          }
90          catch( final SAXException 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
100     {
101         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
102         final SAXParseException spe = new SAXParseException( "", null );
103         try
104         {
105             handler.error( spe );
106         }
107         catch( final SAXException 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
117     {
118         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
119         final SAXParseException spe = new SAXParseException( "", null );
120         try
121         {
122             handler.fatalError( spe );
123         }
124         catch( final SAXException 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
134     {
135         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
136         final String qName = "myElement";
137         handler.startElement( "", "", qName, new AttributesImpl() );
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
147     {
148         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
149         final String qName = "myElement";
150         final String value = "value";
151         handler.startElement( "", "", qName, new AttributesImpl() );
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
163     {
164         final SAXConfigurationHandler handler = new MockSAXConfigurationHandler();
165         final String qName = "myElement";
166         final String value = "value";
167         handler.startElement( "", "", qName, new AttributesImpl() );
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
180     {
181         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
182         final String qName = "myElement";
183         final String value = "value";
184         handler.startElement( "", "", qName, new AttributesImpl() );
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
199     {
200         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
201         final String qName = "myElement";
202         final String childName = "myChild";
203         handler.startElement( "", "", qName, new AttributesImpl() );
204         handler.startElement( "", "", childName, new AttributesImpl() );
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
221     {
222         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
223         final String qName = "myElement";
224         final String childName = "myChild";
225         final String grandChildName = "myGrandChild";
226         handler.startElement( "", "", qName, new AttributesImpl() );
227         handler.startElement( "", "", childName, new AttributesImpl() );
228         handler.startElement( "", "", grandChildName, new AttributesImpl() );
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
252     {
253         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
254         final String qName = "myElement";
255         final String childName = "myChild";
256         final String value = "value";
257         handler.startElement( "", "", qName, new AttributesImpl() );
258         handler.startElement( "", "", childName, new AttributesImpl() );
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
277     {
278         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
279         final String qName = "myElement";
280         final String childName = "myChild";
281         final String value = "value";
282         handler.startElement( "", "", qName, new AttributesImpl() );
283         handler.characters( value.toCharArray(), 0, value.length() );
284         handler.startElement( "", "", childName, new AttributesImpl() );
285         handler.endElement( "", "", childName );
286         try
287         {
288             handler.endElement( "", "", qName );
289         }
290         catch( SAXException 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
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
307     {
308         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
309         final String qName = "myElement";
310         final String value = "   \n   \t";
311         handler.startElement( "", "", qName, new AttributesImpl() );
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
323     {
324         final SAXConfigurationHandler handler = new SAXConfigurationHandler();
325         final String qName = "myElement";
326         final AttributesImpl attributes = new AttributesImpl();
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[] 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
343     {
344         final SAXConfigurationHandler handler = new MockSAXConfigurationHandler();
345         final String qName = "myElement";
346         final AttributesImpl attributes = new AttributesImpl();
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[] 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 }