001: /* $Id: DigesterLoaderTest.java 474932 2006-11-14 19:36:29Z rahul $
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.apache.commons.digester.xmlrules;
020:
021: import java.io.InputStream;
022: import java.io.StringReader;
023: import java.net.URL;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import org.apache.commons.digester.Address;
031: import org.apache.commons.digester.Digester;
032: import org.apache.commons.digester.TestObjectCreationFactory;
033:
034: import org.w3c.dom.Node;
035: import org.xml.sax.InputSource;
036:
037: /**
038: * Tests loading Digester rules from an XML file.
039: *
040: * @author David H. Martin - Initial Contribution
041: * @author Scott Sanders - Added ASL, removed external dependencies
042: */
043:
044: public class DigesterLoaderTest extends TestCase {
045:
046: public DigesterLoaderTest(java.lang.String testName) {
047: super (testName);
048: }
049:
050: public static void main(java.lang.String[] args) {
051: junit.textui.TestRunner.run(suite());
052: }
053:
054: public static junit.framework.Test suite() {
055: TestSuite suite = new TestSuite(DigesterLoaderTest.class);
056:
057: return suite;
058: }
059:
060: /**
061: * Tests the DigesterLoader.createDigester(), with multiple
062: * included rule sources: testrules.xml includes another rules xml
063: * file, and also includes programmatically created rules.
064: */
065: public void testCreateDigester() throws Exception {
066: URL rules = getClass().getClassLoader().getResource(
067: "org/apache/commons/digester/xmlrules/testrules.xml");
068: URL input = getClass().getClassLoader().getResource(
069: "org/apache/commons/digester/xmlrules/test.xml");
070: assertNotNull("The test could not locate testrules.xml", rules);
071: assertNotNull("The test could not locate test.xml", input);
072: Digester digester = DigesterLoader.createDigester(rules);
073: digester.push(new ArrayList());
074: Object root = digester.parse(input.openStream());
075: assertEquals("[foo1 baz1 foo2, foo3 foo4]", root.toString());
076: }
077:
078: /**
079: * Tests the DigesterLoader.load(), with multiple included rule
080: * sources: testrules.xml includes another rules xml file, and
081: * also includes programmatically created rules.
082: */
083: public void testLoad1() throws Exception {
084: ClassLoader classLoader = getClass().getClassLoader();
085: URL rules = classLoader
086: .getResource("org/apache/commons/digester/xmlrules/testrules.xml");
087: URL input = classLoader
088: .getResource("org/apache/commons/digester/xmlrules/test.xml");
089: assertNotNull("The test could not locate testrules.xml", rules);
090: assertNotNull("The test could not locate test.xml", input);
091: Object root = DigesterLoader.load(rules, classLoader, input,
092: new ArrayList());
093: if (!(root instanceof ArrayList)) {
094: fail("Unexpected object returned from DigesterLoader. Expected ArrayList; got "
095: + root.getClass().getName());
096: }
097: assertEquals("[foo1 baz1 foo2, foo3 foo4]", root.toString());
098:
099: ArrayList al = (ArrayList) root;
100: Object obj = al.get(0);
101: if (!(obj instanceof TestObject)) {
102: fail("Unexpected object returned from DigesterLoader. Expected TestObject; got "
103: + obj.getClass().getName());
104: }
105: TestObject to = (TestObject) obj;
106: assertEquals(new Long(555), to.getLongValue());
107: assertEquals("foo", to.getMapValue("test1"));
108: assertEquals("bar", to.getMapValue("test2"));
109: }
110:
111: /**
112: * The same as testLoad1, exception the input file is passed to
113: * DigesterLoader as an InputStream instead of a URL.
114: */
115: public void testLoad2() throws Exception {
116: URL rules = getClass().getClassLoader().getResource(
117: "org/apache/commons/digester/xmlrules/testrules.xml");
118: InputStream input = getClass().getClassLoader().getResource(
119: "org/apache/commons/digester/xmlrules/test.xml")
120: .openStream();
121: Object root = DigesterLoader.load(rules, getClass()
122: .getClassLoader(), input, new ArrayList());
123: if (!(root instanceof ArrayList)) {
124: fail("Unexpected object returned from DigesterLoader. Expected ArrayList; got "
125: + root.getClass().getName());
126: }
127: ArrayList list = (ArrayList) root;
128: assertEquals(root.toString(), "[foo1 baz1 foo2, foo3 foo4]");
129: assertEquals("Wrong number of classes created", 2, list.size());
130: assertEquals("Pushed first", true, ((TestObject) list.get(0))
131: .isPushed());
132: assertEquals("Didn't push second", false, ((TestObject) list
133: .get(1)).isPushed());
134: assertTrue("Property was set properly", ((TestObject) list
135: .get(0)).getProperty().equals("I am a property!"));
136: }
137:
138: /**
139: * Validates that circular includes are detected and result in an exception
140: */
141: public void testCircularInclude1() {
142: URL rules = ClassLoader
143: .getSystemResource("org/apache/commons/digester/xmlrules/testCircularRules.xml");
144: try {
145: Digester digester = DigesterLoader.createDigester(rules);
146: assertNotNull(digester); // just to prevent compiler warning on unused var
147: } catch (Exception ex) {
148: return;
149: }
150: fail("Creating a digester with circular rules should have thrown CircularIncludeException.");
151: }
152:
153: /**
154: */
155: public void testSetCustomProperties() throws Exception {
156: URL rules = getClass()
157: .getClassLoader()
158: .getResource(
159: "org/apache/commons/digester/xmlrules/testPropertyAliasRules.xml");
160: InputStream input = getClass().getClassLoader().getResource(
161: "org/apache/commons/digester/Test7.xml").openStream();
162:
163: Object obj = DigesterLoader.load(rules, getClass()
164: .getClassLoader(), input, new ArrayList());
165:
166: if (!(obj instanceof ArrayList)) {
167: fail("Unexpected object returned from DigesterLoader. Expected ArrayList; got "
168: + obj.getClass().getName());
169: }
170:
171: ArrayList root = (ArrayList) obj;
172:
173: assertEquals("Wrong array size", 4, root.size());
174:
175: // note that the array is in popped order (rather than pushed)
176:
177: obj = root.get(0);
178: assertTrue("(1) Should be an Address ", obj instanceof Address);
179: Address addressOne = (Address) obj;
180: assertEquals("(1) Street attribute", "New Street", addressOne
181: .getStreet());
182: assertEquals("(1) City attribute", "Las Vegas", addressOne
183: .getCity());
184: assertEquals("(1) State attribute", "Nevada", addressOne
185: .getState());
186:
187: obj = root.get(1);
188: assertTrue("(2) Should be an Address ", obj instanceof Address);
189: Address addressTwo = (Address) obj;
190: assertEquals("(2) Street attribute", "Old Street", addressTwo
191: .getStreet());
192: assertEquals("(2) City attribute", "Portland", addressTwo
193: .getCity());
194: assertEquals("(2) State attribute", "Oregon", addressTwo
195: .getState());
196:
197: obj = root.get(2);
198: assertTrue("(3) Should be an Address ", obj instanceof Address);
199: Address addressThree = (Address) obj;
200: assertEquals("(3) Street attribute", "4th Street", addressThree
201: .getStreet());
202: assertEquals("(3) City attribute", "Dayton", addressThree
203: .getCity());
204: assertEquals("(3) State attribute", "US", addressThree
205: .getState());
206:
207: obj = root.get(3);
208: assertTrue("(4) Should be an Address ", obj instanceof Address);
209: Address addressFour = (Address) obj;
210: assertEquals("(4) Street attribute", "6th Street", addressFour
211: .getStreet());
212: assertEquals("(4) City attribute", "Cleveland", addressFour
213: .getCity());
214: assertEquals("(4) State attribute", "Ohio", addressFour
215: .getState());
216:
217: }
218:
219: public void testFactoryCreateRule() throws Exception {
220: URL rules = getClass().getClassLoader().getResource(
221: "org/apache/commons/digester/xmlrules/testfactory.xml");
222:
223: String xml = "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><foo/></root>";
224: Object obj = DigesterLoader.load(rules, getClass()
225: .getClassLoader(), new StringReader(xml),
226: new ArrayList());
227:
228: if (!(obj instanceof ArrayList)) {
229: fail("Unexpected object returned from DigesterLoader. Expected ArrayList; got "
230: + obj.getClass().getName());
231: }
232:
233: ArrayList list = (ArrayList) obj;
234:
235: assertEquals("List should contain only the factory object",
236: list.size(), 1);
237: TestObjectCreationFactory factory = (TestObjectCreationFactory) list
238: .get(0);
239: assertEquals("Object create not called(1)", factory.called,
240: true);
241: assertEquals("Attribute not passed (1)", factory.attributes
242: .getValue("one"), "good");
243: assertEquals("Attribute not passed (2)", factory.attributes
244: .getValue("two"), "bad");
245: assertEquals("Attribute not passed (3)", factory.attributes
246: .getValue("three"), "ugly");
247:
248: rules = getClass()
249: .getClassLoader()
250: .getResource(
251: "org/apache/commons/digester/xmlrules/testfactoryignore.xml");
252:
253: xml = "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><foo/></root>";
254: try {
255: DigesterLoader.load(rules, getClass().getClassLoader(),
256: new StringReader(xml));
257: } catch (Exception e) {
258: fail("This exception should have been ignored: "
259: + e.getClass().getName());
260: }
261:
262: rules = getClass()
263: .getClassLoader()
264: .getResource(
265: "org/apache/commons/digester/xmlrules/testfactorynoignore.xml");
266:
267: xml = "<?xml version='1.0' ?><root one='good' two='bad' three='ugly'><foo/></root>";
268: try {
269: DigesterLoader.load(rules, getClass().getClassLoader(),
270: new StringReader(xml));
271: fail("Exception should have been propagated from create method.");
272: } catch (Exception e) {
273: /* What we expected */
274: assertEquals(org.xml.sax.SAXParseException.class, e
275: .getClass());
276: }
277: }
278:
279: public void testCallParamRule() throws Exception {
280:
281: URL rules = getClass()
282: .getClassLoader()
283: .getResource(
284: "org/apache/commons/digester/xmlrules/test-call-param-rules.xml");
285:
286: String xml = "<?xml version='1.0' ?>"
287: + "<root><foo attr='long'><bar>short</bar><foobar><ping>tosh</ping></foobar></foo></root>";
288:
289: CallParamTestObject testObject = new CallParamTestObject();
290:
291: DigesterLoader.load(rules, getClass().getClassLoader(),
292: new StringReader(xml), testObject);
293:
294: assertEquals("Incorrect left value", "long", testObject
295: .getLeft());
296: assertEquals("Incorrect middle value", "short", testObject
297: .getMiddle());
298: assertEquals("Incorrect right value", "", testObject.getRight());
299: }
300:
301: public void testInputSourceLoader() throws Exception {
302: String rulesXml = "<?xml version='1.0'?>"
303: + "<digester-rules>"
304: + " <pattern value='root'>"
305: + " <pattern value='foo'>"
306: + " <call-method-rule methodname='triple' paramcount='3'"
307: + " paramtypes='java.lang.String,java.lang.String,java.lang.String'/>"
308: + " <call-param-rule paramnumber='0' attrname='attr'/>"
309: + " <pattern value='bar'>"
310: + " <call-param-rule paramnumber='1' from-stack='false'/>"
311: + " </pattern>"
312: + " <pattern value='foobar'>"
313: + " <object-create-rule classname='java.lang.String'/>"
314: + " <pattern value='ping'>"
315: + " <call-param-rule paramnumber='2' from-stack='true'/>"
316: + " </pattern>" + " </pattern>"
317: + " </pattern>" + " </pattern>" + "</digester-rules>";
318:
319: String xml = "<?xml version='1.0' ?>"
320: + "<root><foo attr='long'><bar>short</bar><foobar><ping>tosh</ping></foobar></foo></root>";
321:
322: CallParamTestObject testObject = new CallParamTestObject();
323:
324: Digester digester = DigesterLoader
325: .createDigester(new InputSource(new StringReader(
326: rulesXml)));
327: digester.push(testObject);
328: digester.parse(new StringReader(xml));
329:
330: assertEquals("Incorrect left value", "long", testObject
331: .getLeft());
332: assertEquals("Incorrect middle value", "short", testObject
333: .getMiddle());
334: assertEquals("Incorrect right value", "", testObject.getRight());
335: }
336:
337: public void testNodeCreateRule() throws Exception {
338:
339: URL rules = getClass()
340: .getClassLoader()
341: .getResource(
342: "org/apache/commons/digester/xmlrules/test-node-create-rules.xml");
343: URL input = getClass()
344: .getClassLoader()
345: .getResource(
346: "org/apache/commons/digester/xmlrules/test-node-create-rules-input.xml");
347: assertNotNull(
348: "The test could not locate test-node-create-rules.xml",
349: rules);
350: assertNotNull(
351: "The test could not locate test-node-create-rules-input.xml",
352: input);
353: Digester digester = DigesterLoader.createDigester(rules);
354: digester.push(new ArrayList());
355: Object root = digester.parse(input.openStream());
356:
357: assertNotNull("root was null", root);
358: assertTrue("no nodes were captured.",
359: (((List) root).size() > 0));
360: Object[] nodeArray = (Object[]) ((List) root).toArray();
361: assertNotNull("resulting node array from array list was null",
362: nodeArray);
363:
364: // test foo1 structure
365: Node foo1 = (Node) nodeArray[0];
366: assertTrue("foo1 didn't have any children", foo1
367: .hasChildNodes());
368:
369: Node foo1Bar1 = foo1.getFirstChild();
370: assertTrue("foo1's child was not named bar1", "bar1"
371: .equals(foo1Bar1.getNodeName()));
372: assertTrue("foo1/bar1 value was not bar-1-value", "bar1-value"
373: .equals(foo1Bar1.getFirstChild().getNodeValue()));
374: }
375:
376: }
|