1 /* tests/regression/base/TestCloning.java
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 import org.junit.Test;
27 import static org.junit.Assert.*;
29 public class TestCloning
31 private static class FooUncloneable {
32 public Object clone() throws CloneNotSupportedException {
37 private static class FooCloneable implements Cloneable {
39 public FooCloneable(String s) {
42 public Object clone() throws CloneNotSupportedException {
47 private static class FooOverridden {
48 public Object clone() {
54 public void testObject() throws CloneNotSupportedException {
55 // Test cloning of cloneable object.
56 FooCloneable o1 = new FooCloneable("Simple Test");
57 Object oc1 = o1.clone();
59 assertNotSame(o1, oc1);
60 assertEquals(o1.getClass(), oc1.getClass());
61 assertSame(o1.s, ((FooCloneable) oc1).s);
63 // Test cloning of uncloneable object.
65 FooUncloneable o2 = new FooUncloneable();
66 Object oc2 = o2.clone();
67 fail("Exception expected");
68 } catch (CloneNotSupportedException e) {
71 // Test cloning of object with overridden clone method.
72 FooOverridden o3 = new FooOverridden();
73 Object oc3 = o3.clone();
79 public void testArray() {
80 // Test cloning of integer array.
81 int[] a = { 1, 23, 42 };
85 assertEquals(a.getClass(), ac.getClass());
86 assertEquals(a.length, ac.length);
87 assertArrayEquals(a, ac);
91 public void testLocked() throws CloneNotSupportedException {
92 // Test cloning of locked object.
93 FooCloneable o = new FooCloneable("Locked Test");
95 Object oc = o.clone();
97 assertTrue(Thread.holdsLock(o));
98 assertFalse(Thread.holdsLock(oc));