private Processor<SchemaContext, ValidatorList> buildProcessor() { final RefResolver resolver = new RefResolver(loader); final Map<JsonRef, Library> libraries = validationCfg.getLibraries(); final Library defaultLibrary = validationCfg.getDefaultLibrary(); final ValidationChain defaultChain = new ValidationChain(resolver, defaultLibrary, validationCfg); final ProcessorMap<JsonRef, SchemaContext, ValidatorList> map = new ProcessorMap<JsonRef, SchemaContext, ValidatorList>(FUNCTION); map.setDefaultProcessor(defaultChain); JsonRef ref; ValidationChain chain; for (final Map.Entry<JsonRef, Library> entry: libraries.entrySet()) { ref = entry.getKey(); chain = new ValidationChain(resolver, entry.getValue(), validationCfg); map.addEntry(ref, chain); } final Processor<SchemaContext, ValidatorList> processor = map.getProcessor(); return new CachingProcessor<SchemaContext, ValidatorList>(processor, SchemaContextEquivalence.getInstance(), validationCfg.getCacheSize()); } }
/** * Constructor * * @param cfg the validation configuration to use */ public SyntaxValidator(final ValidationConfiguration cfg) { final MessageBundle syntaxMessages = cfg.getSyntaxMessages(); final ProcessorMap<JsonRef, ValueHolder<SchemaTree>, ValueHolder<SchemaTree>> map = new ProcessorMap<JsonRef, ValueHolder<SchemaTree>, ValueHolder<SchemaTree>>(FUNCTION); Dictionary<SyntaxChecker> dict; dict = cfg.getDefaultLibrary().getSyntaxCheckers(); final SyntaxProcessor byDefault = new SyntaxProcessor( cfg.getSyntaxMessages(), dict); map.setDefaultProcessor(byDefault); final Map<JsonRef,Library> libraries = cfg.getLibraries(); JsonRef ref; SyntaxProcessor syntaxProcessor; for (final Map.Entry<JsonRef, Library> entry: libraries.entrySet()) { ref = entry.getKey(); dict = entry.getValue().getSyntaxCheckers(); syntaxProcessor = new SyntaxProcessor(syntaxMessages, dict); map.addEntry(ref, syntaxProcessor); } processor = map.getProcessor(); }
@Test public void cannotInputNullDefaultProcessor() { try { new ProcessorMap<Key, In, Out>(fn).addEntry(Key.ONE, processor1) .setDefaultProcessor(null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getMessage("processing.nullProcessor")); } }
@Test public void appropriateProcessorIsSelectedAndRun() throws ProcessingException { final ProcessorMap<Key, In, Out> processorMap = new ProcessorMap<Key, In, Out>(fn) .addEntry(Key.ONE, processor1).addEntry(Key.TWO, processor2) .setDefaultProcessor(byDefault); when(fn.apply(input)).thenReturn(Key.ONE); final Processor<In, Out> processor = processorMap.getProcessor(); processor.process(report, input); verify(processor1, only()).process(report, input); verify(processor2, never()).process(anyReport(), any(In.class)); verify(byDefault, never()).process(anyReport(), any(In.class)); }
@Test public void noMatchingKeyCallsDefaultProcessorWhenSet() throws ProcessingException { final ProcessorMap<Key, In, Out> processorMap = new ProcessorMap<Key, In, Out>(fn) .addEntry(Key.ONE, processor1).addEntry(Key.TWO, processor2) .setDefaultProcessor(byDefault); when(fn.apply(input)).thenReturn(Key.THREE); final Processor<In, Out> processor = processorMap.getProcessor(); processor.process(report, input); verify(processor1, never()).process(anyReport(), any(In.class)); verify(processor2, never()).process(anyReport(), any(In.class)); verify(byDefault, only()).process(report, input); }