Search in sources :

Example 1 with Reader

use of org.apache.beam.sdk.io.Source.Reader in project beam by apache.

the class XmlSourceTest method testReadXMLInvalidRecordClassWithCustomEventHandler.

@Test
public void testReadXMLInvalidRecordClassWithCustomEventHandler() throws IOException {
    File file = tempFolder.newFile("trainXMLSmall");
    Files.write(file.toPath(), trainXML.getBytes(StandardCharsets.UTF_8));
    ValidationEventHandler validationEventHandler = event -> {
        throw new RuntimeException("MyCustomValidationEventHandler failure mesage");
    };
    BoundedSource<WrongTrainType> source = XmlIO.<WrongTrainType>read().from(file.toPath().toString()).withRootElement("trains").withRecordElement("train").withRecordClass(WrongTrainType.class).withValidationEventHandler(validationEventHandler).createSource();
    exception.expect(RuntimeException.class);
    // JAXB internationalizes the error message. So this is all we can match for.
    exception.expectMessage("MyCustomValidationEventHandler failure mesage");
    try (Reader<WrongTrainType> reader = source.createReader(null)) {
        for (boolean available = reader.start(); available; available = reader.advance()) {
            reader.getCurrent();
        }
    }
}
Also used : RunWith(org.junit.runner.RunWith) Random(java.util.Random) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) ArrayList(java.util.ArrayList) SourceTestUtils.assertSplitAtFractionFails(org.apache.beam.sdk.testing.SourceTestUtils.assertSplitAtFractionFails) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) ExpectedException(org.junit.rules.ExpectedException) Nullable(org.checkerframework.checker.nullness.qual.Nullable) XmlAttribute(javax.xml.bind.annotation.XmlAttribute) Files(java.nio.file.Files) PAssert(org.apache.beam.sdk.testing.PAssert) BufferedWriter(java.io.BufferedWriter) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) PCollection(org.apache.beam.sdk.values.PCollection) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) BoundedSource(org.apache.beam.sdk.io.BoundedSource) Rule(org.junit.Rule) Ignore(org.junit.Ignore) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) SourceTestUtils.assertSplitAtFractionExhaustive(org.apache.beam.sdk.testing.SourceTestUtils.assertSplitAtFractionExhaustive) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) SourceTestUtils.assertSplitAtFractionSucceedsAndConsistent(org.apache.beam.sdk.testing.SourceTestUtils.assertSplitAtFractionSucceedsAndConsistent) Reader(org.apache.beam.sdk.io.Source.Reader) Assert.assertEquals(org.junit.Assert.assertEquals) TemporaryFolder(org.junit.rules.TemporaryFolder) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) File(java.io.File) Test(org.junit.Test)

Aggregations

BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Random (java.util.Random)1 ValidationEventHandler (javax.xml.bind.ValidationEventHandler)1 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)1 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)1 BoundedSource (org.apache.beam.sdk.io.BoundedSource)1 Reader (org.apache.beam.sdk.io.Source.Reader)1 PipelineOptions (org.apache.beam.sdk.options.PipelineOptions)1 PipelineOptionsFactory (org.apache.beam.sdk.options.PipelineOptionsFactory)1 PAssert (org.apache.beam.sdk.testing.PAssert)1 SourceTestUtils.assertSplitAtFractionExhaustive (org.apache.beam.sdk.testing.SourceTestUtils.assertSplitAtFractionExhaustive)1 SourceTestUtils.assertSplitAtFractionFails (org.apache.beam.sdk.testing.SourceTestUtils.assertSplitAtFractionFails)1 SourceTestUtils.assertSplitAtFractionSucceedsAndConsistent (org.apache.beam.sdk.testing.SourceTestUtils.assertSplitAtFractionSucceedsAndConsistent)1 TestPipeline (org.apache.beam.sdk.testing.TestPipeline)1