Search in sources :

Example 6 with Unmarshaller

use of org.springframework.oxm.Unmarshaller in project spring-framework by spring-projects.

the class MarshallingHttpMessageConverterTests method canRead.

@Test
public void canRead() throws Exception {
    Unmarshaller unmarshaller = mock(Unmarshaller.class);
    given(unmarshaller.supports(Integer.class)).willReturn(false);
    given(unmarshaller.supports(String.class)).willReturn(true);
    MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
    converter.setUnmarshaller(unmarshaller);
    assertFalse(converter.canRead(Boolean.class, MediaType.TEXT_PLAIN));
    assertFalse(converter.canRead(Integer.class, MediaType.TEXT_XML));
    assertTrue(converter.canRead(String.class, MediaType.TEXT_XML));
}
Also used : Unmarshaller(org.springframework.oxm.Unmarshaller) Test(org.junit.Test)

Example 7 with Unmarshaller

use of org.springframework.oxm.Unmarshaller in project uPortal by Jasig.

the class JaxbPortalDataHandlerServiceTest method testImportTarGzipArchive.

@Test
public void testImportTarGzipArchive() throws Exception {
    final Unmarshaller unmarshaller = mock(Unmarshaller.class);
    final List<IDataImporter<? extends Object>> importers = setupAllImporters(new MockDataImporterSetup() {

        @Override
        public void setup(IPortalDataType dataType, IDataImporter<? extends Object> dataImporter) {
            when(dataImporter.getUnmarshaller()).thenReturn(unmarshaller);
        }
    });
    this.dataImportExportService.setDataImporters(importers);
    final Resource archiveResource = new ClassPathResource("/org/apereo/portal/io/xml/import_archive.tar.gz");
    final IPortalDataHandlerService.BatchImportOptions options = new IPortalDataHandlerService.BatchImportOptions();
    options.setLogDirectoryParent(tempFolder.newFolder("targzipArchiveImport"));
    this.dataImportExportService.importDataArchive(archiveResource, options);
    verify(unmarshaller, times(16)).unmarshal(any(Source.class));
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Unmarshaller(org.springframework.oxm.Unmarshaller) Test(org.junit.Test)

Example 8 with Unmarshaller

use of org.springframework.oxm.Unmarshaller in project uPortal by Jasig.

the class JaxbPortalDataHandlerServiceTest method testImportTGZArchive.

@Test
public void testImportTGZArchive() throws Exception {
    final Unmarshaller unmarshaller = mock(Unmarshaller.class);
    final List<IDataImporter<? extends Object>> importers = setupAllImporters(new MockDataImporterSetup() {

        @Override
        public void setup(IPortalDataType dataType, IDataImporter<? extends Object> dataImporter) {
            when(dataImporter.getUnmarshaller()).thenReturn(unmarshaller);
        }
    });
    this.dataImportExportService.setDataImporters(importers);
    final Resource archiveResource = new ClassPathResource("/org/apereo/portal/io/xml/import_archive.tgz");
    final IPortalDataHandlerService.BatchImportOptions options = new IPortalDataHandlerService.BatchImportOptions();
    options.setLogDirectoryParent(tempFolder.newFolder("tgzArchiveImport"));
    this.dataImportExportService.importDataArchive(archiveResource, options);
    verify(unmarshaller, times(16)).unmarshal(any(Source.class));
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Unmarshaller(org.springframework.oxm.Unmarshaller) Test(org.junit.Test)

Example 9 with Unmarshaller

use of org.springframework.oxm.Unmarshaller in project uPortal by Jasig.

the class IdentityImportExportTestUtilities method testIdentityImportExport.

public static <T> void testIdentityImportExport(TransactionOperations transactionOperations, final IDataImporter<T> dataImporter, final IDataExporter<?> dataExporter, Resource resource, Function<T, String> getName) throws Exception {
    final String importData = toString(resource);
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
    final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new StringReader(importData));
    //Unmarshall from XML
    final Unmarshaller unmarshaller = dataImporter.getUnmarshaller();
    final StAXSource source = new StAXSource(xmlEventReader);
    @SuppressWarnings("unchecked") final T dataImport = (T) unmarshaller.unmarshal(source);
    //Make sure the data was unmarshalled
    assertNotNull("Unmarshalled import data was null", dataImport);
    //Import the data
    dataImporter.importData(dataImport);
    //Export the data
    final String name = getName.apply(dataImport);
    final Object dataExport = transactionOperations.execute(new TransactionCallback<Object>() {

        /* (non-Javadoc)
                             * @see org.springframework.transaction.support.TransactionCallback#doInTransaction(org.springframework.transaction.TransactionStatus)
                             */
        @Override
        public Object doInTransaction(TransactionStatus status) {
            return dataExporter.exportData(name);
        }
    });
    //Make sure the data was exported
    assertNotNull("Exported data was null", dataExport);
    //Marshall to XML
    final Marshaller marshaller = dataExporter.getMarshaller();
    final StringWriter result = new StringWriter();
    marshaller.marshal(dataExport, new StreamResult(result));
    //Compare the exported XML data with the imported XML data, they should match
    final String resultString = result.toString();
    try {
        XMLUnit.setIgnoreWhitespace(true);
        Diff d = new Diff(new StringReader(importData), new StringReader(resultString));
        assertTrue("Export result differs from import" + d, d.similar());
    } catch (Exception e) {
        throw new XmlTestException("Failed to assert similar between import XML and export XML", resultString, e);
    } catch (Error e) {
        throw new XmlTestException("Failed to assert similar between import XML and export XML", resultString, e);
    }
}
Also used : Marshaller(org.springframework.oxm.Marshaller) StreamResult(javax.xml.transform.stream.StreamResult) Diff(org.custommonkey.xmlunit.Diff) TransactionStatus(org.springframework.transaction.TransactionStatus) StAXSource(javax.xml.transform.stax.StAXSource) IOException(java.io.IOException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) Unmarshaller(org.springframework.oxm.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

Unmarshaller (org.springframework.oxm.Unmarshaller)9 Test (org.junit.Test)8 StreamSource (javax.xml.transform.stream.StreamSource)7 Source (javax.xml.transform.Source)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 Resource (org.springframework.core.io.Resource)4 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)3 Marshaller (org.springframework.oxm.Marshaller)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 StAXSource (javax.xml.transform.stax.StAXSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Diff (org.custommonkey.xmlunit.Diff)1 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)1 UnmarshallingFailureException (org.springframework.oxm.UnmarshallingFailureException)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1