Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ffeaebd17b9480bd395cd4ffc39040b208100049 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/

package org.eclipse.osgi.tests.util;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.osgi.framework.util.ObjectPool;
import org.osgi.framework.Version;

public class ObjectPoolTestCase extends CoreTest {
	public void testObjectPool01() {
		// Tests ObjectPool with strings only
		List objects = new ArrayList();
		int num = 2000;
		// new objects are added to the object pool; interning should add the object to the pool and return the same object
		for (int i = 0; i < num; i++) {
			String test1 = getName() + "_" + i; //$NON-NLS-1$
			String test2 = (String) ObjectPool.intern(test1);
			assertTrue("Strings are not the same: " + test1, test1 == test2); //$NON-NLS-1$
			objects.add(test2);
		}
		doGC();
		// after doing a GC the interned objects should still be in the pool; interning a duplicate should return the objects that were added above
		for (int i = 0; i < num; i++) {
			String test1 = getName() + "_" + i; //$NON-NLS-1$
			String test2 = (String) ObjectPool.intern(test1);
			assertFalse("Strings are the same: " + test1, test1 == test2); //$NON-NLS-1$
			assertTrue("Strings are not the same: " + test1, test2 == objects.get(i)); //$NON-NLS-1$
		}
		// clear the hard references to the interned objects
		objects.clear();
		doGC();
		// after doing a GC the interned objects should have been removed from the object pool
		for (int i = 0; i < num; i++) {
			String test1 = getName() + "_" + i; //$NON-NLS-1$
			String test2 = (String) ObjectPool.intern(test1);
			assertTrue("Strings are not the same: " + test1, test1 == test2); //$NON-NLS-1$
			objects.add(test2);
		}
		// flush out the objects again.
		objects.clear();
		doGC();
	}

	public void testObjectPool02() {
		// Test both strings and versions
		List strings = new ArrayList();
		List versions = new ArrayList();
		int num = 2000;
		// new objects are added to the object pool; interning should add the object to the pool and return the same object
		for (int i = 0; i < num; i++) {
			String testString1 = getName() + "_" + i; //$NON-NLS-1$
			String testString2 = (String) ObjectPool.intern(testString1);
			assertTrue("Strings are not the same: " + testString1, testString1 == testString2); //$NON-NLS-1$
			strings.add(testString2);
			Version testVersion1 = new Version(i, i, i, getName() + "_" + i); //$NON-NLS-1$
			Version testVersion2 = (Version) ObjectPool.intern(testVersion1);
			assertTrue("Versions are not the same: " + testVersion1, testVersion1 == testVersion2); //$NON-NLS-1$
			versions.add(testVersion2);
		}
		doGC();
		// after doing a GC the interned objects should still be in the pool; interning a duplicate should return the objects that were added above
		for (int i = 0; i < num; i++) {
			String testString1 = getName() + "_" + i; //$NON-NLS-1$
			String testString2 = (String) ObjectPool.intern(testString1);
			assertFalse("Strings are the same: " + testString1, testString1 == testString2); //$NON-NLS-1$
			assertTrue("Strings are not the same: " + testString1, testString2 == strings.get(i)); //$NON-NLS-1$
			Version testVersion1 = new Version(i, i, i, getName() + "_" + i); //$NON-NLS-1$
			Version testVersion2 = (Version) ObjectPool.intern(testVersion1);
			assertFalse("Versions are the same: " + testVersion1, testVersion1 == testVersion2); //$NON-NLS-1$
			assertTrue("Versions are not the same: " + testVersion1, testVersion2 == versions.get(i)); //$NON-NLS-1$
		}
		// clear the hard references to the interned objects
		strings.clear();
		versions.clear();
		// after doing a GC the interned objects should have been removed from the object pool
		doGC();
		for (int i = 0; i < num; i++) {
			String testString1 = getName() + "_" + i; //$NON-NLS-1$
			String testString2 = (String) ObjectPool.intern(testString1);
			assertTrue("Strings are not the same: " + testString1, testString1 == testString2); //$NON-NLS-1$
			strings.add(testString2);
			Version testVersion1 = new Version(i, i, i, getName() + "_" + i); //$NON-NLS-1$
			Version testVersion2 = (Version) ObjectPool.intern(testVersion1);
			assertTrue("Versions are not the same: " + testVersion1, testVersion1 == testVersion2); //$NON-NLS-1$
			versions.add(testVersion2);
		}
		// flush out the objects again.
		strings.clear();
		versions.clear();
		doGC();
	}

	private static void doGC() {
		// We go through great effort to force the VM to throw our weakly referenced objects away.
		System.gc();
		System.runFinalization();
		System.gc();
		System.runFinalization();
	}
}

Back to the top