KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > util > ResourceManager


1 /*
2  * The JUnit-addons Software License, Version 1.0
3  * (based on the Apache Software License, Version 1.1)
4  *
5  * Copyright (c) 2002-2003 Vladimir R. Bossicard. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by Vladimir R.
22  * Bossicard as well as other contributors
23  * (http://junit-addons.sourceforge.net/)."
24  * Alternately, this acknowlegement may appear in the software itself,
25  * if and wherever such third-party acknowlegements normally appear.
26  *
27  * 4. The name "JUnit-addons" must not be used to endorse or promote
28  * products derived from this software without prior written
29  * permission. For written permission, please contact
30  * vbossica@users.sourceforge.net.
31  *
32  * 5. Products derived from this software may not be called "JUnit-addons"
33  * nor may "JUnit-addons" appear in their names without prior written
34  * permission of the project managers.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ======================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals. For more information on the JUnit-addons Project, please
52  * see <http://junit-addons.sourceforge.net/>.
53  */

54
55 package junitx.util;
56
57 import java.util.Hashtable JavaDoc;
58 import java.util.Map JavaDoc;
59
60 /**
61  * Utility class that manages resources and allows separate tests to share them.
62  *
63  * <h4>Example</h4>
64  *
65  * The AllTests class registers all external resourses by calling the
66  * <tt>addResource</tt> method of the ResourceManager:<p>
67  *
68  * <pre>
69  * public class AllTests {
70  *
71  * public static Test suite() {
72  * TestSuite suite = new TestSuite();
73  * suite.addTestSuite(MyClassTest.class);
74  *
75  * return new TestSetup(suite) {
76  * public void setUp()
77  * throws Exception {
78  * ResourceManager.addResource("RESOURCE_ID", new Object());
79  * }
80  * };
81  * }
82  * }
83  * </pre>
84  *
85  * The test class simply gets the resource from the ResourceManager during the
86  * set up process:<p>
87  *
88  * <pre>
89  * public class MyClassTest extends TestCase {
90  *
91  * private Object res;
92  *
93  * public void setUp() {
94  * res = ResourceManager.getResource("RESOURCE_ID");
95  * }
96  *
97  * public void testXYZ() {
98  * res.XYZ();
99  * }
100  * }
101  * </pre>
102  *
103  * To share a resource whithin the same <tt>TestCase</tt>:
104  *
105  * <pre>
106  * public class MyClassTest extends TestCase {
107  *
108  * private Object res;
109  *
110  * public void setUp() {
111  * if (!ResourceManager.contains("RESOURCE_ID")) {
112  * ResourceManager.addResource("RESOURCE_ID", new Object());
113  * }
114  * res = ResourceManager.getResource("RESOURCE_ID");
115  * }
116  *
117  * public void testXYZ() {
118  * res.XYZ();
119  * }
120  * }
121  * </pre>
122  *
123  * @version $Revision: 1.4 $ $Date: 2003/03/23 01:25:24 $
124  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir R. Bossicard</a>
125  */

126 public class ResourceManager {
127
128     private static Map JavaDoc resources = new Hashtable JavaDoc();
129
130     /**
131      * Don't let anyone have access to this constructor.
132      */

133     private ResourceManager() {
134     }
135
136     /**
137      * Associates the resource to the key in the manager. Neither the key nor
138      * the value can be null. The value can be retrieved by calling the
139      * getResource method with a key that is equal to the original key.
140      *
141      * @param key the resource key.
142      * @param value the resource.
143      * @throws IllegalArgumentException if the key is already used.
144      */

145     public static void addResource(String JavaDoc key,
146                                    Object JavaDoc value)
147             throws IllegalArgumentException JavaDoc {
148         if (resources.containsKey(key)) {
149             throw new IllegalArgumentException JavaDoc("Resource with key '" + key + "' already exists");
150         }
151         resources.put(key, value);
152     }
153
154     /**
155      * Returns the resource to which the specified key is mapped in this
156      * ResourceManager or <tt>null</tt> if the resource was not found.
157      *
158      * @param key a key in the ResourceManager.
159      * @return Object the resource to which the key is mapped in this hashtable.
160      * @throws NullPointerException if the key is null.
161      */

162     public static Object JavaDoc getResource(String JavaDoc key)
163             throws NullPointerException JavaDoc {
164         if (key == null) {
165             throw new NullPointerException JavaDoc("Invalid key <null>");
166         }
167         return resources.get(key);
168     }
169
170     /**
171      * Returns <tt>true</tt> if the ResourceManager contains a resource with
172      * the specified key.
173      */

174     public static boolean containsResource(String JavaDoc key) {
175         return resources.containsKey(key);
176     }
177     
178     /**
179      * Removes the key (and its corresponding resource) from this
180      * ResourceManager.
181      *
182      * @param key the key that needs to be removed.
183      */

184     public static void removeResource(String JavaDoc key) {
185         resources.remove(key);
186     }
187
188 }
189
Popular Tags