File: TestTrace2DLtd.java

package info (click to toggle)
libjchart2d-java 3.2.2%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,800 kB
  • sloc: java: 24,747; xml: 827; makefile: 13
file content (118 lines) | stat: -rw-r--r-- 3,664 bytes parent folder | download | duplicates (3)
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
112
113
114
115
116
117
118
/*
 *  TestTrace2DLtd.java, a Junit test case for Trace2DLtd.
 *  Copyright (C) Achim Westermann, created on 23.04.2005, 08:21:12
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  If you modify or optimize the code in a useful way please let me know.
 *  Achim.Westermann@gmx.de
 *
 */
package info.monitorenter.gui.chart.traces;

import info.monitorenter.gui.chart.Chart2D;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.ITracePoint2D;
import info.monitorenter.gui.chart.TracePoint2D;

import java.util.Map;
import java.util.WeakHashMap;

import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * A Junit test case for <code>{@link Trace2DLtd}</code>.
 * <p>
 * 
 * @author <a href="mailto:Achim.Westermann@gmx.de">Achim Westermann</a>
 * 
 */
public class TestTrace2DLtd extends TestCase {
	/**
	 * Test suite for this test class.
	 * <p>
	 * 
	 * @return the test suite.
	 */
	public static Test suite() {

		TestSuite suite = new TestSuite();
		suite.setName(TestTrace2DLtd.class.getName());

		suite.addTest(new TestTrace2DLtd("testMemoryLeak"));

		return suite;
	}

	/**
	 * Creates a test case with the given name.
	 * <p>
	 * 
	 * @param testName
	 *            the name of the test case.
	 */
	public TestTrace2DLtd(final String testName) {
		super(testName);
	}

	/**
	 * Adds 1000000 <code>{@link TracePoint2D}</code> instances to a
	 * <code>{@link Trace2DLtd}</code> and asserts that not more points than
	 * <code>{@link Trace2DLtd#getMaxSize()} </code> are remaining in memory.
	 * <p>
	 * 
	 */
	public void testMemoryLeak() {
		Chart2D dummyChart = new Chart2D();
		int traceSize = 10;
		ITrace2D trace = new Trace2DLtd(traceSize);
		dummyChart.addTrace(trace);
		long max = 1000000;
		long percentModulo = max / 20;
		System.out.println("Adding " + max
				+ " points to a Trace2DLtd and a WeakHashMap...");
		ITracePoint2D point;
		Map<ITracePoint2D, String> weakMap = new WeakHashMap<ITracePoint2D, String>();
		for (long i = 0; i < max; i++) {
			point = new TracePoint2D(i, i);
			trace.addPoint(point);
			weakMap.put(point, point.toString());
			if (i % percentModulo == 0) {
				System.out.println((i * 100 / max) + " %");
			}
		}
		System.out.println("Dropping point reference (but holding trace)...");
		point = null;
		// trace=null;
		long keys = weakMap.size();
		System.out.println("Points remaining in the weakMap: " + keys);
		System.out.println("System.runFinalization()... ");
		System.runFinalization();
		System.out.println("System.gc()... ");
		System.gc();
		keys = 0;
		for (ITracePoint2D pt : weakMap.keySet()) {
			keys++;
			System.out.println("Point " + pt.toString() + " was not dropped.");
		}
		System.out.println("Points remaining in the weakMap: " + keys);
		Assert.assertFalse("There are " + keys
				+ " TracePoint2D instances not deleted from the WeakHashMap.",
				keys > traceSize);
	}
}