Skip to content

Commit 1a25380

Browse files
committed
Add some more tests
1 parent 9e2927e commit 1a25380

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

cdt-java-client/src/test/java/com/github/kklisura/cdt/launch/ChromeLauncherTest.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,23 @@
3333
import static org.powermock.api.easymock.PowerMock.mockStatic;
3434
import static org.powermock.api.easymock.PowerMock.replay;
3535

36+
import ch.qos.logback.classic.Level;
37+
import ch.qos.logback.classic.Logger;
38+
import ch.qos.logback.classic.LoggerContext;
3639
import com.github.kklisura.cdt.launch.ChromeLauncher.Environment;
3740
import com.github.kklisura.cdt.launch.ChromeLauncher.ShutdownHookRegistry;
3841
import com.github.kklisura.cdt.launch.config.ChromeLauncherConfiguration;
3942
import com.github.kklisura.cdt.launch.exceptions.ChromeProcessTimeoutException;
4043
import com.github.kklisura.cdt.launch.support.ProcessLauncher;
44+
import com.github.kklisura.cdt.launch.utils.LogCollector;
4145
import com.github.kklisura.cdt.services.ChromeService;
4246
import com.github.kklisura.cdt.services.impl.ChromeServiceImpl;
4347
import com.github.kklisura.cdt.utils.FilesUtils;
4448
import java.io.ByteArrayInputStream;
4549
import java.io.IOException;
4650
import java.nio.file.Path;
4751
import java.nio.file.Paths;
52+
import java.util.ArrayList;
4853
import java.util.List;
4954
import java.util.concurrent.TimeUnit;
5055
import org.easymock.Capture;
@@ -56,6 +61,7 @@
5661
import org.powermock.api.easymock.PowerMock;
5762
import org.powermock.core.classloader.annotations.PrepareForTest;
5863
import org.powermock.modules.junit4.PowerMockRunner;
64+
import org.slf4j.LoggerFactory;
5965

