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.Field;
41 import java.lang.reflect.Modifier;
42 import java.util.Map;
43
44 import junitx.ddtunit.DDTException;
45 import junitx.ddtunit.data.DDTTestDataException;
46 import junitx.ddtunit.data.TypedObject;
47 import junitx.ddtunit.util.ClassAnalyser;
48
49
50
51
52
53
54
55 public class ConstantCreatorAction extends ActionBase {
56
57
58
59
60
61
62
63 public ConstantCreatorAction(Map<String, String> attrMap) {
64 super(attrMap);
65 }
66
67
68
69
70
71
72 public IAction process() {
73 log.debug("process ConstantCreator - START");
74 IAction rootAction = this.getPrevious();
75 if (rootAction != null) {
76 String hintValue = rootAction.getHint();
77
78 if (HintTypes.COLLECTION.equals(hintValue)
79 || HintTypes.MAP.equals(hintValue)
80 || HintTypes.ATTRLIST.equals(hintValue)
81 || HintTypes.FIELDS.equals(hintValue)
82 || HintTypes.CONSTRUCTOR.equals(hintValue)
83 || HintTypes.CALL.equals(hintValue)
84 || HintTypes.BEAN.equals(hintValue)
85 || HintTypes.ARRAY.equals(hintValue)
86 || HintTypes.INTERNAL_MAPENTRY.equals(hintValue)) {
87 rootAction.processSuccessor(this);
88 } else {
89 throw new DDTException("Unknown hint (" + hintValue
90 + ")- stop processing.");
91 }
92 } else {
93 if (hasReferenceInfo()) {
94 TypedObject destObject;
95 if (this.attrMap.get(ParserConstants.XML_ATTR_TYPE) == null) {
96 destObject = new TypedObject(getAttribute("refid"));
97 } else {
98 destObject = new TypedObject(getAttribute("refid"),
99 getType());
100 }
101 IReferenceInfo refInfo = new ObjectReferenceInfo(this
102 .getObject(), destObject);
103 add(refInfo);
104 }
105 rootAction = this;
106 }
107 this.pop();
108 return this;
109 }
110
111
112
113
114
115
116 public IAction inject() {
117 String type = (String) this.attrMap.get(ParserConstants.XML_ATTR_TYPE);
118 String id = (String) this.attrMap.get(ParserConstants.XML_ATTR_ID);
119 this.injectedObject = new TypedObject(id, type);
120 return this;
121 }
122
123 public void processSuccessor(IAction successor) {
124 log.debug("processSuccessor(" + successor + ") - START");
125
126 if (HintTypes.CONTENT.equals(successor.getHint())) {
127 try {
128 Field field = ClassAnalyser.getSelectedField(this.getType(),
129 successor.getValue().toString());
130 if (((Modifier.STATIC & field.getModifiers()) == Modifier.STATIC)
131
132
133 || ((16384 & field.getModifiers()) == 16384)) {
134 field.setAccessible(true);
135 Object obj = field.get(null);
136 this.setValue(obj);
137 this.setType(field.getType().getName());
138 } else {
139 throw new DDTTestDataException(
140 "Constant field must be defined static: "
141 + this.getType() + "."
142 + successor.getValue().toString());
143 }
144 } catch (Exception ex) {
145 throw new DDTTestDataException(
146 "Error on creation of constant field: "
147 + this.getType() + "."
148 + successor.getValue().toString(), ex);
149 }
150
151 } else {
152 throw new DDTTestDataException("Unsupported successor action");
153 }
154 }
155
156 }