public ValidationChain(final RefResolver refResolver, final Library library, final ValidationConfiguration cfg) { final SyntaxProcessor syntaxProcessor = new SyntaxProcessor( cfg.getSyntaxMessages(), library.getSyntaxCheckers()); final ProcessorChain<ValueHolder<SchemaTree>, ValueHolder<SchemaTree>> chain1 = ProcessorChain.startWith(refResolver).chainWith(syntaxProcessor); resolver = new CachingProcessor<ValueHolder<SchemaTree>, ValueHolder<SchemaTree>>( chain1.getProcessor(), SchemaHolderEquivalence.INSTANCE, cfg.getCacheSize() ); final SchemaDigester digester = new SchemaDigester(library); final ValidatorBuilder keywordBuilder = new ValidatorBuilder(library); ProcessorChain<SchemaContext, ValidatorList> chain2 = ProcessorChain.startWith(digester).chainWith(keywordBuilder); if (cfg.getUseFormat()) { final FormatProcessor format = new FormatProcessor(library, cfg); chain2 = chain2.chainWith(format); } builder = new CachingProcessor<SchemaContext, ValidatorList>( chain2.getProcessor(), SchemaContextEquivalence.getInstance(), cfg.getCacheSize() ); }
@Test public void noFailureDoesNotTriggerEarlyExit() throws ProcessingException { @SuppressWarnings("unchecked") final Processor<MessageProvider, MessageProvider> p1 = mock(Processor.class); @SuppressWarnings("unchecked") final Processor<MessageProvider, MessageProvider> p2 = mock(Processor.class); final Processor<MessageProvider, MessageProvider> processor = ProcessorChain.startWith(p1).failOnError().chainWith(p2) .getProcessor(); final MessageProvider input = mock(MessageProvider.class); final ProcessingReport report = new DummyReport(LogLevel.DEBUG); when(p1.process(report, input)).thenReturn(input); processor.process(report, input); verify(p1).process(same(report), any(MessageProvider.class)); verify(p2).process(same(report), any(MessageProvider.class)); }
/** * Start a processing chain with a single processor * * @param p the processor * @param <X> the input type * @param <Y> the output type * @return a single element processing chain * @throws NullPointerException processor is null */ public static <X extends MessageProvider, Y extends MessageProvider> ProcessorChain<X, Y> startWith(final Processor<X, Y> p) { BUNDLE.checkNotNull(p, "processing.nullProcessor"); return new ProcessorChain<X, Y>(p); }
/** * Stop the processing chain on failure * * <p>Inserting this into a chain will stop the processing chain if the * previous processor ended up with an error (ie, {@link * ProcessingReport#isSuccess()} returns {@code false}).</p> * * @return a new chain */ public ProcessorChain<IN, OUT> failOnError() { return failOnError(new ProcessingMessage() .setMessage(BUNDLE.getMessage("processing.chainStopped"))); }
@Test public void cannotChainWithNullProcessor() { @SuppressWarnings("unchecked") final Processor<MessageProvider, MessageProvider> p = mock(Processor.class); try { ProcessorChain.startWith(p).chainWith(null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getMessage("processing.nullProcessor")); } }
@Test public void cannotInitiateWithNullProcessor() { try { ProcessorChain.startWith(null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getMessage("processing.nullProcessor")); } }
@Test public void failingOnErrorExitsEarly() throws ProcessingException { @SuppressWarnings("unchecked") final Processor<MessageProvider, MessageProvider> p1 = mock(Processor.class); @SuppressWarnings("unchecked") final Processor<MessageProvider, MessageProvider> p2 = mock(Processor.class); final Processor<MessageProvider, MessageProvider> processor = ProcessorChain.startWith(p1).failOnError().chainWith(p2) .getProcessor(); final MessageProvider input = mock(MessageProvider.class); final ProcessingReport report = new DummyReport(LogLevel.ERROR); try { processor.process(report, input); fail("No exception thrown!!"); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.getMessage("processing.chainStopped")); } verify(p1).process(same(report), any(MessageProvider.class)); verify(p2, never()).process(any(ProcessingReport.class), any(MessageProvider.class)); }
/** * Start a processing chain with a single processor * * @param p the processor * @param <X> the input type * @param <Y> the output type * @return a single element processing chain * @throws NullPointerException processor is null */ public static <X extends MessageProvider, Y extends MessageProvider> ProcessorChain<X, Y> startWith(final Processor<X, Y> p) { BUNDLE.checkNotNull(p, "processing.nullProcessor"); return new ProcessorChain<X, Y>(p); }
/** * Stop the processing chain on failure * * <p>Inserting this into a chain will stop the processing chain if the * previous processor ended up with an error (ie, {@link * ProcessingReport#isSuccess()} returns {@code false}).</p> * * @return a new chain */ public ProcessorChain<IN, OUT> failOnError() { return failOnError(new ProcessingMessage() .setMessage(BUNDLE.getMessage("processing.chainStopped"))); }
/** * Start a processing chain with a single processor * * @param p the processor * @param <X> the input type * @param <Y> the output type * @return a single element processing chain * @throws NullPointerException processor is null */ public static <X extends MessageProvider, Y extends MessageProvider> ProcessorChain<X, Y> startWith(final Processor<X, Y> p) { BUNDLE.checkNotNull(p, "processing.nullProcessor"); return new ProcessorChain<X, Y>(p); }
/** * Stop the processing chain on failure * * <p>Inserting this into a chain will stop the processing chain if the * previous processor ended up with an error (ie, {@link * ProcessingReport#isSuccess()} returns {@code false}).</p> * * @return a new chain */ public ProcessorChain<IN, OUT> failOnError() { return failOnError(new ProcessingMessage() .setMessage(BUNDLE.getMessage("processing.chainStopped"))); }
/** * Stop the processing chain on failure * * <p>Inserting this into a chain will stop the processing chain if the * previous processor ended up with an error (ie, {@link * ProcessingReport#isSuccess()} returns {@code false}).</p> * * @param message the processing message to use * @return a new chain * @see ProcessingMessage#asException() * @see ProcessingMessage#setExceptionProvider(ExceptionProvider) */ public ProcessorChain<IN, OUT> failOnError(final ProcessingMessage message) { final Processor<OUT, OUT> fail = new Processor<OUT, OUT>() { @Override public OUT process(final ProcessingReport report, final OUT input) throws ProcessingException { if (!report.isSuccess()) throw message.asException(); return input; } }; final ProcessorMerger<IN, OUT, OUT> merger = new ProcessorMerger<IN, OUT, OUT>(processor, fail); return new ProcessorChain<IN, OUT>(merger); }
/** * Stop the processing chain on failure * * <p>Inserting this into a chain will stop the processing chain if the * previous processor ended up with an error (ie, {@link * ProcessingReport#isSuccess()} returns {@code false}).</p> * * @param message the processing message to use * @return a new chain * @see ProcessingMessage#asException() * @see ProcessingMessage#setExceptionProvider(ExceptionProvider) */ public ProcessorChain<IN, OUT> failOnError(final ProcessingMessage message) { final Processor<OUT, OUT> fail = new Processor<OUT, OUT>() { @Override public OUT process(final ProcessingReport report, final OUT input) throws ProcessingException { if (!report.isSuccess()) throw message.asException(); return input; } }; final ProcessorMerger<IN, OUT, OUT> merger = new ProcessorMerger<IN, OUT, OUT>(processor, fail); return new ProcessorChain<IN, OUT>(merger); }
/** * Stop the processing chain on failure * * <p>Inserting this into a chain will stop the processing chain if the * previous processor ended up with an error (ie, {@link * ProcessingReport#isSuccess()} returns {@code false}).</p> * * @param message the processing message to use * @return a new chain * @see ProcessingMessage#asException() * @see ProcessingMessage#setExceptionProvider(ExceptionProvider) */ public ProcessorChain<IN, OUT> failOnError(final ProcessingMessage message) { final Processor<OUT, OUT> fail = new Processor<OUT, OUT>() { @Override public OUT process(final ProcessingReport report, final OUT input) throws ProcessingException { if (!report.isSuccess()) throw message.asException(); return input; } }; final ProcessorMerger<IN, OUT, OUT> merger = new ProcessorMerger<IN, OUT, OUT>(processor, fail); return new ProcessorChain<IN, OUT>(merger); }
/** * Add a processor to the chain * * @param p the processor to add * @param <NEWOUT> the return type for that new processor * @return a new chain consisting of the previous chain with the new * processor appended * @throws NullPointerException processor to append is null */ public <NEWOUT extends MessageProvider> ProcessorChain<IN, NEWOUT> chainWith(final Processor<OUT, NEWOUT> p) { BUNDLE.checkNotNull(p, "processing.nullProcessor"); final Processor<IN, NEWOUT> merger = new ProcessorMerger<IN, OUT, NEWOUT>(processor, p); return new ProcessorChain<IN, NEWOUT>(merger); }
/** * Add a processor to the chain * * @param p the processor to add * @param <NEWOUT> the return type for that new processor * @return a new chain consisting of the previous chain with the new * processor appended * @throws NullPointerException processor to append is null */ public <NEWOUT extends MessageProvider> ProcessorChain<IN, NEWOUT> chainWith(final Processor<OUT, NEWOUT> p) { BUNDLE.checkNotNull(p, "processing.nullProcessor"); final Processor<IN, NEWOUT> merger = new ProcessorMerger<IN, OUT, NEWOUT>(processor, p); return new ProcessorChain<IN, NEWOUT>(merger); }
/** * Add a processor to the chain * * @param p the processor to add * @param <NEWOUT> the return type for that new processor * @return a new chain consisting of the previous chain with the new * processor appended * @throws NullPointerException processor to append is null */ public <NEWOUT extends MessageProvider> ProcessorChain<IN, NEWOUT> chainWith(final Processor<OUT, NEWOUT> p) { BUNDLE.checkNotNull(p, "processing.nullProcessor"); final Processor<IN, NEWOUT> merger = new ProcessorMerger<IN, OUT, NEWOUT>(processor, p); return new ProcessorChain<IN, NEWOUT>(merger); }