Search in sources :

Example 1 with Unmarshaller

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

the class MarshallingHttpMessageConverterTests method readWithTypeMismatchException.

@Test(expected = TypeMismatchException.class)
public void readWithTypeMismatchException() throws Exception {
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);
    Marshaller marshaller = mock(Marshaller.class);
    Unmarshaller unmarshaller = mock(Unmarshaller.class);
    given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(Integer.valueOf(3));
    MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller);
    converter.read(String.class, inputMessage);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Marshaller(org.springframework.oxm.Marshaller) StreamSource(javax.xml.transform.stream.StreamSource) Unmarshaller(org.springframework.oxm.Unmarshaller) Test(org.junit.Test)

Example 2 with Unmarshaller

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

the class MarshallingHttpMessageConverterTests method readWithMarshallingFailureException.

@Test
public void readWithMarshallingFailureException() throws Exception {
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);
    UnmarshallingFailureException ex = new UnmarshallingFailureException("forced");
    Unmarshaller unmarshaller = mock(Unmarshaller.class);
    given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(ex);
    MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
    converter.setUnmarshaller(unmarshaller);
    try {
        converter.read(Object.class, inputMessage);
        fail("HttpMessageNotReadableException should be thrown");
    } catch (HttpMessageNotReadableException e) {
        assertTrue("Invalid exception hierarchy", e.getCause() == ex);
    }
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) StreamSource(javax.xml.transform.stream.StreamSource) UnmarshallingFailureException(org.springframework.oxm.UnmarshallingFailureException) Unmarshaller(org.springframework.oxm.Unmarshaller) Test(org.junit.Test)

Example 3 with Unmarshaller

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

the class MarshallingHttpMessageConverterTests method read.

@Test
public void read() throws Exception {
    String body = "<root>Hello World</root>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
    Unmarshaller unmarshaller = mock(Unmarshaller.class);
    given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body);
    MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
    converter.setUnmarshaller(unmarshaller);
    String result = (String) converter.read(Object.class, inputMessage);
    assertEquals("Invalid result", body, result);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) StreamSource(javax.xml.transform.stream.StreamSource) Unmarshaller(org.springframework.oxm.Unmarshaller) Test(org.junit.Test)

Example 4 with Unmarshaller

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

the class JaxbPortalDataHandlerServiceTest method testImportJarArchive.

@Test
public void testImportJarArchive() 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.jar");
    final IPortalDataHandlerService.BatchImportOptions options = new IPortalDataHandlerService.BatchImportOptions();
    options.setLogDirectoryParent(tempFolder.newFolder("jarArchiveImport"));
    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 5 with Unmarshaller

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

the class JaxbPortalDataHandlerServiceTest method testImportZipArchive.

@Test
public void testImportZipArchive() 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.zip");
    final IPortalDataHandlerService.BatchImportOptions options = new IPortalDataHandlerService.BatchImportOptions();
    options.setLogDirectoryParent(tempFolder.newFolder("zipArchiveImport"));
    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)

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