protected TileLayer findLayer(String layerName) throws NoSuchElementException { TileLayer layer = getLayer(layerName) .orElseThrow( () -> new NoSuchElementException( "Layer " + layerName + " does not exist")); return layer; }
@Test public void testDefaultToCacheDir() throws Exception { shouldLoadFrom(cacheDir); XMLConfiguration config = new XMLConfiguration(provider, storageFinder); config.setGridSetBroker(broker); config.afterPropertiesSet(); config.getLayerCount(); assertThat(config.getLayer(MARKED_LAYER), notNullValue()); }
@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); }
@Test public void testAddLayer() throws Exception { int count = config.getLayerCount(); TileLayer tl = createTestLayer("testLayer"); config.addLayer(tl); assertEquals(count + 1, config.getLayerCount()); assertSame(tl, config.getLayer("testLayer").get()); try { config.addLayer(tl); fail("Expected IllegalArgumentException on duplicate layer name"); } catch (IllegalArgumentException e) { assertEquals("Layer 'testLayer' already exists", e.getMessage()); } }
/** @see TileLayerConfiguration#getTileLayer(java.lang.String) */ @Deprecated public @Nullable TileLayer getTileLayer(String layerName) { return getLayer(layerName).orElse(null); }
@Test public void testOverrideWithProperty() throws Exception { shouldLoadFrom(configDir); shouldNotLoadFrom(cacheDir); gwcConfigDirProp.setValue(configDir.getCanonicalPath()); XMLConfiguration config = new XMLConfiguration(provider, storageFinder); config.setGridSetBroker(broker); config.afterPropertiesSet(); config.getLayerCount(); assertThat(config.getLayer(MARKED_LAYER), notNullValue()); }
/** * @param tl the layer to add to this configuration * @return * @throws IllegalArgumentException if a layer named the same than {@code tl} already exists * @see TileLayerConfiguration#addLayer(org.geowebcache.layer.TileLayer) */ public synchronized void addLayer(TileLayer tl) throws IllegalArgumentException { if (tl == null) { throw new NullPointerException(); } if (!canSaveIfNotTransient(tl)) { throw new IllegalArgumentException( "Can't add layers of type " + tl.getClass().getName()); } if (getLayer(tl.getName()).isPresent()) { throw new IllegalArgumentException("Layer '" + tl.getName() + "' already exists"); } initialize(tl); getGwcConfig().getLayers().add(tl); updateLayers(); try { save(); } catch (IOException e) { // If save fails, try to revert the change to maintain a consistent state. if (getGwcConfig().getLayers().remove(tl)) { updateLayers(); } throw new ConfigurationPersistenceException("Unable to add layer " + tl.getName(), e); } }
@Test public void testOverrideWithPropertyRelative() throws Exception { shouldLoadFrom(configDir); shouldNotLoadFrom(cacheDir); gwcConfigDirProp.setValue(configDir.getName()); XMLConfiguration config = new XMLConfiguration(provider, storageFinder); config.setGridSetBroker(broker); config.afterPropertiesSet(); config.getLayerCount(); assertThat(config.getLayer(MARKED_LAYER), notNullValue()); }
@Test public void testPathAsArgument() throws Exception { shouldLoadFrom(configDir); shouldNotLoadFrom(cacheDir); XMLConfiguration config = new XMLConfiguration(provider, configDir.getCanonicalPath()); config.setGridSetBroker(broker); config.afterPropertiesSet(); assertThat(config.getLayer(MARKED_LAYER), notNullValue()); }
@Test public void testModifyLayer() throws Exception { WMSLayer layer1 = createTestLayer("testLayer"); config.addLayer(layer1); int count = config.getLayerCount(); WMSLayer layer2 = createTestLayer("testLayer"); config.modifyLayer(layer2); assertEquals(count, config.getLayerCount()); assertSame(layer2, config.getLayer("testLayer").get()); layer1 = createTestLayer("another"); try { config.modifyLayer(layer1); fail("Expected NoSuchElementException"); } catch (NoSuchElementException e) { assertTrue(true); } }