JUnit-addons
version 1.4

junitx.util
Class ResourceManager

java.lang.Object
  |
  +--junitx.util.ResourceManager

public class ResourceManager
extends java.lang.Object

Utility class that manages resources and allows separate tests to share them.

Example

The AllTests class registers all external resourses by calling the addResource method of the ResourceManager:

    public class AllTests {

       public static Test suite() {
          TestSuite suite = new TestSuite();
          suite.addTestSuite(MyClassTest.class);

          return new TestSetup(suite) {
              public void setUp()
                    throws Exception {
                 ResourceManager.addResource("RESOURCE_ID", new Object());
              }
          };
       }
    }
 
The test class simply gets the resource from the ResourceManager during the set up process:

    public class MyClassTest extends TestCase {

       private Object res;

       public void setUp() {
          res = ResourceManager.getResource("RESOURCE_ID");
       }

       public void testXYZ() {
          res.XYZ();
       }
    }
 
To share a resource whithin the same TestCase:
    public class MyClassTest extends TestCase {
 
       private Object res;

       public void setUp() {
          if (!ResourceManager.contains("RESOURCE_ID")) {
             ResourceManager.addResource("RESOURCE_ID", new Object());
          }
          res = ResourceManager.getResource("RESOURCE_ID");
       }

       public void testXYZ() {
          res.XYZ();
       }
    }
 

Version:
$Revision: 1.4 $ $Date: 2003/03/23 01:25:24 $
Author:
Vladimir R. Bossicard

Method Summary
static void addResource(java.lang.String key, java.lang.Object value)
          Associates the resource to the key in the manager.
static boolean containsResource(java.lang.String key)
          Returns true if the ResourceManager contains a resource with the specified key.
static java.lang.Object getResource(java.lang.String key)
          Returns the resource to which the specified key is mapped in this ResourceManager or null if the resource was not found.
static void removeResource(java.lang.String key)
          Removes the key (and its corresponding resource) from this ResourceManager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addResource

public static void addResource(java.lang.String key,
                               java.lang.Object value)
                        throws java.lang.IllegalArgumentException
Associates the resource to the key in the manager. Neither the key nor the value can be null. The value can be retrieved by calling the getResource method with a key that is equal to the original key.

Parameters:
key - the resource key.
value - the resource.
Throws:
java.lang.IllegalArgumentException - if the key is already used.

getResource

public static java.lang.Object getResource(java.lang.String key)
                                    throws java.lang.NullPointerException
Returns the resource to which the specified key is mapped in this ResourceManager or null if the resource was not found.

Parameters:
key - a key in the ResourceManager.
Returns:
Object the resource to which the key is mapped in this hashtable.
Throws:
java.lang.NullPointerException - if the key is null.

containsResource

public static boolean containsResource(java.lang.String key)
Returns true if the ResourceManager contains a resource with the specified key.


removeResource

public static void removeResource(java.lang.String key)
Removes the key (and its corresponding resource) from this ResourceManager.

Parameters:
key - the key that needs to be removed.

JUnit-addons
version 1.4

Copyright © 2002-2003 Vladimir R. Bossicard. All Rights Reserved.