Search in sources :

Example 1 with MockJAXBArchive

use of com.helger.jaxb.mock.external.MockJAXBArchive in project ph-commons by phax.

the class JAXBMarshallerHelperTest method testCloseInternalOnWriteToOutputStream.

@Test
public void testCloseInternalOnWriteToOutputStream() {
    final MockMarshallerExternal m = new MockMarshallerExternal();
    final MutableBoolean aClosed = new MutableBoolean(false);
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream() {

        @Override
        public void close() {
            super.close();
            aClosed.set(true);
        }
    };
    {
        final MockJAXBArchive aArc = new MockJAXBArchive();
        aArc.setVersion("1.24");
        m.write(aArc, aBAOS);
    }
    LOGGER.info(aBAOS.getAsString(StandardCharsets.UTF_8));
    // Must be closed!
    assertTrue("Not closed!", aClosed.booleanValue());
}
Also used : MutableBoolean(com.helger.commons.mutable.MutableBoolean) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) MockJAXBArchive(com.helger.jaxb.mock.external.MockJAXBArchive) MockMarshallerExternal(com.helger.jaxb.mock.MockMarshallerExternal) Test(org.junit.Test)

Example 2 with MockJAXBArchive

use of com.helger.jaxb.mock.external.MockJAXBArchive in project ph-commons by phax.

the class JAXBMarshallerHelperTest method testGetAsBytes.

@Test
public void testGetAsBytes() {
    final MockMarshallerExternal m = new MockMarshallerExternal();
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream();
    byte[] aDirectBytes;
    {
        final MockJAXBArchive aArc = new MockJAXBArchive();
        aArc.setVersion("1.24");
        for (int i = 0; i < 100; ++i) {
            final MockJAXBCollection aCollection = new MockJAXBCollection();
            aCollection.setDescription("Internal bla foo");
            aCollection.setID(i);
            aArc.getCollection().add(aCollection);
        }
        m.write(aArc, aBAOS);
        aDirectBytes = m.getAsBytes(aArc);
    }
    assertArrayEquals(aBAOS.toByteArray(), aDirectBytes);
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) MockJAXBArchive(com.helger.jaxb.mock.external.MockJAXBArchive) MockJAXBCollection(com.helger.jaxb.mock.external.MockJAXBCollection) MockMarshallerExternal(com.helger.jaxb.mock.MockMarshallerExternal) Test(org.junit.Test)

Example 3 with MockJAXBArchive

use of com.helger.jaxb.mock.external.MockJAXBArchive in project ph-commons by phax.

the class CollectingValidationEventHandlerTest method testReadWrite.

@Test
public void testReadWrite() throws JAXBException {
    final JAXBContext aCtx = JAXBContextCache.getInstance().getFromCache(MockJAXBArchive.class);
    final Unmarshaller um = aCtx.createUnmarshaller();
    {
        final CollectingValidationEventHandler cveh = new CollectingValidationEventHandler();
        um.setEventHandler(cveh.andThen(new LoggingValidationEventHandler()));
        // read valid
        final JAXBElement<MockJAXBArchive> o = um.unmarshal(TransformSourceFactory.create(new FileSystemResource("src/test/resources/xml/test-archive-01.xml")), MockJAXBArchive.class);
        assertNotNull(o);
        assertTrue(cveh.getErrorList().isEmpty());
    }
    // read invalid
    {
        final CollectingValidationEventHandler cveh = new CollectingValidationEventHandler();
        um.setEventHandler(cveh);
        final JAXBElement<MockJAXBArchive> o = um.unmarshal(TransformSourceFactory.create(new FileSystemResource("src/test/resources/xml/buildinfo.xml")), MockJAXBArchive.class);
        assertNotNull(o);
        assertFalse(cveh.getErrorList().isEmpty());
    }
    // Read invalid (but close to valid)
    {
        final CollectingValidationEventHandler cveh = new CollectingValidationEventHandler();
        um.setEventHandler(cveh.andThen(new LoggingValidationEventHandler()));
        final JAXBElement<MockJAXBArchive> o = um.unmarshal(TransformSourceFactory.create(new FileSystemResource("src/test/resources/xml/test-archive-03.xml")), MockJAXBArchive.class);
        assertNotNull(o);
        assertEquals(1, cveh.getErrorList().size());
        // For code coverage completion
        CommonsTestHelper.testToStringImplementation(cveh);
    }
}
Also used : JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) Unmarshaller(javax.xml.bind.Unmarshaller) MockJAXBArchive(com.helger.jaxb.mock.external.MockJAXBArchive) Test(org.junit.Test)

Aggregations

MockJAXBArchive (com.helger.jaxb.mock.external.MockJAXBArchive)3 Test (org.junit.Test)3 NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)2 MockMarshallerExternal (com.helger.jaxb.mock.MockMarshallerExternal)2 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)1 MutableBoolean (com.helger.commons.mutable.MutableBoolean)1 MockJAXBCollection (com.helger.jaxb.mock.external.MockJAXBCollection)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBElement (javax.xml.bind.JAXBElement)1 Unmarshaller (javax.xml.bind.Unmarshaller)1