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.ElementKind;
23  import javax.lang.model.element.ExecutableElement;
24  import javax.lang.model.element.TypeElement;
25  
26  import de.smartics.testdoc.collect.extractor.TestDocAnnotationExtractor.UutInfo;
27  import de.smartics.testdoc.core.doc.ScenarioTestDoc;
28  import de.smartics.testdoc.core.doc.UnitTestDoc;
29  
30  /**
31   * Extracts unit test doc information from a specified method.
32   *
33   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
34   * @version $Revision:591 $
35   */
36  public final class TestDocMethodExtractor extends AbstractExtractor
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    // --- members --------------------------------------------------------------
43  
44    /**
45     * The element that contains information about a method in the test case class
46     * that points to the unit under test.
47     */
48    private final ExecutableElement declaredTestCaseMethod;
49  
50    // ****************************** Initializer *******************************
51  
52    // ****************************** Constructors ******************************
53  
54    /**
55     * Default constructor.
56     *
57     * @param config the configuration to access processing information.
58     * @param declaredTestCaseMethod the element that contains information about a
59     *          variable in the test case class that points to the unit under
60     *          test.
61     * @throws IllegalArgumentException if the <code>declaredUutField</code> is
62     *           not a field declaration within a class or any of the arguments is
63     *           <code>null</code>.
64     */
65    public TestDocMethodExtractor(final ExtractorConfig config,
66        final ExecutableElement declaredTestCaseMethod)
67      throws IllegalArgumentException
68    {
69      super(config, initTypeElement(declaredTestCaseMethod));
70      this.declaredTestCaseMethod = declaredTestCaseMethod;
71    }
72  
73    // ****************************** Inner Classes *****************************
74  
75    // ********************************* Methods ********************************
76  
77    // --- init -----------------------------------------------------------------
78  
79    private static void checkArguments(final ExecutableElement declaredUutMethod)
80    {
81      if (null == declaredUutMethod)
82      {
83        throw new IllegalArgumentException(
84            "The method, declared in the test case, that points to the UUT is required.");
85      }
86    }
87  
88    private static TypeElement initTypeElement(
89        final ExecutableElement declaredUutMethod)
90      throws IllegalArgumentException
91    {
92      checkArguments(declaredUutMethod);
93  
94      Element current = declaredUutMethod;
95      while (current != null)
96      {
97        final ElementKind kind = current.getKind();
98        if (ElementKind.CLASS.equals(kind))
99        {
100         return (TypeElement) current;
101       }
102       current = current.getEnclosingElement();
103     }
104 
105     throw new IllegalArgumentException("Declaration of UUT '"
106                                        + declaredUutMethod.asType()
107                                        + "' is not within a test case class.");
108   }
109 
110   // --- get&set --------------------------------------------------------------
111 
112   // --- business -------------------------------------------------------------
113 
114   /**
115    * Reads the test documentation from the element the extractor instance is
116    * associated with.
117    *
118    * @return the test documentation read.
119    */
120   public UnitTestDoc readTestDoc()
121   {
122     final UutInfo info = uutExtractor.read(declaredTestCaseMethod);
123     final List<ScenarioTestDoc> scenarios = calcScenario(info);
124     final String type = info.getType();
125     if (type != null)
126     {
127       final UnitTestDoc testDoc = new UnitTestDoc(type, scenarios);
128       return testDoc;
129     }
130 
131     return null;
132   }
133 
134   private List<ScenarioTestDoc> calcScenario(final UutInfo uutInfo)
135   {
136     final List<ScenarioTestDoc> scenarios = new ArrayList<ScenarioTestDoc>();
137 
138     final String testCaseTypeName = calcTestCaseType();
139     final ScenarioTestDoc scenario =
140         createScenarioIfElementIsTestMethod(uutInfo, testCaseTypeName,
141             declaredTestCaseMethod);
142 
143     if (scenario != null)
144     {
145       scenarios.add(scenario);
146     }
147 
148     return scenarios;
149   }
150 
151   private String calcTestCaseType()
152   {
153     return testCaseType.asType().toString();
154   }
155 
156   // --- object basics --------------------------------------------------------
157 
158 }