1   /*
2    * Copyright 2007 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 static org.easymock.EasyMock.aryEq;
20  import static org.easymock.EasyMock.createMock;
21  import static org.easymock.EasyMock.eq;
22  import static org.easymock.EasyMock.expect;
23  import static org.easymock.EasyMock.expectLastCall;
24  import static org.easymock.EasyMock.replay;
25  import static org.easymock.EasyMock.verify;
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertTrue;
29  
30  import org.junit.After;
31  import org.junit.Before;
32  import org.junit.Ignore;
33  import org.junit.Test;
34  
35  import com.thoughtworks.selenium.CommandProcessor;
36  import com.thoughtworks.selenium.SeleniumException;
37  
38  public class LoggingCommandProcessorTest {
39      CommandProcessor mockCommandProcessor;
40      LoggingResultsFormatter mockformatter;
41      LoggingBean testBean;
42      LoggingCommandProcessor testProcessor;
43  
44      @Before
45      public void setUp() {
46          mockCommandProcessor = createMock(CommandProcessor.class);
47          mockformatter = createMock(LoggingResultsFormatter.class);
48  
49          testBean = new LoggingBean();
50          testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
51      }
52  
53      @After
54      public void tearDown() {
55          verify(mockCommandProcessor);
56          verify(mockformatter);
57      }
58  
59      void doReplayMocks() {
60          replay(mockCommandProcessor);
61          replay(mockformatter);
62      }
63  
64      @Ignore("Should already be handled by WaitUseCases")
65      @Test
66      public void seleniumTestFinished_noWaitTimeout() {
67          testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
68  
69          doReplayMocks();
70  
71          testProcessor.seleniumTestFinished();
72          // FIXME assertions please
73      }
74  
75      private void configureMocks_GenerateFilenameForAutomaticScreenshot(String screenshotBaseName) {
76          // good easymock exercise, as return values and custom matchers (array) are involved
77          String[] emptyArgs = new String[] {""};
78          expect(mockCommandProcessor.doCommand(eq("captureScreenshot"), aryEq(emptyArgs))).andReturn((String) "");
79          mockformatter.generateFilenameForAutomaticScreenshot(screenshotBaseName);
80          expectLastCall().andReturn((String) "");
81      }
82  
83      @Test
84      public void doLogging_commandExcludedFromLogging() {
85          doReplayMocks();
86  
87          testProcessor.setExcludedCommands(new String[] {"doNotLogMe"});
88          testProcessor.doLogging("doNotLogMe", new String[] {}, "", 0L);
89          assertEquals(1, testProcessor.loggingEventsQueue.size());
90          LoggingBean lastLogEvent = testProcessor.loggingEventsQueue.get(testProcessor.loggingEventsQueue.size() - 1);
91          assertNotNull(lastLogEvent);
92          assertEquals("doNotLogMe", lastLogEvent.getCommandName());
93          assertTrue(lastLogEvent.isExcludeFromLogging());
94      }
95  
96      @Test
97      public void doLogging_defaultExclCmdIsExcludedFromLogging() {
98          doReplayMocks();
99  
100         final String lastDefaultExclCmd = testProcessor.getExcludedCommands()[testProcessor.getExcludedCommands().length - 1];
101 
102         testProcessor.doLogging(lastDefaultExclCmd, new String[] {}, "", 0L);
103         assertEquals(1, testProcessor.loggingEventsQueue.size());
104         LoggingBean lastLogEvent = testProcessor.loggingEventsQueue.get(testProcessor.loggingEventsQueue.size() - 1);
105         assertNotNull(lastLogEvent);
106         assertEquals(lastDefaultExclCmd, lastLogEvent.getCommandName());
107         assertTrue(lastLogEvent.isExcludeFromLogging());
108     }
109 
110     @Test
111     public void doExceptionLogging_exceptionIsNull() {
112         configureMocks_GenerateFilenameForAutomaticScreenshot("Error");
113 
114         doReplayMocks();
115 
116         testProcessor.doExceptionLogging("fancyTestCommand", new String[] {}, "", null, 0L);
117         assertEquals("Test should have added an item to loggingEventsQueue", 2, testProcessor.loggingEventsQueue.size());
118         assertEquals("Item should be the captureScreenshot", "captureScreenshot", testProcessor.loggingEventsQueue.get(0)
119                 .getCommandName());
120         assertEquals("Item should be the fancyTestCommand", "fancyTestCommand", testProcessor.loggingEventsQueue.get(1)
121                 .getCommandName());
122     }
123 
124     @Test
125     public void doExceptionLogging_anyException() {
126         configureMocks_GenerateFilenameForAutomaticScreenshot("Error");
127 
128         doReplayMocks();
129 
130         testProcessor.doExceptionLogging("fancyTestCommand", new String[] {}, "", new Exception("testException"), 0L);
131         assertEquals("Test should have added an item to loggingEventsQueue", 2, testProcessor.loggingEventsQueue.size());
132         assertEquals("Item should be the captureScreenshot", "captureScreenshot", testProcessor.loggingEventsQueue.get(0)
133                 .getCommandName());
134         assertEquals("Item should be the fancyTestCommand", "fancyTestCommand", testProcessor.loggingEventsQueue.get(1)
135                 .getCommandName());
136         assertTrue("Item should contain exception details", testProcessor.loggingEventsQueue.get(1).getResult().contains(
137                 "Exception"));
138         assertTrue("Item should contain exception details", testProcessor.loggingEventsQueue.get(1).getResult().contains(
139                 "testException"));
140     }
141 
142     @Test
143     public void logExecutionEnvironment() {
144         expect(mockCommandProcessor.getString(eq("getEval"), aryEq(new String[] {"navigator.userAgent"}))).andReturn(
145                 (String) "testUserAgent");
146         expect(mockCommandProcessor.getString(eq("getEval"), aryEq(new String[] {"window.top.Selenium.coreVersion"}))).andReturn(
147                 (String) "testCoreVersion");
148         expect(mockCommandProcessor.getString(eq("getEval"), aryEq(new String[] {"window.top.Selenium.coreRevision"})))
149                 .andReturn((String) "testCoreRevision");
150         expect(mockCommandProcessor.getString(eq("getEval"), aryEq(new String[] {"window.top.Selenium.rcVersion"}))).andReturn(
151                 (String) "testRcVersion");
152         expect(mockCommandProcessor.getString(eq("getEval"), aryEq(new String[] {"window.top.Selenium.rcRevision"}))).andReturn(
153                 (String) "testRcRevision");
154 
155         doReplayMocks();
156 
157         testProcessor.logExecutionEnvironment();
158         assertEquals("loggingEventsQueue should be empty as only testMetrics got updated", 0, testProcessor.loggingEventsQueue
159                 .size());
160         assertEquals("testUserAgent", testProcessor.seleniumTestMetrics.getUserAgent());
161         assertEquals("testCoreVersion", testProcessor.seleniumTestMetrics.getSeleniumCoreVersion());
162         assertEquals("testCoreRevision", testProcessor.seleniumTestMetrics.getSeleniumCoreRevision());
163         assertEquals("testRcVersion", testProcessor.seleniumTestMetrics.getSeleniumRcVersion());
164         assertEquals("testRcRevision", testProcessor.seleniumTestMetrics.getSeleniumRcRevision());
165     }
166 
167     @Test
168     public void logExecutionEnvironment_Exception() {
169         expect(mockCommandProcessor.getString(eq("getEval"), aryEq(new String[] {"navigator.userAgent"}))).andThrow(new SeleniumException("Some Error"));
170         expect(mockCommandProcessor.getString(eq("getEval"), aryEq(new String[] {"window.top.Selenium.coreVersion"}))).andThrow(new SeleniumException("Some Error"));
171         expect(mockCommandProcessor.getString(eq("getEval"), aryEq(new String[] {"window.top.Selenium.coreRevision"})))
172                 .andThrow(new SeleniumException("Some Error"));
173         expect(mockCommandProcessor.getString(eq("getEval"), aryEq(new String[] {"window.top.Selenium.rcVersion"}))).andThrow(new SeleniumException("Some Error"));
174         expect(mockCommandProcessor.getString(eq("getEval"), aryEq(new String[] {"window.top.Selenium.rcRevision"}))).andThrow(new SeleniumException("Some Error"));
175 
176         doReplayMocks();
177 
178         testProcessor.logExecutionEnvironment();
179         assertEquals("loggingEventsQueue should be empty as only testMetrics got updated", 0, testProcessor.loggingEventsQueue
180                 .size());
181         assertEquals("UNKNOWN", testProcessor.seleniumTestMetrics.getUserAgent());
182         assertEquals("UNKNOWN", testProcessor.seleniumTestMetrics.getSeleniumCoreVersion());
183         assertEquals("UNKNOWN", testProcessor.seleniumTestMetrics.getSeleniumCoreRevision());
184         assertEquals("UNKNOWN", testProcessor.seleniumTestMetrics.getSeleniumRcVersion());
185         assertEquals("UNKNOWN", testProcessor.seleniumTestMetrics.getSeleniumRcRevision());
186     }
187 }