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.lang.reflect.Array;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44
45 import junitx.ddtunit.DDTException;
46 import junitx.ddtunit.data.DDTTestDataException;
47 import junitx.ddtunit.data.TypedObject;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56
57
58 public class ArrayCreatorAction extends ActionBase {
59 protected Logger log = LoggerFactory.getLogger(ArrayCreatorAction.class);
60
61
62
63
64
65
66
67
68 public ArrayCreatorAction(Map<String, String> attrMap) {
69 super(attrMap);
70 }
71
72
73
74
75
76
77 public IAction process() {
78 log.debug("process - process new signature for call...");
79 if (!this.successorProcessed) {
80 processNoSuccessor();
81 }
82 IAction rootAction = getPrevious();
83 if (rootAction != null) {
84 String hintValue = rootAction.getHint();
85
86 if (HintTypes.INTERNAL_MAPENTRY.equals(hintValue)
87 || HintTypes.FIELDS.equals(hintValue)
88 || HintTypes.COLLECTION.equals(hintValue)
89 || HintTypes.ATTRLIST.equals(hintValue)) {
90 rootAction.processSuccessor(this);
91 } else {
92 throw new UnsupportedOperationException(
93 "CollectionCreatorAction does not support hint '"
94 + hintValue + "'");
95 }
96 } else {
97 rootAction = this;
98 }
99
100
101 pop();
102
103 return rootAction;
104 }
105
106 public void processSuccessor(IAction successor) {
107 log.debug("processSuccessor(" + successor + " - START");
108 if (successor != null) {
109 if (HintTypes.FIELDS.equals(successor.getHint())
110 || HintTypes.COLLECTION.equals(successor.getHint())) {
111 Map attribMap = new HashMap();
112 IAction attribListAction = ActionFactory.getAction(
113 ActionState.ATTRLIST_CREATION, attribMap);
114 this.insert(attribListAction);
115 try {
116 attribListAction.createObject();
117
118 ((List) attribListAction.getValue()).add(successor
119 .getObject());
120 } catch (Exception ex) {
121 throw new DDTException("Error on action processing", ex);
122 }
123 } else if (HintTypes.CONTENT.equals(successor.getHint())) {
124 String value = successor.getValue().toString();
125 if (!ContentCreatorAction.CONTENT_NULL.equals(value)) {
126 try {
127 int size = Integer.parseInt(value);
128 instanciateArray(size);
129 } catch (NumberFormatException ex) {
130 throw new DDTTestDataException(
131 "Must provide an integer as size of array.");
132 } catch (Exception ex) {
133
134 ex.printStackTrace();
135 }
136 }
137 } else {
138 List fields = (List) successor.getObject().getValue();
139 TypedObject field = null;
140 try {
141 instanciateArray(fields.size());
142 for (int count = 0; count < fields.size(); count++) {
143 field = (TypedObject) fields.get(count);
144
145 ((Object[]) this.getValue())[count] = field.getValue();
146 }
147 } catch (Exception ex) {
148 StringBuffer sb = new StringBuffer(
149 "Error on setting elements of array.");
150 throw new DDTException(sb.toString(), ex);
151 }
152 }
153 if (!getType().startsWith("[L")) {
154 this.setType(getType());
155 }
156 this.successorProcessed = true;
157 }
158 }
159
160
161
162
163
164
165 private void instanciateArray(int size) throws ClassNotFoundException {
166 if (this.getValue() == null) {
167 String localType = null;
168 if (getType().startsWith("[L")) {
169
170 localType = getType().substring(2, getType().length() - 1);
171 } else {
172 localType = getType();
173 }
174 Class clazz = Class.forName(localType);
175 Object arrayObj = Array.newInstance(clazz, size);
176 this.setValue(arrayObj);
177 }
178 }
179
180 public void processNoSuccessor() {
181 try {
182 instanciateArray(1);
183 if (getType() != null && !getType().startsWith("[L")) {
184 setType(getType());
185 }
186 } catch (ClassNotFoundException ex) {
187 StringBuffer sb = new StringBuffer(
188 "Error on setting elements of array.");
189 throw new DDTException(sb.toString(), ex);
190 }
191 }
192
193 public void setType(String type) {
194 if (this.injectedObject == null) {
195 inject();
196 }
197 if (type != null && !type.startsWith("[L")) {
198 this.injectedObject.setType("[L" + type + ";");
199 }
200 }
201
202
203
204
205
206
207
208 public IAction inject() {
209 String type = (String) this.attrMap.get(ParserConstants.XML_ATTR_TYPE);
210 String id = (String) this.attrMap.get(ParserConstants.XML_ATTR_ID);
211
212 try {
213 if (type == null && ParserConstants.XML_ELEM_ITEM.equals(id)) {
214 type = TypedObject.UNKNOWN_TYPE;
215 }
216 } catch (Exception ex) {
217 throw new DDTException("Container class '" + type
218 + "' does not implement java.util.Collection.", ex);
219 }
220 this.injectedObject = new TypedObject(id, type);
221 return this;
222 }
223
224 }