View Javadoc

1   /**
2    * 2009, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
3    * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
4    */
5   
6   package pt.digitalis.dif.sanitycheck.manager;
7   
8   import java.lang.reflect.InvocationTargetException;
9   import java.lang.reflect.Method;
10  import java.util.ArrayList;
11  import java.util.Arrays;
12  import java.util.List;
13  
14  import pt.digitalis.dif.sanitycheck.ISanityCheckTestSuite;
15  import pt.digitalis.dif.sanitycheck.SanityCheckResult;
16  import pt.digitalis.dif.sanitycheck.annotations.SanityCheckDependency;
17  import pt.digitalis.dif.sanitycheck.annotations.SanityCheckFinalize;
18  import pt.digitalis.dif.sanitycheck.annotations.SanityCheckSetup;
19  import pt.digitalis.dif.sanitycheck.annotations.SanityCheckTest;
20  import pt.digitalis.dif.sanitycheck.exceptions.WrongTestMethodSignature;
21  import pt.digitalis.utils.common.StringUtils;
22  
23  /**
24   * Defines a given test suite for usage by the {@link SanityCheckManager} internals
25   * 
26   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
27   * @created 15 de Jul de 2011
28   */
29  public class SanityCheckTestSuiteDefinition {
30  
31      /** the list of dependencies for execution of the current test suite */
32      private List<String> dependencies;
33  
34      /** the last execution result */
35      private SanityCheckResult executionResult;
36  
37      /** The tests finalize method */
38      private Method finalizeMethod;
39  
40      /** Unique identifier */
41      private String id;
42  
43      /** the test suite name */
44      private String name;
45  
46      /** The tests setup method */
47      private Method setupMethod;
48  
49      /** the test methods */
50      private List<TestMethodDefinition> testMethods;
51  
52      /** The test suite instance */
53      private ISanityCheckTestSuite testSuiteInstance;
54  
55      /**
56       * @param id
57       * @param testSuiteInstance
58       */
59      public SanityCheckTestSuiteDefinition(String id, ISanityCheckTestSuite testSuiteInstance)
60      {
61          super();
62          this.id = id;
63          this.name = StringUtils.camelCaseToString(testSuiteInstance.getClass().getSimpleName());
64          this.testSuiteInstance = testSuiteInstance;
65  
66          // Dependencies will be lazy initialized from the annotation in the first access.
67          this.dependencies = null;
68  
69          // Initialize the execution methods
70          this.testMethods = new ArrayList<TestMethodDefinition>();
71  
72          // Parse all methods...
73          for (Method method: testSuiteInstance.getClass().getMethods())
74          {
75              if (method.isAnnotationPresent(SanityCheckSetup.class))
76                  setupMethod = method;
77  
78              else if (method.isAnnotationPresent(SanityCheckFinalize.class))
79                  finalizeMethod = method;
80  
81              else if (method.isAnnotationPresent(SanityCheckTest.class))
82                  try
83                  {
84                      testMethods.add(new TestMethodDefinition(testSuiteInstance, method));
85                  }
86                  catch (WrongTestMethodSignature e)
87                  {
88                      e.printStackTrace();
89                  }
90          }
91      }
92  
93      /**
94       * @return the dependent list of
95       */
96      public List<SanityCheckTestSuiteDefinition> getDependencies()
97      {
98          if (dependencies == null)
99          {
100             // First access. Parse dependencies
101             dependencies = new ArrayList<String>();
102             SanityCheckDependency dependenciesAnnotation = testSuiteInstance.getClass().getAnnotation(
103                     SanityCheckDependency.class);
104 
105             if (dependenciesAnnotation != null)
106             {
107                 String[] depencencyList = dependenciesAnnotation.value().split(",");
108                 dependencies = Arrays.asList(depencencyList);
109             }
110         }
111 
112         List<SanityCheckTestSuiteDefinition> result = new ArrayList<SanityCheckTestSuiteDefinition>();
113 
114         for (String dependencyID: dependencies)
115             result.add(SanityCheckManager.getTestSuites().get(dependencyID));
116 
117         return result;
118     }
119 
120     /**
121      * Inspector for the 'executionResult' attribute.
122      * 
123      * @return the executionResult value
124      */
125     public SanityCheckResult getExecutionResult()
126     {
127         return executionResult;
128     }
129 
130     /**
131      * Inspector for the 'finalizeMethod' attribute.
132      * 
133      * @return the finalizeMethod value
134      */
135     public Method getFinalizeMethod()
136     {
137         return finalizeMethod;
138     }
139 
140     /**
141      * Inspector for the 'id' attribute.
142      * 
143      * @return the id value
144      */
145     public String getId()
146     {
147         return id;
148     }
149 
150     /**
151      * Inspector for the 'name' attribute.
152      * 
153      * @return the name value
154      */
155     public String getName()
156     {
157         return name;
158     }
159 
160     /**
161      * Inspector for the 'setupMethod' attribute.
162      * 
163      * @return the setupMethod value
164      */
165     public Method getSetupMethod()
166     {
167         return setupMethod;
168     }
169 
170     /**
171      * Inspector for the 'testMethods' attribute.
172      * 
173      * @return the testMethods value
174      */
175     public List<TestMethodDefinition> getTestMethods()
176     {
177         return testMethods;
178     }
179 
180     /**
181      * Inspector for the 'testSuiteInstance' attribute.
182      * 
183      * @return the testSuiteInstance value
184      */
185     public ISanityCheckTestSuite getTestSuiteInstance()
186     {
187         return testSuiteInstance;
188     }
189 
190     /**
191      * Runs the finalize method of the test suite
192      * 
193      * @throws InvocationTargetException
194      * @throws IllegalAccessException
195      * @throws IllegalArgumentException
196      */
197     public void runTestsFinalize() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
198     {
199         if (finalizeMethod != null)
200             finalizeMethod.invoke(testSuiteInstance);
201     }
202 
203     /**
204      * Runs the setup method of the test suite
205      * 
206      * @throws InvocationTargetException
207      * @throws IllegalAccessException
208      * @throws IllegalArgumentException
209      */
210     public void runTestsSetup() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
211     {
212         if (setupMethod != null)
213             setupMethod.invoke(testSuiteInstance);
214     }
215 
216     /**
217      * Modifier for the 'executionResult' attribute.
218      * 
219      * @param executionResult
220      *            the new executionResult value to set
221      */
222     public void setExecutionResult(SanityCheckResult executionResult)
223     {
224         this.executionResult = executionResult;
225     }
226 }