001 /*
002 * Copyright 1999-2002,2004 The Apache Software Foundation.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.apache.commons.latka;
017
018 import org.apache.commons.latka.event.LatkaEventInfo;
019 import org.apache.commons.latka.event.LatkaEventListener;
020 import org.apache.commons.latka.event.ReportMessageEvent;
021 import org.apache.commons.latka.event.RequestEvent;
022 import org.apache.commons.latka.event.SuiteEvent;
023 import org.apache.commons.latka.http.Request;
024 import org.apache.commons.latka.http.Session;
025
026 import java.util.HashMap;
027 import java.util.LinkedList;
028 import java.util.List;
029 import java.util.Map;
030
031 import org.apache.log4j.Category;
032
033 /**
034 * The base class for several 'reporters'.
035 *
036 * A reporter is a class that will store or record information about events that
037 * occur during Latka processing
038 *
039 * @author Rodney Waldhoff
040 * @author Morgan Delagrange
041 * @version $Revision: 155424 $
042 */
043 public class DefaultLatkaEventInfo implements LatkaEventInfo {
044
045 /**
046 * maps the request to it's success or failure as a Boolean
047 */
048 protected Map _requestSucceeded = new HashMap();
049
050 /**
051 * maps the session to it's success or failure as a Boolean
052 */
053 protected Map _sessionSucceeded = new HashMap();
054
055 /**
056 * Holds whether or not the entire suite has succeeded
057 * This is set to false when <strong>any</strong> request fails.
058 */
059 protected boolean _suiteSucceeded = true;
060
061 /**
062 * Holds the failed responses received by this object.
063 * @see #requestFailed(RequestEvent)
064 */
065 protected List _failedResponses = new LinkedList();
066
067 protected LatkaEventListener _listener = null;
068
069 /**
070 * The log4J category used for processing log requests.
071 */
072 protected static final Category _log =
073 Category.getInstance(DefaultLatkaEventInfo.class);
074
075 public DefaultLatkaEventInfo(LatkaEventListener listener) {
076 _listener = listener;
077 }
078
079 /**
080 * Invoked if the request succeeds.
081 * Records the success
082 *
083 * @param event a successful request event
084 */
085 public void requestSucceeded(RequestEvent event) {
086 recordSuccess(event, true);
087 _listener.requestSucceeded(event);
088 }
089
090 /**
091 * Invoked if the request failed.
092 * Records the failure and adds the request's response to the list of
093 * failed responses
094 *
095 * @param event a "failed" request event.
096 */
097 public void requestFailed(RequestEvent event) {
098 recordSuccess(event, false);
099 _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 }