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.export.doc;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.NullArgumentException;
25  
26  import de.smartics.maven.util.report.link.ExternalReport;
27  import de.smartics.maven.util.report.link.ExternalReportFactory;
28  import de.smartics.maven.util.report.link.LinkConstructorStrategy;
29  import de.smartics.maven.util.report.link.ReportId;
30  
31  /**
32   * Manages report instances and their grouping.
33   */
34  public class ExternalReportReferences
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    /**
41     * An empty report reference instance that prohibits adding information.
42     */
43    public static final ExternalReportReferences EMPTY =
44        new ExternalReportReferences(null)
45        {
46          public boolean registerReport(final boolean create,
47              final ReportId reportId, final LinkConstructorStrategy strategy)
48          {
49            throw new UnsupportedOperationException(
50                "It is not allowed to add to the empty report references.");
51          }
52  
53          public void addUutReportId(final ReportId reportId)
54          {
55            throw new UnsupportedOperationException(
56                "It is not allowed to add report IDs.");
57          }
58  
59          public void addTestCaseReportId(final ReportId reportId)
60          {
61            throw new UnsupportedOperationException(
62                "It is not allowed to add report IDs.");
63          }
64        };
65  
66    // --- members --------------------------------------------------------------
67  
68    /**
69     * The factory to create new reports.
70     */
71    private final ExternalReportFactory factory;
72  
73    /**
74     * The map of all registered reports.
75     */
76    private final Map<ReportId, ExternalReport> reports =
77        new HashMap<ReportId, ExternalReport>();
78  
79    /**
80     * The ordered list of report IDs to render for the UUT.
81     */
82    private final List<ReportId> uutReportIds = new ArrayList<ReportId>();
83  
84    /**
85     * The ordered list of report IDs to render for the test case.
86     */
87    private final List<ReportId> testCaseReportIds = new ArrayList<ReportId>();
88  
89    // ****************************** Initializer *******************************
90  
91    // ****************************** Constructors ******************************
92  
93    /**
94     * Default constructor.
95     */
96    public ExternalReportReferences(final ExternalReportFactory factory)
97    {
98      this.factory = factory;
99    }
100 
101   // ****************************** Inner Classes *****************************
102 
103   // ********************************* Methods ********************************
104 
105   // --- init -----------------------------------------------------------------
106 
107   // --- get&set --------------------------------------------------------------
108 
109   /**
110    * Returns the list of report IDs that are to referenced from a UUT.
111    *
112    * @return the list of report IDs
113    */
114   public List<ReportId> getUutReportIds()
115   {
116     return Collections.unmodifiableList(uutReportIds);
117   }
118 
119   /**
120    * Returns the list of report IDs that are to referenced from a test case.
121    *
122    * @return the list of report IDs
123    */
124   public List<ReportId> getTestCaseReportIds()
125   {
126     return Collections.unmodifiableList(testCaseReportIds);
127   }
128 
129   /**
130    * Returns the ordered list of report IDs to render for the UUT.
131    *
132    * @return the ordered list of report IDs to render for the UUT.
133    */
134   public List<ExternalReport> getUutReports()
135   {
136     return getReports(getUutReportIds());
137   }
138 
139   /**
140    * Returns the ordered list of report IDs to render for the test case.
141    *
142    * @return the ordered list of report IDs to render for the test case.
143    */
144   public List<ExternalReport> getTestCaseReports()
145   {
146     return getReports(getTestCaseReportIds());
147   }
148 
149   // --- business -------------------------------------------------------------
150 
151   /**
152    * Adds the report ID to the list of IDs to render UUT report references.
153    *
154    * @param reportId the report ID to add.
155    * @throws NullArgumentException if {@code reportId} is <code>null</code>.
156    */
157   public void addUutReportId(final ReportId reportId)
158     throws NullArgumentException
159   {
160     if (reportId == null)
161     {
162       throw new NullArgumentException("reportId"); // NOPMD
163     }
164 
165     uutReportIds.add(reportId);
166   }
167 
168   /**
169    * Adds the report ID to the list of IDs to render test case report
170    * references.
171    *
172    * @param reportId the report ID to add.
173    * @throws NullArgumentException if {@code reportId} is <code>null</code>.
174    */
175   public void addTestCaseReportId(final ReportId reportId)
176     throws NullArgumentException
177   {
178     if (reportId == null)
179     {
180       throw new NullArgumentException("reportId"); // NOPMD
181     }
182     testCaseReportIds.add(reportId);
183   }
184 
185   /**
186    * Registers a report with the given identifier and strategy.
187    * <p>
188    * Note that no report will be registered, if the factory of this instance is
189    * not set.
190    * </p>
191    *
192    * @param reportId the identifier of the report to register.
193    * @param strategy the link constructor strategy to use.
194    * @return <code>true</code> if the report has been created and registered,
195    *         <code>false</code> otherwise.
196    */
197   public boolean registerReport(final ReportId reportId,
198       final LinkConstructorStrategy strategy)
199   {
200     return registerReport(true, reportId, strategy);
201   }
202 
203   /**
204    * Registers a report with the given identifier and strategy with a guard. The
205    * guard allows to conveniently call this method without the burden of
206    * specifying an if block on the caller's side.
207    * <p>
208    * Note that no report will be registered, if the factory of this instance is
209    * not set.
210    * </p>
211    *
212    * @param create convenience flag to not register the instance.
213    * @param reportId the identifier of the report to register.
214    * @param strategy the link constructor strategy to use.
215    * @return <code>true</code> if the report has been created and registered,
216    *         <code>false</code> otherwise.
217    */
218   public boolean registerReport(final boolean create, final ReportId reportId,
219       final LinkConstructorStrategy strategy)
220   {
221     if (create && factory != null)
222     {
223       if (reportId == null)
224       {
225         throw new NullArgumentException("reportId"); // NOPMD
226       }
227 
228       final ExternalReport report =
229           factory.createExternalReport(reportId, strategy);
230       final boolean created = report != null;
231       if (created)
232       {
233         reports.put(reportId, report);
234       }
235       return created;
236     }
237 
238     return false;
239   }
240 
241   /**
242    * Returns the report with the given ID.
243    *
244    * @param reportId the ID of the requested report.
245    * @return the requested report instance or <code>null</code> if the ID does
246    *         not reference to such an instance.
247    */
248   public ExternalReport get(final ReportId reportId)
249   {
250     return reports.get(reportId);
251   }
252 
253   /**
254    * Returns the list of reports with the given IDs.
255    *
256    * @param reportIds the IDs of the requested reports.
257    * @return the list of reports, which may be empty, but is never
258    *         <code>null</code>.
259    */
260   public List<ExternalReport> getReports(final List<ReportId> reportIds)
261   {
262     final List<ExternalReport> reports =
263         new ArrayList<ExternalReport>(reportIds.size());
264 
265     for (final ReportId reportId : reportIds)
266     {
267       reports.add(get(reportId));
268     }
269 
270     return reports;
271   }
272 
273   // --- object basics --------------------------------------------------------
274 
275 }