View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.collect.extractor;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.lang.model.element.Element;
22  import javax.lang.model.element.ExecutableElement;
23  import javax.lang.model.element.TypeElement;
24  
25  import de.smartics.testdoc.collect.extractor.TestDocAnnotationExtractor.UutInfo;
26  import de.smartics.testdoc.core.doc.ScenarioTestDoc;
27  import de.smartics.testdoc.core.doc.UnitTestDoc;
28  
29  /**
30   * Extracts unit test doc information from a specified type.
31   *
32   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
33   * @version $Revision:591 $
34   */
35  public final class TestDocTypeExtractor extends AbstractExtractor
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    // --- members --------------------------------------------------------------
42  
43    // ****************************** Initializer *******************************
44  
45    // ****************************** Constructors ******************************
46  
47    /**
48     * Default constructor.
49     *
50     * @param config the configuration to access processing information.
51     * @param testCaseType the test case that has an annotation that potentially
52     *          specifies the unit under test.
53     * @throws IllegalArgumentException if the <code>declaredUutField</code> is
54     *           not a field declaration within a class or any of the arguments is
55     *           <code>null</code>.
56     */
57    public TestDocTypeExtractor(final ExtractorConfig config,
58        final TypeElement testCaseType) throws IllegalArgumentException
59    {
60      super(config, testCaseType);
61    }
62  
63    // ****************************** Inner Classes *****************************
64  
65    // ********************************* Methods ********************************
66  
67    // --- init -----------------------------------------------------------------
68  
69    // --- get&set --------------------------------------------------------------
70  
71    // --- business -------------------------------------------------------------
72  
73    /**
74     * Reads the test documentation from the element the extractor instance is
75     * associated with.
76     *
77     * @return the test documentation read, <code>null</code> if no information is
78     *         found in class.
79     */
80    public UnitTestDoc readTestDoc()
81    {
82      final UutInfo uutInfo = uutExtractor.read(testCaseType);
83      final String type = uutInfo.getType();
84      if (type != null)
85      {
86        final List<ScenarioTestDoc> scenarios = calcScenarios(uutInfo);
87  
88        final UnitTestDoc testDoc = new UnitTestDoc(type, scenarios);
89  
90        return testDoc;
91      }
92      return null;
93    }
94  
95    private List<ScenarioTestDoc> calcScenarios(final UutInfo uutInfo)
96      throws ExtractorException
97    {
98      final List<ScenarioTestDoc> scenarios = new ArrayList<ScenarioTestDoc>();
99  
100     final String testCaseTypeName = testCaseType.toString();
101     for (final Element element : elementUtils.getAllMembers(testCaseType))
102     {
103       if (isTestMethodWithoutUutAnnotation(element))
104       {
105         final ExecutableElement testMethod = (ExecutableElement) element;
106         final ScenarioTestDoc scenario =
107             createScenarioIfElementIsTestMethod(uutInfo, testCaseTypeName,
108                 testMethod);
109         if (scenario != null)
110         {
111           scenarios.add(scenario);
112         }
113       }
114     }
115 
116     return scenarios;
117   }
118 
119   // --- object basics --------------------------------------------------------
120 
121 }