1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
34
35
36
37
38
39
40
41 public class DirectoryJUnitTestCaseManager implements JUnitTestCaseManager
42 {
43
44
45
46
47
48
49
50
51
52 private final File rootDirectory;
53
54
55
56
57 private final JUnitXmlReportParser parser = new JUnitXmlReportParser();
58
59
60
61
62 private final Map<Type, JUnitTestCaseDoc> cache =
63 new HashMap<Type, JUnitTestCaseDoc>();
64
65
66
67
68
69
70
71
72 public DirectoryJUnitTestCaseManager(final File rootDirectory)
73 {
74 this.rootDirectory = rootDirectory;
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public void evict(final Type testCaseType)
93 {
94 cache.remove(testCaseType);
95 }
96
97
98
99
100 public void clearCache()
101 {
102 cache.clear();
103 }
104
105
106
107
108
109
110
111
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
164
165 }