View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003-2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.result;
20  
21  import net.sf.jameleon.util.JameleonUtility;
22  
23  /***
24   * An implementation of the @see TestResult that represents an HttpRequest. This class keeps track of the resource requested, the function
25   * that requested it and the status code returned by the resource.
26   */
27  public class HttpRequestResult{
28  
29      /***
30       * The id of the tag that created this result
31       */
32      protected String functionId;
33      /***
34       * The url requested
35       */
36      protected String url;
37      /***
38       * The execution time
39       */
40      protected long executionTime;
41      /***
42       * THe status returned by the response
43       */
44      protected int statusCode;
45  
46      /***
47       * Constructor
48       * @param url - the url requested.
49       * @param executionTime - the time taken to return a response.
50       * @param statusCode - the status code returned by the request.
51       * @param functionId - the id of the function point that requested the url.
52       * @param testCaseName - the name of the test case that contained the function point that requested the url.
53       */
54      public HttpRequestResult(String url, long executionTime, int statusCode, String functionId){
55          this.url = url;
56          this.executionTime = executionTime;
57          this.statusCode = statusCode;
58          this.functionId = functionId;
59      }
60  
61      /***
62       * Sets the url requested
63       * @param url - the url requested.
64       */
65      public void setUrl(String url){
66          this.url = url;
67      }
68  
69      /***
70       * Sets the status code returned by the url requested.
71       * @param statusCode the status code returned by the url requested.
72       */
73      public void setStatusCode(int statusCode){
74          this.statusCode = statusCode;
75      }
76  
77      /***
78       * @return a String representation of the url results.
79       */
80      public String toString(){
81          StringBuffer sb = new StringBuffer();
82          sb.append("Function ID: ").append(functionId).append("\n");
83          sb.append("Requested URL: ").append(url).append("\n");
84          sb.append("Resposne Time: ").append(JameleonUtility.executionTimeToString(executionTime)).append("\n");
85          sb.append("Response Code: ").append(new Integer(statusCode)).append("\n");
86          return sb.toString();
87      }
88  
89      /***
90       * @return an XML representation of the results.
91       */
92      public String toXML(){
93          StringBuffer str = new StringBuffer("\n\t");
94          str.append("<http-request>\n");
95          str.append("\t\t<functionId>").append(functionId).append("</functionId>\n");
96          str.append("\t\t<requested-url>").append(escapeXML(url)).append("</requested-url>\n");
97          str.append("\t\t<response-time>").append(JameleonUtility.executionTimeToString(executionTime)).append("</response-time>\n");
98          str.append("\t\t<response-code>").append(new Long(statusCode)).append("</response-code>\n");
99          str.append("\t</http-request>\n");
100         return str.toString();
101     }
102 
103     // Append to the given StringBuffer an escaped version of the
104     // given text string where XML special characters have been escaped
105     // For a null string we appebd "<null>"
106     protected String escapeXML(String text) {
107         String result = text;
108         if (result == null) {
109             result = "<null>";
110         }else{
111             result = result.replaceAll("<","&lt;");
112             result = result.replaceAll(">","&gt;");
113             result = result.replaceAll("&","&amp;");
114         }
115         return result;
116     }
117 
118 }