View Javadoc

1   /*
2    * Copyright 1999-2002,2004 The Apache Software Foundation.
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 org.apache.commons.latka;
17  
18  import org.apache.commons.latka.event.LatkaEventInfo;
19  import org.apache.commons.latka.event.LatkaEventListener;
20  import org.apache.commons.latka.event.ReportMessageEvent;
21  import org.apache.commons.latka.event.RequestEvent;
22  import org.apache.commons.latka.event.SuiteEvent;
23  import org.apache.commons.latka.http.Request;
24  import org.apache.commons.latka.http.Session;
25  
26  import java.util.HashMap;
27  import java.util.LinkedList;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.log4j.Category;
32  
33  /**
34   * The base class for several 'reporters'.
35   *
36   * A reporter is a class that will store or record information about events that
37   * occur during Latka processing
38   *
39   * @author Rodney Waldhoff
40   * @author Morgan Delagrange
41   * @version $Revision: 155424 $
42   */
43  public class DefaultLatkaEventInfo implements LatkaEventInfo {
44  
45      /**
46       * maps the request to it's success or failure as a Boolean
47       */
48      protected Map _requestSucceeded = new HashMap();
49  
50      /**
51       * maps the session to it's success or failure as a Boolean
52       */
53      protected Map _sessionSucceeded = new HashMap();
54  
55      /**
56       * Holds whether or not the entire suite has succeeded
57       * This is set to false when <strong>any</strong> request fails.
58       */
59      protected boolean _suiteSucceeded = true;
60  
61      /**
62       * Holds the failed responses received by this object.
63       * @see #requestFailed(RequestEvent)
64       */
65      protected List _failedResponses = new LinkedList();
66  
67      protected LatkaEventListener _listener = null;
68  
69      /**
70       * The log4J category used for processing log requests.
71       */
72      protected static final Category _log =
73                                  Category.getInstance(DefaultLatkaEventInfo.class);
74  
75      public DefaultLatkaEventInfo(LatkaEventListener listener) {
76          _listener = listener;
77      }
78  
79      /**
80       * Invoked if the request succeeds.
81       * Records the success
82       *
83       * @param event  a successful request event
84       */
85      public void requestSucceeded(RequestEvent event) {
86          recordSuccess(event, true);
87          _listener.requestSucceeded(event);
88      }
89  
90      /**
91       * Invoked if the request failed.
92       * Records the failure and adds the request's response to the list of
93       * failed responses
94       *
95       * @param event  a "failed" request event.
96       */
97      public void requestFailed(RequestEvent event) {
98          recordSuccess(event, false);
99          _failedResponses.add(event.getResponse());
100         _listener.requestFailed(event);
101     }
102 
103     /**
104      * A skipped request.
105      * Records the skip as a failure
106      *
107      * @param event  a "skipped" request.
108      */
109     public void requestSkipped(RequestEvent event) {
110         recordSuccess(event, false);
111         _listener.requestSkipped(event);
112     }
113 
114     /**
115      * Invoked if a request error occurs.
116      * Records the error as a failure
117      *
118      * @param event  a request "error" event.
119      */
120     public void requestError(RequestEvent event) {
121         recordSuccess(event, false);
122         _listener.requestError(event);
123     }
124 
125     /**
126      * Invoked if a Latka suite wants to send a message to the 
127      * report generated for the test.  Some implementations
128      * of the LatkaEventListener may not generate reports.
129      * 
130      * @param event  Event containing the report message
131      */
132     public void reportMessage(ReportMessageEvent event) {
133         _listener.reportMessage(event);
134     }
135 
136     /**
137      * Invoke when all requests completed.
138      *
139      * @param event suite event
140      */
141     public void suiteCompleted(SuiteEvent event) {
142         _listener.suiteCompleted(event);
143     }
144 
145     /**
146      * Record the "success status" of a request event.
147      * On a failed request, the current suite, request and session are
148      * marked as failed
149      *
150      * @param event  a request event.
151      * @param bool   the success (<code>true</code>) or failure
152      * (<code>false</code>) of the request.
153      */
154     protected void recordSuccess (RequestEvent event, boolean bool) {
155         if (bool == false) {
156             _suiteSucceeded = false;
157             _requestSucceeded.put(event.getRequest(), new Boolean(bool));
158             _sessionSucceeded.put(event.getSession(), new Boolean(bool));
159             _log.info("request failed");
160         }
161     }
162 
163     /**
164      * Check to see if a particular Request succeeded or failed.
165      *
166      * @param request the request to check for success or
167      * failure
168      * @return true if request succeeded
169      */
170     public boolean didRequestSucceed(Request request) {
171         Boolean bool = (Boolean) _requestSucceeded.get(request);
172 
173         if (bool == null || bool.booleanValue() == true) {
174             return true;
175         } else {
176             return false;
177         }
178 
179     }
180 
181     /**
182      * Check to see if a particular Session succeeded or failed.
183      * Once a request inside a session fails, the session itself
184      * is marked as a failure.
185      *
186      * @param session the session to check for success or
187      * failure
188      * @return true if all requests in the session succeeded
189      */
190     public boolean didSessionSucceed(Session session) {
191         Boolean bool = (Boolean) _sessionSucceeded.get(session);
192 
193         if (bool == null || bool.booleanValue() == true) {
194             return true;
195         } else {
196             return false;
197         }
198     }
199 
200     /**
201      * Returns true if all Requests in the suite succeed.
202      *
203      * @return true if all Requests have succeeded
204      */
205     public boolean didSuiteSucceed() {
206         return _suiteSucceeded;
207     }
208 
209     /**
210      * List failed responses
211      * @return a list of all responses of failed requests
212      */
213     public List getFailedResponses() {
214         return _failedResponses;
215     }
216 }