1 7 8 package org.dom4j.util; 9 10 import junit.framework.Test; 11 import junit.framework.TestSuite; 12 import junit.framework.TestCase; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 29 public class SimpleSingletonTest extends TestCase { 30 public SimpleSingletonTest(String name) { 31 super(name); 32 } 33 34 private static SingletonStrategy singleton; 35 36 private static Object reference; 37 38 public void setUp() throws Exception { 39 super.setUp(); 40 if (singleton == null) { 41 singleton = new PerThreadSingleton(); 42 singleton.setSingletonClassName(HashMap .class.getName()); 43 } 44 } 45 46 public void tearDown() throws Exception { 47 super.tearDown(); 48 } 49 50 public void testFirstInstance() throws Exception { 51 Map map = (Map ) singleton.instance(); 52 String expected = null; 53 String actual = (String ) map.get("Test"); 54 assertEquals("testInstance", expected, actual); 55 56 expected = "new value"; 57 map.put("Test", expected); 58 59 map = (Map ) singleton.instance(); 60 reference = map; 61 actual = (String ) map.get("Test"); 62 assertEquals("testFirstInstance", expected, actual); 63 } 64 65 public void testSecondInstance() throws Exception { 66 Map map = (Map ) singleton.instance(); 67 assertEquals("testSecondInstance reference", reference, map); 68 String actual = (String ) map.get("Test"); 69 String expected = "new value"; 70 assertEquals("testInstance", expected, actual); 71 } 72 73 public static Test suite() { 74 TestSuite suite = new TestSuite(); 75 suite.addTest(TestSuite.createTest(SimpleSingletonTest.class, 76 "testFirstInstance")); 77 suite.addTest(TestSuite.createTest(SimpleSingletonTest.class, 78 "testSecondInstance")); 79 return suite; 80 } 81 } 82 83 119 120 | Popular Tags |