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;
39
40 import java.util.Iterator;
41 import java.util.Map;
42 import java.util.Set;
43
44 import junitx.ddtunit.data.db.DbDataRestorer;
45 import junitx.ddtunit.util.DDTConfiguration;
46 import junitx.ddtunit.util.SoftHashMap;
47
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52
53
54
55
56
57 public class DDTDataRepository extends DataSet {
58
59 private static DDTDataRepository repository;
60
61 private Map clusterDataSets;
62
63 private Logger log = LoggerFactory.getLogger(DDTDataRepository.class);
64
65
66
67
68
69
70
71
72 private DDTDataRepository(int hardCacheSize) {
73 super("DDTUnitDataRepository", null);
74 this.clusterDataSets = new SoftHashMap(hardCacheSize);
75 }
76
77
78
79
80 public static DDTDataRepository getInstance() {
81 if (repository == null) {
82 repository = new DDTDataRepository(DDTConfiguration.getInstance()
83 .getHardCacheSize());
84 }
85 return repository;
86 }
87
88
89
90
91
92
93
94
95 public boolean containsKey(String classId) {
96 boolean contains = false;
97 contains = this.clusterDataSets.containsKey(classId);
98 log.debug("containsKey(" + classId + ")=" + contains);
99 return contains;
100 }
101
102
103
104
105
106
107
108 public void put(String classId, IDataSet dataSet) {
109 this.clusterDataSets.put(classId, dataSet);
110 log.debug("put(" + classId + ") - size " + this.size());
111 }
112
113
114
115
116
117
118
119
120
121
122 public TestClusterDataSet get(String resource, String clusterId) {
123 if (!containsKey(clusterId)) {
124 restore(resource, clusterId);
125 }
126 return (TestClusterDataSet) this.clusterDataSets.get(clusterId);
127 }
128
129
130
131
132
133
134
135
136
137
138 public IDataSet restore(String resource, String clusterId) {
139 try {
140
141 IDataSet dataSet = null;
142 if (resource.startsWith("/db:")) {
143 IDataSetSerializer dbRetriever = new DbDataRestorer();
144 dataSet = dbRetriever.restore(resource, clusterId);
145 } else {
146 IDataSetSerializer xmlRetriever = new XmlDataRestorer(this);
147 dataSet = xmlRetriever.restore(resource, clusterId);
148 }
149 put(clusterId, dataSet);
150 log.debug("restore(" + resource + ", " + clusterId + ")");
151 return dataSet;
152 } catch (DDTTestDataException ex) {
153 throw ex;
154 } catch (Throwable ex) {
155 throw new DDTTestDataException(ex.getMessage(), ex);
156 }
157 }
158
159
160
161
162
163
164
165
166 @SuppressWarnings("rawtypes")
167 static public Object find(IDataSet dataSet, SearchCriteria searchedData) {
168 String methodName = searchedData.getMethodName();
169 assert (methodName != null);
170 TestClusterDataSet classDataSet = (TestClusterDataSet) dataSet;
171
172 if (classDataSet.get(methodName) == null) {
173 throw new RuntimeException("Metoden " + methodName + " finns inte i -DDT.xml!!");
174 } else if (classDataSet.size(methodName) == 0) {
175 throw new RuntimeException("Metoden " + methodName + " saknar testdata (0 definitioner)!");
176 } else {
177 Iterator testIterator = classDataSet.getTestEntries(methodName);
178
179 while (testIterator != null && testIterator.hasNext()) {
180 Map.Entry testEntry = (Map.Entry) testIterator.next();
181 TestDataSet testDataSet = (TestDataSet) testEntry.getValue();
182 boolean found = false;
183 for (Iterator<SearchParameter> iter = searchedData.getIterator(); iter.hasNext();) {
184 SearchParameter parameter = iter.next();
185 String testDataId = parameter.getTestDataId();
186 TypedObject testDataObject = testDataSet.getObject(testDataId);
187 if (testDataObject == null) {
188 throw new IllegalArgumentException("Testdatat saknar object med id=\"" + testDataId + "\"!");
189 }
190 Object testDataValue = testDataObject.getValue();
191 @SuppressWarnings("unchecked")
192 int compare = parameter.getComparator().compare(parameter.getSearchedObject(), testDataValue);
193 if (compare == 0) {
194 found = true;
195 break;
196 }
197 }
198 if (found) {
199 String assertDataId = searchedData.getAssertDataId();
200 AssertObject assertObject = testDataSet.getAssert(assertDataId);
201 if (assertObject == null) {
202 throw new RuntimeException("Testdatat saknar assert med id=\"" + assertDataId + "\"!");
203 }
204 Object result = assertObject.getValue();
205
206 if (result instanceof Throwable) {
207 throw (RuntimeException) result;
208 }
209 return result;
210 }
211 }
212
213 throw new IllegalArgumentException("Sökt testdata saknas i metoden " + methodName);
214 }
215 }
216
217
218
219
220 public int size() {
221 log.debug("size = " + this.clusterDataSets.size());
222 return this.clusterDataSets.size();
223 }
224
225 public Set entrySet() {
226 return this.clusterDataSets.entrySet();
227 }
228 }