/** * Method responsible for writing out the entire GeoWebCacheConfiguration object * * <p>throws an exception if it does not succeed */ private void persistToFile() throws IOException { Assert.isTrue(resourceProvider.hasOutput()); // create the XStream for serializing the configuration XStream xs = getConfiguredXStreamWithContext(new GeoWebCacheXStream(), Context.PERSIST); try (OutputStreamWriter writer = new OutputStreamWriter(resourceProvider.out(), "UTF-8")) { // set version to latest String currentSchemaVersion = getCurrentSchemaVersion(); getGwcConfig().setVersion(currentSchemaVersion); writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); xs.toXML(getGwcConfig(), writer); } catch (UnsupportedEncodingException uee) { throw new IOException(uee.getMessage(), uee); } catch (FileNotFoundException fnfe) { throw fnfe; } catch (IOException e) { throw (IOException) new IOException( "Error writing to " + resourceProvider.getId() + ": " + e.getMessage()) .initCause(e); } log.info("Wrote configuration to " + resourceProvider.getId()); }
@Test public void testSaveCurrentVersion() throws Exception { URL source = XMLConfiguration.class.getResource( XMLConfigurationBackwardsCompatibilityTest.GWC_125_CONFIG_FILE); configFile = new File(configDir, "geowebcache.xml"); FileUtils.copyURLToFile(source, configFile); gridSetBroker = new GridSetBroker(true, true); config = new XMLConfiguration(null, configDir.getAbsolutePath()); config.setGridSetBroker(gridSetBroker); config.afterPropertiesSet(); config.getLayerCount(); final String previousVersion = config.getVersion(); assertNotNull(previousVersion); // Do a modify without any changes to trigger a save; config.modifyLayer(config.getLayer(config.getLayerNames().iterator().next()).get()); final String currVersion = XMLConfiguration.getCurrentSchemaVersion(); assertNotNull(currVersion); assertFalse(previousVersion.equals(currVersion)); config = new XMLConfiguration(null, configDir.getAbsolutePath()); config.setGridSetBroker(gridSetBroker); config.afterPropertiesSet(); config.getLayerCount(); final String savedVersion = config.getVersion(); assertEquals(currVersion, savedVersion); }