@Override public Object unmarshal(Source source) throws XmlMappingException { return unmarshal(source, null); }
@Test public void unmarshalFile() throws IOException { Resource resource = new ClassPathResource("jaxb2.xml", getClass()); File file = resource.getFile(); Flights f = (Flights) unmarshaller.unmarshal(new StreamSource(file)); testFlights(f); }
@Test @Override @SuppressWarnings("unchecked") public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); streamReader.nextTag(); // skip to flights streamReader.nextTag(); // skip to flight Source source = StaxUtils.createStaxSource(streamReader); JAXBElement<FlightType> element = (JAXBElement<FlightType>) unmarshaller.unmarshal(source); FlightType flight = element.getValue(); testFlight(flight); }
@Test @SuppressWarnings("unchecked") public void unmarshalAnXmlReferingToAWrappedXmlElementDecl() throws Exception { // SPR-10714 unmarshaller = new Jaxb2Marshaller(); unmarshaller.setPackagesToScan(new String[] { "org.springframework.oxm.jaxb" }); unmarshaller.afterPropertiesSet(); Source source = new StreamSource(new StringReader( "<brand-airplane><name>test</name></brand-airplane>")); JAXBElement<Airplane> airplane = (JAXBElement<Airplane>) unmarshaller.unmarshal(source); assertEquals("Unmarshalling via explicit @XmlRegistry tag should return correct type", "test", airplane.getValue().getName()); }
@Test // SPR-10806 public void unmarshalSaxSourceWithXmlOptions() throws Exception { final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class); Jaxb2Marshaller marshaller = new Jaxb2Marshaller() { @Override protected javax.xml.bind.Unmarshaller createUnmarshaller() { return unmarshaller; } }; // 1. external-general-entities and dtd support disabled (default) marshaller.unmarshal(new SAXSource(new InputSource("1"))); ArgumentCaptor<SAXSource> sourceCaptor = ArgumentCaptor.forClass(SAXSource.class); verify(unmarshaller).unmarshal(sourceCaptor.capture()); SAXSource result = sourceCaptor.getValue(); assertEquals(true, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl")); assertEquals(false, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities")); // 2. external-general-entities and dtd support enabled reset(unmarshaller); marshaller.setProcessExternalEntities(true); marshaller.setSupportDtd(true); marshaller.unmarshal(new SAXSource(new InputSource("1"))); verify(unmarshaller).unmarshal(sourceCaptor.capture()); result = sourceCaptor.getValue(); assertEquals(false, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl")); assertEquals(true, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities")); }
@Test // SPR-10806 public void unmarshalStreamSourceWithXmlOptions() throws Exception { final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class); Jaxb2Marshaller marshaller = new Jaxb2Marshaller() { @Override protected javax.xml.bind.Unmarshaller createUnmarshaller() { return unmarshaller; } }; // 1. external-general-entities and dtd support disabled (default) marshaller.unmarshal(new StreamSource("1")); ArgumentCaptor<SAXSource> sourceCaptor = ArgumentCaptor.forClass(SAXSource.class); verify(unmarshaller).unmarshal(sourceCaptor.capture()); SAXSource result = sourceCaptor.getValue(); assertEquals(true, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl")); assertEquals(false, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities")); // 2. external-general-entities and dtd support enabled reset(unmarshaller); marshaller.setProcessExternalEntities(true); marshaller.setSupportDtd(true); marshaller.unmarshal(new StreamSource("1")); verify(unmarshaller).unmarshal(sourceCaptor.capture()); result = sourceCaptor.getValue(); assertEquals(false, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl")); assertEquals(true, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities")); }
@Test public void marshalAttachments() throws Exception { unmarshaller = new Jaxb2Marshaller(); unmarshaller.setClassesToBeBound(BinaryObject.class); unmarshaller.setMtomEnabled(true); unmarshaller.afterPropertiesSet(); MimeContainer mimeContainer = mock(MimeContainer.class); Resource logo = new ClassPathResource("spring-ws.png", getClass()); DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile())); given(mimeContainer.isXopPackage()).willReturn(true); given(mimeContainer.getAttachment("<6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws>")).willReturn(dataHandler); given(mimeContainer.getAttachment("<99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws>")).willReturn(dataHandler); given(mimeContainer.getAttachment("696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png")).willReturn(dataHandler); String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" + "<xop:Include href='cid:6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" + "</bytes>" + "<dataHandler>" + "<xop:Include href='cid:99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" + "</dataHandler>" + "<swaDataHandler>696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png</swaDataHandler>" + "</binaryObject>"; StringReader reader = new StringReader(content); Object result = unmarshaller.unmarshal(new StreamSource(reader), mimeContainer); assertTrue("Result is not a BinaryObject", result instanceof BinaryObject); BinaryObject object = (BinaryObject) result; assertNotNull("bytes property not set", object.getBytes()); assertTrue("bytes property not set", object.getBytes().length > 0); assertNotNull("datahandler property not set", object.getSwaDataHandler()); }
@Override public Object unmarshal(final Source source) throws IOException, XmlMappingException { return jaxb2Marshaller.unmarshal(source); }
private ThesisMetadata extractThesisMetadata(File metadataFile, Institution basicOrgUnit, String thesisExternalId) throws IOException, XmlMappingException { DepositRequestThesisMetadata depositThesisMetadata; try (FileInputStream fileInputStream = new FileInputStream(metadataFile)) { depositThesisMetadata = (DepositRequestThesisMetadata)thesisMetadataMarshaller.unmarshal(new StreamSource(fileInputStream)); } return thesisMetadataConverter.convert(depositThesisMetadata, basicOrgUnit, thesisExternalId); }
@Override public void buttonClick(ClickEvent event) { contextHolder.setNavigationContext(marshaller.unmarshal(new StreamSource(new ByteArrayInputStream(textArea.getValue().toString().getBytes()) ))); if(specificClickListener != null) { specificClickListener.buttonClick(event); } } });
@SuppressWarnings("CastToConcreteClass") @Override public Autnresponse parseResponse(final InputStream inputStream) { final Autnresponse autnresponse = (Autnresponse) responseEnvelopeMarshaller.unmarshal(new StreamSource(inputStream)); if (!SUCCESS_STATE.equals(autnresponse.getResponse())) { final Node responseData = (Node) autnresponse.getResponseData(); final ErrorResponse errorResponse = (ErrorResponse) errorMarshaller.unmarshal(new DOMSource(responseData)); throw new AciErrorExceptionBuilder(errorResponse.getError()).build(); } return autnresponse; } }
Object parse(String resource) { Object result = marshaller.unmarshal(new StreamSource( NodeServiceTest.class.getResourceAsStream(resource) ) ); return result; }
@Override public Autnresponse parseResponse(final InputStream inputStream) throws AciErrorException { final Autnresponse autnresponse = responseParser.parseResponse(inputStream); final Node responseData = (Node) autnresponse.getResponseData(); autnresponse.setResponseData(marshaller.unmarshal(new DOMSource(responseData))); return autnresponse; }
@Test public void testDetachedState() throws Exception { LOG.info("<-- start of test-case"); tested.add(Uri.parse("http://test-data/STATE/3/@2")); Object result; result = marshaller.unmarshal(new StreamSource(TestData.get("/detached/state.xml"))); assertEquals(1, handeled.size()); NodeData node = handeled.get(Uri.parse("http://test-data/STATE/3/@2")); assertTrue("http://test-data/STATE/3/@2 was not stored", node != null); compareState(node); assertEquals(node, result); }
@Test public void testFlatState() throws Exception { LOG.info("<-- start of test-case"); tested.add(Uri.parse("http://test-data/STATE/3/@2")); Object result; result = marshaller.unmarshal(new StreamSource(TestData.get("/flat/state.xml"))); assertEquals(1, handeled.size()); NodeData node = handeled.get(Uri.parse("http://test-data/STATE/3/@2")); assertTrue("http://test-data/STATE/3/@2 was not stored", node != null); compareState(node); assertEquals(node, result); }
@Test public void testRefCity() throws Exception { LOG.info("<-- start of test-case"); tested.add(Uri.parse("http://test-data/CITY/6/@2")); Object result; result = marshaller.unmarshal(new StreamSource(TestData.get("/ref/city.xml"))); assertEquals(1, handeled.size()); NodeData node = handeled.get(Uri.parse("http://test-data/CITY/6/@2")); assertTrue("http://test-data/CITY/6/@2 was not stored", node != null); compareCity(node); assertEquals(node, result); }
@Test public void testFlatExhibition() throws Exception { LOG.info("<-- start of test-case"); tested.add(Uri.parse("http://test-data/EXHIBITION/19/")); Object result; result = marshaller.unmarshal(new StreamSource(TestData.get("/flat/exhibition.xml"))); assertEquals(1, handeled.size()); NodeData node = handeled.get(Uri.parse("http://test-data/EXHIBITION/19/")); assertTrue("http://test-data/EXHIBITION/19/ was not stored", node != null); compareExhibition(node); assertEquals(node, result); }
@Test public void testRefExhibition() throws Exception { LOG.info("<-- start of test-case"); tested.add(Uri.parse("http://test-data/EXHIBITION/19/")); Object result; result = marshaller.unmarshal(new StreamSource(TestData.get("/flat/exhibition.xml"))); assertEquals(1, handeled.size()); NodeData node = handeled.get(Uri.parse("http://test-data/EXHIBITION/19/")); assertTrue("http://test-data/EXHIBITION/19/ was not stored", node != null); compareExhibition(node); assertEquals(node, result); }
@Test public void testFlatPlace() throws Exception { LOG.info("<-- start of test-case"); tested.add(Uri.parse("http://test-data/PLACE/4/@2")); Object result; result = marshaller.unmarshal(new StreamSource(TestData.get("/flat/place.xml"))); assertEquals(1, handeled.size()); NodeData node = handeled.get(Uri.parse("http://test-data/PLACE/4/@2")); assertTrue("http://test-data/PLACE/4/@2 was not stored", node != null); comparePlace(node); assertEquals(node, result); }
@Test public void testRefLocation() throws Exception { LOG.info("<-- start of test-case"); tested.add(Uri.parse("http://test-data/LOCATION/11/@2")); Object result; result = marshaller.unmarshal(new StreamSource(TestData.get("/ref/location.xml"))); assertEquals(1, handeled.size()); NodeData node = handeled.get(Uri.parse("http://test-data/LOCATION/11/@2")); assertTrue("http://test-data/LOCATION/11/@2 was not stored", node != null); compareLocation(node); assertEquals(node, result); }