001: //$Id: ConstantCreatorAction.java 282 2007-07-19 22:46:27Z 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 id 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;
038:
039: import java.lang.reflect.Field;
040: import java.lang.reflect.Modifier;
041: import java.util.Map;
042:
043: import junitx.ddtunit.DDTException;
044: import junitx.ddtunit.data.DDTTestDataException;
045: import junitx.ddtunit.data.TypedObject;
046: import junitx.ddtunit.util.ClassAnalyser;
047:
048: /**
049: * This class contains object state and other information to create object from
050: * SAX event stream.
051: *
052: * @author jg
053: */
054: public class ConstantCreatorAction extends ActionBase {
055: /**
056: *
057: * Constructor used as standard constructor to instanciate actions of this
058: * type
059: *
060: * @param attrMap
061: */
062: public ConstantCreatorAction(Map<String, String> attrMap) {
063: super (attrMap);
064: }
065:
066: /*
067: * (non-Javadoc)
068: *
069: * @see junitx.ddtunit.parser.ActionBase#process()
070: */
071: public IAction process() {
072: log.debug("process ConstantCreator - START");
073: IAction rootAction = this .getPrevious();
074: if (rootAction != null) {
075: String hintValue = rootAction.getHint();
076:
077: if (HintTypes.COLLECTION.equals(hintValue)
078: || HintTypes.MAP.equals(hintValue)
079: || HintTypes.ATTRLIST.equals(hintValue)
080: || HintTypes.FIELDS.equals(hintValue)
081: || HintTypes.CONSTRUCTOR.equals(hintValue)
082: || HintTypes.CALL.equals(hintValue)
083: || HintTypes.BEAN.equals(hintValue)
084: || HintTypes.ARRAY.equals(hintValue)
085: || HintTypes.INTERNAL_MAPENTRY.equals(hintValue)) {
086: rootAction.processSuccessor(this );
087: } else {
088: throw new DDTException("Unknown hint (" + hintValue
089: + ")- stop processing.");
090: }
091: } else {
092: if (hasReferenceInfo()) {
093: TypedObject destObject;
094: if (this .attrMap.get(ParserConstants.XML_ATTR_TYPE) == null) {
095: destObject = new TypedObject(getAttribute("refid"));
096: } else {
097: destObject = new TypedObject(getAttribute("refid"),
098: getType());
099: }
100: IReferenceInfo refInfo = new ObjectReferenceInfo(this
101: .getObject(), destObject);
102: add(refInfo);
103: }
104: rootAction = this ;
105: }
106: this .pop();
107: return this ;
108: }
109:
110: /*
111: * (non-Javadoc)
112: *
113: * @see junitx.ddtunit.parser.ActionBase#inject()
114: */
115: public IAction inject() {
116: String type = (String) this .attrMap
117: .get(ParserConstants.XML_ATTR_TYPE);
118: String id = (String) this .attrMap
119: .get(ParserConstants.XML_ATTR_ID);
120: this .injectedObject = new TypedObject(id, type);
121: return this ;
122: }
123:
124: public void processSuccessor(IAction successor) {
125: log.debug("processSuccessor(" + successor + ") - START");
126: // create attribute list action and insert after rootAction
127: if (HintTypes.CONTENT.equals(successor.getHint())) {
128: try {
129: Field field = ClassAnalyser.getSelectedField(this
130: .getType(), successor.getValue().toString());
131: if (((Modifier.STATIC & field.getModifiers()) == Modifier.STATIC)
132: // or java 5 enumeration is found
133: // Modifier.ENUM=16384
134: || ((16384 & field.getModifiers()) == 16384)) {
135: field.setAccessible(true);
136: Object obj = field.get(null);
137: this .setValue(obj);
138: this .setType(field.getType().getName());
139: } else {
140: throw new DDTTestDataException(
141: "Constant field must be defined static: "
142: + this .getType() + "."
143: + successor.getValue().toString());
144: }
145: } catch (Exception ex) {
146: throw new DDTTestDataException(
147: "Error on creation of constant field: "
148: + this .getType() + "."
149: + successor.getValue().toString(), ex);
150: }
151:
152: } else {
153: throw new DDTTestDataException(
154: "Unsupported successor action");
155: }
156: }
157:
158: }
|