|
1 | 1 | package io.cucumber.core.resource;
|
2 | 2 |
|
| 3 | +import io.cucumber.core.logging.LogRecordListener; |
| 4 | +import io.cucumber.core.logging.LoggerFactory; |
3 | 5 | import io.cucumber.core.resource.test.ExampleClass;
|
4 | 6 | import io.cucumber.core.resource.test.ExampleInterface;
|
5 | 7 | import io.cucumber.core.resource.test.OtherClass;
|
| 8 | +import org.hamcrest.CoreMatchers; |
| 9 | +import org.hamcrest.Matchers; |
| 10 | +import org.junit.jupiter.api.AfterEach; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
6 | 12 | import org.junit.jupiter.api.Test;
|
| 13 | +import org.mockito.Mockito; |
7 | 14 |
|
| 15 | +import java.io.IOException; |
| 16 | +import java.net.URI; |
| 17 | +import java.net.URL; |
| 18 | +import java.net.URLConnection; |
| 19 | +import java.net.URLStreamHandler; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Enumeration; |
8 | 23 | import java.util.List;
|
| 24 | +import java.util.Vector; |
| 25 | +import java.util.logging.Level; |
| 26 | +import java.util.logging.LogRecord; |
9 | 27 |
|
| 28 | +import static java.util.Arrays.asList; |
| 29 | +import static java.util.Collections.enumeration; |
| 30 | +import static java.util.Collections.singletonList; |
10 | 31 | import static org.hamcrest.MatcherAssert.assertThat;
|
| 32 | +import static org.hamcrest.Matchers.containsString; |
11 | 33 | import static org.hamcrest.collection.IsEmptyCollection.empty;
|
12 | 34 | import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
|
13 | 35 | import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | +import static org.mockito.Mockito.when; |
14 | 38 |
|
15 | 39 | class ClasspathScannerTest {
|
16 | 40 |
|
17 | 41 | private final ClasspathScanner scanner = new ClasspathScanner(
|
18 | 42 | ClasspathScannerTest.class::getClassLoader);
|
19 | 43 |
|
| 44 | + private LogRecordListener logRecordListener; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + void setup() { |
| 48 | + logRecordListener = new LogRecordListener(); |
| 49 | + LoggerFactory.addListener(logRecordListener); |
| 50 | + } |
| 51 | + |
| 52 | + @AfterEach |
| 53 | + void tearDown() { |
| 54 | + LoggerFactory.removeListener(logRecordListener); |
| 55 | + } |
| 56 | + |
20 | 57 | @Test
|
21 | 58 | void scanForSubClassesInPackage() {
|
22 | 59 | List<Class<? extends ExampleInterface>> classes = scanner.scanForSubClassesInPackage("io.cucumber",
|
@@ -49,4 +86,21 @@ void scanForClassesInNonExistingPackage() {
|
49 | 86 | assertThat(classes, empty());
|
50 | 87 | }
|
51 | 88 |
|
| 89 | + @Test |
| 90 | + void scanForResourcesInUnsupportedFileSystem() throws IOException { |
| 91 | + ClassLoader classLoader = mock(ClassLoader.class); |
| 92 | + ClasspathScanner scanner = new ClasspathScanner(() -> classLoader); |
| 93 | + URLStreamHandler handler = new URLStreamHandler() { |
| 94 | + @Override |
| 95 | + protected URLConnection openConnection(URL u) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + }; |
| 99 | + URL resourceUrl = new URL(null, "bundle-resource:com/cucumber/bundle", handler); |
| 100 | + when(classLoader.getResources("com/cucumber/bundle")).thenReturn(enumeration(singletonList(resourceUrl))); |
| 101 | + assertThat(scanner.scanForClassesInPackage("com.cucumber.bundle"), empty()); |
| 102 | + assertThat(logRecordListener.getLogRecords().get(0).getMessage(), |
| 103 | + containsString("Failed to find resources for 'bundle-resource:com/cucumber/bundle'")); |
| 104 | + } |
| 105 | + |
52 | 106 | }
|
0 commit comments