View Javadoc

1   /*
2    * Copyright 2012-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.maven.issue.command;
17  
18  import java.io.Serializable;
19  
20  import de.smartics.maven.issue.command.AbstractCommand.Expectation;
21  
22  /**
23   * The result of the execution of a {@link Command}.
24   *
25   * @param <T> the type of the command this is a result for.
26   */
27  public final class CommandResult<T extends Command<T>> implements Serializable
28  {
29  
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    /**
35     * The class version identifier.
36     * <p>
37     * The value of this constant is {@value}.
38     * </p>
39     */
40    private static final long serialVersionUID = 1L;
41  
42    // --- members --------------------------------------------------------------
43  
44    /**
45     * The return code.
46     *
47     * @serial
48     */
49    private final int code;
50  
51    /**
52     * The information about the HTTP status returned by the result.
53     *
54     * @serial
55     */
56    private final HttpStatus httpStatus;
57  
58    /**
59     * The information about the returned page.
60     *
61     * @serial
62     */
63    private final Page page;
64  
65    /**
66     * The expected information on the page. If the page title of the expectation
67     * differs from {@link #getTitle() title}, the result is not as it has been
68     * expected.
69     *
70     * @serial
71     */
72    private final Expectation expectation;
73  
74    /**
75     * The description to the result. This description explains what happened in a
76     * short sentence. This is the essence of the command's result. The
77     * description of a command result may be <code>null</code>, if the command
78     * has no substantial result. A non-substantial result, which justifies a
79     * value of <code>null</code>, would be a navigation command to a certain
80     * page.
81     *
82     * @serial
83     */
84    private final String description;
85  
86    // ****************************** Initializer *******************************
87  
88    // ****************************** Constructors ******************************
89  
90    /**
91     * Default constructor.
92     *
93     * @param code the return code.
94     * @param httpStatus the information about the HTTP status returned by the
95     *          result.
96     * @param description the description to the result.
97     * @param page the information about the returned page.
98     * @param expectation the expectations on the returned page.
99     */
100   public CommandResult(final int code, final HttpStatus httpStatus,
101       final String description, final Page page, final Expectation expectation)
102   {
103     this.code = code;
104     this.httpStatus = httpStatus;
105     this.description = description;
106     this.page = page;
107     this.expectation = expectation;
108   }
109 
110   // ****************************** Inner Classes *****************************
111 
112   /**
113    * Provides information about the HTTP status returned by the result.
114    */
115   public static final class HttpStatus implements Serializable
116   {
117     // ******************************** Fields ********************************
118 
119     // --- constants ----------------------------------------------------------
120 
121     /**
122      * The class version identifier.
123      * <p>
124      * The value of this constant is {@value}.
125      * </p>
126      */
127     private static final long serialVersionUID = 1L;
128 
129     // --- members ------------------------------------------------------------
130 
131     /**
132      * The HTTP status code of the result.
133      *
134      * @serial
135      */
136     private final int code;
137 
138     /**
139      * The HTTP status text of the result.
140      *
141      * @serial
142      */
143     private final String text;
144 
145     // ***************************** Initializer ******************************
146 
147     // ***************************** Constructors *****************************
148 
149     /**
150      * Default constructor.
151      *
152      * @param code the HTTP status code of the result.
153      * @param text the HTTP status text of the result.
154      */
155     public HttpStatus(final int code, final String text)
156     {
157       this.code = code;
158       this.text = text;
159     }
160 
161     // ***************************** Inner Classes ****************************
162 
163     // ******************************** Methods *******************************
164 
165     // --- init ---------------------------------------------------------------
166 
167     // --- get&set ------------------------------------------------------------
168 
169     /**
170      * Returns the HTTP status code of the result.
171      *
172      * @return the HTTP status code of the result.
173      */
174     public int getCode()
175     {
176       return code;
177     }
178 
179     /**
180      * Returns the HTTP status text of the result.
181      *
182      * @return the HTTP status text of the result.
183      */
184     public String getText()
185     {
186       return text;
187     }
188 
189     // --- business -----------------------------------------------------------
190 
191     // --- object basics ------------------------------------------------------
192 
193     /**
194      * {@inheritDoc}
195      *
196      * @see java.lang.Object#toString()
197      */
198     @Override
199     public String toString()
200     {
201       return code + ": " + text;
202     }
203   }
204 
205   /**
206    * Page information.
207    */
208   public static final class Page implements Serializable
209   {
210     // ******************************** Fields ********************************
211 
212     // --- constants ----------------------------------------------------------
213 
214     // --- members ------------------------------------------------------------
215 
216     /**
217      * The class version identifier.
218      * <p>
219      * The value of this constant is {@value}.
220      * </p>
221      */
222     private static final long serialVersionUID = 1L;
223 
224     /**
225      * The token generated to prevent request forgery. May be <code>null</code>
226      * if result page provided no token.
227      *
228      * @serial
229      */
230     private final String token;
231 
232     /**
233      * The title of the page.
234      *
235      * @serial
236      */
237     private final String pageTitle;
238 
239     /**
240      * The page returned on the request. Since this may be a performance issue,
241      * this information is not collected by default. Therefore the value may be
242      * <code>null</code>.
243      *
244      * @serial
245      */
246     private final String pageContent;
247 
248     // ***************************** Initializer ******************************
249 
250     // ***************************** Constructors *****************************
251 
252     /**
253      * Default constructor.
254      *
255      * @param token the token generated to prevent request forgery.
256      * @param pageTitle the title of the page.
257      * @param pageContent the page returned on the request.
258      */
259     public Page(final String token, final String pageTitle,
260         final String pageContent)
261     {
262       this.token = token;
263       this.pageTitle = pageTitle;
264       this.pageContent = pageContent;
265     }
266 
267     // ***************************** Inner Classes ****************************
268 
269     // ******************************** Methods *******************************
270 
271     // --- init ---------------------------------------------------------------
272 
273     // --- get&set ------------------------------------------------------------
274 
275     /**
276      * Returns the token generated to prevent request forgery. May be
277      * <code>null</code> if result page provided no token.
278      *
279      * @return the token generated to prevent request forgery.
280      */
281     public String getToken()
282     {
283       return token;
284     }
285 
286     /**
287      * Returns the title of the page.
288      *
289      * @return the title of the page.
290      */
291     public String getTitle()
292     {
293       return pageTitle;
294     }
295 
296     /**
297      * Returns the page returned on the request. Since this may be a performance
298      * issue, this information is not collected by default. Therefore the value
299      * may be <code>null</code>.
300      *
301      * @return the page returned on the request.
302      */
303     public String getContent()
304     {
305       return pageContent;
306     }
307 
308     // --- business -----------------------------------------------------------
309 
310     // --- object basics ------------------------------------------------------
311   }
312 
313   // ********************************* Methods ********************************
314 
315   // --- init -----------------------------------------------------------------
316 
317   // --- get&set --------------------------------------------------------------
318 
319   /**
320    * Returns the return code.
321    *
322    * @return the return code.
323    */
324   public int getCode()
325   {
326     return code;
327   }
328 
329   /**
330    * Returns the information about the HTTP status returned by the result.
331    *
332    * @return the information about the HTTP status returned by the result.
333    */
334   public HttpStatus getHttpStatus()
335   {
336     return httpStatus;
337   }
338 
339   /**
340    * Returns the description to the result. This description explains what
341    * happened in a short sentence. This is the essence of the command's result.
342    * The description of a command result may be <code>null</code>, if the
343    * command has no substantial result. A non-substantial result, which
344    * justifies a value of <code>null</code>, would be a navigation command to a
345    * certain page.
346    *
347    * @return the description to the result.
348    */
349   public String getDescription()
350   {
351     return description;
352   }
353 
354   /**
355    * Returns the information about the returned page.
356    *
357    * @return the information about the returned page.
358    */
359   public Page getPage()
360   {
361     return page;
362   }
363 
364   /**
365    * Returns the token generated to prevent request forgery. May be
366    * <code>null</code> if result page provided no token.
367    *
368    * @return the token generated to prevent request forgery.
369    */
370   public String getToken()
371   {
372     return page.getToken();
373   }
374 
375   /**
376    * Returns the expected title of the page. If this differs from
377    * {@link #getTitle() title}, the result is not as it has been expected.
378    *
379    * @return the expected title of the page.
380    */
381   public String getExpectedPageTitle()
382   {
383     return expectation.getExpectedPageTitle();
384   }
385 
386   /**
387    * Checks if the result is as it has been expected.
388    *
389    * @return <code>true</code> if the result is as expected, <code>false</code>
390    *         otherwise.
391    */
392   public boolean isExpected()
393   {
394     return expectation.isExpected();
395   }
396 
397   // --- business -------------------------------------------------------------
398 
399   // --- object basics --------------------------------------------------------
400 
401   /**
402    * {@inheritDoc}
403    *
404    * @see java.lang.Object#toString()
405    */
406   @Override
407   public String toString()
408   {
409     return httpStatus
410            + " ("
411            + code
412            + "): "
413            + page.getTitle()
414            + (!isExpected() ? " DIFFERS from expected: "
415                               + getExpectedPageTitle() : "");
416   }
417 }