Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

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.