From 504accfe8b1392678a2efbb3232c257c6419feea Mon Sep 17 00:00:00 2001 From: Michael Starzinger Date: Tue, 30 Dec 2008 03:12:18 +0100 Subject: [PATCH] * tests/regression/base/TestCloning.java: Added testcase for object cloning. * tests/regression/base/All.java: Added above test to our base suite. --- tests/regression/base/All.java | 1 + tests/regression/base/TestCloning.java | 101 +++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 tests/regression/base/TestCloning.java diff --git a/tests/regression/base/All.java b/tests/regression/base/All.java index 05085c32a..4f421c763 100644 --- a/tests/regression/base/All.java +++ b/tests/regression/base/All.java @@ -30,6 +30,7 @@ import org.junit.runners.Suite; @Suite.SuiteClasses({ TestArrayClasses.class, +TestCloning.class, TestExceptionInStaticClassInitializer.class, TestPatcher.class }) diff --git a/tests/regression/base/TestCloning.java b/tests/regression/base/TestCloning.java new file mode 100644 index 000000000..35dad40fe --- /dev/null +++ b/tests/regression/base/TestCloning.java @@ -0,0 +1,101 @@ +/* tests/regression/base/TestCloning.java + + Copyright (C) 2008 + CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO + + This file is part of CACAO. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. + + This program 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. + +*/ + + +import org.junit.Test; +import static org.junit.Assert.*; + +public class TestCloning +{ + private static class FooUncloneable { + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + } + + private static class FooCloneable implements Cloneable { + public String s; + public FooCloneable(String s) { + this.s = s; + } + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + } + + private static class FooOverridden { + public Object clone() { + return this; + } + } + + @Test + public void testObject() throws CloneNotSupportedException { + // Test cloning of cloneable object. + FooCloneable o1 = new FooCloneable("Simple Test"); + Object oc1 = o1.clone(); + assertNotNull(oc1); + assertNotSame(o1, oc1); + assertEquals(o1.getClass(), oc1.getClass()); + assertSame(o1.s, ((FooCloneable) oc1).s); + + // Test cloning of uncloneable object. + try { + FooUncloneable o2 = new FooUncloneable(); + Object oc2 = o2.clone(); + fail("Exception expected"); + } catch (CloneNotSupportedException e) { + } + + // Test cloning of object with overridden clone method. + FooOverridden o3 = new FooOverridden(); + Object oc3 = o3.clone(); + assertNotNull(oc3); + assertSame(o3, oc3); + } + + @Test + public void testArray() { + // Test cloning of integer array. + int[] a = { 1, 23, 42 }; + int[] ac = a.clone(); + assertNotNull(ac); + assertNotSame(a, ac); + assertEquals(a.getClass(), ac.getClass()); + assertEquals(a.length, ac.length); + assertArrayEquals(a, ac); + } + + @Test + public void testLocked() throws CloneNotSupportedException { + // Test cloning of locked object. + FooCloneable o = new FooCloneable("Locked Test"); + synchronized(o) { + Object oc = o.clone(); + assertNotNull(oc); + assertTrue(Thread.holdsLock(o)); + assertFalse(Thread.holdsLock(oc)); + } + } +} -- 2.25.1