Fix serialization of GSWorkspaceEncoder and introducing test Class. Fix defect #12

This commit is contained in:
ccancellieri 2011-07-31 12:15:34 +02:00
parent 11204e4e29
commit a899315650
11 changed files with 134 additions and 37 deletions

View File

@ -86,11 +86,11 @@ public class GeoServerRESTPublisher {
* http://$GSIP:$GSPORT/$SERVLET/rest/workspaces
* }</PRE>
*/
public boolean createWorkspace(String workspace) {
String sUrl = restURL + "/rest/workspaces";
GSWorkspaceEncoder wsenc = new GSWorkspaceEncoder(workspace);
String wsxml = wsenc.toString();
String result = HTTPUtils.postXml(sUrl, wsxml, gsuser, gspass);
public boolean createWorkspace(final String workspace) {
final String sUrl = restURL + "/rest/workspaces";
final GSWorkspaceEncoder wsenc = new GSWorkspaceEncoder(workspace);
final String wsxml = wsenc.toString();
final String result = HTTPUtils.postXml(sUrl, wsxml, gsuser, gspass);
return result != null;
}

View File

@ -37,7 +37,7 @@ import it.geosolutions.geoserver.rest.encoder.utils.PropertyXMLEncoder;
*/
public class GSPostGISDatastoreEncoder extends PropertyXMLEncoder {
private NestedElementEncoder<String> connectionParameters = new NestedElementEncoder<String>("connectionParameters");
private NestedElementEncoder connectionParameters = new NestedElementEncoder("connectionParameters");
public GSPostGISDatastoreEncoder() {

View File

@ -57,14 +57,14 @@ public abstract class GSResourceEncoder<T extends GSDimensionInfoEncoder> extend
add("enabled", "true");
// Link members to the parent
addContent(metadata);
addContent(metadata.getRoot());
addContent(keywordsListEncoder);
}
final private GSMetadataEncoder<T> metadata=new GSMetadataEncoder<T>();
public void addMetadata(String key, T dimensionInfo) {
metadata.add(key, dimensionInfo);
metadata.add(key, dimensionInfo.getRoot());
}
final private Element keywordsListEncoder = new Element("keywords");

View File

@ -41,20 +41,41 @@ public class GSWorkspaceEncoder extends PropertyXMLEncoder {
}
public GSWorkspaceEncoder(String name) {
this();
super("workspace");
addName(name);
}
/**
* Add the name to this workspace
* @param name
* @throws IllegalStateException if name is already set
*/
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(){
final Element el=contains("name");
if (el!=null)
return el.getName();
return el.getTextTrim();
else
return "";
return null;
}
}

View File

@ -35,11 +35,11 @@ import org.jdom.Element;
*/
public class GSImageMosaicEncoder extends GSCoverageEncoder {
final private NestedElementEncoder<Element> parameters=new NestedElementEncoder<Element>("parameters");
final private NestedElementEncoder parameters=new NestedElementEncoder("parameters");
public GSImageMosaicEncoder() {
// Link members to the parent
addContent(parameters);
addContent(parameters.getRoot());
}
/**

View File

@ -27,14 +27,14 @@ package it.geosolutions.geoserver.rest.encoder.metadata;
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() {
super("metadata");
}
public void addMetadata(final String key, final T value) {
this.add(key, value);
this.add(key, value.getRoot());
}
}

View File

@ -67,26 +67,30 @@ import org.jdom.Element;
* @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*
*/
public class NestedElementEncoder<T> extends XmlElement {
public class NestedElementEncoder extends XmlElement {
public NestedElementEncoder(String listName) {
super(listName);
}
public void add(String key, T value) {
public void add(String key, String value) {
final Element entryElem = new Element("entry");
if (key != null)
entryElem.setAttribute("key", key);
entryElem.setText(value);
this.addContent(entryElem);
}
public void add(String key, Element value) {
final Element entryElem = new Element("entry");
if (key != null)
entryElem.setAttribute("key", key);
if (value instanceof String)
entryElem.setText((String) value);
else if (value instanceof Element)
entryElem.addContent((Element) value);
else
throw new IllegalArgumentException(
"Unable to add entry: unrecognized object");
entryElem.addContent(value);
this.addContent(entryElem);
}
}

View File

@ -59,7 +59,7 @@ public class PropertyXMLEncoder extends XmlElement {
protected void add(String key, String value) {
if (key != null && value != null) {
add(this, key, value);
add(this.getRoot(), key, value);
}
}

View File

@ -26,6 +26,7 @@
package it.geosolutions.geoserver.rest.encoder.utils;
import org.jdom.Content;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
@ -35,7 +36,10 @@ import org.jdom.output.XMLOutputter;
* @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());
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() {
return getChildren().isEmpty();
return root.getChildren().isEmpty();
}
public boolean remove(final Element el){
return ElementUtils.remove(this,el);
return ElementUtils.remove(root,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){
return ElementUtils.contains(this,key,val);
return ElementUtils.contains(root,key,val);
}
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
public String toString() {
return OUTPUTTER.outputString(this);
return OUTPUTTER.outputString(root);
}
}

View File

@ -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());
}
}

View File

@ -42,7 +42,7 @@ public class EntryKeyListEncoderTest extends TestCase {
ekle.add("k3", "v3");
Element root = new Element("root");
root.addContent(ekle);
root.addContent(ekle.getRoot());
assertEquals(1, root.getChildren().size());
assertNotNull(root.getChild("EKL"));