1 //$Id: ContentCreatorAction.java 378 2011-05-25 17:03:31Z jg_hamburg $
2 /********************************************************************************
3 * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
4 * Copyright (c) 2004, Joerg and Kai Gellien
5 * All rights reserved.
6 *
7 * The Software is provided under the terms of the Common Public License 1.0
8 * as provided with the distribution of DDTUnit in the file cpl-v10.html.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * + Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * + Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 *
21 * + Neither the id of the authors or DDTUnit, nor the
22 * names of its contributors may be used to endorse or promote
23 * products derived from this software without specific prior
24 * written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 ********************************************************************************/
38 package junitx.ddtunit.data.processing;
39
40 import java.util.Map;
41
42 import junitx.ddtunit.DDTException;
43 import junitx.ddtunit.data.DDTTestDataException;
44 import junitx.ddtunit.data.TypedObject;
45
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50 * This class contains object state and other information to create object from
51 * SAX event stream.
52 * @author jg
53 */
54 public class ContentCreatorAction extends ActionBase {
55 private Logger log = LoggerFactory.getLogger(ContentCreatorAction.class);
56
57 /**
58 * Constant representing null assignment in xml resource
59 */
60 public final static String CONTENT_NULL = "!NULL!";
61
62 /**
63 * Constant representation of empty string value in xml resource
64 */
65 public final static String CONTENT_EMPTY = "!EMPTY!";
66
67 /**
68 * Content representing string value of one space
69 */
70 public final static String CONTENT_BLANK = "!BLANK!";
71
72 /**
73 * Constructor used as standard constructor to instanciate actions of this
74 * type
75 * @param attrMap
76 */
77 public ContentCreatorAction(Map<String, String> attrMap) {
78 super(attrMap);
79 }
80
81 /**
82 * Contract constraints on processing of Content action: <br/>
83 * <ul>
84 * <li>Content action must be the last on action stack</li>
85 * <li>There must be a valid root action distinct from content action.</li>
86 * <li> </li>
87 * </ul>
88 * @see junitx.ddtunit.data.processing.ActionBase#process()
89 */
90 public IAction process() {
91 log.debug("Process ContentCreator - START");
92 // check if action is last on stack - if not throw error
93 if (this.getNext() != null) {
94 throw new DDTException(
95 "Contract error - Content action must be last on stack");
96 }
97 IAction rootAction = this.getPrevious();
98 if (this == rootAction) {
99 throw new DDTTestDataException(
100 "Contract error - there must be at least one valid root action");
101 }
102 // check if rootAction is ContentCreatorAction as well - then concat
103 // content
104 IAction actionBase = null;
105 String hintValue = rootAction.getHint();
106 if (this.getValue() != null) {
107 boolean cdataProcessing = "true".equals((String) this.attrMap
108 .get(ParserConstants.XML_ATTR_PICDATA));
109 if (HintTypes.CONTENT.equals(hintValue)) {
110 rootAction.processSuccessor(this);
111 } else if (HintTypes.CONSTANT.equals(hintValue)
112 || HintTypes.DATE.equals(hintValue)
113 || HintTypes.ARRAY.equals(hintValue)
114 || HintTypes.FIELDS.equals(hintValue)
115 || HintTypes.COLLECTION.equals(hintValue)
116 || HintTypes.MAP.equals(hintValue)
117 || HintTypes.BEAN.equals(hintValue)) {
118 if (!cdataProcessing) {
119 // check for !BLANK! and replace it
120 StringBuffer sb = new StringBuffer(this.getValue().toString().trim());
121 int start = -1;
122 while ((start = sb.indexOf(CONTENT_BLANK)) != -1) {
123 sb.replace(start, start + CONTENT_BLANK.length(), " ");
124 }
125 this.setValue(sb);
126 }
127 rootAction.processSuccessor(this);
128 } else {
129 throw new UnsupportedOperationException(
130 "ContentCreatorAction does not support hint '" + hintValue + "'");
131 }
132
133 }
134 this.pop();
135 actionBase = rootAction.process();
136 return actionBase;
137 }
138 /*
139 * (non-Javadoc)
140 * @see junitx.ddtunit.parser.ActionBase#inject()
141 */
142 public IAction inject() {
143 String type = (String) this.attrMap.get(ParserConstants.XML_ATTR_TYPE);
144 String id = (String) this.attrMap.get(ParserConstants.XML_ATTR_ID);
145 this.injectedObject = new TypedObject(id, type);
146 this.injectedObject.setValue(this.attrMap
147 .get(ParserConstants.XML_ATTR_CONTENT));
148 this.attrMap.remove(ParserConstants.XML_ATTR_CONTENT);
149 return this;
150 }
151
152 public void processSuccessor(IAction successor) {
153 log.debug("processSuccessor(" + successor + ") - START");
154 // create attribute list action and insert after rootAction
155 if (HintTypes.CONTENT.equals(successor.getHint())) {
156 StringBuffer value = new StringBuffer().append(this.getValue());
157 value.append(successor.getValue());
158 this.setValue(value);
159 } else {
160 throw new DDTTestDataException("Unsupported successor action");
161 }
162 }
163 }