Author: allain.lalonde
Date: Fri Jul 17 16:45:27 2009
New Revision: 494
Added:
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/MemoryLeakTests.java
- copied, changed from r483,
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/pswing/PSwingRepaintManagerTest.java
Log:
Added Memory Leak Test for PCanvas, should demonstrate the memory leak
installInputSources has
Copied:
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/MemoryLeakTests.java
(from r483,
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/pswing/PSwingRepaintManagerTest.java)
==============================================================================
---
/piccolo2d.java/trunk/extras/src/test/java/edu/umd/cs/piccolox/pswing/PSwingRepaintManagerTest.java
(original)
+++
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/MemoryLeakTests.java
Fri Jul 17 16:45:27 2009
@@ -1,177 +1,33 @@
-/*
- * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org
- * Copyright (c) 1998-2008, University of Maryland
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
modification, are permitted provided
- * that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright notice,
this list of conditions
- * and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright
notice, this list of conditions
- * and the following disclaimer in the documentation and/or other
materials provided with the
- * distribution.
- *
- * None of the name of the University of Maryland, the name of the
Piccolo2D project, or the names of its
- * contributors may be used to endorse or promote products derived from
this software without specific
- * prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR
- * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-package edu.umd.cs.piccolox.pswing;
+package edu.umd.cs.piccolo;
-import java.awt.Canvas;
-import java.awt.Component;
-
-import javax.swing.JComponent;
import javax.swing.JPanel;
-import javax.swing.RepaintManager;
import junit.framework.TestCase;
+import edu.umd.cs.piccolo.PCanvas;
-/**
- * Unit test for PSwingRepaintManager.
- */
-public class PSwingRepaintManagerTest extends TestCase {
-
- public void testConstructor() {
- PSwingRepaintManager repaintManager = new PSwingRepaintManager();
- assertNotNull(repaintManager);
- }
-
- public void testCurrentManager() {
- RepaintManager currentManager =
RepaintManager.currentManager(null);
- assertNotNull(currentManager);
- // TODO: this assertion is true when running this test case in
isolation
- // but since PSwingCanvas may have been instantiated elsewhere
in the test suite
- // may not be true when running this test case as part of a
test suite
- //assertFalse(currentManager instanceof PSwingRepaintManager);
-
- Component awtComponent = new Canvas();
- currentManager = RepaintManager.currentManager(awtComponent);
- assertNotNull(currentManager);
- //assertFalse(currentManager instanceof PSwingRepaintManager);
-
- JComponent swingComponent = new JPanel();
- currentManager = RepaintManager.currentManager(swingComponent);
- assertNotNull(currentManager);
- //assertFalse(currentManager instanceof PSwingRepaintManager);
-
- PSwingCanvas pswingCanvas = new PSwingCanvas();
- currentManager = RepaintManager.currentManager(pswingCanvas);
- assertNotNull(currentManager);
- assertTrue(currentManager instanceof PSwingRepaintManager);
-
- // once a PSwingCanvas has been instantiated,
- // PSwingRepaintManager replaces RepaintManager everwhere
- currentManager = RepaintManager.currentManager(awtComponent);
- assertTrue(currentManager instanceof PSwingRepaintManager);
-
- currentManager = RepaintManager.currentManager(swingComponent);
- assertTrue(currentManager instanceof PSwingRepaintManager);
-
- currentManager = RepaintManager.currentManager(pswingCanvas);
- assertTrue(currentManager instanceof PSwingRepaintManager);
- }
-
- public void testLockRepaint() {
- PSwingCanvas canvas = new PSwingCanvas();
- RepaintManager currentManager =
RepaintManager.currentManager(canvas);
- assertNotNull(currentManager);
- assertTrue(currentManager instanceof PSwingRepaintManager);
-
- PSwingRepaintManager repaintManager = (PSwingRepaintManager)
currentManager;
- // TODO: should lockRepaint allow null?
- repaintManager.lockRepaint(null);
- repaintManager.lockRepaint(canvas);
- }
-
- public void testUnlockRepaint() {
- PSwingCanvas canvas = new PSwingCanvas();
- RepaintManager currentManager =
RepaintManager.currentManager(canvas);
- assertNotNull(currentManager);
- assertTrue(currentManager instanceof PSwingRepaintManager);
-
- PSwingRepaintManager repaintManager = (PSwingRepaintManager)
currentManager;
- repaintManager.lockRepaint(null);
- repaintManager.lockRepaint(canvas);
-
- repaintManager.unlockRepaint(null);
- repaintManager.unlockRepaint(canvas);
-
- // TODO: catch this array index out of bounds exception?
- JComponent notLocked = new JPanel();
- try {
- repaintManager.unlockRepaint(notLocked);
+public class MemoryLeakTests extends TestCase {
+ private int pCanvasFinalizerCount;
+
+ public void setUp() {
+ pCanvasFinalizerCount = 0;
+ }
+
+ public void testMemoryLeakWithPCanvas() throws InterruptedException {
+ JPanel panel = new JPanel();
+ for (int i=0; i < 10; i++) {
+ PCanvas canvas = new PCanvas() {
+ public void finalize() {
+ pCanvasFinalizerCount ++;
+ }
+ };
+ panel.add(canvas);
+ panel.remove(canvas);
+ canvas = null;
}
- catch (ArrayIndexOutOfBoundsException e) {
- // expected
- }
- }
-
- public void testIsPainting() {
- PSwingCanvas canvas = new PSwingCanvas();
- RepaintManager currentManager =
RepaintManager.currentManager(canvas);
- assertNotNull(currentManager);
- assertTrue(currentManager instanceof PSwingRepaintManager);
-
- PSwingRepaintManager repaintManager = (PSwingRepaintManager)
currentManager;
- repaintManager.lockRepaint(null);
- repaintManager.lockRepaint(canvas);
- JComponent notLocked = new JPanel();
-
- assertTrue(repaintManager.isPainting(null));
- assertTrue(repaintManager.isPainting(canvas));
- assertFalse(repaintManager.isPainting(notLocked));
- }
-
- public void testAddDirtyRegion() {
- PSwingCanvas canvas = new PSwingCanvas();
- RepaintManager currentManager =
RepaintManager.currentManager(canvas);
- assertNotNull(currentManager);
- assertTrue(currentManager instanceof PSwingRepaintManager);
-
- PSwingRepaintManager repaintManager = (PSwingRepaintManager)
currentManager;
- repaintManager.addDirtyRegion(canvas, 0, 0, canvas.getWidth(),
canvas.getHeight());
-
- JComponent child = new JPanel();
- canvas.add(child);
- repaintManager.addDirtyRegion(child, 0, 0, child.getWidth(),
child.getHeight());
-
- // TODO: will need some additional work here for full test
coverage
- }
-
- public void testAddInvalidComponent() {
- PSwingCanvas canvas = new PSwingCanvas();
- RepaintManager currentManager =
RepaintManager.currentManager(canvas);
- assertNotNull(currentManager);
- assertTrue(currentManager instanceof PSwingRepaintManager);
-
- PSwingRepaintManager repaintManager = (PSwingRepaintManager)
currentManager;
- // TODO: should check for null and throw IAE, or keep NPE?
- try {
- repaintManager.addInvalidComponent(null);
- }
- catch (NullPointerException e) {
- // expected
- }
-
- JComponent component = new JPanel();
- JComponent child = new JPanel();
- canvas.add(child);
-
- repaintManager.addInvalidComponent(canvas);
- repaintManager.addInvalidComponent(component);
- repaintManager.addInvalidComponent(child);
-
- // TODO: will need some additional work here for full test
coverage
+ System.gc();
+ System.runFinalization();
+
+ // Not sure why I need -1 here, but I do. If I create 10000 it'll
always be 1 less
+ assertEquals(10-1, pCanvasFinalizerCount);
}
}
--~--~---------~--~----~------------~-------~--~----~
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en
-~----------~----~----~----~------~----~------~--~---