Merge pull request #111 from geosolutions-it/1.5.x-110-IllegalArgument-getVersion

close 110. IllegalArgument on getVersion on Geoserver 2.1
This commit is contained in:
Carlo Cancellieri 2013-10-21 06:50:00 -07:00
commit cb6fc418f2
3 changed files with 58 additions and 14 deletions

View File

@ -43,7 +43,7 @@ import org.jdom.Element;
public class GSVersionDecoder extends XmlElement { public class GSVersionDecoder extends XmlElement {
public final static String ABOUT = "about"; public final static String ABOUT = "about";
final private GSAboutResource geoserver; private GSAboutResource geoserver;
public class GSAboutResource extends XmlElement { public class GSAboutResource extends XmlElement {
public final static String RESOURCE = "resource"; public final static String RESOURCE = "resource";
@ -52,10 +52,14 @@ public class GSVersionDecoder extends XmlElement {
public final static String VERSION = "Version"; public final static String VERSION = "Version";
final private Element version; private Element version;
public GSAboutResource() { public GSAboutResource() {
super(RESOURCE); create();
}
private void create(){
setRoot(RESOURCE);
version = new Element(VERSION); version = new Element(VERSION);
addContent(version); addContent(version);
} }
@ -78,8 +82,14 @@ public class GSVersionDecoder extends XmlElement {
} }
public GSAboutResource(Element el) { public GSAboutResource(Element el) {
super(el); super();
version = ElementUtils.contains(el, GSAboutResource.VERSION); if (el!=null){
setRoot(el);
version = ElementUtils.contains(el, GSAboutResource.VERSION);
} else {
create();
setVersion(GSVersionDecoder.VERSION.UNRECOGNIZED.toString());
}
} }
public void setVersion(String v){ public void setVersion(String v){
@ -94,13 +104,22 @@ public class GSVersionDecoder extends XmlElement {
* @param document * @param document
*/ */
public GSVersionDecoder(String document) { public GSVersionDecoder(String document) {
super(JDOMBuilder.buildElement(document)); Element root=JDOMBuilder.buildElement(document);
geoserver = new GSAboutResource(ElementUtils.contains(this.getRoot(), if (root!=null){
GSAboutResource.RESOURCE)); setRoot(root);
geoserver = new GSAboutResource(ElementUtils.contains(this.getRoot(),
GSAboutResource.RESOURCE));
}else {
create();
}
} }
public GSVersionDecoder() { public GSVersionDecoder() {
super("about"); create();
}
private void create(){
setRoot("about");
geoserver = new GSAboutResource(); geoserver = new GSAboutResource();
addContent(geoserver.getRoot()); addContent(geoserver.getRoot());
} }

View File

@ -39,9 +39,10 @@ import org.jdom.output.XMLOutputter;
*/ */
public class XmlElement{ public class XmlElement{
private final Element root; private Element root;
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
private final static XMLOutputter OUTPUTTER = new XMLOutputter(Format.getCompactFormat()); private final static XMLOutputter OUTPUTTER = new XMLOutputter(Format.getCompactFormat());
@ -52,14 +53,34 @@ public class XmlElement{
public XmlElement(final Element e) { public XmlElement(final Element e) {
root = e; root = e;
} }
/**
* Empty constructor:<br/>
* Use {@link #setRoot()} to initialize the root
*/
protected XmlElement() {
}
/**
* update the root of this node
* @param root
*/
protected void setRoot(final Element root) {
this.root = root;
}
/**
* update the root of this node
* @param name is the name of the root node
*/
protected void setRoot(final String name){
root=new Element(name);
}
public Element getRoot(){ public Element getRoot(){
return root; return root;
} }
@SuppressWarnings("unused")
private XmlElement(){root=null;};
protected void add(final String nodename, final String nodetext) { protected void add(final String nodename, final String nodetext) {
add(nodename,new Text(nodetext)); add(nodename,new Text(nodetext));
} }

View File

@ -66,6 +66,10 @@ public class VersionDecoderTest extends GeoserverRESTTest {
Assert.assertEquals(GSVersionDecoder.VERSION.v23, dec.getVersion()); Assert.assertEquals(GSVersionDecoder.VERSION.v23, dec.getVersion());
Assert.assertEquals("_CustomGeoServerName_", dec.getGeoServer().getName()); Assert.assertEquals("_CustomGeoServerName_", dec.getGeoServer().getName());
dec=new GSVersionDecoder(null);
Assert.assertEquals(GSVersionDecoder.VERSION.UNRECOGNIZED, dec.getVersion());
Assert.assertEquals(null, dec.getGeoServer().getName());
//print(dec.getRoot()); //print(dec.getRoot());
} }