From 0c6823057ba3d0bca88f5bc5f0ca704391349ee9 Mon Sep 17 00:00:00 2001 From: carlo cancellieri Date: Fri, 18 Oct 2013 14:29:22 +0200 Subject: [PATCH] close #105. add getVersion capabilities --- .../geoserver/rest/GeoServerRESTReader.java | 16 ++ .../rest/decoder/about/GSVersionDecoder.java | 163 ++++++++++++++++++ .../rest/encoder/utils/XmlElement.java | 6 +- .../geoserver/decoder/VersionDecoderTest.java | 73 ++++++++ .../geoserver/rest/GeoserverRESTTest.java | 15 +- 5 files changed, 269 insertions(+), 4 deletions(-) create mode 100644 src/main/java/it/geosolutions/geoserver/rest/decoder/about/GSVersionDecoder.java create mode 100644 src/test/java/it/geosolutions/geoserver/decoder/VersionDecoderTest.java diff --git a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java index e207f17..dfee204 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java +++ b/src/main/java/it/geosolutions/geoserver/rest/GeoServerRESTReader.java @@ -41,6 +41,7 @@ import it.geosolutions.geoserver.rest.decoder.RESTNamespaceList; import it.geosolutions.geoserver.rest.decoder.RESTResource; import it.geosolutions.geoserver.rest.decoder.RESTStyleList; import it.geosolutions.geoserver.rest.decoder.RESTWorkspaceList; +import it.geosolutions.geoserver.rest.decoder.about.GSVersionDecoder; import java.net.MalformedURLException; import java.net.URL; @@ -166,6 +167,21 @@ public class GeoServerRESTReader { public boolean existGeoserver() { return HTTPUtils.httpPing(baseurl + "/rest/", username, password); } + + /** + * Return the version of the target GeoServer + */ + public GSVersionDecoder getGeoserverVersion() { + final String url = baseurl + "/rest/about/version.xml"; + String xml = load(url); + if (xml == null) { + GSVersionDecoder v = new GSVersionDecoder(); + v.getGeoServer().setVersion(GSVersionDecoder.VERSION.BEFORE); + return v; + } else { + return GSVersionDecoder.build(load(url)); + } + } //========================================================================== //=== STYLES 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 new file mode 100644 index 0000000..2fb95c1 --- /dev/null +++ b/src/main/java/it/geosolutions/geoserver/rest/decoder/about/GSVersionDecoder.java @@ -0,0 +1,163 @@ +/* + * GeoServer-Manager - Simple Manager Library for GeoServer + * + * Copyright (C) 2007,2011 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 it.geosolutions.geoserver.rest.decoder.utils.JDOMBuilder; +import it.geosolutions.geoserver.rest.encoder.utils.ElementUtils; +import it.geosolutions.geoserver.rest.encoder.utils.XmlElement; + +import org.jdom.Attribute; +import org.jdom.Element; + +/** + * Encode an XML for about/version.xml + * + * + * + * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it + * + */ +public class GSVersionDecoder extends XmlElement { + public final static String ABOUT = "about"; + + final private GSAboutResource geoserver; + + public class GSAboutResource extends XmlElement { + public final static String RESOURCE = "resource"; + + public final static String NAME = "name"; + + public final static String VERSION = "Version"; + + final private Element version; + + public GSAboutResource() { + super(RESOURCE); + version = new Element(VERSION); + addContent(version); + } + + public void setName(String name) { + final Attribute _name = this.getRoot().getAttribute(GSAboutResource.NAME); + if (name!=null) + _name.setValue(name); + else + this.getRoot().setAttribute(GSAboutResource.NAME, name); + + } + + public String getName() { + final Attribute name = this.getRoot().getAttribute(GSAboutResource.NAME); + if (name!=null) + return name.getValue(); + else + return null; + } + + public GSAboutResource(Element el) { + super(el); + version = ElementUtils.contains(el, GSAboutResource.VERSION); + } + + public void setVersion(String v){ + version.setText(v); + } + } + + + /** + * Load the string representation into this encoder + * + * @param document + */ + public GSVersionDecoder(String document) { + super(JDOMBuilder.buildElement(document)); + geoserver = new GSAboutResource(ElementUtils.contains(this.getRoot(), + GSAboutResource.RESOURCE)); + } + + public GSVersionDecoder() { + super("about"); + geoserver = new GSAboutResource(); + addContent(geoserver.getRoot()); + } + + public GSAboutResource getGeoServer(){ + return geoserver; + } + + public VERSION getVersion() { + Element e = ElementUtils.contains(geoserver.version, GSAboutResource.VERSION); + return VERSION.getVersion(e.getTextTrim()); + } + + public static GSVersionDecoder build(String response) { + return new GSVersionDecoder(response); + } + + public enum VERSION { + BEFORE(0), v21(21), v22(22), v23(23), v24(24), v25(25), ABOVE(9999), URECOGNIZED(-1); + + final private int version; + + private VERSION(int val) { + version = val; + } + + public int getVersion() { + return version; + } + + public String toString(){ + return Integer.toString(version); + } + + public static VERSION getVersion(String v) { + if (v.matches("2\\.0.*") || v.matches("1\\..*")) { + return BEFORE; + } else if (v.matches("2\\.1.*")) { + return v21; + } 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 { + return URECOGNIZED; + } + } + + public static String print(){ + return "["+BEFORE+", "+v21+", "+v22+", "+v23+", "+v24+", "+v25+", "+ABOVE+"]"; + } + } + +} diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/XmlElement.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/XmlElement.java index 05d2fb1..ce6573e 100644 --- a/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/XmlElement.java +++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/XmlElement.java @@ -48,7 +48,11 @@ public class XmlElement{ public XmlElement(final String name){ root=new Element(name); } - + + public XmlElement(final Element e) { + root = e; + } + public Element getRoot(){ return root; } diff --git a/src/test/java/it/geosolutions/geoserver/decoder/VersionDecoderTest.java b/src/test/java/it/geosolutions/geoserver/decoder/VersionDecoderTest.java new file mode 100644 index 0000000..27c0176 --- /dev/null +++ b/src/test/java/it/geosolutions/geoserver/decoder/VersionDecoderTest.java @@ -0,0 +1,73 @@ +/* + * GeoBatch - Open Source geospatial batch processing system + * https://github.com/nfms4redd/nfms-geobatch + * Copyright (C) 2007-2012 GeoSolutions S.A.S. + * http://www.geo-solutions.it + * + * GPLv3 + Classpath exception + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package it.geosolutions.geoserver.decoder; + +import it.geosolutions.geoserver.rest.decoder.about.GSVersionDecoder; +import it.geosolutions.geoserver.rest.decoder.about.GSVersionDecoder.VERSION; +import junit.framework.Assert; + +import org.jdom.Element; +import org.jdom.output.XMLOutputter; +import org.junit.Test; + +/** + * @author Carlo Cancellieri {@code + * + * + * 10-Oct-2013 03:08 + * 32db076555e57cc5f826b0361d1af4efe6d3f01b + * 2.2-ENTERPRISE-SNAPSHOT + * + * + * 10-Oct-2013 03:01 + * da12effd42a9545628bd6e8ec20494607fbfe3a4 + * 8-ENTERPRISE-SNAPSHOT + * + * + * } + */ +public class VersionDecoderTest { + + private final String version = "10-Oct-2013 03:08" + + "32db076555e57cc5f826b0361d1af4efe6d3f01b2.2-ENTERPRISE-SNAPSHOT"; + + @Test + public void testVersionDecoder() { + + GSVersionDecoder dec=new GSVersionDecoder(version); + Assert.assertEquals(VERSION.v22, dec.getVersion()); + Assert.assertEquals("GeoServer", dec.getGeoServer().getName()); + + GSVersionDecoder.GSAboutResource geoserver=dec.getGeoServer(); + geoserver.setVersion("2.3-SNAPSHOT"); + geoserver.setName("_CustomGeoServerName_"); + Assert.assertEquals(VERSION.v23, dec.getVersion()); + Assert.assertEquals("_CustomGeoServerName_", dec.getGeoServer().getName()); + + //print(dec.getRoot()); + } + + public String print(Element e){ + return new XMLOutputter().outputString(e); + } + +} diff --git a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java b/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java index 1d3902e..cb54927 100644 --- a/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java +++ b/src/test/java/it/geosolutions/geoserver/rest/GeoserverRESTTest.java @@ -31,6 +31,7 @@ import it.geosolutions.geoserver.rest.decoder.RESTDataStore; import it.geosolutions.geoserver.rest.decoder.RESTFeatureType; import it.geosolutions.geoserver.rest.decoder.RESTLayer; import it.geosolutions.geoserver.rest.decoder.RESTLayerGroup; +import it.geosolutions.geoserver.rest.decoder.about.GSVersionDecoder; import it.geosolutions.geoserver.rest.decoder.utils.NameLinkElem; import java.net.MalformedURLException; @@ -68,6 +69,9 @@ public abstract class GeoserverRESTTest extends Assert { public static final String RESTPW; + // geoserver target version + public static final String VERSION; + public static URL URL; public static GeoServerRESTManager manager; @@ -84,6 +88,7 @@ public abstract class GeoserverRESTTest extends Assert { RESTURL = getenv("gsmgr_resturl", "http://localhost:8080/geoserver"); RESTUSER = getenv("gsmgr_restuser", "admin"); RESTPW = getenv("gsmgr_restpw", "geoserver"); + VERSION = getenv("gsmgr_version", "2.4"); // These tests will destroy data, so let's make sure we do want to run them enabled = getenv("gsmgr_resttest", "false").equalsIgnoreCase("true"); @@ -120,12 +125,16 @@ public abstract class GeoserverRESTTest extends Assert { LOGGER.info("Using geoserver instance " + RESTUSER + ":" + RESTPW + " @ " + RESTURL); } - } - - if (!existgs) { + } else { System.out.println("Failing tests : geoserver not found"); fail("GeoServer not found"); } + + GSVersionDecoder v=reader.getGeoserverVersion(); + if (v.getVersion().equals(GSVersionDecoder.VERSION.getVersion(VERSION))){ + System.out.println("Failing tests : geoserver version does not match.\nAccepted versions: "+GSVersionDecoder.VERSION.print()); + fail("GeoServer version ("+v.getVersion()+") does not match the desired one (+VERSION+)"); + } } else { System.out.println("Skipping tests "); }