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.maven.export.sink;
17  
18  import java.io.IOException;
19  import java.util.List;
20  import java.util.Set;
21  
22  import de.smartics.maven.util.report.MessageHelper;
23  import de.smartics.testdoc.core.doc.Type;
24  import de.smartics.testdoc.core.doc.UnitTestDoc;
25  import de.smartics.testdoc.report.export.doc.InformationFilter;
26  import de.smartics.testdoc.report.index.ExportIndex;
27  import de.smartics.testdoc.report.index.ExportMultiIndex;
28  import de.smartics.testdoc.report.index.Section;
29  
30  /**
31   * Renders the page with the test documentation.
32   *
33   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
34   * @version $Revision:591 $
35   */
36  class TestDocPageRenderer extends AbstractPageRenderer
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    // --- members --------------------------------------------------------------
43  
44    /**
45     * The exporter that knows the format of the file to be written.
46     */
47    protected final SinkExporter exporter;
48  
49    /**
50     * Helper for rendering page parts.
51     */
52    private final RenderHelper helper;
53  
54    // ****************************** Initializer *******************************
55  
56    // ****************************** Constructors ******************************
57  
58    /**
59     * Default constructor.
60     *
61     * @param reportName
62     * @param messages the resource bundles with messages to display as labels in
63     *          the generated report.
64     * @param informationFilter the configuration that tells which report
65     *          information is to be displayed.
66     * @param exporter the exporter that knows the format of the file to be
67     *          written.
68     */
69    TestDocPageRenderer(final String reportName, final MessageHelper messages,
70        final SinkExporter exporter)
71    {
72      super(reportName, messages, exporter.getSink());
73      this.exporter = exporter;
74      this.helper = new RenderHelper(messages, sink);
75  
76    }
77  
78    // ****************************** Inner Classes *****************************
79  
80    // ********************************* Methods ********************************
81  
82    // --- init -----------------------------------------------------------------
83  
84    // --- get&set --------------------------------------------------------------
85  
86    // --- business -------------------------------------------------------------
87  
88    void renderPage(final ExportMultiIndex index, final Set<UnitTestDoc> testDocs)
89      throws IOException
90    {
91      renderPageStart();
92      renderBody(index, testDocs);
93      renderPageEndAndFinalize();
94    }
95  
96    private void renderBody(final ExportMultiIndex index,
97        final Set<UnitTestDoc> testDocs) throws IOException
98    {
99      renderBodyStart();
100 
101     renderIndexes(index);
102     for (final Section<UnitTestDoc> section : index.getSection()
103         .getSubSections())
104     {
105       renderSections(section, 2);
106     }
107     renderTestDocs(testDocs);
108 
109     renderBodyEnd();
110   }
111 
112   private void renderIndexes(final ExportMultiIndex index)
113   {
114     renderIndexOfIndexes(index);
115 
116     final Section<UnitTestDoc> section = index.getSection();
117     for (final Section<UnitTestDoc> subSection : section.getSubSections())
118     {
119       renderSectionIndex(subSection);
120     }
121   }
122 
123   private void renderIndexOfIndexes(final ExportMultiIndex index)
124   {
125     final InformationFilter filter = exporter.getInformationFilter();
126     if (filter.isShowIndexOfIndices() && !index.isEmpty())
127     {
128       sink.section2();
129 
130       sink.anchor("indexOfIndexes");
131       sink.anchor_();
132       sink.sectionTitle2();
133       sink.text(messages.getLabel("report.indexOfIndexes"));
134       sink.sectionTitle2_();
135 
136       helper.renderIndexTableStart();
137       int counter = 1;
138       for (final ExportIndex exportIndex : index.getIndexes())
139       {
140         if (!exportIndex.isEmpty())
141         {
142           final String name = exportIndex.getSectionName();
143           final String label = messages.getLabel(name);
144           helper.renderIndexRow(counter, label);
145           counter++;
146         }
147       }
148       helper.renderIndexTableEnd();
149 
150       sink.section2_();
151     }
152   }
153 
154   private void renderSectionIndex(final Section<UnitTestDoc> section)
155   {
156     final List<Section<UnitTestDoc>> subSections = section.getSubSections();
157     if (!subSections.isEmpty())
158     {
159       sink.section3();
160 
161       final String id = section.getName();
162       helper.renderAnchor(id);
163       sink.sectionTitle3();
164       sink.text(messages.getLabel("index." + id));
165       sink.sectionTitle3_();
166 
167       helper.renderIndexTableStart();
168       int counter = 1;
169       for (final Section<UnitTestDoc> subSection : subSections)
170       {
171         final String name = subSection.getName();
172         helper.renderIndexRow(counter, name);
173         counter++;
174       }
175       helper.renderIndexTableEnd();
176 
177       sink.section3_();
178     }
179   }
180 
181   private void renderSections(final Section<UnitTestDoc> section,
182       final int level)
183   {
184     final String name = section.getName();
185     final String sectionLabel = messages.getLabel(name);
186     helper.renderSectionStart(level, sectionLabel);
187 
188     if (!section.containsSubSections())
189     {
190       helper.renderIndexTableStart();
191       int counter = 1;
192       for (final UnitTestDoc testDoc : section.getItems())
193       {
194         final Type type = testDoc.getUutType();
195         final String label = type.getTypeName();
196         final String id = type.toString();
197         helper.renderIndexRow(counter, label, id);
198         counter++;
199       }
200       helper.renderIndexTableEnd();
201     }
202     else
203     {
204       final int nextLevel = level + 1;
205       for (final Section<UnitTestDoc> subSection : section.getSubSections())
206       {
207         renderSections(subSection, nextLevel);
208       }
209     }
210 
211     helper.renderSectionEnd(level);
212   }
213 
214   private void renderTestDocs(final Set<UnitTestDoc> testDocs)
215     throws IOException
216   {
217     sink.section2();
218 
219     helper.renderAnchor("unitTestDoc");
220     sink.sectionTitle2();
221     sink.text(messages.getLabel("report.unitTestDoc"));
222     sink.sectionTitle2_();
223 
224     for (final UnitTestDoc testDoc : testDocs)
225     {
226       exporter.export(testDoc);
227     }
228 
229     sink.section2_();
230   }
231 
232   // --- object basics --------------------------------------------------------
233 
234 }