@Override protected Jaxb2Marshaller createMarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath(CONTEXT_PATH); marshaller.afterPropertiesSet(); return marshaller; }
@Test(expected = IllegalArgumentException.class) public void noContextPathOrClassesToBeBound() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.afterPropertiesSet(); }
@Override protected Jaxb2Marshaller createUnmarshaller() throws Exception { Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller(); unmarshaller.setContextPath("org.springframework.oxm.jaxb.test"); unmarshaller.setSchema(new ClassPathResource("org/springframework/oxm/flight.xsd")); unmarshaller.afterPropertiesSet(); return unmarshaller; }
@Test(expected = XmlMappingException.class) public void marshalInvalidClass() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(FlightType.class); marshaller.afterPropertiesSet(); Result result = new StreamResult(new StringWriter()); Flights flights = new Flights(); marshaller.marshal(flights, result); }
@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(expected = UncategorizedMappingException.class) public void testInvalidContextPath() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath("ab"); marshaller.afterPropertiesSet(); }
@Test public void properties() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath(CONTEXT_PATH); marshaller.setMarshallerProperties( Collections.<String, Object>singletonMap(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE)); marshaller.afterPropertiesSet(); }
@Test public void supportsClassesToBeBound() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(Flights.class, FlightType.class); marshaller.afterPropertiesSet(); testSupports(); }
@Test public void supportsPackagesToScan() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setPackagesToScan(CONTEXT_PATH); marshaller.afterPropertiesSet(); testSupports(); }
@Before public void createMarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(MyBean.class); marshaller.afterPropertiesSet(); this.converter = new MarshallingMessageConverter(marshaller); }
@Test public void marshalAttachments() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(BinaryObject.class); marshaller.setMtomEnabled(true); marshaller.afterPropertiesSet(); MimeContainer mimeContainer = mock(MimeContainer.class); Resource logo = new ClassPathResource("spring-ws.png", getClass()); DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile())); given(mimeContainer.convertToXopPackage()).willReturn(true); byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream()); BinaryObject object = new BinaryObject(bytes, dataHandler); StringWriter writer = new StringWriter(); marshaller.marshal(object, new StreamResult(writer), mimeContainer); assertTrue("No XML written", writer.toString().length() > 0); verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class)); }
@Test public void lazyInit() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setContextPath(CONTEXT_PATH); marshaller.setLazyInit(true); marshaller.afterPropertiesSet(); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); marshaller.marshal(flights, result); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING).withDifferenceEvaluator(ev)); }
protected Marshaller getMarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(new Class<?>[] { QualifiedTrade.class }); marshaller.afterPropertiesSet(); StringWriter string = new StringWriter(); marshaller.marshal(new QualifiedTrade("FOO", 100, BigDecimal.valueOf(10.), "bar"), new StreamResult(string)); String content = string.toString(); assertTrue("Wrong content: "+content, content.contains("<customer>bar</customer>")); return marshaller; }
@Override protected Marshaller getMarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(new Class<?>[] { Trade.class }); marshaller.afterPropertiesSet(); StringWriter string = new StringWriter(); marshaller.marshal(new Trade("FOO", 100, BigDecimal.valueOf(10.), "bar"), new StreamResult(string)); String content = string.toString(); assertTrue("Wrong content: "+content, content.contains("<customer>bar</customer>")); return marshaller; }
protected Unmarshaller getUnmarshaller() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(new Class<?>[] { QualifiedTrade.class }); marshaller.setSchema(new ClassPathResource("trade.xsd", Trade.class)); marshaller.afterPropertiesSet(); return marshaller; }
@Override protected Unmarshaller getUnmarshaller() throws Exception { reader.setFragmentRootElementName("trade"); Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(new Class<?>[] { Trade.class }); // marshaller.setSchema(new ClassPathResource("trade.xsd", Trade.class)); marshaller.afterPropertiesSet(); return marshaller; }
@Test // SPR-10714 public void marshalAWrappedObjectHoldingAnXmlElementDeclElement() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setPackagesToScan("org.springframework.oxm.jaxb"); marshaller.afterPropertiesSet(); Airplane airplane = new Airplane(); airplane.setName("test"); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); marshaller.marshal(airplane, result); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Marshalling should use root Element", writer.toString(), isSimilarTo("<airplane><name>test</name></airplane>").withDifferenceEvaluator(ev)); }
@Test public void responseBodyArgMismatch() throws Exception { initServlet(wac -> { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(A.class, B.class); try { marshaller.afterPropertiesSet(); } catch (Exception ex) { throw new BeanCreationException(ex.getMessage(), ex); } MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller); RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class); adapterDef.getPropertyValues().add("messageConverters", messageConverter); wac.registerBeanDefinition("handlerAdapter", adapterDef); }, RequestBodyArgMismatchController.class); MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something"); String requestBody = "<b/>"; request.setContent(requestBody.getBytes("UTF-8")); request.addHeader("Content-Type", "application/xml; charset=utf-8"); MockHttpServletResponse response = new MockHttpServletResponse(); getServlet().service(request, response); assertEquals(400, response.getStatus()); }
@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()); }
@Test public void supportsXmlRootElement() throws Exception { marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(DummyRootElement.class, DummyType.class); marshaller.afterPropertiesSet(); assertTrue("Jaxb2Marshaller does not support XmlRootElement class", marshaller.supports(DummyRootElement.class)); assertTrue("Jaxb2Marshaller does not support XmlRootElement generic type", marshaller.supports((Type)DummyRootElement.class)); assertFalse("Jaxb2Marshaller supports DummyType class", marshaller.supports(DummyType.class)); assertFalse("Jaxb2Marshaller supports DummyType type", marshaller.supports((Type)DummyType.class)); }