JAXBService { public <T> T unMarshall(String xml) { try { Source source = new SAXSource(nameSpaceFilter, new InputSource(new StringReader(xml))); return (T) unmarshaller.unmarshal(source); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } JAXBService(Class<?>... xsdRootClasses); static JAXBService create(Class<?>... xsdClasses); static JAXBService createWithValidation(Class<?>... xsdClasses); void validateWithLocalCacheXSD(); String marshall(Object object); T unMarshall(String xml); T unMarshall(String xml, Class<T> klass);  }
@Test public void unmarshallPartialElement() { String xml = "<region name=\"one\">\n" + " <region-attributes scope=\"distributed-ack\" data-policy=\"replicate\"/>\n" + " </region>"; RegionConfig config = service2.unMarshall(xml, RegionConfig.class); assertThat(config.getName()).isEqualTo("one"); }
@Test public void unmarshallAnyElement() { String xml = "<region name=\"one\">\n" + " <region-attributes scope=\"distributed-ack\" data-policy=\"replicate\"/>\n" + " <custom:any xmlns:custom=\"http: + " </region>"; RegionConfig config = service2.unMarshall(xml, RegionConfig.class); assertThat(config.getName()).isEqualTo("one"); assertThat(config.getCustomRegionElements()).hasSize(1); }
@Test public void unmarshallIgnoresUnknownProperties() { String existingXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<cache version=\"1.0\" xsi:schemaLocation=\"http: + "http: + "xmlns=\"http: + "xmlns:ns2=\"http: + "xmlns:xsi=\"http: + " <ns2:id>one</ns2:id>\n" + " <ns2:type>onetype</ns2:type>\n" + " <ns2:value>onevalue</ns2:value>\n" + " </ns2:custom-one>\n" + "</cache>"; CacheConfig cacheConfig = service.unMarshall(existingXML); List elements = cacheConfig.getCustomCacheElements(); assertThat(elements.get(0)).isInstanceOf(ElementOne.class); }
@Test public void marshalOlderNameSpace() { String xml = "<cache xsi:schemaLocation=\"http: + " version=\"8.1\"\n" + " xmlns=\"http: " xmlns:xsi=\"http: " <region name=\"one\">\n" + " <region-attributes scope=\"distributed-ack\" data-policy=\"replicate\"/>\n" + " </region>\n" + "</cache>"; CacheConfig cacheConfig = service2.unMarshall(xml); assertThat(cacheConfig.getRegions()).hasSize(1); assertThat(cacheConfig.getRegions().get(0).getName()).isEqualTo("one"); }
@Test public void marshallNoNameSpace() { String xml = "<cache version=\"1.0\">\n" + " <region name=\"one\">\n" + " <region-attributes scope=\"distributed-ack\" data-policy=\"replicate\"/>\n" + " </region>\n" + "</cache>"; CacheConfig cacheConfig = service2.unMarshall(xml); assertThat(cacheConfig.getRegions()).hasSize(1); assertThat(cacheConfig.getRegions().get(0).getName()).isEqualTo("one"); }