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.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.chemistry.opencmis.commons.impl.ClassLoaderUtil;
31 import org.apache.chemistry.opencmis.commons.impl.IOUtils;
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 public static final String TCK_REVISION = "/META-INF/tck-revision.txt";
47 public static final String TCK_REVISION_PARAMETER = "org.apache.chemistry.opencmis.tck.revision";
48
49 private Map<String, String> parameters;
50 private final List<CmisTestGroup> groups = new ArrayList<CmisTestGroup>();
51 private boolean isCanceled = false;
52
53
54
55 public void setParameters(Map<String, String> orgParameters) {
56 this.parameters = new HashMap<String, String>();
57 if (orgParameters != null) {
58 this.parameters.putAll(orgParameters);
59 }
60
61
62 for (Object key : System.getProperties().keySet()) {
63 if (!key.toString().startsWith(OVERRIDE_KEY)) {
64 continue;
65 }
66
67 parameters.put(key.toString(), System.getProperties().getProperty(key.toString()));
68 }
69
70
71 parameters.put(TCK_BUILD_TIMESTAMP_PARAMETER, loadTCKTimestamp());
72 String revision = loadTCKRevision();
73 if (revision != null) {
74 parameters.put(TCK_REVISION_PARAMETER, revision);
75 }
76 }
77
78 public void loadParameters(File file) throws IOException {
79 if (file == null || !file.isFile()) {
80 throw new IllegalArgumentException("File not found!");
81 }
82
83 InputStream stream = new FileInputStream(file);
84 try {
85 loadParameters(stream);
86 } finally {
87 IOUtils.closeQuietly(stream);
88 }
89 }
90
91 public void loadParameters(InputStream stream) throws IOException {
92 if (stream == null) {
93 throw new IllegalArgumentException("Stream is null!");
94 }
95
96 setParameters(IOUtils.readAllLinesAsMap(stream));
97 }
98
99 public Map<String, String> getParameters() {
100 return parameters;
101 }
102
103 private String loadTCKTimestamp() {
104 InputStream stream = getClass().getResourceAsStream(TCK_BUILD_TIMESTAMP);
105
106 if (stream != null) {
107 try {
108 return IOUtils.readAllLines(stream, 1000);
109 } catch (IOException e) {
110 return "";
111 } finally {
112 IOUtils.closeQuietly(stream);
113 }
114 }
115
116 return "";
117 }
118
119 private String loadTCKRevision() {
120 InputStream stream = getClass().getResourceAsStream(TCK_REVISION);
121
122 if (stream != null) {
123 try {
124 String revStr = IOUtils.readFirstLine(stream);
125 return String.valueOf(Integer.parseInt(revStr.trim()));
126 } catch (NumberFormatException e) {
127 return null;
128 } catch (IOException e) {
129 return null;
130 } finally {
131 IOUtils.closeQuietly(stream);
132 }
133 }
134
135 return null;
136 }
137
138
139
140 public void loadDefaultTckGroups() throws Exception {
141 InputStream stream = this.getClass().getResourceAsStream(DEFAULT_TCK_GROUPS);
142 if (stream != null) {
143 try {
144 loadGroups(stream);
145 } finally {
146 IOUtils.closeQuietly(stream);
147 }
148 }
149 }
150
151 public void loadGroups(File file) throws Exception {
152 if (file == null || !file.isFile()) {
153 throw new IllegalArgumentException("File not found!");
154 }
155
156 InputStream stream = new FileInputStream(file);
157 try {
158 loadGroups(stream);
159 } finally {
160 IOUtils.closeQuietly(stream);
161 }
162 }
163
164 public void loadGroups(InputStream stream) throws Exception {
165 if (stream == null) {
166 throw new IllegalArgumentException("Stream is null!");
167 }
168
169 for (String groupName : IOUtils.readAllLinesAsList(stream, 10000)) {
170 addGroup(groupName);
171 }
172 }
173
174 public void addGroups(String[] groupClasses) throws Exception {
175 if (groupClasses == null) {
176 return;
177 }
178
179 for (String groupClass : groupClasses) {
180 addGroup(groupClass);
181 }
182 }
183
184 public void addGroup(String groupClass) throws Exception {
185 if (groupClass == null) {
186 return;
187 }
188
189 groupClass = groupClass.trim();
190 if (groupClass.length() == 0) {
191 return;
192 }
193
194 Class<?> clazz = ClassLoaderUtil.loadClass(groupClass);
195 Object o = clazz.newInstance();
196 CmisTestGroup group = null;
197
198 if (o instanceof CmisTestGroup) {
199 group = (CmisTestGroup) o;
200 } else if (o instanceof CmisTest) {
201 group = new WrapperCmisTestGroup((CmisTest) o);
202 } else {
203 throw new InstantiationException("Not a CmisTestGroup or CmisTest class!");
204 }
205
206 addGroup(group);
207 }
208
209 public void addGroup(CmisTestGroup group) throws Exception {
210 if (group != null) {
211 group.init(parameters);
212 groups.add(group);
213 }
214 }
215
216 public List<CmisTestGroup> getGroups() {
217 return groups;
218 }
219
220
221
222
223
224
225 public void run(CmisTestProgressMonitor monitor) throws Exception {
226 synchronized (this) {
227 isCanceled = false;
228 }
229
230 for (CmisTestGroup group : groups) {
231 synchronized (this) {
232 if (isCanceled) {
233 break;
234 }
235 }
236
237 if (group == null || !group.isEnabled()) {
238 continue;
239 }
240
241 group.setProgressMonitor(monitor);
242 group.run();
243 }
244 }
245
246 public synchronized boolean isCanceled() {
247 return isCanceled;
248 }
249
250 public synchronized void cancel() {
251 isCanceled = true;
252 }
253 }