From 631f3a9ffa84e0458f0b9cea8af15cf4e87595c6 Mon Sep 17 00:00:00 2001 From: etj Date: Tue, 31 Mar 2015 17:26:32 +0200 Subject: [PATCH] Improve GeoServer version handling. --- .../rest/decoder/about/GSVersionDecoder.java | 46 +++++++++------ .../decoder/about/GSVersionDecoderTest.java | 58 +++++++++++++++++++ .../encoder/feature/GSFeatureEncoderTest.java | 9 +-- 3 files changed, 92 insertions(+), 21 deletions(-) create mode 100644 src/test/java/it/geosolutions/geoserver/rest/decoder/about/GSVersionDecoderTest.java diff --git a/src/main/java/it/geosolutions/geoserver/rest/decoder/about/GSVersionDecoder.java b/src/main/java/it/geosolutions/geoserver/rest/decoder/about/GSVersionDecoder.java index 410f56e..32e8ceb 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/decoder/about/GSVersionDecoder.java +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/about/GSVersionDecoder.java @@ -1,7 +1,7 @@ /* * GeoServer-Manager - Simple Manager Library for GeoServer * - * Copyright (C) 2007,2011 GeoSolutions S.A.S. + * Copyright (C) 2007,2015 GeoSolutions S.A.S. * http://www.geo-solutions.it * * Permission is hereby granted, free of charge, to any person obtaining a copy @@ -147,12 +147,22 @@ public class GSVersionDecoder extends XmlElement { } public enum VERSION { - UNRECOGNIZED(-1), v22(22), v23(23), v24(24), v25(25), ABOVE(9999); + v22(22, "2\\.2([^0-9]|$).*"), + v23(23, "2\\.3([^0-9]|$).*"), + v24(24, "2\\.4([^0-9]|$).*"), + v25(25, "2\\.5([^0-9]|$).*"), + v26(26, "2\\.6([^0-9]|$).*"), + v27(27, "2\\.7([^0-9]|$).*"), + v28(28, "2\\.8([^0-9]|$).*"), + ABOVE(9999, "2\\..+"), + UNRECOGNIZED(-1, null); final private int version; + final private String pattern; - private VERSION(int val) { + private VERSION(int val, String pattern) { version = val; + this.pattern = pattern; } public int getVersion() { @@ -164,25 +174,27 @@ public class GSVersionDecoder extends XmlElement { } public static VERSION getVersion(String v) { - if (v==null) { - return UNRECOGNIZED; - } else if (v.matches("2\\.2.*")) { - return v22; - } else if (v.matches("2\\.3.*")) { - return v23; - } else if (v.matches("2\\.4.*")) { - return v24; - } else if (v.matches("2\\.5.*")) { - return v25; - } else if (v.matches("2\\..+")) { - return ABOVE; - } else { + if (v == null) { return UNRECOGNIZED; } + + for (VERSION version : VERSION.values()) { + if(version.pattern != null && v.matches(version.pattern)) { + return version; + } + } + + return UNRECOGNIZED; } public static String print(){ - return "["+v22+", "+v23+", "+v24+", "+v25+", "+ABOVE+", "+UNRECOGNIZED+"]"; + + StringBuilder sb = new StringBuilder("["); + for (VERSION v : VERSION.values()) { + sb.append(v.toString()).append(' '); + } + sb.append("]"); + return sb.toString(); } } diff --git a/src/test/java/it/geosolutions/geoserver/rest/decoder/about/GSVersionDecoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/decoder/about/GSVersionDecoderTest.java new file mode 100644 index 0000000..349a78b --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/rest/decoder/about/GSVersionDecoderTest.java @@ -0,0 +1,58 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2015 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package it.geosolutions.geoserver.rest.decoder.about; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author etj + */ +public class GSVersionDecoderTest { + + @Test + public void testGetGeoServer() { + + assertEquals(GSVersionDecoder.VERSION.v22, GSVersionDecoder.VERSION.getVersion("2.2")); + assertEquals(GSVersionDecoder.VERSION.v22, GSVersionDecoder.VERSION.getVersion("2.2-SNAPSHOT")); + + assertEquals(GSVersionDecoder.VERSION.v26, GSVersionDecoder.VERSION.getVersion("2.6")); + assertEquals(GSVersionDecoder.VERSION.v26, GSVersionDecoder.VERSION.getVersion("2.6-SNAPSHOT")); + assertEquals(GSVersionDecoder.VERSION.v26, GSVersionDecoder.VERSION.getVersion("2.6-ENTERPRISE")); + assertEquals(GSVersionDecoder.VERSION.v26, GSVersionDecoder.VERSION.getVersion("2.6-ENTERPRISE-SNAPSHOT")); + + assertEquals(GSVersionDecoder.VERSION.ABOVE, GSVersionDecoder.VERSION.getVersion("2.21")); + assertEquals(GSVersionDecoder.VERSION.ABOVE, GSVersionDecoder.VERSION.getVersion("2.21-SNAPSHOT")); + assertEquals(GSVersionDecoder.VERSION.ABOVE, GSVersionDecoder.VERSION.getVersion("2.42")); + assertEquals(GSVersionDecoder.VERSION.ABOVE, GSVersionDecoder.VERSION.getVersion("2.42-SNAPSHOT")); + + assertEquals(GSVersionDecoder.VERSION.UNRECOGNIZED, GSVersionDecoder.VERSION.getVersion("3.0")); + assertEquals(GSVersionDecoder.VERSION.UNRECOGNIZED, GSVersionDecoder.VERSION.getVersion("anystring")); + assertEquals(GSVersionDecoder.VERSION.UNRECOGNIZED, GSVersionDecoder.VERSION.getVersion(null)); + } + +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java index 9a5bc99..d2c381c 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/feature/GSFeatureEncoderTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007 - 2011 GeoSolutions S.A.S. + * Copyright (C) 2007 - 2015 GeoSolutions S.A.S. * http://www.geo-solutions.it * * GPLv3 + Classpath exception @@ -104,11 +104,12 @@ public class GSFeatureEncoderTest extends GeoserverRESTTest { fte.addMetadataLinkInfo(metadatalink); GSLayerEncoder layerEncoder = null; - if (VERSION.getVersion(GS_VERSION).compareTo(VERSION.UNRECOGNIZED) > 0) { - layerEncoder = new GSLayerEncoder(); - } else if (VERSION.getVersion(GS_VERSION).compareTo(VERSION.UNRECOGNIZED) == 0) { + if (VERSION.getVersion(GS_VERSION) == VERSION.UNRECOGNIZED ) { layerEncoder = new GSLayerEncoder21(); + } else { + layerEncoder = new GSLayerEncoder(); } + layerEncoder.setEnabled(true); layerEncoder.setQueryable(true); layerEncoder.setAdvertised(true);