org.geotools.xml.test
Class XMLTestSupport

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.geotools.xml.test.XMLTestSupport
All Implemented Interfaces:
junit.framework.Test

public abstract class XMLTestSupport
extends junit.framework.TestCase

Abstract test class to be used to unit test bindings.

Subclasses must implement the createConfiguration() method. It must return a new instance of Configuration. Example:

         
  public MailTypeBindingTest extends XMLTestSupport {

      protected Configuration createConfiguration() {
         return new MLConfiguration();
      }
  }
         
 

The parse() method is used to test binding parsing. Subclasses should call this from test methods after building up an instance document with document. Example

         
  public void testParsing() throws Exception {
      //build up an instance document

      //the root element
      Element mailElement = document.createElementNS( ML.NAMESPACE, "mail" );
      document.appendChild( mailElement );

      mailElement.setAttribute( "id", "someId" );
      ....

      //call parse
      Mail mail = (Mail) parse();

      //make assertions
      assertEquals( "someId", mail.getId() );
  }
         
 

The encode(Object, QName) method is used to test binding encoding. Subclasses should call this method from test methods after creating an object to be encoded. Example:

         
 public void testEncoding() throws Exception {
    //create the mail object
    Mail mail = new Mail( "someId" );
    mail.setEnvelope( ... );
    ....

    //call encode
    Document document = encode( mail, new QName( ML.NAMESPACE, "mail" );

    //make assertions
    assertEquals( "mail", document.getDocumentElement().getNodeName() );
    assertEquals( "someId", document.getDocumentElement().getAttribute( "id" ) );
 }
         
 

The binding(QName) method is used to obtain an instance of a particular binding. Subclasses should call this method to assert other properties of the binding, such as type mapping and execution mode. Example:

         
  public void testType() {
     //get an instance of the binding
     Binding binding = binding( new QName( ML.NAMESPACE, "MailType" ) );

     //make assertions
     assertEquals( Mail.class, binding.getType() );
  }

  public void testExecutionMode() {
    //get an instance of the binding
    Binding binding = binding( new QName( ML.NAMESPACE, "MailType" ) );

    //make assertions
    assertEquals( Binding.OVERRIDE, binding.getExecutionMode() );
  }
         
 

Author:
Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org

Field Summary
protected  org.w3c.dom.Document document
          the instance document
protected static java.util.logging.Logger logger
          Logging instance
protected  java.util.HashMap namespaceMappings
          additional namespace mappings
 
Constructor Summary
XMLTestSupport()
           
 
Method Summary
protected  Binding binding(javax.xml.namespace.QName name)
          Convenience method for obtaining an instance of a binding.
protected  void buildDocument(java.lang.String xml)
          Convenience method which parses the specified string into a dom and sets the built document which is to be parsed.
protected abstract  Configuration createConfiguration()
          Tempalte method for subclasses to create the configuration to be used by the parser.
protected  org.w3c.dom.Document encode(java.lang.Object object, javax.xml.namespace.QName element)
          Encodes an object, element name pair.
protected  org.w3c.dom.Document encode(java.lang.Object object, javax.xml.namespace.QName element, javax.xml.namespace.QName type)
          Encodes an object, element name pair explicitly specifying the type of the root element.
protected  org.w3c.dom.Element getElementByQName(org.w3c.dom.Document dom, javax.xml.namespace.QName name)
          Convenience method for finding a node in a document which matches the specified name.
protected  org.w3c.dom.Element getElementByQName(org.w3c.dom.Element parent, javax.xml.namespace.QName name)
          Convenience method for finding a single descendant of a particular node which matches the specified name.
protected  org.w3c.dom.NodeList getElementsByQName(org.w3c.dom.Document dom, javax.xml.namespace.QName name)
          Convenience method for finding nodes in a document which matche the specified name.
protected  org.w3c.dom.NodeList getElementsByQName(org.w3c.dom.Element parent, javax.xml.namespace.QName name)
          Convenience method for finding decendants of a particular node which match the specified name.
protected  java.lang.Object parse()
          Parses the built document.
protected  java.lang.Object parse(javax.xml.namespace.QName type)
          Parses the build document, explicity specifying the type of the root element.
protected  void print(org.w3c.dom.Node dom)
          Convenience method to dump the contents of the document to stdout.
protected  void registerNamespaceMapping(java.lang.String prefix, java.lang.String uri)
          Registers a namespace mapping.
protected  void registerNamespaces(org.w3c.dom.Element root)
          Deprecated. use registerNamespaceMapping(String, String)
protected  void setUp()
          Creates an empty xml document.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, tearDown, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

protected static java.util.logging.Logger logger
Logging instance


document

protected org.w3c.dom.Document document
the instance document


namespaceMappings

protected java.util.HashMap namespaceMappings
additional namespace mappings

Constructor Detail

XMLTestSupport

public XMLTestSupport()
Method Detail

setUp

protected void setUp()
              throws java.lang.Exception
Creates an empty xml document.

Overrides:
setUp in class junit.framework.TestCase
Throws:
java.lang.Exception

registerNamespaces

protected void registerNamespaces(org.w3c.dom.Element root)
Deprecated. use registerNamespaceMapping(String, String)

Template method for subclasses to register namespace mappings on the root element of an instance document.

Namespace mappings should be set as follows:

 
        root.setAttribute( "xmlns:gml", http://www.opengis.net/gml" );
 
 

Subclasses of this method should register the default namespace, the default namesapce is the one returned by the configuration.

This method is intended to be extended or overiden. This implementation registers the xsi,http://www.w3.org/2001/XMLSchema-instance namespace.

Parameters:
root - The root node of the instance document.

registerNamespaceMapping

protected void registerNamespaceMapping(java.lang.String prefix,
                                        java.lang.String uri)
Registers a namespace mapping.

This mapping will be included in the "namespace context" of both the parser and the encoder.

Parameters:
prefix - The prefix of the namespace, not null.
uri - The uri of the namespace, not null.

createConfiguration

protected abstract Configuration createConfiguration()
Tempalte method for subclasses to create the configuration to be used by the parser.

Returns:
A parser configuration.

parse

protected java.lang.Object parse(javax.xml.namespace.QName type)
                          throws java.lang.Exception
Parses the build document, explicity specifying the type of the root element.

This method should be called after building the entire document.

Parameters:
type - The name of the type of the root element of the build document.
Throws:
java.lang.Exception

parse

protected java.lang.Object parse()
                          throws java.lang.Exception
Parses the built document.

This method should be called after building the entire document.

Throws:
java.lang.Exception

encode

protected org.w3c.dom.Document encode(java.lang.Object object,
                                      javax.xml.namespace.QName element,
                                      javax.xml.namespace.QName type)
                               throws java.lang.Exception
Encodes an object, element name pair explicitly specifying the type of the root element.

Parameters:
object - The object to encode.
element - The name of the element to encode.
type - The type of the element
Returns:
The object encoded.
Throws:
java.lang.Exception

encode

protected org.w3c.dom.Document encode(java.lang.Object object,
                                      javax.xml.namespace.QName element)
                               throws java.lang.Exception
Encodes an object, element name pair.

Parameters:
object - The object to encode.
element - The name of the element to encode.
Returns:
The object encoded.
Throws:
java.lang.Exception

print

protected void print(org.w3c.dom.Node dom)
              throws java.lang.Exception
Convenience method to dump the contents of the document to stdout.

Throws:
java.lang.Exception

binding

protected Binding binding(javax.xml.namespace.QName name)
Convenience method for obtaining an instance of a binding.

Parameters:
name - The qualified name of the element,attribute,or type the binding "binds" to, the key of the binding in the container.
Returns:
The binding.

buildDocument

protected void buildDocument(java.lang.String xml)
                      throws java.lang.Exception
Convenience method which parses the specified string into a dom and sets the built document which is to be parsed.

Parameters:
xml - A string of xml
Throws:
java.lang.Exception

getElementByQName

protected org.w3c.dom.Element getElementByQName(org.w3c.dom.Document dom,
                                                javax.xml.namespace.QName name)
Convenience method for finding a node in a document which matches the specified name.


getElementByQName

protected org.w3c.dom.Element getElementByQName(org.w3c.dom.Element parent,
                                                javax.xml.namespace.QName name)
Convenience method for finding a single descendant of a particular node which matches the specified name.


getElementsByQName

protected org.w3c.dom.NodeList getElementsByQName(org.w3c.dom.Document dom,
                                                  javax.xml.namespace.QName name)
Convenience method for finding nodes in a document which matche the specified name.


getElementsByQName

protected org.w3c.dom.NodeList getElementsByQName(org.w3c.dom.Element parent,
                                                  javax.xml.namespace.QName name)
Convenience method for finding decendants of a particular node which match the specified name.



Copyright © 1996-2010 Geotools. All Rights Reserved.