View Javadoc

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.impl;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.chemistry.opencmis.tck.CmisTest;
26  import org.apache.chemistry.opencmis.tck.CmisTestGroup;
27  import org.apache.chemistry.opencmis.tck.CmisTestProgressMonitor;
28  import org.junit.Test;
29  
30  /**
31   * Base class for test groups.
32   */
33  public abstract class AbstractCmisTestGroup implements CmisTestGroup {
34  
35      private Map<String, String> parameters;
36      private String name;
37      private String description;
38      private final List<CmisTest> tests = new ArrayList<CmisTest>();
39      private boolean isEnabled = true;
40      private CmisTestProgressMonitor progressMonitor;
41  
42      @Override
43      public void init(Map<String, String> parameters) throws Exception {
44          this.parameters = parameters;
45      }
46  
47      protected Map<String, String> getParameters() {
48          return parameters;
49      }
50  
51      @Override
52      public String getName() {
53          return name;
54      }
55  
56      public void setName(String name) {
57          this.name = name;
58      }
59  
60      @Override
61      public String getDescription() {
62          return description;
63      }
64  
65      public void setDescription(String description) {
66          this.description = description;
67      }
68  
69      @Override
70      public List<CmisTest> getTests() {
71          return tests;
72      }
73  
74      public void addTest(CmisTest test) throws Exception {
75          if (test != null) {
76              tests.add(test);
77              if (test instanceof AbstractCmisTest) {
78                  ((AbstractCmisTest) test).setGroup(this);
79              }
80              test.init(parameters);
81          }
82      }
83  
84      @Override
85      public void setProgressMonitor(CmisTestProgressMonitor progressMonitor) {
86          this.progressMonitor = progressMonitor;
87      }
88  
89      protected CmisTestProgressMonitor getProgressMonitor() {
90          return this.progressMonitor;
91      }
92  
93      @Override
94      public void run() throws Exception {
95          if (progressMonitor != null) {
96              progressMonitor.startGroup(this);
97          }
98  
99          try {
100             preRun();
101             for (CmisTest test : tests) {
102                 if (test == null || !test.isEnabled()) {
103                     continue;
104                 }
105 
106                 try {
107                     if (progressMonitor != null) {
108                         progressMonitor.startTest(test);
109                     }
110 
111                     preTest(test);
112 
113                     long start = System.currentTimeMillis();
114 
115                     test.run();
116 
117                     long end = System.currentTimeMillis();
118                     if (test instanceof AbstractCmisTest) {
119                         ((AbstractCmisTest) test).setTime(end - start);
120                     }
121                 } catch (Exception e) {
122                     if (!(e instanceof FatalTestException)) {
123                         throw e;
124                     }
125                 } finally {
126                     postTest(test);
127 
128                     if (progressMonitor != null) {
129                         progressMonitor.endTest(test);
130                     }
131                 }
132             }
133         } finally {
134             postRun();
135         }
136 
137         if (progressMonitor != null) {
138             progressMonitor.endGroup(this);
139         }
140     }
141 
142     @Test
143     public void junit() throws Exception {
144         JUnitHelper.run(this);
145     }
146 
147     protected void preRun() {
148     }
149 
150     protected void postRun() {
151     }
152 
153     protected void preTest(CmisTest test) {
154     }
155 
156     protected void postTest(CmisTest test) {
157     }
158 
159     @Override
160     public boolean isEnabled() {
161         return isEnabled;
162     }
163 
164     @Override
165     public void setEnabled(boolean enabled) {
166         this.isEnabled = enabled;
167     }
168 }