Working XML

Compiling the paths and automating tests

A closer look at algorithms and JUnit

Return to article.

Listing 4. QNameTest.java
package org.ananas.hc.test;
import junit.framework.TestCase;
import org.ananas.hc.QName;
public class QNameTest
   extends TestCase
{
   private QName simpara,
                 _simpara,
                 sect1,
                 _sect1,
                 href,
                 root;
   public QNameTest(String name)
   {
      super(name);
   }
   protected void setUp()
   {
      // StringBuffers enable testing internalization of Strings
      // otherwise "simpara" == "simpara"
      StringBuffer uri = new StringBuffer("http://www.ananas.org/2001/docbook"),
                   tSimpara = new StringBuffer("simpara"),
                   tSect1 = new StringBuffer("sect1"),
                   empty = new StringBuffer("");
      simpara = new QName(QName.ELEMENT,
                          uri.toString(),
                          tSimpara.toString());
      _simpara = new QName(QName.ELEMENT,
                           "http://www.ananas.org/2001/docbook",
                           "simpara");
      sect1 = new QName(QName.ELEMENT,null,tSect1.toString());
      _sect1 = new QName(QName.ELEMENT,empty.toString(),"sect1");
      href = new QName(QName.ATTRIBUTE,null,"href");
      root = new QName(QName.ROOT,null,"/");
   }
   public void testConstructor()
   {
      int exceptionCount = 0,
          testCount = 3;
      for(int i = 0;i < testCount;i++)
         try
         {
            switch(i)
            {
               case 0:
                  new QName(256,null,"simpara");
                  break;
               case 1:
                  new QName(-256,null,"simpara");
                  break;
               case 2:
                  new QName(QName.ELEMENT,null,null);
                  break;
            }
         }
         catch(IllegalArgumentException e)
         {
            exceptionCount++;
         }
      assertTrue(exceptionCount == testCount);
   }
   public void testGetter()
   {
      assertEquals(simpara.getNamespaceURI(),"http://www.ananas.org/2001/docbook");
      assertEquals(simpara.getLocalName(),"simpara");
      assertEquals(sect1.getNamespaceURI(),"");
      assertTrue(!sect1.getNamespaceURI().equals(null));
      assertTrue(root.getType() == QName.ROOT);
      assertTrue(simpara.getType() == QName.ELEMENT);
      assertTrue(href.getType() == QName.ATTRIBUTE);
   }
   public void testEquals()
   {
      assertEquals(simpara,simpara);
      assertEquals(simpara,_simpara);
      assertEquals(_simpara,simpara);
      assertTrue(simpara.hashCode() == _simpara.hashCode());
      assertTrue(!simpara.equals(sect1));
      assertTrue(!simpara.equals(new QName(QName.ELEMENT,
                                           simpara.getNamespaceURI(),
                                           "hc")));
      assertTrue(!simpara.equals(new QName(QName.ELEMENT,
                                           "hc",
                                           simpara.getLocalName())));
      assertTrue(!simpara.equals(new String("hc")));
      assertEquals(sect1,sect1);
      assertEquals(sect1,_sect1);
      assertEquals(_sect1,sect1);
      assertTrue(sect1.hashCode() == _sect1.hashCode());
   }
	public static void main(String params[])
   {
		junit.awtui.TestRunner.run(QNameTest.class);
	}
}

Return to article.