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.report.index;
17  
18  import java.util.Map;
19  import java.util.TreeMap;
20  
21  import de.smartics.testdoc.core.doc.Type;
22  import de.smartics.testdoc.core.doc.UnitTestDoc;
23  
24  /**
25   * Generates an index on the packages the UUT belong to.
26   *
27   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
28   * @version $Revision:591 $
29   */
30  public class UutPackageIndex implements ExportIndex
31  {
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    /**
37     * The class version identifier.
38     * <p>
39     * The value of this constant is {@value}.
40     * </p>
41     */
42    private static final long serialVersionUID = 1L;
43  
44    /**
45     * The name of the section generated by this index.
46     * <p>
47     * The value of this constant is {@value}.
48     * </p>
49     */
50    public static final String SECTION_NAME = "uutPackage";
51  
52    // --- members --------------------------------------------------------------
53  
54    /**
55     * The index is ordered by the natural order of package names and within that
56     * by the natural order of type names.
57     *
58     * @serial
59     */
60    private final Map<String, Map<String, UnitTestDoc>> index =
61        new TreeMap<String, Map<String, UnitTestDoc>>();
62  
63    // ****************************** Initializer *******************************
64  
65    // ****************************** Constructors ******************************
66  
67    /**
68     * Default constructor.
69     */
70    public UutPackageIndex()
71    {
72    }
73  
74    // ****************************** Inner Classes *****************************
75  
76    // ********************************* Methods ********************************
77  
78    // --- init -----------------------------------------------------------------
79  
80    // --- get&set --------------------------------------------------------------
81  
82    /**
83     * {@inheritDoc}
84     *
85     * @see de.smartics.testdoc.report.index.ExportIndex#getSectionName()
86     */
87    @Override
88    public String getSectionName()
89    {
90      return SECTION_NAME;
91    }
92  
93    // --- business -------------------------------------------------------------
94  
95    /**
96     * {@inheritDoc}
97     *
98     * @see de.smartics.testdoc.report.index.ExportIndex#addToIndex(de.smartics.testdoc.core.doc.UnitTestDoc)
99     */
100   @Override
101   public void addToIndex(final UnitTestDoc testDoc)
102   {
103     final Type type = testDoc.getUutType();
104     final String packageName = type.getPackageName();
105     final Map<String, UnitTestDoc> nameIndex = getNameIndex(packageName);
106 
107     final String typeName = type.getTypeName();
108     nameIndex.put(typeName, testDoc);
109   }
110 
111   private Map<String, UnitTestDoc> getNameIndex(final String packageName)
112   {
113     Map<String, UnitTestDoc> nameIndex = index.get(packageName);
114     if (nameIndex == null)
115     {
116       nameIndex = new TreeMap<String, UnitTestDoc>();
117       index.put(packageName, nameIndex);
118     }
119 
120     return nameIndex;
121   }
122 
123   /**
124    * {@inheritDoc}
125    *
126    * @see de.smartics.testdoc.report.index.ExportIndex#getSection()
127    */
128   @Override
129   public Section<UnitTestDoc> getSection()
130   {
131     final Section<UnitTestDoc> section =
132         new StaticSection<UnitTestDoc>(getSectionName());
133     for (final Map.Entry<String, Map<String, UnitTestDoc>> packageIndexEntry : index
134         .entrySet())
135     {
136       final String packageName = packageIndexEntry.getKey();
137       final Section<UnitTestDoc> subSection =
138           new StaticSection<UnitTestDoc>(packageName);
139       final Map<String, UnitTestDoc> nameIndex = packageIndexEntry.getValue();
140       for (final UnitTestDoc testDoc : nameIndex.values())
141       {
142         subSection.addItem(testDoc);
143       }
144       section.addSubSection(subSection);
145     }
146     return section;
147   }
148 
149   /**
150    * {@inheritDoc}
151    *
152    * @see de.smartics.testdoc.report.index.ExportIndex#isEmpty()
153    */
154   @Override
155   public boolean isEmpty()
156   {
157     return index.isEmpty();
158   }
159 
160   // --- object basics --------------------------------------------------------
161 
162 }