ScannerImpl implements Scanner { @Override public <I, D extends Descriptor> D scan(final I item, final String path, final Scope scope) { return scan(item, null, path, scope); } ScannerImpl(ScannerConfiguration configuration, Map<String, Object> pluginProperties, ScannerContext scannerContext,
            ScannerPluginRepository scannerPluginRepository); @Override D scan(final I item, final String path, final Scope scope); @Override D scan(I item, D descriptor, String path, Scope scope); @Override ScannerContext getContext(); @Override Scope resolveScope(String name);  }
@Test public void failOnError() throws IOException { Scanner scanner = new ScannerImpl(configuration, emptyMap(), context, scannerPluginRepository); stubExceptionDuringScan(scanner); try { scanner.scan("test", "test", scope); fail("Expecting an " + UnrecoverableScannerException.class.getName()); } catch (UnrecoverableScannerException e) { String message = e.getMessage(); assertThat(message, containsString("test")); } verify(store).beginTransaction(); verify(store).rollbackTransaction(); verify(store, never()).commitTransaction(); assertThat(transaction, equalTo(false)); }
@Test public void continueOnError() throws IOException { configuration.setContinueOnError(true); Scanner scanner = new ScannerImpl(configuration, emptyMap(), context, scannerPluginRepository); stubExceptionDuringScan(scanner); scanner.scan("test", "test", scope); scanner.scan("test", "test", scope); verify(store, times(2)).beginTransaction(); verify(store, times(2)).rollbackTransaction(); verify(store, never()).commitTransaction(); }
@Test public void continueOnErrorDuringCommit() { doThrow(new IllegalStateException("Exception during commit")).when(store).commitTransaction(); configuration.setContinueOnError(true); Scanner scanner = new ScannerImpl(configuration, emptyMap(), context, scannerPluginRepository); scanner.scan("test1", "test1", scope); scanner.scan("test2", "test2", scope); verify(store, times(2)).beginTransaction(); verify(store, times(2)).commitTransaction(); verify(store, times(2)).rollbackTransaction(); }
@Test public void pluginPipeline() { Store store = mock(Store.class); ScannerContext scannerContext = new ScannerContextImpl(store, OUTPUT_DIRECTORY); when(store.create(any(Class.class))).thenAnswer((Answer<Descriptor>) invocation -> { Class<? extends Descriptor> descriptorType = (Class<? extends Descriptor>) invocation.getArguments()[0]; return mock(descriptorType); }); when(store.addDescriptorType(any(Descriptor.class), any(Class.class))).thenAnswer((Answer<Descriptor>) invocation -> { Class<? extends Descriptor> descriptorType = (Class<? extends Descriptor>) invocation.getArguments()[1]; return mock(descriptorType); }); Map<String, ScannerPlugin<?, ?>> scannerPlugins = new HashMap<>(); scannerPlugins.put("TestScanner", new TestItemScannerPlugin()); scannerPlugins.put("DependentTestScanner", new DependentTestItemScannerPlugin()); scannerPlugins.put("NestedTestScanner", new NestedTestItemScannerPlugin()); doReturn(scannerPlugins).when(scannerPluginRepository).getScannerPlugins(scannerContext, emptyMap()); Scanner scanner = new ScannerImpl(new ScannerConfiguration(), emptyMap(), scannerContext, scannerPluginRepository); Descriptor descriptor = scanner.scan(new TestItem(), "/", DefaultScope.NONE); assertThat(descriptor, instanceOf(DependentTestItemDescriptor.class)); verify(store).create(eq(TestItemDescriptor.class)); verify(store).addDescriptorType(any(TestItemDescriptor.class), eq(NestedTestItemDescriptor.class)); verify(store).addDescriptorType(any(NestedTestItemDescriptor.class), eq(DependentTestItemDescriptor.class)); }