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.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  
23  import de.smartics.testdoc.report.junit.JUnitTestMethodDoc.ResultType;
24  
25  /**
26   * Helps to provide links to images.
27   */
28  public class ImageHelper
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    /**
35     * The prefix for link keys of type test case.
36     * <p>
37     * The value of this constant is {@value}.
38     * </p>
39     */
40    private static final String TESTCASE_PREFIX = "testcase.";
41  
42    /**
43     * The key to select the link to an image that signals a test case success.
44     * <p>
45     * The value of this constant is {@value}.
46     * </p>
47     */
48    public static final String TESTCASE_SUCCESS = TESTCASE_PREFIX + "SUCCESS";
49  
50    /**
51     * The key to select the link to an image that signals that the test case has
52     * been skipped.
53     * <p>
54     * The value of this constant is {@value}.
55     * </p>
56     */
57    public static final String TESTCASE_SKIPPED = TESTCASE_PREFIX + "SKIPPED";
58  
59    /**
60     * The key to select the link to an image that signals a test case failure.
61     * <p>
62     * The value of this constant is {@value}.
63     * </p>
64     */
65    public static final String TESTCASE_FAILURE = TESTCASE_PREFIX + "FAILURE";
66  
67    /**
68     * The key to select the link to an image that signals a test case error.
69     * <p>
70     * The value of this constant is {@value}.
71     * </p>
72     */
73    public static final String TESTCASE_ERROR = TESTCASE_PREFIX + "ERROR";
74  
75    // --- members --------------------------------------------------------------
76  
77    /**
78     * The link to the root directory. The value is either <code>null</code> or
79     * ends with a slash.
80     */
81    private final String upLink;
82  
83    /**
84     * Maps a key to the configured link to an image. The links are relative to
85     * the root dir.
86     */
87    private final Map<String, String> links = new HashMap<String, String>();
88  
89    // ****************************** Initializer *******************************
90  
91    // ****************************** Constructors ******************************
92  
93    /**
94     * Default constructor.
95     */
96    public ImageHelper(final String upLink)
97    {
98      if (upLink == null || (".".equals(upLink) || StringUtils.isBlank(upLink)))
99      {
100       this.upLink = null;
101     }
102     else
103     {
104       if (!upLink.endsWith("/"))
105       {
106         this.upLink = upLink + '/';
107       }
108       else
109       {
110         this.upLink = upLink;
111       }
112     }
113 
114     initLinks();
115   }
116 
117   // ****************************** Inner Classes *****************************
118 
119   // ********************************* Methods ********************************
120 
121   // --- init -----------------------------------------------------------------
122 
123   private void initLinks()
124   {
125     addLink(TESTCASE_SUCCESS, "images/icon_success_sml.gif");
126     addLink(TESTCASE_SKIPPED, "images/icon_warning_sml.gif");
127     addLink(TESTCASE_FAILURE, "images/icon_error_sml.gif");
128     addLink(TESTCASE_ERROR, "images/icon_error_sml.gif");
129   }
130 
131   // --- get&set --------------------------------------------------------------
132 
133   // --- business -------------------------------------------------------------
134 
135   /**
136    * Adds the link to the map. If a link with the given identifier is already
137    * present, the new link replaces the current link.
138    *
139    * @param key the identifier of the link.
140    * @param link the link relative to the root directory.
141    */
142   public final void addLink(final String key, final String link)
143   {
144     final String normalizedLink;
145     if (upLink == null)
146     {
147       normalizedLink = link;
148     }
149     else
150     {
151       normalizedLink = upLink + link;
152     }
153 
154     links.put(key, normalizedLink);
155   }
156 
157   /**
158    * Returns the link for the given identifier.
159    *
160    * @param key the identifier of the link.
161    * @return link the link relative to the root directory or <code>null</code>
162    *         if no link is registered for that <code>key</code>.
163    */
164   public String getImageLink(final String key)
165   {
166     return links.get(key);
167   }
168 
169   /**
170    * Returns the link for the given identifier.
171    *
172    * @param key the identifier of the link.
173    * @return link the link relative to the root directory or <code>null</code>
174    *         if no link is registered for that <code>key</code>.
175    */
176   public String getImageLink(final ResultType key)
177   {
178     return links.get(TESTCASE_PREFIX + key);
179   }
180 
181   // --- object basics --------------------------------------------------------
182 
183 }