|
33 | 33 | import static org.powermock.api.easymock.PowerMock.mockStatic; |
34 | 34 | import static org.powermock.api.easymock.PowerMock.replay; |
35 | 35 |
|
| 36 | +import ch.qos.logback.classic.Level; |
| 37 | +import ch.qos.logback.classic.Logger; |
| 38 | +import ch.qos.logback.classic.LoggerContext; |
36 | 39 | import com.github.kklisura.cdt.launch.ChromeLauncher.Environment; |
37 | 40 | import com.github.kklisura.cdt.launch.ChromeLauncher.ShutdownHookRegistry; |
38 | 41 | import com.github.kklisura.cdt.launch.config.ChromeLauncherConfiguration; |
39 | 42 | import com.github.kklisura.cdt.launch.exceptions.ChromeProcessTimeoutException; |
40 | 43 | import com.github.kklisura.cdt.launch.support.ProcessLauncher; |
| 44 | +import com.github.kklisura.cdt.launch.utils.LogCollector; |
41 | 45 | import com.github.kklisura.cdt.services.ChromeService; |
42 | 46 | import com.github.kklisura.cdt.services.impl.ChromeServiceImpl; |
43 | 47 | import com.github.kklisura.cdt.utils.FilesUtils; |
44 | 48 | import java.io.ByteArrayInputStream; |
45 | 49 | import java.io.IOException; |
46 | 50 | import java.nio.file.Path; |
47 | 51 | import java.nio.file.Paths; |
| 52 | +import java.util.ArrayList; |
48 | 53 | import java.util.List; |
49 | 54 | import java.util.concurrent.TimeUnit; |
50 | 55 | import org.easymock.Capture; |
|
56 | 61 | import org.powermock.api.easymock.PowerMock; |
57 | 62 | import org.powermock.core.classloader.annotations.PrepareForTest; |
58 | 63 | import org.powermock.modules.junit4.PowerMockRunner; |
| 64 | +import org.slf4j.LoggerFactory; |
59 | 65 |
|
60 | 66 | /** |
61 | 67 | * Chrome launcher test. |
@@ -119,6 +125,8 @@ public void testGetChromeBinaryPathThrowsExceptionWhenNoBinaryFound() { |
119 | 125 | processLauncher.isExecutable( |
120 | 126 | "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe")) |
121 | 127 | .andReturn(false); |
| 128 | + expect(processLauncher.isExecutable("C:/Program Files/Google/Chrome/Application/chrome.exe")) |
| 129 | + .andReturn(false); |
122 | 130 |
|
123 | 131 | replayAll(); |
124 | 132 |
|
@@ -636,6 +644,102 @@ public void testExitValue() throws IOException { |
636 | 644 | verifyAll(); |
637 | 645 | } |
638 | 646 |
|
| 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 | + |
639 | 743 | private static void assertUserDataDir(List<String> arguments) { |
640 | 744 | boolean hasUserDataDir = false; |
641 | 745 | for (String argument : arguments) { |
|
0 commit comments