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 java.lang.reflect.Proxy;
011: import junit.framework.TestCase;
012:
013: import org.codehaus.dna.impl.DefaultConfiguration;
014: import org.codehaus.dna.impl.SAXConfigurationSerializer;
015: import org.xml.sax.ContentHandler;
016: import org.xml.sax.helpers.AttributesImpl;
017:
018: public class SAXConfigurationSerializerTestCase extends TestCase {
019: public void testSerializeZeroLengthAttributes() throws Exception {
020: final DefaultConfiguration configuration = new DefaultConfiguration(
021: "element", "", "");
022: final SAXConfigurationSerializer serializer = new SAXConfigurationSerializer();
023: final AttributesImpl attributes = serializer
024: .serializeAttributes(configuration);
025: assertEquals("attributes.getLength()", 0, attributes
026: .getLength());
027: }
028:
029: public void testSerializeAttributes() throws Exception {
030: final DefaultConfiguration configuration = new DefaultConfiguration(
031: "element", "", "");
032: final String name = "key";
033: final String value = "value";
034: configuration.setAttribute(name, value);
035: final SAXConfigurationSerializer serializer = new SAXConfigurationSerializer();
036: final AttributesImpl attributes = serializer
037: .serializeAttributes(configuration);
038: assertEquals("attributes.getLength()", 1, attributes
039: .getLength());
040: assertEquals("attributes.getLocalName(0)", name, attributes
041: .getLocalName(0));
042: assertEquals("attributes.getQName(0)", name, attributes
043: .getQName(0));
044: assertEquals("attributes.getURI(0)", "", attributes.getURI(0));
045: assertEquals("attributes.getType(0)", "CDATA", attributes
046: .getType(0));
047: assertEquals("attributes.getType(0)", value, attributes
048: .getValue(0));
049: }
050:
051: public void testSerializeElementWithNoContentOrChildren()
052: throws Exception {
053: final String name = "element";
054: final DefaultConfiguration configuration = new DefaultConfiguration(
055: name, "", "");
056: final SAXConfigurationSerializer serializer = new MockSAXConfigurationSerializer();
057:
058: final MockInvocationRecorder recorder = new MockInvocationRecorder();
059: recorder.addInvocation(SAXMethods.START_ELEMENT, new Object[] {
060: "", name, name,
061: MockSAXConfigurationSerializer.ATTRIBUTES }, null);
062: recorder.addInvocation(SAXMethods.END_ELEMENT, new Object[] {
063: "", name, name }, null);
064:
065: final ContentHandler handler = (ContentHandler) Proxy
066: .newProxyInstance(getClass().getClassLoader(),
067: new Class[] { ContentHandler.class }, recorder);
068:
069: serializer.serializeElement(configuration, handler);
070: }
071:
072: public void testSerializeElementWithValue() throws Exception {
073: final String name = "element";
074: final String value = "value";
075: final DefaultConfiguration configuration = new DefaultConfiguration(
076: name, "", "");
077: configuration.setValue(value);
078: final SAXConfigurationSerializer serializer = new MockSAXConfigurationSerializer();
079:
080: final MockInvocationRecorder recorder = new MockInvocationRecorder();
081: recorder.addInvocation(SAXMethods.START_ELEMENT, new Object[] {
082: "", name, name,
083: MockSAXConfigurationSerializer.ATTRIBUTES }, null);
084: recorder.addInvocation(SAXMethods.CHARACTERS, new Object[] {
085: value.toCharArray(), new Integer(0), new Integer(0) },
086: null);
087: recorder.addInvocation(SAXMethods.END_ELEMENT, new Object[] {
088: "", name, name }, null);
089:
090: final ContentHandler handler = (ContentHandler) Proxy
091: .newProxyInstance(getClass().getClassLoader(),
092: new Class[] { ContentHandler.class }, recorder);
093:
094: serializer.serializeElement(configuration, handler);
095: }
096:
097: public void testSerializeElementWithChild() throws Exception {
098: final String name = "element";
099: final String childName = "child";
100: final DefaultConfiguration configuration = new DefaultConfiguration(
101: name, "", "");
102: final DefaultConfiguration child = new DefaultConfiguration(
103: childName, "", "");
104: configuration.addChild(child);
105: final SAXConfigurationSerializer serializer = new MockSAXConfigurationSerializer();
106:
107: final MockInvocationRecorder recorder = new MockInvocationRecorder();
108: recorder.addInvocation(SAXMethods.START_ELEMENT, new Object[] {
109: "", name, name,
110: MockSAXConfigurationSerializer.ATTRIBUTES }, null);
111: recorder.addInvocation(SAXMethods.START_ELEMENT, new Object[] {
112: "", childName, childName,
113: MockSAXConfigurationSerializer.ATTRIBUTES }, null);
114: recorder.addInvocation(SAXMethods.END_ELEMENT, new Object[] {
115: "", childName, childName }, null);
116: recorder.addInvocation(SAXMethods.END_ELEMENT, new Object[] {
117: "", name, name }, null);
118:
119: final ContentHandler handler = (ContentHandler) Proxy
120: .newProxyInstance(getClass().getClassLoader(),
121: new Class[] { ContentHandler.class }, recorder);
122:
123: serializer.serializeElement(configuration, handler);
124: }
125:
126: public void testSerializeElementWithChildAndContent()
127: throws Exception {
128: final String name = "element";
129: final String childName = "child";
130: final String value = "text";
131: final DefaultConfiguration configuration = new DefaultConfiguration(
132: name, "", "");
133: final DefaultConfiguration child = new DefaultConfiguration(
134: childName, "", "");
135: configuration.addChild(child);
136: child.setValue(value);
137: final SAXConfigurationSerializer serializer = new MockSAXConfigurationSerializer();
138:
139: final MockInvocationRecorder recorder = new MockInvocationRecorder();
140: recorder.addInvocation(SAXMethods.START_ELEMENT, new Object[] {
141: "", name, name,
142: MockSAXConfigurationSerializer.ATTRIBUTES }, null);
143: recorder.addInvocation(SAXMethods.START_ELEMENT, new Object[] {
144: "", childName, childName,
145: MockSAXConfigurationSerializer.ATTRIBUTES }, null);
146: recorder.addInvocation(SAXMethods.CHARACTERS, new Object[] {
147: value.toCharArray(), new Integer(0), new Integer(0) },
148: null);
149: recorder.addInvocation(SAXMethods.END_ELEMENT, new Object[] {
150: "", childName, childName }, null);
151: recorder.addInvocation(SAXMethods.END_ELEMENT, new Object[] {
152: "", name, name }, null);
153:
154: final ContentHandler handler = (ContentHandler) Proxy
155: .newProxyInstance(getClass().getClassLoader(),
156: new Class[] { ContentHandler.class }, recorder);
157:
158: serializer.serializeElement(configuration, handler);
159: }
160:
161: public void testSerializeElementAsPartOfDocument() throws Exception {
162: final String name = "element";
163: final DefaultConfiguration configuration = new DefaultConfiguration(
164: name, "", "");
165: final SAXConfigurationSerializer serializer = new MockSAXConfigurationSerializer();
166:
167: final MockInvocationRecorder recorder = new MockInvocationRecorder();
168: recorder.addInvocation(SAXMethods.START_DOCUMENT,
169: new Object[0], null);
170: recorder.addInvocation(SAXMethods.START_ELEMENT, new Object[] {
171: "", name, name,
172: MockSAXConfigurationSerializer.ATTRIBUTES }, null);
173: recorder.addInvocation(SAXMethods.END_ELEMENT, new Object[] {
174: "", name, name }, null);
175: recorder.addInvocation(SAXMethods.END_DOCUMENT, new Object[0],
176: null);
177:
178: final ContentHandler handler = (ContentHandler) Proxy
179: .newProxyInstance(getClass().getClassLoader(),
180: new Class[] { ContentHandler.class }, recorder);
181:
182: serializer.serialize(configuration, handler);
183: }
184: }
|