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.List;
41 import java.util.Map;
42
43 import junitx.ddtunit.DDTException;
44 import junitx.ddtunit.data.TypedObject;
45
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class AttributeListCreatorAction extends ActionBase {
50 protected Logger log = LoggerFactory.getLogger(AttributeListCreatorAction.class);
51
52 public AttributeListCreatorAction(Map<String, String> attrMap) {
53 super(attrMap);
54 this.attrMap.put(ParserConstants.XML_ATTR_ID, "attrlist");
55 this.attrMap.put(ParserConstants.XML_ATTR_TYPE, "java.util.Vector");
56 this.attrMap.put(ParserConstants.XML_ATTR_HINT, "attrlist");
57 }
58
59
60
61
62
63
64 public IAction process() {
65 log.debug("process AttributeListCreator - START");
66 IAction rootAction = getPrevious();
67 if (rootAction == null) {
68 throw new DDTException(
69 "AttributeListCreatorAction must follow another action in action stack.");
70 }
71
72 String hintValue = rootAction.getHint();
73
74 if (HintTypes.INTERNAL_MAPENTRY.equals(hintValue)) {
75 rootAction.processSuccessor(this);
76 } else if (HintTypes.CONSTRUCTOR.equals(hintValue)
77 || HintTypes.CALL.equals(hintValue)) {
78 rootAction.processSuccessor(this);
79 } else if (HintTypes.FIELDS.equals(hintValue)) {
80 rootAction.processSuccessor(this);
81 } else if (HintTypes.BEAN.equals(hintValue)) {
82 rootAction.processSuccessor(this);
83 } else if (HintTypes.ARRAY.equals(hintValue)) {
84 rootAction.processSuccessor(this);
85 } else {
86 throw new UnsupportedOperationException(
87 "AttributeListCreatorAction does not support hint '"
88 + hintValue + "'");
89 }
90 pop();
91
92 if (rootAction.getPrevious() != null) {
93 rootAction = rootAction.process();
94 }
95 return rootAction;
96 }
97
98
99
100
101
102
103 public IAction inject() {
104 String type = (String) this.attrMap.get(ParserConstants.XML_ATTR_TYPE);
105 String id = (String) this.attrMap.get(ParserConstants.XML_ATTR_ID);
106
107 try {
108 if (type == null && ParserConstants.XML_ELEM_ITEM.equals(id)) {
109 type = TypedObject.UNKNOWN_TYPE;
110 } else if (HintTypes.ARRAY.equals(this.getHint())) {
111
112 } else {
113 Class clazz = Class.forName(type);
114 Class superclazz = clazz;
115 boolean found = false;
116 while (!found && superclazz != null) {
117 if ("java.util.Collection".equals(superclazz.getName())
118 || "java.util.AbstractCollection".equals(superclazz
119 .getName())) {
120 found = true;
121 }
122 superclazz = superclazz.getSuperclass();
123 }
124 if (!found) {
125 throw new DDTException("Container class '" + type
126 + "' does not implement java.util.Collection.");
127 }
128 }
129 } catch (Exception ex) {
130 throw new DDTException("Container class '" + type
131 + "' does not implement java.util.Collection.", ex);
132 }
133 this.injectedObject = new TypedObject(id, type);
134 return this;
135 }
136
137 public void processSuccessor(IAction successor) {
138 log.debug("processSuccessor(" + successor + ") - START");
139 ((List<TypedObject>) this.getValue()).add(successor.getObject());
140 }
141
142 }