This project has retired. For details please refer to its Attic page.
AbstractRunner xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.tck.CmisTest;
32  import org.apache.chemistry.opencmis.tck.CmisTestGroup;
33  import org.apache.chemistry.opencmis.tck.CmisTestProgressMonitor;
34  import org.apache.chemistry.opencmis.tck.impl.WrapperCmisTestGroup;
35  
36  /**
37   * Base class for runners.
38   */
39  public abstract class AbstractRunner {
40  
41      public static final String OVERRIDE_KEY = "org.apache.chemistry";
42      public static final String DEFAULT_TCK_GROUPS = "/cmis-tck-groups.txt";
43      public static final String TCK_BUILD_TIMESTAMP = "/META-INF/build-timestamp.txt";
44      public static final String TCK_BUILD_TIMESTAMP_PARAMETER = "org.apache.chemistry.opencmis.tck.timestamp";
45  
46      private Map<String, String> parameters;
47      private final List<CmisTestGroup> groups = new ArrayList<CmisTestGroup>();
48      private boolean isCanceled = false;
49  
50      // --- parameters ---
51  
52      public void setParameters(Map<String, String> orgParameters) {
53          this.parameters = new HashMap<String, String>();
54          if (orgParameters != null) {
55              this.parameters.putAll(orgParameters);
56          }
57  
58          // override with system properties
59          for (Object key : System.getProperties().keySet()) {
60              if (!key.toString().startsWith(OVERRIDE_KEY)) {
61                  continue;
62              }
63  
64              parameters.put(key.toString(), System.getProperties().getProperty(key.toString()));
65          }
66  
67          // set TCK build timestamp
68          parameters.put(TCK_BUILD_TIMESTAMP_PARAMETER, loadTCKTimestamp());
69      }
70  
71      public void loadParameters(File file) throws Exception {
72          if (file == null || !file.isFile()) {
73              throw new IllegalArgumentException("File not found!");
74          }
75  
76          loadParameters(new FileInputStream(file));
77      }
78  
79      public void loadParameters(InputStream stream) throws Exception {
80          if (stream == null) {
81              throw new IllegalArgumentException("Stream is null!");
82          }
83  
84          BufferedReader reader = null;
85          Map<String, String> loadParams = new HashMap<String, String>();
86  
87          try {
88              reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
89  
90              String line;
91              while ((line = reader.readLine()) != null) {
92                  line = line.trim();
93                  if (line.startsWith("#") || line.length() == 0) {
94                      continue;
95                  }
96  
97                  int x = line.indexOf('=');
98                  if (x < 0) {
99                      loadParams.put(line.trim(), "");
100                 } else {
101                     loadParams.put(line.substring(0, x).trim(), line.substring(x + 1).trim());
102                 }
103             }
104 
105             setParameters(loadParams);
106         } finally {
107             if (reader != null) {
108                 try {
109                     reader.close();
110                 } catch (Exception e) {
111                 }
112             }
113         }
114     }
115 
116     public Map<String, String> getParameters() {
117         return parameters;
118     }
119 
120     private String loadTCKTimestamp() {
121         StringBuilder result = new StringBuilder();
122 
123         InputStream stream = getClass().getResourceAsStream(TCK_BUILD_TIMESTAMP);
124         if (stream != null) {
125             try {
126                 BufferedReader br = new BufferedReader(new InputStreamReader(stream));
127 
128                 String s = null;
129                 while ((s = br.readLine()) != null) {
130                     result.append(s);
131                 }
132 
133                 br.close();
134             } catch (Exception e) {
135             }
136         }
137 
138         return result.toString();
139     }
140 
141     // --- groups ---
142 
143     public void loadDefaultTckGroups() throws Exception {
144         loadGroups(this.getClass().getResourceAsStream(DEFAULT_TCK_GROUPS));
145     }
146 
147     public void loadGroups(File file) throws Exception {
148         if (file == null || !file.isFile()) {
149             throw new IllegalArgumentException("File not found!");
150         }
151 
152         loadGroups(new FileInputStream(file));
153     }
154 
155     public void loadGroups(InputStream stream) throws Exception {
156         if (stream == null) {
157             throw new IllegalArgumentException("Stream is null!");
158         }
159 
160         BufferedReader reader = null;
161 
162         try {
163             reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
164 
165             String line;
166             while ((line = reader.readLine()) != null) {
167                 line = line.trim();
168                 if (line.startsWith("#") || line.length() == 0) {
169                     continue;
170                 }
171 
172                 addGroup(line);
173             }
174         } finally {
175             if (reader != null) {
176                 try {
177                     reader.close();
178                 } catch (Exception e) {
179                 }
180             }
181         }
182     }
183 
184     public void addGroups(String[] groupClasses) throws Exception {
185         if (groupClasses == null) {
186             return;
187         }
188 
189         for (String groupClass : groupClasses) {
190             addGroup(groupClass);
191         }
192     }
193 
194     public void addGroup(String groupClass) throws Exception {
195         if (groupClass == null) {
196             return;
197         }
198 
199         groupClass = groupClass.trim();
200         if (groupClass.length() == 0) {
201             return;
202         }
203 
204         Class<?> clazz = Class.forName(groupClass);
205         Object o = clazz.newInstance();
206         CmisTestGroup group = null;
207 
208         if (o instanceof CmisTestGroup) {
209             group = (CmisTestGroup) o;
210         } else if (o instanceof CmisTest) {
211             group = new WrapperCmisTestGroup((CmisTest) o);
212         } else {
213             throw new Exception("Not a CmisTestGroup or CmisTest class!");
214         }
215 
216         addGroup(group);
217     }
218 
219     public void addGroup(CmisTestGroup group) throws Exception {
220         if (group != null) {
221             group.init(parameters);
222             groups.add(group);
223         }
224     }
225 
226     public List<CmisTestGroup> getGroups() {
227         return groups;
228     }
229 
230     // --- run ---
231 
232     /**
233      * Runs all configured groups.
234      */
235     public void run(CmisTestProgressMonitor monitor) throws Exception {
236         isCanceled = false;
237 
238         for (CmisTestGroup group : groups) {
239             synchronized (this) {
240                 if (isCanceled) {
241                     break;
242                 }
243             }
244 
245             if (group == null || !group.isEnabled()) {
246                 continue;
247             }
248 
249             group.setProgressMonitor(monitor);
250             group.run();
251         }
252     }
253 
254     public synchronized boolean isCanceled() {
255         return isCanceled;
256     }
257 
258     public synchronized void cancel() {
259         isCanceled = true;
260     }
261 }