001: //$Id: ParserTest.java 218 2006-03-02 23:23:29Z jg_hamburg $
002: /********************************************************************************
003: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
004: * Copyright (c) 2004, Joerg and Kai Gellien
005: * All rights reserved.
006: *
007: * The Software is provided under the terms of the Common Public License 1.0
008: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * + Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * + Redistributions in binary form must reproduce the above
017: * copyright notice, this list of conditions and the following
018: * disclaimer in the documentation and/or other materials provided
019: * with the distribution.
020: *
021: * + Neither the name of the authors or DDTUnit, nor the
022: * names of its contributors may be used to endorse or promote
023: * products derived from this software without specific prior
024: * written permission.
025: *
026: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
027: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
031: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
032: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
033: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: ********************************************************************************/package junitx.ddtunit.data.processing.parser;
038:
039: import junit.framework.TestCase;
040: import junitx.ddtunit.data.TypedObject;
041: import junitx.ddtunit.data.processing.IParser;
042: import junitx.ddtunit.resources.SimpleVO;
043:
044: import org.apache.log4j.BasicConfigurator;
045:
046: /**
047: * Read object definition from xml resource and generate single object instance.
048: *
049: * @author jg
050: */
051: public class ParserTest extends TestCase {
052: private String expectedString = "Hallo obj5";
053:
054: private int expectedInt = 4711;
055:
056: private String objDef = "<obj id=\"myObj\" type=\"string\">"
057: + expectedString + "</obj>";
058:
059: private String obj2Def = "<obj id=\"myObj\" type=\"junitx.ddtunit.resources.SimpleVO\">"
060: + "<stringValue>"
061: + expectedString
062: + "</stringValue>"
063: + "</obj>";
064:
065: private String obj3Def = "<obj id=\"myObj\" baseid=\"obj5\" "
066: + "type=\"junitx.ddtunit.resources.SimpleVO\">"
067: + "<integerValue>" + expectedInt + "</integerValue>"
068: + "</obj>";
069:
070: private static final String XML_HEADER = "<?xml version=\"1.0\"?>";
071:
072: public ParserTest(String name) {
073: super (name);
074: BasicConfigurator.configure();
075: }
076:
077: /*
078: * Read in object definition as defined in xml resource for a single
079: * <obj> definition and instanciate appropriate object.
080: */
081: public void testParseElementFromStringBufferAutoHeader() {
082: IParser parser = new ParserImpl();
083: TypedObject myObj = parser.parseElement(objDef, true);
084: assertEquals("Wrong object generated", expectedString, myObj
085: .getValue());
086: }
087:
088: /*
089: * Read in object definition as defined in xml resource for a single
090: * <obj> definition and instanciate appropriate object.
091: */
092: public void testParseElementFromStringBufferPredefinedHeader() {
093: IParser parser = new ParserImpl();
094: TypedObject myObj = parser.parseElement(obj2Def, true);
095: assertNotNull("Generated object should not be null", myObj);
096: assertNotNull("Generated value should not be null", myObj
097: .getValue());
098: SimpleVO simple = (SimpleVO) myObj.getValue();
099: assertEquals("Wrong object generated", expectedString, simple
100: .getStringValue());
101: }
102:
103: /**
104: * Provide an object instance that is used as base for object creation.<br/>
105: * The idea is like cloning and extending but without using Clonable.
106: *
107: * @throws Exception
108: */
109: public void testParseElementWithBaseObject() throws Exception {
110: IParser parser = new ParserImpl();
111: TypedObject baseObj = parser.parseElement(obj2Def, true);
112: TypedObject extendedObj = parser.parseElement(obj3Def, true);
113: assertNotNull("Generated object should not be null", baseObj);
114: assertNotNull("Generated value should not be null", baseObj
115: .getValue());
116: SimpleVO simple = (SimpleVO) extendedObj.getValue();
117: assertEquals("Wrong object generated", expectedString, simple
118: .getStringValue());
119: assertEquals("Wrong object generated",
120: new Integer(expectedInt), simple.getIntegerValue());
121: }
122: }
|