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.Method;
41 import java.util.Collection;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Vector;
46
47 import junitx.ddtunit.DDTException;
48 import junitx.ddtunit.data.DDTTestDataException;
49 import junitx.ddtunit.data.TypedObject;
50 import junitx.ddtunit.util.ClassAnalyser;
51
52
53
54
55
56
57
58 public class BeanCreatorAction extends ActionBase {
59
60
61
62
63
64
65
66 public BeanCreatorAction(Map attrMap) {
67 super(attrMap);
68 }
69
70
71
72
73
74
75 public IAction process() {
76 log.debug("process BeanCreator - START");
77 IAction rootAction = this.getPrevious();
78 if (!this.successorProcessed) {
79 processNoSuccessor();
80 }
81 if (rootAction != null) {
82 String hintValue = rootAction.getHint();
83
84 if (HintTypes.COLLECTION.equals(hintValue)) {
85 rootAction.processSuccessor(this);
86 } else if (HintTypes.MAP.equals(hintValue)) {
87 rootAction.processSuccessor(this);
88 } else if (HintTypes.ATTRLIST.equals(hintValue)) {
89 rootAction.processSuccessor(this);
90 } else if (HintTypes.FIELDS.equals(hintValue)) {
91 rootAction.processSuccessor(this);
92 } else if (HintTypes.CONSTRUCTOR.equals(hintValue)
93 || HintTypes.CALL.equals(hintValue)) {
94 rootAction.processSuccessor(this);
95 } else if (HintTypes.BEAN.equals(hintValue)) {
96 rootAction.processSuccessor(this);
97 } else if (HintTypes.ARRAY.equals(hintValue)) {
98 rootAction.processSuccessor(this);
99 } else if (HintTypes.INTERNAL_MAPENTRY.equals(hintValue)) {
100 rootAction.processSuccessor(this);
101 } else {
102 throw new DDTException("Unknown hint (" + hintValue
103 + ")- stop processing.");
104 }
105 }
106 this.pop();
107 return this;
108 }
109
110
111
112
113
114 public void processNoSuccessor() {
115
116 IAction action = new AttributeListCreatorAction(new HashMap());
117 action.inject();
118 action.setValue(new Vector());
119 this.insert(action);
120 action.process();
121 }
122
123
124
125
126
127
128 public IAction inject() {
129 String type = (String) this.attrMap.get(ParserConstants.XML_ATTR_TYPE);
130 String id = (String) this.attrMap.get(ParserConstants.XML_ATTR_ID);
131 this.injectedObject = new TypedObject(id, type);
132 return this;
133 }
134
135 public void processSuccessor(IAction successor) {
136 log.debug("processSuccessor(" + successor + ") - START");
137
138 if (HintTypes.ATTRLIST.equals(successor.getHint())) {
139 List fields = (List) successor.getObject().getValue();
140 TypedObject field = null;
141 StringBuffer setter = new StringBuffer();
142 try {
143
144
145 this.getType();
146 if (this.getValue() == null) {
147 this.createObject();
148 }
149 for (int count = 0; count < fields.size(); count++) {
150 field = (TypedObject) fields.get(count);
151
152 setter.replace(0, setter.length(), "set");
153 setter.append(field.getId().substring(0, 1).toUpperCase())
154 .append(field.getId().substring(1));
155 Class fieldClazz = null;
156 if (field.getValue() != null) {
157 fieldClazz = field.getValue().getClass();
158 Class[] args = new Class[1];
159 args[0] = fieldClazz;
160 Method setMethod = (Method) ClassAnalyser
161 .findMethodByParams(this.getType(), setter
162 .toString(), args);
163 setMethod.setAccessible(true);
164 Object[] objs = new Object[1];
165 objs[0] = field.getValue();
166 setMethod.invoke(this.getValue(), objs);
167 }
168 }
169 } catch (Exception ex) {
170 StringBuffer sb = new StringBuffer("Error executing setter ")
171 .append(this.getType())
172 .append(".")
173 .append(setter)
174 .append("(...). ")
175 .append(
176 "Check if hint and setter are correct (Do not provide set-prefix!).");
177 throw new DDTException(sb.toString(), ex);
178 }
179 } else if (HintTypes.COLLECTION.equals(successor.getHint())) {
180
181 Collection param = (Collection) successor.getObject().getValue();
182 TypedObject field = successor.getObject();
183 StringBuffer setter = new StringBuffer();
184 try {
185
186
187 this.getType();
188 if (this.getValue() == null) {
189 this.createObject();
190 }
191
192 setter.replace(0, setter.length(), "set");
193 setter.append(field.getId().substring(0, 1).toUpperCase())
194 .append(field.getId().substring(1));
195 Class fieldClazz = field.getValue().getClass();
196 Class[] args = new Class[1];
197 args[0] = fieldClazz;
198 Method setMethod = (Method) ClassAnalyser.findMethodByParams(
199 this.getType(), setter.toString(), args);
200 setMethod.setAccessible(true);
201 Object[] objs = new Object[1];
202 objs[0] = field.getValue();
203 setMethod.invoke(this.getValue(), objs);
204 } catch (Exception ex) {
205 StringBuffer sb = new StringBuffer("Error executing setter ")
206 .append(this.getType())
207 .append(".")
208 .append(setter)
209 .append("(...). ")
210 .append(
211 "Check if hint and setter are correct (Do not provide set-prefix!).");
212 throw new DDTException(sb.toString(), ex);
213 }
214 } else if (HintTypes.CONTENT.equals(successor.getHint())) {
215 if (!ContentCreatorAction.CONTENT_NULL.equals(successor.getValue()
216 .toString())) {
217 throw new DDTTestDataException(
218 "Unsupported content for BeanCreator:"
219 + successor.getValue().toString());
220 }
221 } else {
222
223 Map attribMap = new HashMap();
224 IAction attribListAction = ActionFactory.getAction(
225 ActionState.ATTRLIST_CREATION, attribMap);
226 this.insert(attribListAction);
227 try {
228
229 attribListAction.createObject();
230
231 ((List) attribListAction.getValue()).add(successor.getObject());
232 } catch (Exception ex) {
233 throw new DDTException("Error on action processing", ex);
234 }
235 }
236 this.successorProcessed = true;
237 }
238
239 }