6066
/**
6167
* Chrome launcher test.
@@ -119,6 +125,8 @@ public void testGetChromeBinaryPathThrowsExceptionWhenNoBinaryFound() {
119125
processLauncher.isExecutable(
120126
"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"))
121127
.andReturn(false);
128+
expect(processLauncher.isExecutable("C:/Program Files/Google/Chrome/Application/chrome.exe"))
129+
.andReturn(false);
122130

123131
replayAll();
124132

@@ -636,6 +644,102 @@ public void testExitValue() throws IOException {
636644
verifyAll();
637645
}
638646

647+
/**
648+
* Registers appender that adds logging events to the argument loggingEvents. This also sets the
649+
* level of the logger to DEBUG.
650+
*
651+
* @param name Name of the logger.
652+
* @param loggingEvents Logging events.
653+
*/
654+
private static void registerAppenderOnDebugLogger(
655+
String name, Level level, List<String> loggingEvents) {
656+
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
657+
658+
final Logger logger = loggerContext.getLogger(name);
659+
logger.setLevel(level);
660+
logger.addAppender(new LogCollector(loggingEvents));
661+
}
662+
663+
@Test
664+
public void testLaunchWithDebugLogging()
665+
throws IOException, ChromeProcessTimeoutException, InterruptedException {
666+
List<String> loggingEvents = new ArrayList<>();
667+
registerAppenderOnDebugLogger(
668+
"com.github.kklisura.cdt.launch.chrome.output", Level.DEBUG, loggingEvents);
669+
670+
expect(environment.getEnv("CHROME_PATH")).andReturn("/test-binary-path");
671+
expect(processLauncher.isExecutable("/test-binary-path")).andReturn(true);
672+
673+
shutdownHookRegistry.register(anyObject());
674+
675+
final String trigger =
676+
"first-line\r\nsecond-line\r\nDevTools listening on ws://127.0.0.1:9123/\r\nthird-line\r\nforth-line\r\n";
677+
expect(process.getInputStream()).andReturn(new ByteArrayInputStream(trigger.getBytes()));
678+
679+
expect(processLauncher.launch(eq("/test-binary-path"), anyObject())).andReturn(process);
680+
681+
expect(FilesUtils.randomTempDir("cdt-user-data-dir")).andReturn("temp-user-data-dir");
682+
683+
replayAll();
684+
PowerMock.replay(FilesUtils.class);
685+
686+
ChromeService launch = launcher.launch();
687+
688+
verifyAll();
689+
PowerMock.verify(FilesUtils.class);
690+
691+
assertNotNull(launch);
692+
assertTrue(launch instanceof ChromeServiceImpl);
693+
694+
assertEquals(9123, ((ChromeServiceImpl) launch).getPort());
695+
696+
// Give a thread in waitForDevToolsServer method a bit more chance to collect logging events.
697+
Thread.sleep(100);
698+
699+
assertEquals(5, loggingEvents.size());
700+
assertEquals("[DEBUG] first-line", loggingEvents.get(0));
701+
assertEquals("[DEBUG] second-line", loggingEvents.get(1));
702+
assertEquals("[DEBUG] DevTools listening on ws://127.0.0.1:9123/", loggingEvents.get(2));
703+
assertEquals("[DEBUG] third-line", loggingEvents.get(3));
704+
assertEquals("[DEBUG] forth-line", loggingEvents.get(4));
705+
}
706+
707+
@Test
708+
public void testLaunchWithErrorLoggingDoesNotLogAnything()
709+
throws IOException, ChromeProcessTimeoutException {
710+
List<String> loggingEvents = new ArrayList<>();
711+
registerAppenderOnDebugLogger(
712+
"com.github.kklisura.cdt.launch.chrome.output", Level.ERROR, loggingEvents);
713+
714+
expect(environment.getEnv("CHROME_PATH")).andReturn("/test-binary-path");
715+
expect(processLauncher.isExecutable("/test-binary-path")).andReturn(true);
716+
717+
shutdownHookRegistry.register(anyObject());
718+
719+
final String trigger =
720+
"first-line\r\nsecond-line\r\nDevTools listening on ws://127.0.0.1:9123/\r\nthird-line\r\nforth-line\r\n";
721+
expect(process.getInputStream()).andReturn(new ByteArrayInputStream(trigger.getBytes()));
722+
723+
expect(processLauncher.launch(eq("/test-binary-path"), anyObject())).andReturn(process);
724+
725+
expect(FilesUtils.randomTempDir("cdt-user-data-dir")).andReturn("temp-user-data-dir");
726+
727+
replayAll();
728+
PowerMock.replay(FilesUtils.class);
729+
730+
ChromeService launch = launcher.launch();
731+
732+
verifyAll();
733+
PowerMock.verify(FilesUtils.class);
734+
735+
assertNotNull(launch);
736+
assertTrue(launch instanceof ChromeServiceImpl);
737+
738+
assertEquals(9123, ((ChromeServiceImpl) launch).getPort());
739+
740+
assertEquals(0, loggingEvents.size());
741+
}
742+
639743
private static void assertUserDataDir(List<String> arguments) {
640744
boolean hasUserDataDir = false;
641745
for (String argument : arguments) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.kklisura.cdt.launch.utils;
2+
3+
/*-
4+
* #%L
5+
* cdt-java-client
6+
* %%
7+
* Copyright (C) 2018 - 2021 Kenan Klisura
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import ch.qos.logback.classic.spi.ILoggingEvent;
24+
import ch.qos.logback.core.AppenderBase;
25+
import java.util.List;
26+
27+
/**
28+
* An appender that collects the log events to an array.
29+
*
30+
* @author Kenan Klisura
31+
*/
32+
public class LogCollector extends AppenderBase<ILoggingEvent> {
33+
private final List<String> loggingEventList;
34+
35+
public LogCollector(List<String> loggingEventList) {
36+
this.name = "loggingEvents-appender";
37+
this.loggingEventList = loggingEventList;
38+
start();
39+
}
40+
41+
@Override
42+
protected void append(ILoggingEvent eventObject) {
43+
loggingEventList.add("[" + eventObject.getLevel() + "] " + eventObject.getMessage());
44+
}
45+
}

0 commit comments

Comments
 (0)