1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
51
52
53
54 public class ContentCreatorAction extends ActionBase {
55 private Logger log = LoggerFactory.getLogger(ContentCreatorAction.class);
56
57
58
59
60 public final static String CONTENT_NULL = "!NULL!";
61
62
63
64
65 public final static String CONTENT_EMPTY = "!EMPTY!";
66
67
68
69
70 public final static String CONTENT_BLANK = "!BLANK!";
71
72
73
74
75
76
77 public ContentCreatorAction(Map<String, String> attrMap) {
78 super(attrMap);
79 }
80
81
82
83
84
85
86
87
88
89
90 public IAction process() {
91 log.debug("Process ContentCreator - START");
92
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
103
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
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
140
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
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 }