Step 8a: Implement previous   contents   next
next 

Lets drive the development forward by XP-style tests. The test case for InvalidURLException ensures that maintenance does not break the behaviour we expect from the class. The remaining tests in this tutorial ensure that the URL and Parser classes fulfill the customer requirements.

First we define the missing details for the URL class:

  1. A class like URL should have a constructor which takes it's attributes as arguments.

  2. The constructor should be able to handle null values.

  3. Another method should take a possible URL string, parse it and return a filled URL object or throw an exception.

  4. A method toString() should format the attributes of the URL into a well-formed string.

  5. URLs should be comparable, so we should have an equals() methods.

The method (3.) requiring the parser will be postponed since we want to focus on the URL class for now. The other requirements will now be written down as tests:

  1. Create a test case
    Open the UML diagram for the de.extremejava.url package and right-click the URL class. Since we do not want to go through the pattern dialog, just select 'Create test case' in the XP submenu. You can also use the hotkey Ctrl-Shift-E.
    A test case will be created based on the XPTest configuration and the last choices in the TestCase pattern dialog. In the UML diagram you will see that the URL class gets the stereotype <<tested>>.

  2. Edit the test case
    On the left side of Together, a tree with the packages and classes will be displayed. Open the package diagfrom for unittests.de.extremejava.url by opening the tree and double-clicking on the symbol in front of the package name:


    (click on the image for a fullsize screenshot)

    Please remove the generated tests for the getter and setter methods. It is generally a good idea to let XPTest create tests for all public methods, but we do not need those tests now.

  3. Implement the constructor test
    To code the requirement for a constructor taking the attributes of an URL as it's arguments, we use the following method:

    public void testEquals ()
    {
      assert (new URL ("1", "2", "3", "4", 1, "5", "6", "7").equals (
              new URL ("1", "2", "3", "4", 1, "5", "6", "7")));
    }
          

    assert is a method inherited from junit.framework.TestCase. If it's argument is not true, it will throw an exception to show that the test failed. Every test method should use a variant of assert to ensure a result. An alternative is throwing an exception which will then be shown by the JUnit UIs.

  4. Implement null arguments test
    Another requirement is that the constructor should be able to handle null values. A suitable test would be this method:

    public void testEqualsNull ()
    {
      assert (new URL (null, null, null, null, -1, null, null, null).equals (
              new URL (null, null, null, null, -1, null, null, null)));
    }
          

  5. Test toString() method
    The following method will ensure that we can create URL strings:

    public void testToStringNotNull ()
    {
      URL url = new URL ("http", "heilwagen", "mypwd", "www.extreme-java.de", 80,
                         "/links/links.html", "xp", "query=false");
    
      assertEquals ("http://heilwagen:mypwd@www.extreme-java.de:80/"
                      + "links/links.html#xp?query=false",
                    url.toString ());
    }
          

    This method is not sufficient to test all combinations of null and not-null attributes of an URL. You can add more tests and find out my programming errors if you like :)

  6. Implement test for equals()
    We do not need a special test for this method since it is used in all tests above.

To compile the tests we need basic method bodies in the URL class. Please add the following code to that class:

public URL (String aProtocol, String aUser, String aPassword, String aHost,
            int    aPort,     String aPath, String aFragment, String aQuery)
{
  protocol  = aProtocol;
  user      = aUser;
  password  = aPassword;
  host      = aHost;
  port      = aPort;
  path      = aPath;
  fragment  = aFragment;
  query     = aQuery;
}


public String toString ()
{
  return "";
}


public boolean equals (URL anURL)
{
  return false;
}

Now we have defined our second test class. Compile the sources using Ant to ensure that no typos are left. After that we should combine both test classes in a suite to run them together.

  previous   contents   next
next 

© 2001 A. Heilwagen