KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junitx > runner > ResourceManagerTest


1 package junitx.runner;
2
3 import java.util.Properties JavaDoc;
4
5 import junit.framework.TestCase;
6
7 /**
8  * @version $Revision: 1.2 $, $Date: 2003/05/08 02:51:16 $
9  * @author <a HREF="mailto:vbossica@users.sourceforge.net">Vladimir Ritz Bossicard</a>
10  */

11 public class ResourceManagerTest extends TestCase {
12
13     private ResourceManager manager;
14
15     protected void setUp() throws Exception JavaDoc {
16         manager = new ResourceManager();
17         Properties JavaDoc props = new Properties JavaDoc();
18         props.put("junitx.runner.verbose", "false");
19         props.put("junitx.resource.1", "junitx.runner.ResourceManagerTest$MockResource");
20         props.put("junitx.resource.2", "junitx.runner.ResourceManagerTest$MockResource2");
21         manager.init(props);
22     }
23
24     public void testValidResource() throws Exception JavaDoc {
25         Resource resource = manager.lookup(junitx.runner.ResourceManagerTest.MockResource.class);
26         assertNotNull(resource);
27
28         Object JavaDoc integer = resource.getObject("java.lang.String");
29         assertNotNull(integer);
30     }
31
32     public void testInvalidObject() throws Exception JavaDoc {
33         Resource resource = manager.lookup(junitx.runner.ResourceManagerTest.MockResource.class);
34         assertNotNull(resource);
35
36         try {
37             resource.getObject("java.lang.Blah");
38         } catch (ClassNotFoundException JavaDoc e) {
39             return;
40         }
41     }
42
43     public void testInvalidResource() throws Exception JavaDoc {
44         try {
45             manager.lookup(java.lang.Integer JavaDoc.class);
46         } catch (RuntimeException JavaDoc e) {
47             return;
48         }
49         fail();
50     }
51
52     public void testInitialize() throws Exception JavaDoc {
53         MockResource resource =
54             (MockResource) manager.lookup(junitx.runner.ResourceManagerTest.MockResource.class);
55         assertNotNull(resource);
56
57         assertTrue(resource.initialized);
58         try {
59             resource.init(new Properties JavaDoc());
60             fail();
61         } catch (Exception JavaDoc e) {
62         }
63
64         manager.lookup(junitx.runner.ResourceManagerTest.MockResource.class);
65     }
66
67     public void testRelease() throws Exception JavaDoc {
68         MockResource resource =
69             (MockResource) manager.lookup(junitx.runner.ResourceManagerTest.MockResource.class);
70         assertNotNull(resource);
71
72         MockResource resource2 =
73             (MockResource) manager.lookup(junitx.runner.ResourceManagerTest.MockResource2.class);
74         assertNotNull(resource2);
75
76         assertFalse(resource.released);
77         assertFalse(resource2.released);
78
79         manager.release();
80
81         assertTrue(resource.released);
82         assertTrue(resource2.released);
83     }
84
85     static class MockResource implements Resource {
86         boolean initialized = false;
87         boolean released = false;
88
89         public void init(Properties JavaDoc props) throws Exception JavaDoc {
90             if (initialized) {
91                 throw new Exception JavaDoc("Already initialized");
92             }
93             initialized = true;
94         }
95
96         public Object JavaDoc getObject(String JavaDoc name) throws Exception JavaDoc {
97             return Class.forName(name).newInstance();
98         }
99
100         public void release() throws Exception JavaDoc {
101             released = true;
102         }
103     }
104
105     static class MockResource2 extends MockResource {
106     }
107
108 }
109
Popular Tags