1 /*
2 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3 *
4 * This software is open source.
5 * See the bottom of this file for the licence.
6 */
7
8 package org.dom4j.util;
9
10 import junit.framework.Test;
11 import junit.framework.TestSuite;
12 import junit.framework.TestCase;
13 import junit.extensions.RepeatedTest;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import com.clarkware.junitperf.LoadTest;
19 import com.clarkware.junitperf.TimedTest;
20
21 /***
22 * PerThreadSingleton Tester.
23 *
24 * @author ddlucas
25 * @since
26 *
27 * <pre>
28 * 01 / 05 / 2005
29 * </pre>
30 *
31 * @version 1.0
32 */
33 public class PerThreadSingletonTest extends TestCase {
34 public PerThreadSingletonTest(String name) {
35 super(name);
36 }
37
38 private static SingletonStrategy singleton;
39
40 private static ThreadLocal reference = new ThreadLocal();
41
42 public void setUp() throws Exception {
43 super.setUp();
44 synchronized (PerThreadSingletonTest.class) {
45 if (singleton == null) {
46 singleton = new PerThreadSingleton();
47 singleton.setSingletonClassName(HashMap.class.getName());
48 }
49 }
50 }
51
52 public void tearDown() throws Exception {
53 super.tearDown();
54 }
55
56 public void testInstance() throws Exception {
57 String tid = Thread.currentThread().getName();
58 Map map = (Map) singleton.instance();
59
60 String expected = "new value";
61 if (!map.containsKey(tid) && reference.get() != null) {
62 System.out.println("tid=" + tid + " map=" + map);
63 System.out.println("reference=" + reference);
64 System.out.println("singleton=" + singleton);
65 fail("created singleton more than once");
66 } else {
67 map.put(tid, expected);
68 reference.set(map);
69 }
70
71 String actual = (String) map.get(tid);
72 // System.out.println("tid="+tid+ " map="+map);
73 assertEquals("testInstance", expected, actual);
74
75 map = (Map) singleton.instance();
76 expected = "new value";
77 actual = (String) map.get(tid);
78 // System.out.println("tid="+tid+ " map="+map);
79 // System.out.println("reference="+reference);
80 // System.out.println("singleton="+singleton);
81 assertEquals("testInstance", expected, actual);
82 assertEquals("testInstance reference", reference.get(), map);
83
84 }
85
86 /***
87 * Assembles and returns a test suite.
88 *
89 * @return The suite
90 */
91 public static Test suite() {
92 TestSuite suite = new TestSuite();
93 suite.addTest(makeRepeatedLoadTest(5, 100, "testInstance"));
94 return suite;
95 }
96
97 /***
98 * JUnit method to exercise test via threads and loops
99 *
100 * @param users
101 * Number of users to simulate (i.e. Threads).
102 * @param iterations
103 * Number of iterations per user ( repeat the test x times).
104 * @param testMethod
105 * method to execute (testXXX).
106 *
107 * @return A Junit test
108 */
109 protected static Test makeRepeatedLoadTest(int users, int iterations,
110 String testMethod) {
111 long maxElapsedTime = 1200 + (1000 * users * iterations);
112
113 Test testCase = new PerThreadSingletonTest(testMethod);
114
115 Test repeatedTest = new RepeatedTest(testCase, iterations);
116 Test loadTest = new LoadTest(repeatedTest, users);
117 Test timedTest = new TimedTest(loadTest, maxElapsedTime);
118
119 return timedTest;
120 }
121 }
122
123 /*
124 * Redistribution and use of this software and associated documentation
125 * ("Software"), with or without modification, are permitted provided that the
126 * following conditions are met:
127 *
128 * 1. Redistributions of source code must retain copyright statements and
129 * notices. Redistributions must also contain a copy of this document.
130 *
131 * 2. Redistributions in binary form must reproduce the above copyright notice,
132 * this list of conditions and the following disclaimer in the documentation
133 * and/or other materials provided with the distribution.
134 *
135 * 3. The name "DOM4J" must not be used to endorse or promote products derived
136 * from this Software without prior written permission of MetaStuff, Ltd. For
137 * written permission, please contact dom4j-info@metastuff.com.
138 *
139 * 4. Products derived from this Software may not be called "DOM4J" nor may
140 * "DOM4J" appear in their names without prior written permission of MetaStuff,
141 * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
142 *
143 * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
144 *
145 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
146 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
147 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
148 * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
149 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
150 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
151 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
152 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
153 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
154 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
155 * POSSIBILITY OF SUCH DAMAGE.
156 *
157 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
158 */
159