View Javadoc

1   /*
2    * Copyright 2007/2008 united internet (unitedinternet.com) Robert Zimmermann
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   */
17  package com.unitedinternet.portal.selenium.utils.logging;
18  
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  
23  /**
24   *
25   * Post process LoggingBean events queue.
26   *
27   * Combine single Wait-events to one
28   *
29   * Summarize time spent in methods
30   *
31   * @author Robert Zimmermann
32   *
33   * $Id: EventQueuePostProcessor.java 130 2008-07-21 15:19:53Z bobbyde $
34   */
35  public class EventQueuePostProcessor {
36      private LoggingResultsFormatter formatter;
37  
38      boolean logMethodsAsComments = true;
39  
40      /**
41       * Loop through loggingEventsQueue and output it to the formatter.
42       *
43       * Right now filtering is included to feed HtmlResultsFormatter the same way it was donne in 1.0
44       *
45       * @param outputFormatter formatter handling formatting and storing of the gathered log-events
46       * @param loggingEventsQueue all logging events including every event inside a wait and commands not to be logged
47       * @param seleniumTestMetrics some basic metrics and an brief abstract of the test run
48       */
49      public EventQueuePostProcessor(LoggingResultsFormatter outputFormatter, List<LoggingBean> loggingEventsQueue,
50              TestMetricsBean seleniumTestMetrics, boolean logMethodsAsComments) {
51          this.formatter = outputFormatter;
52          this.logMethodsAsComments = logMethodsAsComments;
53          formatAllGatheredInformations(loggingEventsQueue, seleniumTestMetrics);
54      }
55  
56      /**
57       * Calls command-type dependent method in formatter. Decides whether command is a comment, has boolean result or is just a
58       * normal command.
59       *
60       * @param loggingBean containing details of the command to be formatted and logged
61       */
62      void formatAndOutputCommand(LoggingBean loggingBean) {
63  
64          if (SeleniumCommandExtensions.COMMAND_EXTENSION_LOG_COMMENT.getName().equals(loggingBean.getCommandName())) {
65              formatter.commentLogEvent(loggingBean);
66              return;
67          }
68  
69          boolean srcResult = false;
70          String result = loggingBean.getResult();
71  
72          // FIXME: these if's should be done inside LoggingBean.
73          if (result.startsWith(LoggingCommandProcessor.SELENIUM_RC_OK_RESULT)) {
74              if (result.endsWith(LoggingCommandProcessor.SELENIUM_CORE_BOOLEAN_RESULT_TRUE)
75                      || result.endsWith(LoggingCommandProcessor.SELENIUM_CORE_BOOLEAN_RESULT_FALSE)) {
76                  loggingBean.setCommandSuccessful(result.endsWith(LoggingCommandProcessor.SELENIUM_CORE_BOOLEAN_RESULT_TRUE));
77                  formatter.booleanCommandLogEvent(loggingBean);
78                  return;
79              } else {
80                  srcResult = true;
81              }
82          }
83          loggingBean.setCommandSuccessful(srcResult);
84          formatter.commandLogEvent(loggingBean);
85      }
86  
87      /**
88       * Added text prefix if we are inside a wait loop.
89       */
90      private static final String WAIT_PREFIX = "(Wait)";
91  
92      /**
93       * Loop through loggingEventsQueue and output it to the formatter.
94       *
95       * Right now filtering is included to feed HtmlResultsFormatter the same way it was done in 1.0
96       *
97       * @param loggingEventsQueue all logging events including every event inside a wait and commands not to be logged
98       * @param seleniumTestMetrics some basic metrics and an brief abstract of the test run
99       */
100     void formatAllGatheredInformations(List<LoggingBean> loggingEventsQueue, TestMetricsBean seleniumTestMetrics) {
101         formatter.headerLogEvent(seleniumTestMetrics);
102 
103         boolean insideWait = false;
104         long startWaitMillis = 0;
105         LoggingBean lastWaitLoggingEvent = null;
106         String currentMethodName = "";
107         LoggingBean rootLoggingBean = new LoggingBean();
108         boolean completeResult = true;
109         for (LoggingBean currentLoggingEvent : loggingEventsQueue) {
110 
111             if (currentLoggingEvent.isWaitInvolved()) {
112                 lastWaitLoggingEvent = currentLoggingEvent;
113                 if (!insideWait) {
114                     insideWait = true;
115                     startWaitMillis = currentLoggingEvent.getCmdStartMillis();
116                 } // else do nothing as we are inside a wait
117             } else {
118                 if (insideWait) {
119                     insideWait = false;
120                     lastWaitLoggingEvent.setWaitDeltaMillis(currentLoggingEvent.getCmdEndMillis() - startWaitMillis);
121                     startWaitMillis = 0;
122                     // lastWaitLoggingEvent is really the last wait command. So output it to the formatter now
123                     String s = lastWaitLoggingEvent.getCommandName();
124                     if (!s.startsWith(WAIT_PREFIX)) {
125                         lastWaitLoggingEvent.setCommandName(WAIT_PREFIX + s);
126                     }
127                     formatAndOutputCommand(lastWaitLoggingEvent);
128                 }
129                 if (!currentLoggingEvent.getSourceMethod().equals(currentMethodName)) {
130                     if (StringUtils.isNotEmpty(currentMethodName)) {
131                         postProcessMethod(completeResult, rootLoggingBean, currentMethodName);
132                         completeResult = true;
133                     }
134 
135                     currentMethodName = currentLoggingEvent.getSourceMethod();
136                     rootLoggingBean = new LoggingBean();
137                     rootLoggingBean.setCallingClass(currentLoggingEvent.getCallingClass());
138                     logNewMethodEntered(currentMethodName, rootLoggingBean);
139                 }
140                 formatAndOutputCommand(currentLoggingEvent);
141                 rootLoggingBean.addChild(currentLoggingEvent);
142             }
143             completeResult = completeResult && currentLoggingEvent.isCommandSuccessful();
144         }
145 
146         postProcessMethod(completeResult, rootLoggingBean, currentMethodName);
147         formatter.footerLogEvent();
148     }
149 
150     /**
151      * Log test data after execution of each test method.
152      *
153      * @param completeMethodResult result for the whole test method, true if all tests succeded
154      * @param currentLoggingBean logging bean containing the method data
155      * @param currentMethod current executed method
156      */
157     void postProcessMethod(boolean completeMethodResult, LoggingBean currentLoggingBean, String currentMethod) {
158         currentLoggingBean.setCommandSuccessful(completeMethodResult);
159         currentLoggingBean.setArgs(new String[] {"executing " + currentLoggingBean.getCallingClass() + "::" + currentMethod});
160         formatter.methodLogEvent(currentLoggingBean);
161     }
162 
163     /**
164      * Log method-name just entered executing a selenium command as a comment.
165      * @param currentMethodName actual method
166      * @param loggingBean comment loggingbean
167      */
168     void logNewMethodEntered(final String currentMethodName, LoggingBean loggingBean) {
169         if (logMethodsAsComments) {
170             loggingBean.setCommandName(SeleniumCommandExtensions.COMMAND_EXTENSION_LOG_COMMENT.getName());
171             loggingBean.setArgs(new String[] {"executing " + currentMethodName + "()"});
172             formatAndOutputCommand(loggingBean);
173         }
174     }
175 }