View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.report.junit;
17  
18  import java.io.BufferedInputStream;
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.io.FileUtils;
26  import org.apache.commons.io.IOUtils;
27  import org.jdom.JDOMException;
28  
29  import de.smartics.testdoc.core.doc.Type;
30  
31  /**
32   * Provides access to test case information.
33   * <p>
34   * The manager knows the location of the JUnit XML reports and returns the
35   * information on demand.
36   * </p>
37   *
38   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
39   * @version $Revision:591 $
40   */
41  public class DirectoryJUnitTestCaseManager implements JUnitTestCaseManager
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    // --- members --------------------------------------------------------------
48  
49    /**
50     * The root directory that contains all JUnit test case reports.
51     */
52    private final File rootDirectory;
53  
54    /**
55     * The parser used to create report instances.
56     */
57    private final JUnitXmlReportParser parser = new JUnitXmlReportParser();
58  
59    /**
60     * The cache of test case instances.
61     */
62    private final Map<Type, JUnitTestCaseDoc> cache =
63        new HashMap<Type, JUnitTestCaseDoc>();
64  
65    // ****************************** Initializer *******************************
66  
67    // ****************************** Constructors ******************************
68  
69    /**
70     * Default constructor.
71     */
72    public DirectoryJUnitTestCaseManager(final File rootDirectory)
73    {
74      this.rootDirectory = rootDirectory;
75    }
76  
77    // ****************************** Inner Classes *****************************
78  
79    // ********************************* Methods ********************************
80  
81    // --- init -----------------------------------------------------------------
82  
83    // --- get&set --------------------------------------------------------------
84  
85    // --- business -------------------------------------------------------------
86  
87    /**
88     * Allows to selectively remove instances from the cache.
89     *
90     * @param testCaseType the test case instance to remove from the cache.
91     */
92    public void evict(final Type testCaseType)
93    {
94      cache.remove(testCaseType);
95    }
96  
97    /**
98     * Removes all instances from the cache.
99     */
100   public void clearCache()
101   {
102     cache.clear();
103   }
104 
105   /**
106    * {@inheritDoc}
107    * <p>
108    * Reads the XML report form the root directory.
109    * </p>
110    *
111    * @see de.smartics.testdoc.report.junit.JUnitTestCaseManager#readTestCase(de.smartics.testdoc.core.doc.Type)
112    */
113   @Override
114   public JUnitTestCaseDoc readTestCase(final Type testCaseType)
115     throws TestCaseReportException
116   {
117     final JUnitTestCaseDoc cached = cache.get(testCaseType);
118     if (cached != null)
119     {
120       return cached;
121     }
122 
123     final File testCaseFile = createFile(testCaseType);
124     InputStream input = null;
125 
126     try
127     {
128       input = new BufferedInputStream(FileUtils.openInputStream(testCaseFile));
129       final JUnitTestCaseDoc testCase = parser.read(input);
130       cache.put(testCaseType, testCase);
131       return testCase;
132     }
133     catch (final IOException e)
134     {
135       throw new TestCaseReportException(
136           "Cannot read JUnit XML report for test case '" + testCaseType + "'.",
137           e);
138     }
139     catch (final JDOMException e)
140     {
141       throw new TestCaseReportException(
142           "Cannot parse JUnit XML report for test case '" + testCaseType + "'.",
143           e);
144     }
145     finally
146     {
147       IOUtils.closeQuietly(input);
148     }
149   }
150 
151   private File createFile(final Type testCaseType)
152   {
153     final String fileName = createFileName(testCaseType);
154     return new File(rootDirectory, fileName);
155   }
156 
157   private String createFileName(final Type testCaseType)
158   {
159     final String type = testCaseType.toString();
160     return "TEST-" + type + ".xml";
161   }
162 
163   // --- object basics --------------------------------------------------------
164 
165 }