1 package junitx.runner; 2 3 import java.util.Properties ; 4 5 import junit.framework.TestCase; 6 7 11 public class ResourceManagerTest extends TestCase { 12 13 private ResourceManager manager; 14 15 protected void setUp() throws Exception { 16 manager = new ResourceManager(); 17 Properties props = new Properties (); 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 { 25 Resource resource = manager.lookup(junitx.runner.ResourceManagerTest.MockResource.class); 26 assertNotNull(resource); 27 28 Object integer = resource.getObject("java.lang.String"); 29 assertNotNull(integer); 30 } 31 32 public void testInvalidObject() throws Exception { 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 e) { 39 return; 40 } 41 } 42 43 public void testInvalidResource() throws Exception { 44 try { 45 manager.lookup(java.lang.Integer .class); 46 } catch (RuntimeException e) { 47 return; 48 } 49 fail(); 50 } 51 52 public void testInitialize() throws Exception { 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 ()); 60 fail(); 61 } catch (Exception e) { 62 } 63 64 manager.lookup(junitx.runner.ResourceManagerTest.MockResource.class); 65 } 66 67 public void testRelease() throws Exception { 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 props) throws Exception { 90 if (initialized) { 91 throw new Exception ("Already initialized"); 92 } 93 initialized = true; 94 } 95 96 public Object getObject(String name) throws Exception { 97 return Class.forName(name).newInstance(); 98 } 99 100 public void release() throws Exception { 101 released = true; 102 } 103 } 104 105 static class MockResource2 extends MockResource { 106 } 107 108 } 109 | Popular Tags |