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.io.File;
22  import java.io.PrintWriter;
23  
24  import org.apache.chemistry.opencmis.tck.CmisTest;
25  import org.apache.chemistry.opencmis.tck.CmisTestGroup;
26  import org.apache.chemistry.opencmis.tck.CmisTestProgressMonitor;
27  import org.apache.chemistry.opencmis.tck.CmisTestReport;
28  import org.apache.chemistry.opencmis.tck.CmisTestResult;
29  import org.apache.chemistry.opencmis.tck.CmisTestResultStatus;
30  import org.apache.chemistry.opencmis.tck.report.TextReport;
31  import org.apache.chemistry.opencmis.tck.runner.AbstractRunner;
32  import org.junit.Assert;
33  
34  public final class JUnitHelper {
35  
36      public static final String JUNIT_PARAMETERS = "org.apache.chemistry.opencmis.tck.junit.parameters";
37  
38      private JUnitHelper() {
39      }
40  
41      public static void run(CmisTest test) throws Exception {
42          run(new WrapperCmisTestGroup(test));
43      }
44  
45      public static void run(CmisTestGroup group) throws Exception {
46          JUnitRunner runner = new JUnitRunner();
47  
48          String parametersFile = System.getProperty(JUNIT_PARAMETERS);
49          if (parametersFile == null) {
50              runner.setParameters(null);
51          } else {
52              runner.loadParameters(new File(parametersFile));
53          }
54  
55          runner.addGroup(group);
56          runner.run(new JUnitProgressMonitor());
57  
58          CmisTestReport report = new TextReport();
59          report.createReport(runner.getParameters(), runner.getGroups(), new PrintWriter(System.out));
60  
61          checkForFailures(runner);
62      }
63  
64      private static void checkForFailures(JUnitRunner runner) {
65          for (CmisTestGroup group : runner.getGroups()) {
66              for (CmisTest test : group.getTests()) {
67                  for (CmisTestResult result : test.getResults()) {
68                      if (result.getStatus().getLevel() >= CmisTestResultStatus.FAILURE.getLevel()) {
69                          Assert.fail(result.getMessage());
70                      }
71                  }
72              }
73          }
74      }
75  
76      private static class JUnitRunner extends AbstractRunner {
77      }
78  
79      private static class JUnitProgressMonitor implements CmisTestProgressMonitor {
80  
81          @Override
82          @SuppressWarnings("PMD.SystemPrintln")
83          public void startGroup(CmisTestGroup group) {
84              System.out.println(group.getName() + " (" + group.getTests().size() + " tests)");
85          }
86  
87          @Override
88          public void endGroup(CmisTestGroup group) {
89          }
90  
91          @Override
92          @SuppressWarnings("PMD.SystemPrintln")
93          public void startTest(CmisTest test) {
94              System.out.println("  " + test.getName());
95          }
96  
97          @Override
98          public void endTest(CmisTest test) {
99          }
100 
101         @Override
102         @SuppressWarnings("PMD.SystemPrintln")
103         public void message(String msg) {
104             System.out.println(msg);
105         }
106     }
107 }