001: /*
002: * Copyright (C) The DNA Group. All rights reserved.
003: *
004: * This software is published under the terms of the DNA
005: * Software License version 1.1, a copy of which has been included
006: * with this distribution in the LICENSE.txt file.
007: */
008: package org.codehaus.dna.impl;
009:
010: import junit.framework.TestCase;
011:
012: import org.codehaus.dna.Configuration;
013: import org.codehaus.dna.impl.SAXConfigurationHandler;
014: import org.xml.sax.SAXException;
015: import org.xml.sax.SAXParseException;
016: import org.xml.sax.helpers.AttributesImpl;
017:
018: public class SAXConfigurationHandlerTestCase extends TestCase {
019: public void testGetLocationWithNullLocator() throws Exception {
020: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
021: final String location = handler.getLocationDescription();
022: assertEquals("location", "", location);
023: }
024:
025: public void testGetLocationWithNullSystemId() throws Exception {
026: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
027: handler.setDocumentLocator(new MockLocator(null));
028: final String location = handler.getLocationDescription();
029: assertEquals("location", "", location);
030: }
031:
032: public void testGetLocationWithNonNullSystemId() throws Exception {
033: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
034: handler.setDocumentLocator(new MockLocator("file.xml"));
035: final String location = handler.getLocationDescription();
036: assertEquals("location", "file.xml", location);
037: }
038:
039: public void testGetLocationWithLineSet() throws Exception {
040: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
041: final MockLocator locator = new MockLocator("file.xml");
042: locator.setLineNumber(23);
043: handler.setDocumentLocator(locator);
044: final String location = handler.getLocationDescription();
045: assertEquals("location", "file.xml:23", location);
046: }
047:
048: public void testGetLocationWithColSet() throws Exception {
049: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
050: final MockLocator locator = new MockLocator("file.xml");
051: locator.setLineNumber(23);
052: locator.setColumnNumber(15);
053: handler.setDocumentLocator(locator);
054: final String location = handler.getLocationDescription();
055: assertEquals("location", "file.xml:23:15", location);
056: }
057:
058: public void testGetLocationWithColSetButLineNotSet()
059: throws Exception {
060: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
061: final MockLocator locator = new MockLocator("file.xml");
062: locator.setColumnNumber(15);
063: handler.setDocumentLocator(locator);
064: final String location = handler.getLocationDescription();
065: assertEquals("location", "file.xml", location);
066: }
067:
068: public void testWarningRethrowsException() throws Exception {
069: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
070: final SAXParseException spe = new SAXParseException("", null);
071: try {
072: handler.warning(spe);
073: } catch (final SAXException se) {
074: assertEquals(spe, se);
075: return;
076: }
077: fail("Expected exception to be thrown");
078: }
079:
080: public void testErrorRethrowsException() throws Exception {
081: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
082: final SAXParseException spe = new SAXParseException("", null);
083: try {
084: handler.error(spe);
085: } catch (final SAXException se) {
086: assertEquals(spe, se);
087: return;
088: }
089: fail("Expected exception to be thrown");
090: }
091:
092: public void testFatalRethrowsException() throws Exception {
093: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
094: final SAXParseException spe = new SAXParseException("", null);
095: try {
096: handler.fatalError(spe);
097: } catch (final SAXException se) {
098: assertEquals(spe, se);
099: return;
100: }
101: fail("Expected exception to be thrown");
102: }
103:
104: public void testCreateSimpleConfiguration() throws Exception {
105: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
106: final String qName = "myElement";
107: handler.startElement("", "", qName, new AttributesImpl());
108: handler.endElement("", "", qName);
109: final Configuration configuration = handler.getConfiguration();
110: assertEquals("configuration.name", qName, configuration
111: .getName());
112: assertEquals("configuration.location", "", configuration
113: .getLocation());
114: assertEquals("configuration.path", "", configuration.getPath());
115: }
116:
117: public void testCreateConfigurationWithValue() throws Exception {
118: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
119: final String qName = "myElement";
120: final String value = "value";
121: handler.startElement("", "", qName, new AttributesImpl());
122: handler.characters(value.toCharArray(), 0, value.length());
123: handler.endElement("", "", qName);
124: final Configuration configuration = handler.getConfiguration();
125: assertEquals("configuration.name", qName, configuration
126: .getName());
127: assertEquals("configuration.location", "", configuration
128: .getLocation());
129: assertEquals("configuration.path", "", configuration.getPath());
130: assertEquals("configuration.value", value, configuration
131: .getValue());
132: }
133:
134: public void testCreateConfigurationWithValueThatIsIntercepted()
135: throws Exception {
136: final SAXConfigurationHandler handler = new MockSAXConfigurationHandler();
137: final String qName = "myElement";
138: final String value = "value";
139: handler.startElement("", "", qName, new AttributesImpl());
140: handler.characters(value.toCharArray(), 0, value.length());
141: handler.endElement("", "", qName);
142: final Configuration configuration = handler.getConfiguration();
143: assertEquals("configuration.name", qName, configuration
144: .getName());
145: assertEquals("configuration.location", "", configuration
146: .getLocation());
147: assertEquals("configuration.path", "", configuration.getPath());
148: assertEquals("configuration.value",
149: MockSAXConfigurationHandler.REPLACEMENT, configuration
150: .getValue());
151: }
152:
153: public void testCreateConfigurationWithValueInMultipleFragments()
154: throws Exception {
155: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
156: final String qName = "myElement";
157: final String value = "value";
158: handler.startElement("", "", qName, new AttributesImpl());
159: handler.characters(value.toCharArray(), 0, value.length());
160: handler.characters(value.toCharArray(), 0, value.length());
161: handler.endElement("", "", qName);
162: final Configuration configuration = handler.getConfiguration();
163: assertEquals("configuration.name", qName, configuration
164: .getName());
165: assertEquals("configuration.location", "", configuration
166: .getLocation());
167: assertEquals("configuration.path", "", configuration.getPath());
168: assertEquals("configuration.value", value + value,
169: configuration.getValue());
170: }
171:
172: public void testCreateConfigurationWithChildElement()
173: throws Exception {
174: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
175: final String qName = "myElement";
176: final String childName = "myChild";
177: handler.startElement("", "", qName, new AttributesImpl());
178: handler.startElement("", "", childName, new AttributesImpl());
179: handler.endElement("", "", childName);
180: handler.endElement("", "", qName);
181:
182: final Configuration configuration = handler.getConfiguration();
183: assertEquals("configuration.name", qName, configuration
184: .getName());
185: assertEquals("configuration.location", "", configuration
186: .getLocation());
187: assertEquals("configuration.path", "", configuration.getPath());
188: final Configuration[] children = configuration.getChildren();
189: assertEquals("children.length", 1, children.length);
190: assertEquals("children[ 0 ].name", childName, children[0]
191: .getName());
192: assertEquals("children[ 0 ].location", "", children[0]
193: .getLocation());
194: assertEquals("children[ 0 ].path", qName, children[0].getPath());
195: }
196:
197: public void testCreateConfigurationWithDeepChildElements()
198: throws Exception {
199: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
200: final String qName = "myElement";
201: final String childName = "myChild";
202: final String grandChildName = "myGrandChild";
203: handler.startElement("", "", qName, new AttributesImpl());
204: handler.startElement("", "", childName, new AttributesImpl());
205: handler.startElement("", "", grandChildName,
206: new AttributesImpl());
207: handler.endElement("", "", grandChildName);
208: handler.endElement("", "", childName);
209: handler.endElement("", "", qName);
210:
211: final Configuration configuration = handler.getConfiguration();
212: assertEquals("configuration.name", qName, configuration
213: .getName());
214: assertEquals("configuration.location", "", configuration
215: .getLocation());
216: assertEquals("configuration.path", "", configuration.getPath());
217: final Configuration[] children = configuration.getChildren();
218: assertEquals("children.length", 1, children.length);
219: assertEquals("children[ 0 ].name", childName, children[0]
220: .getName());
221: assertEquals("children[ 0 ].location", "", children[0]
222: .getLocation());
223: assertEquals("children[ 0 ].path", qName, children[0].getPath());
224: final Configuration[] grandChildren = children[0].getChildren();
225: assertEquals("grandChildren.length", 1, grandChildren.length);
226: assertEquals("grandChildren[ 0 ].name", grandChildName,
227: grandChildren[0].getName());
228: assertEquals("grandChildren[ 0 ].location", "",
229: grandChildren[0].getLocation());
230: assertEquals("grandChildren[ 0 ].path", "myElement/myChild",
231: grandChildren[0].getPath());
232: }
233:
234: public void testCreateConfigurationWithChildElementContaingContent()
235: throws Exception {
236: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
237: final String qName = "myElement";
238: final String childName = "myChild";
239: final String value = "value";
240: handler.startElement("", "", qName, new AttributesImpl());
241: handler.startElement("", "", childName, new AttributesImpl());
242: handler.characters(value.toCharArray(), 0, value.length());
243: handler.endElement("", "", childName);
244: handler.endElement("", "", qName);
245:
246: final Configuration configuration = handler.getConfiguration();
247: assertEquals("configuration.name", qName, configuration
248: .getName());
249: assertEquals("configuration.location", "", configuration
250: .getLocation());
251: assertEquals("configuration.path", "", configuration.getPath());
252: final Configuration[] children = configuration.getChildren();
253: assertEquals("children.length", 1, children.length);
254: assertEquals("children[ 0 ].name", childName, children[0]
255: .getName());
256: assertEquals("children[ 0 ].location", "", children[0]
257: .getLocation());
258: assertEquals("children[ 0 ].path", qName, children[0].getPath());
259: assertEquals("children[ 0 ].value", value, children[0]
260: .getValue());
261: }
262:
263: public void testCreateConfigurationWithMixedContent()
264: throws Exception {
265: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
266: final String qName = "myElement";
267: final String childName = "myChild";
268: final String value = "value";
269: handler.startElement("", "", qName, new AttributesImpl());
270: handler.characters(value.toCharArray(), 0, value.length());
271: handler.startElement("", "", childName, new AttributesImpl());
272: handler.endElement("", "", childName);
273: try {
274: handler.endElement("", "", qName);
275: } catch (SAXException e) {
276: return;
277: }
278: fail("Expected to fail handling sax events as mixed content");
279: }
280:
281: public void testClearHandler() throws Exception {
282: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
283: //TODO: This is a really bad unit test - should test internal state
284: handler.clear();
285: }
286:
287: public void testCreateConfigurationContainingEmptySeparator()
288: throws Exception {
289: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
290: final String qName = "myElement";
291: final String value = " \n \t";
292: handler.startElement("", "", qName, new AttributesImpl());
293: handler.characters(value.toCharArray(), 0, value.length());
294: handler.endElement("", "", qName);
295: final Configuration configuration = handler.getConfiguration();
296: assertEquals("configuration.name", qName, configuration
297: .getName());
298: assertEquals("configuration.location", "", configuration
299: .getLocation());
300: assertEquals("configuration.path", "", configuration.getPath());
301: assertEquals("configuration.value", null, configuration
302: .getValue(null));
303: }
304:
305: public void testCreateConfigurationWithAttributes()
306: throws Exception {
307: final SAXConfigurationHandler handler = new SAXConfigurationHandler();
308: final String qName = "myElement";
309: final AttributesImpl attributes = new AttributesImpl();
310: attributes.addAttribute("", "", "key", "CDATA", "value");
311: handler.startElement("", "", qName, attributes);
312: handler.endElement("", "", qName);
313: final Configuration configuration = handler.getConfiguration();
314: assertEquals("configuration.name", qName, configuration
315: .getName());
316: assertEquals("configuration.location", "", configuration
317: .getLocation());
318: assertEquals("configuration.path", "", configuration.getPath());
319: final String[] names = configuration.getAttributeNames();
320: assertEquals("names.length", 1, names.length);
321: assertEquals("names[0]", "key", names[0]);
322: assertEquals("configuration.getAttribute( names[ 0 ] )",
323: "value", configuration.getAttribute(names[0]));
324: }
325:
326: public void testCreateConfigurationWithAttributesWithInterception()
327: throws Exception {
328: final SAXConfigurationHandler handler = new MockSAXConfigurationHandler();
329: final String qName = "myElement";
330: final AttributesImpl attributes = new AttributesImpl();
331: attributes.addAttribute("", "", "key", "CDATA", "value");
332: handler.startElement("", "", qName, attributes);
333: handler.endElement("", "", qName);
334: final Configuration configuration = handler.getConfiguration();
335: assertEquals("configuration.name", qName, configuration
336: .getName());
337: assertEquals("configuration.location", "", configuration
338: .getLocation());
339: assertEquals("configuration.path", "", configuration.getPath());
340: final String[] names = configuration.getAttributeNames();
341: assertEquals("names.length", 1, names.length);
342: assertEquals("names[0]", "key", names[0]);
343: assertEquals("configuration.getAttribute( names[ 0 ] )",
344: MockSAXConfigurationHandler.REPLACEMENT, configuration
345: .getAttribute(names[0]));
346: }
347: }
|