1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 import com.thoughtworks.selenium.CommandProcessor;
33
34 public class LoggingCommandProcessorInterfaceMethodsTest {
35 CommandProcessor mockCommandProcessor;
36 LoggingResultsFormatter mockformatter;
37 LoggingBean testBean;
38 LoggingCommandProcessor testProcessor;
39
40 @Before
41 public void setUp() {
42 mockCommandProcessor = createMock(CommandProcessor.class);
43 mockformatter = createMock(LoggingResultsFormatter.class);
44
45 testBean = new LoggingBean();
46 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
47 }
48
49 @After
50 public void tearDown() {
51 verify(mockCommandProcessor);
52 verify(mockformatter);
53 }
54
55 void doReplayMocks() {
56 replay(mockCommandProcessor);
57 replay(mockformatter);
58 }
59
60 @Test
61 public void getStringArray() {
62 expect(mockCommandProcessor.getStringArray(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" })))
63 .andReturn((String[]) new String[] { "verySimpleResult" });
64 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
65
66 doReplayMocks();
67
68 testProcessor.getStringArray("fancyTestCommandName", new String[] { "arg1", "arg2" });
69 assertBaseCmdValues();
70 assertEquals("OK,{verySimpleResult}", testProcessor.loggingEventsQueue.get(0).getResult());
71 }
72
73 private void assertBaseCmdValues() {
74 assertEquals("loggingEventsQueue should contain processed element", 1, testProcessor.loggingEventsQueue.size());
75 assertEquals("fancyTestCommandName", testProcessor.loggingEventsQueue.get(0).getCommandName());
76 assertEquals(2, testProcessor.loggingEventsQueue.get(0).getArgs().length);
77 assertEquals("arg1", testProcessor.loggingEventsQueue.get(0).getArgs()[0]);
78 assertEquals("arg2", testProcessor.loggingEventsQueue.get(0).getArgs()[1]);
79 }
80
81 @Test(expected = RuntimeException.class)
82 public void getStringArray_exceptionThrown() {
83
84 expect(mockCommandProcessor.getStringArray(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" }))).andThrow(
85 (RuntimeException) new RuntimeException("testError"));
86
87 setupMocksForExceptionScreenshot();
88
89 try {
90 testProcessor.getStringArray("fancyTestCommandName", new String[] { "arg1", "arg2" });
91 } catch (RuntimeException catchThisOnlyTemporary) {
92 assertCorrectExecutionDuringRuntimeException();
93 throw catchThisOnlyTemporary;
94 }
95 }
96
97 private void assertCorrectExecutionDuringRuntimeException() {
98 assertEquals("loggingEventsQueue should contain processed element", 2, testProcessor.loggingEventsQueue.size());
99 assertEquals("captureScreenshot", testProcessor.loggingEventsQueue.get(0).getCommandName());
100 assertEquals("fancyTestCommandName", testProcessor.loggingEventsQueue.get(1).getCommandName());
101 assertEquals(2, testProcessor.loggingEventsQueue.get(1).getArgs().length);
102 assertEquals("arg1", testProcessor.loggingEventsQueue.get(1).getArgs()[0]);
103 assertEquals("arg2", testProcessor.loggingEventsQueue.get(1).getArgs()[1]);
104 assertEquals("ERROR, java.lang.RuntimeException - testError", testProcessor.loggingEventsQueue.get(1).getResult());
105 }
106
107 private void setupMocksForExceptionScreenshot() {
108
109 String[] emptyArgs = new String[] { "" };
110 expect(mockCommandProcessor.doCommand(eq("captureScreenshot"), aryEq(emptyArgs))).andReturn((String) "");
111 mockformatter.generateFilenameForAutomaticScreenshot("Error");
112 expectLastCall().andReturn((String) "");
113
114 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
115
116 doReplayMocks();
117 }
118
119 @Test
120 public void getString() {
121 expect(mockCommandProcessor.getString(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" }))).andReturn(
122 (String) "verySimpleResult");
123 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
124
125 doReplayMocks();
126
127 testProcessor.getString("fancyTestCommandName", new String[] { "arg1", "arg2" });
128 assertBaseCmdValues();
129 assertEquals("OK,verySimpleResult", testProcessor.loggingEventsQueue.get(0).getResult());
130 }
131
132 @Test(expected = RuntimeException.class)
133 public void getString_exceptionThrown() {
134 expect(mockCommandProcessor.getString(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" }))).andThrow(
135 (RuntimeException) new RuntimeException("testError"));
136
137 setupMocksForExceptionScreenshot();
138
139 try {
140 testProcessor.getString("fancyTestCommandName", new String[] { "arg1", "arg2" });
141 } catch (RuntimeException catchThisOnlyTemporary) {
142 assertCorrectExecutionDuringRuntimeException();
143 throw catchThisOnlyTemporary;
144 }
145 }
146
147 @Test
148 public void getNumberArray() {
149 expect(mockCommandProcessor.getNumberArray(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" })))
150 .andReturn((Number[]) new Number[] { 4711 });
151 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
152
153 doReplayMocks();
154
155 testProcessor.getNumberArray("fancyTestCommandName", new String[] { "arg1", "arg2" });
156 assertBaseCmdValues();
157 assertEquals("OK,{4711}", testProcessor.loggingEventsQueue.get(0).getResult());
158 }
159
160 @Test(expected = RuntimeException.class)
161 public void getNumberArray_exceptionThrown() {
162
163 expect(mockCommandProcessor.getNumberArray(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" }))).andThrow(
164 (RuntimeException) new RuntimeException("testError"));
165
166 setupMocksForExceptionScreenshot();
167
168 try {
169 testProcessor.getNumberArray("fancyTestCommandName", new String[] { "arg1", "arg2" });
170 } catch (RuntimeException catchThisOnlyTemporary) {
171 assertCorrectExecutionDuringRuntimeException();
172 throw catchThisOnlyTemporary;
173 }
174 }
175
176 @Test
177 public void getNumber() {
178 expect(mockCommandProcessor.getNumber(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" }))).andReturn(
179 (Number) 4711);
180 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
181
182 doReplayMocks();
183
184 testProcessor.getNumber("fancyTestCommandName", new String[] { "arg1", "arg2" });
185 assertBaseCmdValues();
186 assertEquals("OK,4711", testProcessor.loggingEventsQueue.get(0).getResult());
187 }
188
189 @Test(expected = RuntimeException.class)
190 public void getNumber_exceptionThrown() {
191 expect(mockCommandProcessor.getNumber(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" }))).andThrow(
192 (RuntimeException) new RuntimeException("testError"));
193
194 setupMocksForExceptionScreenshot();
195
196 try {
197 testProcessor.getNumber("fancyTestCommandName", new String[] { "arg1", "arg2" });
198 } catch (RuntimeException catchThisOnlyTemporary) {
199 assertCorrectExecutionDuringRuntimeException();
200 throw catchThisOnlyTemporary;
201 }
202 }
203
204 @Test
205 public void getBooleanArray() {
206 expect(mockCommandProcessor.getBooleanArray(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" })))
207 .andReturn((boolean[]) new boolean[] { true });
208 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
209
210 doReplayMocks();
211
212 testProcessor.getBooleanArray("fancyTestCommandName", new String[] { "arg1", "arg2" });
213 assertBaseCmdValues();
214 assertEquals("OK,{true}", testProcessor.loggingEventsQueue.get(0).getResult());
215 }
216
217 @Test(expected = RuntimeException.class)
218 public void getBooleanArray_exceptionThrown() {
219
220 expect(mockCommandProcessor.getBooleanArray(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" }))).andThrow(
221 (RuntimeException) new RuntimeException("testError"));
222
223 setupMocksForExceptionScreenshot();
224
225 try {
226 testProcessor.getBooleanArray("fancyTestCommandName", new String[] { "arg1", "arg2" });
227 } catch (RuntimeException catchThisOnlyTemporary) {
228 assertCorrectExecutionDuringRuntimeException();
229 throw catchThisOnlyTemporary;
230 }
231 }
232
233 @Test
234 public void getBoolean() {
235 expect(mockCommandProcessor.getBoolean(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" }))).andReturn(
236 (boolean) true);
237 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
238
239 doReplayMocks();
240
241 testProcessor.getBoolean("fancyTestCommandName", new String[] { "arg1", "arg2" });
242 assertBaseCmdValues();
243 assertEquals("OK,true", testProcessor.loggingEventsQueue.get(0).getResult());
244 }
245
246 @Test(expected = RuntimeException.class)
247 public void getBoolean_exceptionThrown() {
248 expect(mockCommandProcessor.getBoolean(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" }))).andThrow(
249 (RuntimeException) new RuntimeException("testError"));
250
251 setupMocksForExceptionScreenshot();
252
253 try {
254 testProcessor.getBoolean("fancyTestCommandName", new String[] { "arg1", "arg2" });
255 } catch (RuntimeException catchThisOnlyTemporary) {
256 assertCorrectExecutionDuringRuntimeException();
257 throw catchThisOnlyTemporary;
258 }
259 }
260
261 @Test
262 public void doCommand_logComment_twoArgs() {
263 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
264
265 doReplayMocks();
266
267 testProcessor.doCommand(SeleniumCommandExtensions.COMMAND_EXTENSION_LOG_COMMENT.getName(), new String[] { "arg1", "arg2" });
268 assertEquals("loggingEventsQueue should contain processed element", 1, testProcessor.loggingEventsQueue.size());
269 assertEquals(SeleniumCommandExtensions.COMMAND_EXTENSION_LOG_COMMENT.getName(), testProcessor.loggingEventsQueue.get(0).getCommandName());
270 assertEquals(2, testProcessor.loggingEventsQueue.get(0).getArgs().length);
271 assertEquals("arg1", testProcessor.loggingEventsQueue.get(0).getArgs()[0]);
272 assertEquals("arg2", testProcessor.loggingEventsQueue.get(0).getArgs()[1]);
273 assertEquals("", testProcessor.loggingEventsQueue.get(0).getResult());
274 }
275
276 @Test
277 public void doCommand_logComment_oneArg() {
278 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
279
280 doReplayMocks();
281
282 testProcessor.doCommand(SeleniumCommandExtensions.COMMAND_EXTENSION_LOG_COMMENT.getName(), new String[] { "arg1" });
283 assertEquals("loggingEventsQueue should contain processed element", 1, testProcessor.loggingEventsQueue.size());
284 assertEquals(SeleniumCommandExtensions.COMMAND_EXTENSION_LOG_COMMENT.getName(), testProcessor.loggingEventsQueue.get(0).getCommandName());
285 assertEquals(2, testProcessor.loggingEventsQueue.get(0).getArgs().length);
286 assertEquals("arg1", testProcessor.loggingEventsQueue.get(0).getArgs()[0]);
287 assertEquals("", testProcessor.loggingEventsQueue.get(0).getArgs()[1]);
288 assertEquals("", testProcessor.loggingEventsQueue.get(0).getResult());
289 }
290
291 private void configureMocks_GenerateFilenameForAutomaticScreenshot(String screenshotBaseName) {
292
293 String[] emptyArgs = new String[] { "" };
294 expect(mockCommandProcessor.doCommand(eq("captureScreenshot"), aryEq(emptyArgs))).andReturn((String) "");
295 mockformatter.generateFilenameForAutomaticScreenshot(screenshotBaseName);
296 expectLastCall().andReturn((String) "");
297 }
298
299 @Test
300 public void doCommand_logScreenshot_anyArgs() {
301 configureMocks_GenerateFilenameForAutomaticScreenshot("arg1");
302 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
303
304 doReplayMocks();
305
306 testProcessor.doCommand(SeleniumCommandExtensions.COMMAND_EXTENSION_LOG_AUTO_SCREENSHOT.getName(), new String[] { "arg1" });
307 assertEquals("loggingEventsQueue should contain processed element", 1, testProcessor.loggingEventsQueue.size());
308 assertEquals("captureScreenshot", testProcessor.loggingEventsQueue.get(0).getCommandName());
309 assertEquals(1, testProcessor.loggingEventsQueue.get(0).getArgs().length);
310 assertEquals("", testProcessor.loggingEventsQueue.get(0).getArgs()[0]);
311 assertEquals("", testProcessor.loggingEventsQueue.get(0).getResult());
312 }
313
314 @Test
315 public void doCommand_logAssertion_threeArgs() {
316 configureMocks_GenerateFilenameForAutomaticScreenshot("Error");
317 testProcessor = new LoggingCommandProcessor(mockCommandProcessor, mockformatter);
318
319 doReplayMocks();
320
321 testProcessor.doCommand(SeleniumCommandExtensions.COMMAND_EXTENSION_LOG_ASSERTION.getName(), new String[] { "arg1", "arg2", "arg3" });
322 assertEquals("loggingEventsQueue should contain processed element", 2, testProcessor.loggingEventsQueue.size());
323 assertEquals("captureScreenshot", testProcessor.loggingEventsQueue.get(0).getCommandName());
324 assertEquals("arg1", testProcessor.loggingEventsQueue.get(1).getCommandName());
325 assertEquals(1, testProcessor.loggingEventsQueue.get(0).getArgs().length);
326 assertEquals("", testProcessor.loggingEventsQueue.get(0).getArgs()[0]);
327 assertEquals("", testProcessor.loggingEventsQueue.get(0).getResult());
328 }
329
330 @Test(expected = RuntimeException.class)
331 public void doCommand_exceptionThrown() {
332 expect(mockCommandProcessor.doCommand(eq("fancyTestCommandName"), aryEq(new String[] { "arg1", "arg2" }))).andThrow(
333 (RuntimeException) new RuntimeException("testError"));
334
335 setupMocksForExceptionScreenshot();
336
337 try {
338 testProcessor.doCommand("fancyTestCommandName", new String[] { "arg1", "arg2" });
339 } catch (RuntimeException catchThisOnlyTemporary) {
340 assertCorrectExecutionDuringRuntimeException();
341 throw catchThisOnlyTemporary;
342 }
343 }
344 }