001: //$Id: ContentCreatorAction.java 251 2006-09-05 20:17:42Z 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.util.Map;
040:
041: import junitx.ddtunit.DDTException;
042: import junitx.ddtunit.data.DDTTestDataException;
043: import junitx.ddtunit.data.TypedObject;
044:
045: import org.apache.log4j.Logger;
046:
047: /**
048: * This class contains object state and other information to create object from
049: * SAX event stream.
050: *
051: * @author jg
052: */
053: public class ContentCreatorAction extends ActionBase {
054: private Logger log = Logger.getLogger(ContentCreatorAction.class);
055:
056: /**
057: * Constant representing null assignment in xml resource
058: */
059: public final static String CONTENT_NULL = "!NULL!";
060:
061: /**
062: * Constant representation of empty string value in xml resource
063: */
064: public final static String CONTENT_EMPTY = "!EMPTY!";
065:
066: /**
067: * Content representing string value of one space
068: */
069: public final static String CONTENT_BLANK = "!BLANK!";
070:
071: /**
072: *
073: * Constructor used as standard constructor to instanciate actions of this
074: * type
075: *
076: * @param attrMap
077: */
078: public ContentCreatorAction(Map<String, String> attrMap) {
079: super (attrMap);
080: }
081:
082: /**
083: * Contract constraints on processing of Content action: <br/>
084: * <ul>
085: * <li>Content action must be the last on action stack</li>
086: * <li>There must be a valid root action distinct from content action.
087: * </li>
088: * <li> </li>
089: * </ul>
090: *
091: * @see junitx.ddtunit.data.processing.ActionBase#process()
092: */
093: public IAction process() {
094: log.debug("Process ContentCreator - START");
095: // check if action is last on stack - if not throw error
096: if (this .getNext() != null) {
097: throw new DDTException(
098: "Contract error - Content action must be last on stack");
099: }
100: IAction rootAction = this .getPrevious();
101: if (this == rootAction) {
102: throw new DDTTestDataException(
103: "Contract error - there must be at least one valid root action");
104: }
105: // check if rootAction is ContentCreatorAction as well - then concat
106: // content
107: IAction actionBase = null;
108: String hintValue = rootAction.getHint();
109: if (this .getValue() != null) {
110: boolean cdataProcessing = "true"
111: .equals((String) this .attrMap
112: .get(ParserConstants.XML_ATTR_PICDATA));
113: if (HintTypes.CONTENT.equals(hintValue)) {
114: rootAction.processSuccessor(this );
115: } else if (HintTypes.CONSTANT.equals(hintValue)
116: || HintTypes.DATE.equals(hintValue)
117: || HintTypes.ARRAY.equals(hintValue)
118: || HintTypes.FIELDS.equals(hintValue)
119: || HintTypes.COLLECTION.equals(hintValue)
120: || HintTypes.MAP.equals(hintValue)
121: || HintTypes.BEAN.equals(hintValue)) {
122: if (!cdataProcessing) {
123: this .setValue(new StringBuffer(this .getValue()
124: .toString().trim()));
125: }
126: rootAction.processSuccessor(this );
127: } else {
128: throw new UnsupportedOperationException(
129: "ContentCreatorAction does not support hint '"
130: + hintValue + "'");
131: }
132:
133: }
134: this .pop();
135: actionBase = rootAction.process();
136: return actionBase;
137: }
138:
139: /*
140: * (non-Javadoc)
141: *
142: * @see junitx.ddtunit.parser.ActionBase#inject()
143: */
144: public IAction inject() {
145: String type = (String) this .attrMap
146: .get(ParserConstants.XML_ATTR_TYPE);
147: String id = (String) this .attrMap
148: .get(ParserConstants.XML_ATTR_ID);
149: this .injectedObject = new TypedObject(id, type);
150: this .injectedObject.setValue(this .attrMap
151: .get(ParserConstants.XML_ATTR_CONTENT));
152: this .attrMap.remove(ParserConstants.XML_ATTR_CONTENT);
153: return this ;
154: }
155:
156: public void processSuccessor(IAction successor) {
157: log.debug("processSuccessor(" + successor + ") - START");
158: // create attribute list action and insert after rootAction
159: if (HintTypes.CONTENT.equals(successor.getHint())) {
160: StringBuffer value = new StringBuffer().append(this
161: .getValue());
162: value.append(successor.getValue());
163: this .setValue(value);
164: } else {
165: throw new DDTTestDataException(
166: "Unsupported successor action");
167: }
168: }
169: }
|