View Javadoc
1   /*
2    * #%L
3    * jTimer
4    * %%
5    * Copyright (C) 2008 - 2012 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/gpl-3.0.html>.
20   * #L%
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   * Surcharge la factory du code principal.
44   *
45   * Redefinie les propriétés de test.
46   *
47   * @author chatellier
48   * @version $Revision$
49   *
50   * Last update : $Date$
51   * By : $Author$
52   */
53  public abstract class AbstractJTimerTest {
54  
55      /** Class log */
56      private static Log log = LogFactory.getLog(AbstractJTimerTest.class);
57  
58      /** Saver instance on specific test directory. */
59      protected Saver testSaver;
60  
61      /** Test directory. */
62      protected File testDataDirectory;
63  
64      /**
65       * Return specific temp dir.
66       *
67       * @return system temp dir
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       * Redefinit les proprietes utilisées pour les tests.
81       *
82       * @throws IOException
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          // overload system wide options
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); // not call parse in test
98  
99          if (log.isDebugEnabled()) {
100             log.debug("Copy resource test directory to "
101                     + testDataDirectory.getAbsolutePath());
102         }
103 
104         // HiddenFileFilter.VISIBLE is used to not
105         // copy .svn folders
106         FileUtils.copyDirectory(new File("src/test/resources/testdata"),
107                 new File(testDataDirectory, "data"), HiddenFileFilter.VISIBLE);
108 
109         // force null, to force new instance
110         JTimerFactory.saver = null;
111         testSaver = JTimerFactory.getFileSaver();
112 
113     }
114 
115     /**
116      * Delete data directory defined in saver.
117      *
118      * @throws IOException
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      * Before each test, set up test data directory.
130      *
131      * @throws IOException
132      */
133     @BeforeMethod
134     public void beforeTest() throws IOException {
135         if (log.isDebugEnabled()) {
136             log.debug("Set up test");
137         }
138 
139         // jtimer code use default system locale
140         // test are coded in en_US
141         Locale.setDefault(Locale.US);
142 
143         initDataDirectory();
144     }
145 
146     /**
147      * After each test, remove test data directory.
148      *
149      * @throws IOException
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      * Get A specific task with is path in tree.
162      *
163      * @param tasks tasks list to search into
164      * @param path path to search
165      * @return found task, or <tt>null</tt> if not found
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             // +1 == /
182             String newPath = path.substring(current.length() + 1);
183             foundTask = findTask(foundTask.getSubTasks(), newPath);
184 
185         }
186 
187         return foundTask;
188     }
189 
190     /**
191      * Get a specific project in project list.
192      *
193      * @param projects projects list
194      * @param path path to search
195      * @return found project, or <tt>null</tt> if not found
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      * Get projects count.
212      *
213      * @param projects projects collection
214      * @return project count
215      */
216     protected static int getProjectsCount(Collection<TimerProject> projects) {
217         return projects.size();
218     }
219 
220     /**
221      * Get task count.
222      *
223      * @param projects projects collection
224      * @return task count
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      * Get task count.
237      *
238      * @param tasks tasks collection
239      * @return task count
240      */
241     protected static int getRecursiveTasksCount(Collection<TimerTask> tasks) {
242         int tasksCount = 0;
243 
244         for (TimerTask task : tasks) {
245             tasksCount++; // count me
246             tasksCount += getRecursiveTasksCount(task.getSubTasks());
247         }
248         return tasksCount;
249     }
250 
251     /**
252      * Get annotations count.
253      *
254      * @param projects projects collection
255      * @return annotations count
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      * Get annotations count.
268      *
269      * @param tasks tasks collection
270      * @return annotations count
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      * Get alert count.
284      *
285      * @param projects projects collection
286      * @return annotations count
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      * Get alert count.
299      *
300      * @param tasks tasks collection
301      * @return annotations count
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 }