1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.testdoc.core.adapter;
17
18 import java.io.File;
19 import java.io.FilenameFilter;
20 import java.util.List;
21
22 import de.smartics.testdoc.core.doc.ScenarioTestDoc;
23 import de.smartics.testdoc.core.doc.Type;
24 import de.smartics.testdoc.core.doc.UnitTestDoc;
25 import de.smartics.testdoc.core.export.AbstractExportAdapter;
26 import de.smartics.testdoc.core.export.ExportException;
27
28
29
30
31
32
33
34 public abstract class AbstractFileBasedExportAdapter extends
35 AbstractExportAdapter
36 {
37
38
39
40
41
42
43
44
45
46 protected final PersistenceUtils persist = new PersistenceUtils();
47
48
49
50
51
52
53
54
55 public AbstractFileBasedExportAdapter()
56 {
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 @Override
75 public void export(final UnitTestDoc testDoc) throws ExportException
76 {
77 final Type testCaseType = calcTestCaseType(testDoc);
78 if (testCaseType != null)
79 {
80 try
81 {
82 final Type uutType = testDoc.getUutType();
83 final File file = createFileRef(testCaseType, uutType);
84 if (file.exists())
85 {
86 final UnitTestDoc storedTestDoc =
87 (UnitTestDoc) persist.deserialize(file);
88 storedTestDoc.addScenarios(testDoc);
89 persist.serialize(storedTestDoc, file);
90 }
91 else
92 {
93 persist.serialize(testDoc, file);
94 }
95 }
96 catch (final IllegalStateException e)
97 {
98 throw new IllegalStateException(
99 "Cannot create resource with APT filer.", e);
100 }
101 }
102 }
103
104 protected abstract File createFileRef(final Type testCaseType,
105 final Type uutType) throws IllegalStateException;
106
107 protected abstract File[] createFileRefs(final Type testCaseType)
108 throws IllegalStateException;
109
110 protected final File[] createFileRefs(final Type testCaseType, final File directory)
111 {
112 final String prefix = testCaseType.toString() + '-';
113 final File[] files = directory.listFiles(new FilenameFilter()
114 {
115 @Override
116 public boolean accept(final File dir, final String name)
117 {
118 return name.startsWith(prefix);
119 }
120 });
121 if (files != null)
122 {
123 return files;
124 }
125 return new File[0];
126 }
127
128
129
130
131 protected final Type calcTestCaseType(final UnitTestDoc testDoc)
132 {
133 final List<ScenarioTestDoc> scenarios = testDoc.getScenarios();
134 if (!scenarios.isEmpty())
135 {
136 final Type testCaseType = scenarios.get(0).getTestCaseType();
137 for (final ScenarioTestDoc scenario : scenarios)
138 {
139 final Type currentType = scenario.getTestCaseType();
140 if (!testCaseType.equals(currentType))
141 {
142 throw new IllegalArgumentException("Not all scenarios are of type '"
143 + testCaseType + "': "
144 + currentType);
145 }
146 }
147 return testCaseType;
148 }
149 return null;
150 }
151
152
153
154
155
156
157 @Override
158 public void clear(final Type testCaseType)
159 {
160 if (testCaseType != null)
161 {
162 try
163 {
164 final File[] fileRefs = createFileRefs(testCaseType);
165 for (final File file : fileRefs)
166 {
167 if (file.exists())
168 {
169 final UnitTestDoc testDoc = (UnitTestDoc) persist.deserialize(file);
170 testDoc.removeScenarios(testCaseType);
171 persist.serialize(testDoc, file);
172 }
173 }
174 }
175 catch (final IllegalStateException e)
176 {
177 throw new ExportException("Cannot clear scenarios for test case type '"
178 + testCaseType + "'.", e);
179 }
180 }
181 }
182
183
184
185 }