1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.chorem.jtimer;
24
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.io.filefilter.HiddenFileFilter;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.chorem.jtimer.entities.TimerProject;
30 import org.chorem.jtimer.entities.TimerTask;
31 import org.chorem.jtimer.io.Saver;
32 import org.testng.annotations.AfterMethod;
33 import org.testng.annotations.BeforeMethod;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.net.URL;
38 import java.util.Collection;
39 import java.util.Locale;
40 import java.util.Properties;
41
42
43
44
45
46
47
48
49
50
51
52
53 public abstract class AbstractJTimerTest {
54
55
56 private static Log log = LogFactory.getLog(AbstractJTimerTest.class);
57
58
59 protected Saver testSaver;
60
61
62 protected File testDataDirectory;
63
64
65
66
67
68
69 public static String getTempDirPath() {
70 String tempdir = System.getProperty("java.io.tmpdir");
71
72 if (!(tempdir.endsWith("/") || tempdir.endsWith("\\"))) {
73 tempdir += File.separator;
74 }
75
76 return tempdir;
77 }
78
79
80
81
82
83
84 protected void initDataDirectory() throws IOException {
85
86 JTimer.config = new JTimerConfig();
87
88 URL testConfigFile = AbstractJTimerTest.class.getResource("/jtimertest.properties");
89 Properties props = new Properties(JTimer.config.appConfig.getOptions());
90 props.load(testConfigFile.openStream());
91
92
93 testDataDirectory = new File(getTempDirPath(), String.valueOf(System.currentTimeMillis()));
94 props.put("jtimer.home.directory", testDataDirectory.getAbsolutePath());
95 props.put("jtimer.io.saver.directory", testDataDirectory.getParent() + File.separator + ".gtimer");
96
97 JTimer.config.appConfig.setOptions(props);
98
99 if (log.isDebugEnabled()) {
100 log.debug("Copy resource test directory to "
101 + testDataDirectory.getAbsolutePath());
102 }
103
104
105
106 FileUtils.copyDirectory(new File("src/test/resources/testdata"),
107 new File(testDataDirectory, "data"), HiddenFileFilter.VISIBLE);
108
109
110 JTimerFactory.saver = null;
111 testSaver = JTimerFactory.getFileSaver();
112
113 }
114
115
116
117
118
119
120 protected void deleteDataDirectory() throws IOException {
121 if (log.isDebugEnabled()) {
122 log.debug("Removing test directory");
123 }
124
125 FileUtils.deleteDirectory(testDataDirectory);
126 }
127
128
129
130
131
132
133 @BeforeMethod
134 public void beforeTest() throws IOException {
135 if (log.isDebugEnabled()) {
136 log.debug("Set up test");
137 }
138
139
140
141 Locale.setDefault(Locale.US);
142
143 initDataDirectory();
144 }
145
146
147
148
149
150
151 @AfterMethod
152 public void afterTest() throws IOException {
153 if (log.isDebugEnabled()) {
154 log.debug("Clean after test");
155 }
156 deleteDataDirectory();
157 testSaver = null;
158 }
159
160
161
162
163
164
165
166
167 public static TimerTask findTask(Collection<? extends TimerTask> tasks,
168 String path) {
169
170 String[] paths = path.split("/");
171 String current = paths[0];
172
173 TimerTask foundTask = null;
174 for (TimerTask subtask : tasks) {
175 if (subtask.getName().equals(current)) {
176 foundTask = subtask;
177 }
178 }
179
180 if (foundTask != null && paths.length > 1) {
181
182 String newPath = path.substring(current.length() + 1);
183 foundTask = findTask(foundTask.getSubTasks(), newPath);
184
185 }
186
187 return foundTask;
188 }
189
190
191
192
193
194
195
196
197 public static TimerProject findProject(Collection<TimerProject> projects,
198 String path) {
199
200 TimerProject foundProject = null;
201 for (TimerProject project : projects) {
202 if (project.getName().equals(path)) {
203 foundProject = project;
204 }
205 }
206
207 return foundProject;
208 }
209
210
211
212
213
214
215
216 protected static int getProjectsCount(Collection<TimerProject> projects) {
217 return projects.size();
218 }
219
220
221
222
223
224
225
226 protected static int getTasksCount(Collection<TimerProject> projects) {
227 int tasksCount = 0;
228
229 for (TimerProject project : projects) {
230 tasksCount += getRecursiveTasksCount(project.getSubTasks());
231 }
232 return tasksCount;
233 }
234
235
236
237
238
239
240
241 protected static int getRecursiveTasksCount(Collection<TimerTask> tasks) {
242 int tasksCount = 0;
243
244 for (TimerTask task : tasks) {
245 tasksCount++;
246 tasksCount += getRecursiveTasksCount(task.getSubTasks());
247 }
248 return tasksCount;
249 }
250
251
252
253
254
255
256
257 protected static int getAnnotationsCount(Collection<TimerProject> projects) {
258 int annotationsCount = 0;
259
260 for (TimerProject project : projects) {
261 annotationsCount += getRecursiveAnnotationsCount(project.getSubTasks());
262 }
263 return annotationsCount;
264 }
265
266
267
268
269
270
271
272 protected static int getRecursiveAnnotationsCount(Collection<TimerTask> tasks) {
273 int annotationsCount = 0;
274
275 for (TimerTask task : tasks) {
276 annotationsCount += task.getAllDaysAnnotations().size();
277 annotationsCount += getRecursiveAnnotationsCount(task.getSubTasks());
278 }
279 return annotationsCount;
280 }
281
282
283
284
285
286
287
288 protected static int getAlertsCount(Collection<TimerProject> projects) {
289 int annotationsCount = 0;
290
291 for (TimerProject project : projects) {
292 annotationsCount += getRecursiveAlertsCount(project.getSubTasks());
293 }
294 return annotationsCount;
295 }
296
297
298
299
300
301
302
303 protected static int getRecursiveAlertsCount(Collection<TimerTask> tasks) {
304 int annotationsCount = 0;
305
306 for (TimerTask task : tasks) {
307 annotationsCount += task.getAlerts().size();
308 annotationsCount += getRecursiveAlertsCount(task.getSubTasks());
309 }
310 return annotationsCount;
311 }
312 }