public <T extends CacheElement> List<T> findCustomCacheElements(Class<T> classT) { List<T> newList = new ArrayList<>(); // streaming won't work here, because it's trying to cast element into CacheElement for (Object element : getCustomCacheElements()) { if (classT.isInstance(element)) { newList.add(classT.cast(element)); } } return newList; }
@Test public void unmarshallIgnoresUnknownProperties() { // say xml has a type attribute that is removed in the new version String existingXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<cache version=\"1.0\" xsi:schemaLocation=\"http://geode.apache.org/schema/cache " + "http://geode.apache.org/schema/cache/cache-1.0.xsd\" " + "xmlns=\"http://geode.apache.org/schema/cache\" " + "xmlns:ns2=\"http://geode.apache.org/schema/CustomOne\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + " <ns2:custom-one>\n" + " <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 xmlWithCustomElementsCanBeUnMarshalledByAnotherService() { CacheConfig cache = new CacheConfig(); setBasicValues(cache); cache.getCustomCacheElements().add(new ElementOne("test")); cache.getCustomCacheElements().add(new ElementTwo("test")); String prettyXml = service.marshall(cache); System.out.println(prettyXml); CacheConfig cacheConfig = service2.unMarshall(prettyXml); List elements = cacheConfig.getCustomCacheElements(); assertThat(elements.get(0)).isNotInstanceOf(ElementOne.class); assertThat(elements.get(1)).isNotInstanceOf(ElementTwo.class); String uglyXml = service2.marshall(cacheConfig); System.out.println(uglyXml); assertThat(uglyXml).isNotEqualTo(prettyXml); // the xml can be unmarshalled correctly by the first service String newXml = service.marshall(service.unMarshall(uglyXml)); assertThat(newXml).isEqualTo(prettyXml); }
@Test // in case a locator in the cluster doesn't have the plugin installed public void xmlWithCustomElementsCanBeUnMarshalledByAnotherService() { service.updateCacheConfig("cluster", config -> { config.getCustomCacheElements().add(new ElementOne("one")); config.getCustomCacheElements().add(new ElementTwo("two")); return config; }); String prettyXml = configuration.getCacheXmlContent(); System.out.println(prettyXml); // the xml is sent to another locator with no such plugin installed, it can be parsed // but the element couldn't be recognized by the locator without the plugin service2.updateCacheConfig("cluster", cc -> cc); CacheConfig config = service2.getCacheConfig("cluster"); assertThat(config.findCustomCacheElement("one", ElementOne.class)).isNull(); String uglyXml = configuration.getCacheXmlContent(); System.out.println(uglyXml); assertThat(uglyXml).isNotEqualTo(prettyXml); // the xml can be unmarshalled correctly by the first locator CacheConfig cacheConfig = service.getCacheConfig("cluster"); service.updateCacheConfig("cluster", cc -> cc); assertThat(cacheConfig.getCustomCacheElements()).hasSize(2); assertThat(cacheConfig.getCustomCacheElements().get(0)).isInstanceOf(ElementOne.class); assertThat(cacheConfig.getCustomCacheElements().get(1)).isInstanceOf(ElementTwo.class); assertThat(configuration.getCacheXmlContent()).isEqualTo(prettyXml); }
@Test public void testCacheOneMarshall() throws Exception { CacheConfig cache = new CacheConfig(); setBasicValues(cache); cache.getCustomCacheElements().add(new ElementOne("test")); xml = service.marshall(cache); System.out.println(xml); // cache has the default namespace assertThat(xml).contains("xmlns=\"http://geode.apache.org/schema/cache\""); assertThat(xml).contains("custom-one>"); }
@Test public void testMixMarshall() throws Exception { CacheConfig cache = new CacheConfig(); setBasicValues(cache); cache.getCustomCacheElements().add(new ElementOne("testOne")); xml = service.marshall(cache); System.out.println(xml); assertThat(xml).contains("custom-one>"); unmarshalled = service2.unMarshall(xml); unmarshalled.getCustomCacheElements().add(new ElementTwo("testTwo")); // xml generated wtih CacheConfigTwo has both elements in there. xml = service.marshall(unmarshalled); System.out.println(xml); assertThat(xml).contains("custom-one>"); assertThat(xml).contains("custom-two>"); assertThat(xml).containsPattern("xmlns=\"http://geode.apache.org/schema/cache\""); assertThat(xml).containsPattern("xmlns:ns\\d=\"http://geode.apache.org/schema/CustomOne\""); assertThat(xml).containsPattern("xmlns:ns\\d=\"http://geode.apache.org/schema/CustomTwo\""); }
@Test public void addCustomCacheElement() { ElementOne customOne = new ElementOne("testOne"); service.updateCacheConfig("cluster", config -> { config.getCustomCacheElements().add(customOne); return config; }); System.out.println(configuration.getCacheXmlContent()); assertThat(configuration.getCacheXmlContent()).contains("custom-one>"); JAXBServiceTest.ElementTwo customTwo = new ElementTwo("testTwo"); service.updateCacheConfig("cluster", config -> { config.getCustomCacheElements().add(customTwo); return config; }); System.out.println(configuration.getCacheXmlContent()); assertThat(configuration.getCacheXmlContent()).contains("custom-one>"); assertThat(configuration.getCacheXmlContent()).contains("custom-two>"); }