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.processor;
17  
18  import java.util.HashSet;
19  import java.util.Set;
20  import java.util.logging.Logger;
21  
22  import javax.annotation.processing.RoundEnvironment;
23  import javax.lang.model.element.Element;
24  import javax.lang.model.element.ElementKind;
25  import javax.lang.model.element.ExecutableElement;
26  import javax.lang.model.element.TypeElement;
27  import javax.lang.model.element.VariableElement;
28  
29  import de.smartics.testdoc.collect.extractor.ExtractorConfig;
30  import de.smartics.testdoc.collect.extractor.TestDocMethodExtractor;
31  import de.smartics.testdoc.collect.extractor.TestDocTypeExtractor;
32  import de.smartics.testdoc.collect.extractor.TestDocVariableExtractor;
33  import de.smartics.testdoc.core.doc.ScenarioTestDoc;
34  import de.smartics.testdoc.core.doc.Type;
35  import de.smartics.testdoc.core.doc.UnitTestDoc;
36  import de.smartics.testdoc.core.export.ExportAdapter;
37  
38  /**
39   * Processes the UUT annotations passed to the annotation processor.
40   *
41   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
42   * @version $Revision:591 $
43   */
44  public class UutProcess
45  {
46    // ********************************* Fields *********************************
47  
48    // --- constants ------------------------------------------------------------
49  
50    /**
51     * Class logger.
52     */
53    private static final Logger LOG = Logger
54        .getLogger(UutProcess.class.getName());
55  
56    // --- members --------------------------------------------------------------
57  
58    /**
59     * The set of types that have already been cleaned during the execution of
60     * this process. Each type has to be removed before it is processed so that no
61     * removed information persists in the index.
62     */
63    private final Set<Type> alreadyClearedTestCaseTypes = new HashSet<Type>();
64  
65    /**
66     * The logger to use to log to the processor context.
67     */
68    private final ProcessorLogHelper processorLog;
69  
70    /**
71     * The adapter to export unit test documentation.
72     */
73    private final ExportAdapter exporter;
74  
75    /**
76     * Utilities to extract information.
77     */
78    private final ExtractorConfig extractorConfig;
79  
80    // ****************************** Initializer *******************************
81  
82    // ****************************** Constructors ******************************
83  
84    /**
85     * Default constructor.
86     */
87    public UutProcess(final ProcessorLogHelper processorLog,
88        final ExportAdapter exporter, final ExtractorConfig extractorConfig)
89    {
90      this.processorLog = processorLog;
91      this.exporter = exporter;
92      this.extractorConfig = extractorConfig;
93    }
94  
95    // ****************************** Inner Classes *****************************
96  
97    // ********************************* Methods ********************************
98  
99    // --- init -----------------------------------------------------------------
100 
101   // --- get&set --------------------------------------------------------------
102 
103   // --- business -------------------------------------------------------------
104 
105   public void processElements(final RoundEnvironment roundEnv,
106       final TypeElement annotation)
107   {
108     final AnnotationLogHelper log =
109         new AnnotationLogHelper(LOG, processorLog, annotation);
110 
111     final Set<? extends Element> elements =
112         roundEnv.getElementsAnnotatedWith(annotation);
113     for (final Element element : elements)
114     {
115       final ElementKind kind = element.getKind();
116 
117       log.logProcessingElementEntry(element, kind);
118 
119       if (ElementKind.CLASS.equals(kind))
120       {
121         process((TypeElement) element);
122       }
123       else if (ElementKind.METHOD.equals(kind))
124       {
125         process((ExecutableElement) element);
126       }
127       else if (ElementKind.FIELD.equals(kind))
128       {
129         process((VariableElement) element);
130       }
131       else if (ElementKind.LOCAL_VARIABLE.equals(kind))
132       {
133         // Does not work currently.
134         process((VariableElement) element);
135       }
136       else
137       {
138         log.note("Unsupported annotation target '"
139                  + element.getClass().getName() + "'.");
140       }
141       log.logProcessingElementExit();
142     }
143   }
144 
145   private void process(final TypeElement testCaseType)
146   {
147     final TestDocTypeExtractor extractor =
148         new TestDocTypeExtractor(extractorConfig, testCaseType);
149     final UnitTestDoc testDoc = extractor.readTestDoc();
150     if (testDoc != null)
151     {
152       clearIfRequired(testDoc);
153       exporter.export(testDoc);
154     }
155   }
156 
157   private void process(final ExecutableElement element)
158   {
159     final ElementKind kind = element.getKind();
160     if (ElementKind.METHOD.equals(kind))
161     {
162       final TestDocMethodExtractor extractor =
163           new TestDocMethodExtractor(extractorConfig, element);
164       final UnitTestDoc testDoc = extractor.readTestDoc();
165       if (testDoc != null)
166       {
167         clearIfRequired(testDoc);
168         exporter.export(testDoc);
169       }
170     }
171   }
172 
173   private void process(final VariableElement element)
174   {
175     final ElementKind kind = element.getKind();
176     if (ElementKind.FIELD.equals(kind))
177     {
178       final TestDocVariableExtractor extractor =
179           new TestDocVariableExtractor(extractorConfig, element);
180       final UnitTestDoc testDoc = extractor.readTestDoc();
181       clearIfRequired(testDoc);
182       exporter.export(testDoc);
183     }
184     // else if (ElementKind.LOCAL_VARIABLE.equals(kind))
185     // {
186     // // Does not work currently.
187     // }
188     // else
189     // {
190     // note("Unsupported annotation target type '" + kind.toString()
191     // + "' for a variable.");
192     // }
193   }
194 
195   private void clearIfRequired(final UnitTestDoc testDoc)
196   {
197     for (ScenarioTestDoc scenario : testDoc.getScenarios())
198     {
199       final Type testCaseType = scenario.getTestCaseType();
200       if (!alreadyClearedTestCaseTypes.contains(testCaseType))
201       {
202         exporter.clear(testCaseType);
203         alreadyClearedTestCaseTypes.add(testCaseType);
204       }
205     }
206   }
207 
208   // --- object basics --------------------------------------------------------
209 
210 }