Fix serialization of GSWorkspaceEncoder and introducing test Class. Fix defect #12
This commit is contained in:
parent
e3ab87857d
commit
05838a75df
@ -86,11 +86,11 @@ public class GeoServerRESTPublisher {
|
|||||||
* http://$GSIP:$GSPORT/$SERVLET/rest/workspaces
|
* http://$GSIP:$GSPORT/$SERVLET/rest/workspaces
|
||||||
* }</PRE>
|
* }</PRE>
|
||||||
*/
|
*/
|
||||||
public boolean createWorkspace(String workspace) {
|
public boolean createWorkspace(final String workspace) {
|
||||||
String sUrl = restURL + "/rest/workspaces";
|
final String sUrl = restURL + "/rest/workspaces";
|
||||||
GSWorkspaceEncoder wsenc = new GSWorkspaceEncoder(workspace);
|
final GSWorkspaceEncoder wsenc = new GSWorkspaceEncoder(workspace);
|
||||||
String wsxml = wsenc.toString();
|
final String wsxml = wsenc.toString();
|
||||||
String result = HTTPUtils.postXml(sUrl, wsxml, gsuser, gspass);
|
final String result = HTTPUtils.postXml(sUrl, wsxml, gsuser, gspass);
|
||||||
return result != null;
|
return result != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -37,7 +37,7 @@ import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder;
|
|||||||
*/
|
*/
|
||||||
public class GSPostGISDatastoreEncoder extends PropertyXMLEncoder {
|
public class GSPostGISDatastoreEncoder extends PropertyXMLEncoder {
|
||||||
|
|
||||||
private NestedElementEncoder<String> connectionParameters = new NestedElementEncoder<String>("connectionParameters");
|
private NestedElementEncoder connectionParameters = new NestedElementEncoder("connectionParameters");
|
||||||
|
|
||||||
|
|
||||||
public GSPostGISDatastoreEncoder() {
|
public GSPostGISDatastoreEncoder() {
|
||||||
|
|||||||
@ -57,14 +57,14 @@ public abstract class GSResourceEncoder<T extends GSDimensionInfoEncoder> extend
|
|||||||
add("enabled", "true");
|
add("enabled", "true");
|
||||||
|
|
||||||
// Link members to the parent
|
// Link members to the parent
|
||||||
addContent(metadata);
|
addContent(metadata.getRoot());
|
||||||
addContent(keywordsListEncoder);
|
addContent(keywordsListEncoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
final private GSMetadataEncoder<T> metadata=new GSMetadataEncoder<T>();
|
final private GSMetadataEncoder<T> metadata=new GSMetadataEncoder<T>();
|
||||||
|
|
||||||
public void addMetadata(String key, T dimensionInfo) {
|
public void addMetadata(String key, T dimensionInfo) {
|
||||||
metadata.add(key, dimensionInfo);
|
metadata.add(key, dimensionInfo.getRoot());
|
||||||
}
|
}
|
||||||
|
|
||||||
final private Element keywordsListEncoder = new Element("keywords");
|
final private Element keywordsListEncoder = new Element("keywords");
|
||||||
|
|||||||
@ -41,20 +41,41 @@ public class GSWorkspaceEncoder extends PropertyXMLEncoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public GSWorkspaceEncoder(String name) {
|
public GSWorkspaceEncoder(String name) {
|
||||||
this();
|
super("workspace");
|
||||||
addName(name);
|
addName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add the name to this workspace
|
||||||
|
* @param name
|
||||||
|
* @throws IllegalStateException if name is already set
|
||||||
|
*/
|
||||||
public void addName(String name) {
|
public void addName(String name) {
|
||||||
add("name", name);
|
final Element el=contains("name");
|
||||||
|
if (el==null)
|
||||||
|
add("name", name);
|
||||||
|
else
|
||||||
|
throw new IllegalStateException("Workspace name is already set: "+el.getText());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add or change (if already set) the workspace name
|
||||||
|
* @param name
|
||||||
|
*/
|
||||||
|
public void setName(String name) {
|
||||||
|
final Element el=contains("name");
|
||||||
|
if (el==null)
|
||||||
|
add("name", name);
|
||||||
|
else
|
||||||
|
el.setText(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName(){
|
public String getName(){
|
||||||
final Element el=contains("name");
|
final Element el=contains("name");
|
||||||
if (el!=null)
|
if (el!=null)
|
||||||
return el.getName();
|
return el.getTextTrim();
|
||||||
else
|
else
|
||||||
return "";
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -35,11 +35,11 @@ import org.jdom.Element;
|
|||||||
*/
|
*/
|
||||||
public class GSImageMosaicEncoder extends GSCoverageEncoder {
|
public class GSImageMosaicEncoder extends GSCoverageEncoder {
|
||||||
|
|
||||||
final private NestedElementEncoder<Element> parameters=new NestedElementEncoder<Element>("parameters");
|
final private NestedElementEncoder parameters=new NestedElementEncoder("parameters");
|
||||||
|
|
||||||
public GSImageMosaicEncoder() {
|
public GSImageMosaicEncoder() {
|
||||||
// Link members to the parent
|
// Link members to the parent
|
||||||
addContent(parameters);
|
addContent(parameters.getRoot());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -27,14 +27,14 @@ package it.geosolutions.geoserver.rest.encoder.metadata;
|
|||||||
|
|
||||||
import it.geosolutions.geoserver.rest.encoder.utils.NestedElementEncoder;
|
import it.geosolutions.geoserver.rest.encoder.utils.NestedElementEncoder;
|
||||||
|
|
||||||
public class GSMetadataEncoder <T extends GSDimensionInfoEncoder> extends NestedElementEncoder<T>{
|
public class GSMetadataEncoder <T extends GSDimensionInfoEncoder> extends NestedElementEncoder{
|
||||||
|
|
||||||
public GSMetadataEncoder() {
|
public GSMetadataEncoder() {
|
||||||
super("metadata");
|
super("metadata");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMetadata(final String key, final T value) {
|
public void addMetadata(final String key, final T value) {
|
||||||
this.add(key, value);
|
this.add(key, value.getRoot());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -67,26 +67,30 @@ import org.jdom.Element;
|
|||||||
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
|
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class NestedElementEncoder<T> extends XmlElement {
|
public class NestedElementEncoder extends XmlElement {
|
||||||
|
|
||||||
public NestedElementEncoder(String listName) {
|
public NestedElementEncoder(String listName) {
|
||||||
super(listName);
|
super(listName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(String key, T value) {
|
public void add(String key, String value) {
|
||||||
final Element entryElem = new Element("entry");
|
final Element entryElem = new Element("entry");
|
||||||
|
|
||||||
if (key != null)
|
if (key != null)
|
||||||
entryElem.setAttribute("key", key);
|
entryElem.setAttribute("key", key);
|
||||||
|
|
||||||
if (value instanceof String)
|
entryElem.setText(value);
|
||||||
entryElem.setText((String) value);
|
|
||||||
else if (value instanceof Element)
|
|
||||||
entryElem.addContent((Element) value);
|
|
||||||
else
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"Unable to add entry: unrecognized object");
|
|
||||||
|
|
||||||
this.addContent(entryElem);
|
this.addContent(entryElem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void add(String key, Element value) {
|
||||||
|
final Element entryElem = new Element("entry");
|
||||||
|
if (key != null)
|
||||||
|
entryElem.setAttribute("key", key);
|
||||||
|
|
||||||
|
entryElem.addContent(value);
|
||||||
|
|
||||||
|
this.addContent(entryElem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -59,7 +59,7 @@ public class PropertyXMLEncoder extends XmlElement {
|
|||||||
|
|
||||||
protected void add(String key, String value) {
|
protected void add(String key, String value) {
|
||||||
if (key != null && value != null) {
|
if (key != null && value != null) {
|
||||||
add(this, key, value);
|
add(this.getRoot(), key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -26,6 +26,7 @@
|
|||||||
package it.geosolutions.geoserver.rest.encoder.utils;
|
package it.geosolutions.geoserver.rest.encoder.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import org.jdom.Content;
|
||||||
import org.jdom.Element;
|
import org.jdom.Element;
|
||||||
import org.jdom.output.Format;
|
import org.jdom.output.Format;
|
||||||
import org.jdom.output.XMLOutputter;
|
import org.jdom.output.XMLOutputter;
|
||||||
@ -35,7 +36,10 @@ import org.jdom.output.XMLOutputter;
|
|||||||
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
|
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public abstract class XmlElement extends Element {
|
public class XmlElement{
|
||||||
|
|
||||||
|
private final Element root;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@ -44,29 +48,38 @@ public abstract class XmlElement extends Element {
|
|||||||
private final static XMLOutputter OUTPUTTER = new XMLOutputter(Format.getCompactFormat());
|
private final static XMLOutputter OUTPUTTER = new XMLOutputter(Format.getCompactFormat());
|
||||||
|
|
||||||
public XmlElement(final String name){
|
public XmlElement(final String name){
|
||||||
super(name);
|
root=new Element(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private XmlElement(){};
|
public Element getRoot(){
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
private XmlElement(){root=null;};
|
||||||
|
|
||||||
|
|
||||||
|
public Element addContent(Content child){
|
||||||
|
return root.addContent(child);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return getChildren().isEmpty();
|
return root.getChildren().isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean remove(final Element el){
|
public boolean remove(final Element el){
|
||||||
return ElementUtils.remove(this,el);
|
return ElementUtils.remove(root,el);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Element contains(final Element el){
|
public Element contains(final Element el){
|
||||||
return ElementUtils.contains(this,el);
|
return ElementUtils.contains(root,el);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Element contains(final String key, final String val){
|
public Element contains(final String key, final String val){
|
||||||
return ElementUtils.contains(this,key,val);
|
return ElementUtils.contains(root,key,val);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Element contains(final String key){
|
public Element contains(final String key){
|
||||||
return ElementUtils.contains(this,key);
|
return ElementUtils.contains(root,key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -74,6 +87,6 @@ public abstract class XmlElement extends Element {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return OUTPUTTER.outputString(this);
|
return OUTPUTTER.outputString(root);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2007 - 2011 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package it.geosolutions.geoserver.rest.encoder;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
|
||||||
|
*/
|
||||||
|
public class GSWorkspaceEncoderTest extends TestCase {
|
||||||
|
|
||||||
|
public GSWorkspaceEncoderTest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default logger
|
||||||
|
*/
|
||||||
|
protected final static Logger LOGGER = Logger.getLogger(GSWorkspaceEncoderTest.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAll() {
|
||||||
|
final GSWorkspaceEncoder wsenc = new GSWorkspaceEncoder("WS1");
|
||||||
|
LOGGER.info(wsenc.toString());
|
||||||
|
|
||||||
|
try{
|
||||||
|
wsenc.addName("test_name");
|
||||||
|
// NEVER HERE
|
||||||
|
Assert.assertTrue(false);
|
||||||
|
}catch (IllegalStateException e){
|
||||||
|
Assert.assertTrue(true);
|
||||||
|
}
|
||||||
|
wsenc.setName("new_name");
|
||||||
|
LOGGER.info(wsenc.toString());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -42,7 +42,7 @@ public class EntryKeyListEncoderTest extends TestCase {
|
|||||||
ekle.add("k3", "v3");
|
ekle.add("k3", "v3");
|
||||||
|
|
||||||
Element root = new Element("root");
|
Element root = new Element("root");
|
||||||
root.addContent(ekle);
|
root.addContent(ekle.getRoot());
|
||||||
|
|
||||||
assertEquals(1, root.getChildren().size());
|
assertEquals(1, root.getChildren().size());
|
||||||
assertNotNull(root.getChild("EKL"));
|
assertNotNull(root.getChild("EKL"));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user