1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.tck.runner;
20
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.chemistry.opencmis.commons.impl.ClassLoaderUtil;
32 import org.apache.chemistry.opencmis.tck.CmisTest;
33 import org.apache.chemistry.opencmis.tck.CmisTestGroup;
34 import org.apache.chemistry.opencmis.tck.CmisTestProgressMonitor;
35 import org.apache.chemistry.opencmis.tck.impl.WrapperCmisTestGroup;
36
37
38
39
40 public abstract class AbstractRunner {
41
42 public static final String OVERRIDE_KEY = "org.apache.chemistry";
43 public static final String DEFAULT_TCK_GROUPS = "/cmis-tck-groups.txt";
44 public static final String TCK_BUILD_TIMESTAMP = "/META-INF/build-timestamp.txt";
45 public static final String TCK_BUILD_TIMESTAMP_PARAMETER = "org.apache.chemistry.opencmis.tck.timestamp";
46
47 private Map<String, String> parameters;
48 private final List<CmisTestGroup> groups = new ArrayList<CmisTestGroup>();
49 private boolean isCanceled = false;
50
51
52
53 public void setParameters(Map<String, String> orgParameters) {
54 this.parameters = new HashMap<String, String>();
55 if (orgParameters != null) {
56 this.parameters.putAll(orgParameters);
57 }
58
59
60 for (Object key : System.getProperties().keySet()) {
61 if (!key.toString().startsWith(OVERRIDE_KEY)) {
62 continue;
63 }
64
65 parameters.put(key.toString(), System.getProperties().getProperty(key.toString()));
66 }
67
68
69 parameters.put(TCK_BUILD_TIMESTAMP_PARAMETER, loadTCKTimestamp());
70 }
71
72 public void loadParameters(File file) throws Exception {
73 if (file == null || !file.isFile()) {
74 throw new IllegalArgumentException("File not found!");
75 }
76
77 loadParameters(new FileInputStream(file));
78 }
79
80 public void loadParameters(InputStream stream) throws Exception {
81 if (stream == null) {
82 throw new IllegalArgumentException("Stream is null!");
83 }
84
85 BufferedReader reader = null;
86 Map<String, String> loadParams = new HashMap<String, String>();
87
88 try {
89 reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
90
91 String line;
92 while ((line = reader.readLine()) != null) {
93 line = line.trim();
94 if (line.startsWith("#") || line.length() == 0) {
95 continue;
96 }
97
98 int x = line.indexOf('=');
99 if (x < 0) {
100 loadParams.put(line.trim(), "");
101 } else {
102 loadParams.put(line.substring(0, x).trim(), line.substring(x + 1).trim());
103 }
104 }
105
106 setParameters(loadParams);
107 } finally {
108 if (reader != null) {
109 try {
110 reader.close();
111 } catch (Exception e) {
112 }
113 }
114 }
115 }
116
117 public Map<String, String> getParameters() {
118 return parameters;
119 }
120
121 private String loadTCKTimestamp() {
122 StringBuilder result = new StringBuilder();
123
124 InputStream stream = getClass().getResourceAsStream(TCK_BUILD_TIMESTAMP);
125 if (stream != null) {
126 try {
127 BufferedReader br = new BufferedReader(new InputStreamReader(stream));
128
129 String s = null;
130 while ((s = br.readLine()) != null) {
131 result.append(s);
132 }
133
134 br.close();
135 } catch (Exception e) {
136 }
137 }
138
139 return result.toString();
140 }
141
142
143
144 public void loadDefaultTckGroups() throws Exception {
145 loadGroups(this.getClass().getResourceAsStream(DEFAULT_TCK_GROUPS));
146 }
147
148 public void loadGroups(File file) throws Exception {
149 if (file == null || !file.isFile()) {
150 throw new IllegalArgumentException("File not found!");
151 }
152
153 loadGroups(new FileInputStream(file));
154 }
155
156 public void loadGroups(InputStream stream) throws Exception {
157 if (stream == null) {
158 throw new IllegalArgumentException("Stream is null!");
159 }
160
161 BufferedReader reader = null;
162
163 try {
164 reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
165
166 String line;
167 while ((line = reader.readLine()) != null) {
168 line = line.trim();
169 if (line.startsWith("#") || line.length() == 0) {
170 continue;
171 }
172
173 addGroup(line);
174 }
175 } finally {
176 if (reader != null) {
177 try {
178 reader.close();
179 } catch (Exception e) {
180 }
181 }
182 }
183 }
184
185 public void addGroups(String[] groupClasses) throws Exception {
186 if (groupClasses == null) {
187 return;
188 }
189
190 for (String groupClass : groupClasses) {
191 addGroup(groupClass);
192 }
193 }
194
195 public void addGroup(String groupClass) throws Exception {
196 if (groupClass == null) {
197 return;
198 }
199
200 groupClass = groupClass.trim();
201 if (groupClass.length() == 0) {
202 return;
203 }
204
205 Class<?> clazz = ClassLoaderUtil.loadClass(groupClass);
206 Object o = clazz.newInstance();
207 CmisTestGroup group = null;
208
209 if (o instanceof CmisTestGroup) {
210 group = (CmisTestGroup) o;
211 } else if (o instanceof CmisTest) {
212 group = new WrapperCmisTestGroup((CmisTest) o);
213 } else {
214 throw new Exception("Not a CmisTestGroup or CmisTest class!");
215 }
216
217 addGroup(group);
218 }
219
220 public void addGroup(CmisTestGroup group) throws Exception {
221 if (group != null) {
222 group.init(parameters);
223 groups.add(group);
224 }
225 }
226
227 public List<CmisTestGroup> getGroups() {
228 return groups;
229 }
230
231
232
233
234
235
236 public void run(CmisTestProgressMonitor monitor) throws Exception {
237 isCanceled = false;
238
239 for (CmisTestGroup group : groups) {
240 synchronized (this) {
241 if (isCanceled) {
242 break;
243 }
244 }
245
246 if (group == null || !group.isEnabled()) {
247 continue;
248 }
249
250 group.setProgressMonitor(monitor);
251 group.run();
252 }
253 }
254
255 public synchronized boolean isCanceled() {
256 return isCanceled;
257 }
258
259 public synchronized void cancel() {
260 isCanceled = true;
261 }
262 }