* tests/regression/base/TestCloning.java: Added testcase for object cloning.
[cacao.git] / tests / regression / base / TestCloning.java
1 /* tests/regression/base/TestCloning.java
2
3    Copyright (C) 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
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.
12
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.
17
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
21    02110-1301, USA.
22
23 */
24
25
26 import org.junit.Test;
27 import static org.junit.Assert.*;
28
29 public class TestCloning
30 {
31         private static class FooUncloneable {
32                 public Object clone() throws CloneNotSupportedException {
33                         return super.clone();
34                 }
35         }
36
37         private static class FooCloneable implements Cloneable {
38                 public String s;
39                 public FooCloneable(String s) {
40                         this.s = s;
41                 }
42                 public Object clone() throws CloneNotSupportedException {
43                         return super.clone();
44                 }
45         }
46
47         private static class FooOverridden {
48                 public Object clone() {
49                         return this;
50                 }
51         }
52
53         @Test
54         public void testObject() throws CloneNotSupportedException {
55                 // Test cloning of cloneable object.
56                 FooCloneable o1 = new FooCloneable("Simple Test");
57                 Object oc1 = o1.clone();
58                 assertNotNull(oc1);
59                 assertNotSame(o1, oc1);
60                 assertEquals(o1.getClass(), oc1.getClass());
61                 assertSame(o1.s, ((FooCloneable) oc1).s);
62
63                 // Test cloning of uncloneable object.
64                 try {
65                         FooUncloneable o2 = new FooUncloneable();
66                         Object oc2 = o2.clone();
67                         fail("Exception expected");
68                 } catch (CloneNotSupportedException e) {
69                 }
70
71                 // Test cloning of object with overridden clone method.
72                 FooOverridden o3 = new FooOverridden();
73                 Object oc3 = o3.clone();
74                 assertNotNull(oc3);
75                 assertSame(o3, oc3);
76         }
77
78         @Test
79         public void testArray() {
80                 // Test cloning of integer array.
81                 int[] a = { 1, 23, 42 };
82                 int[] ac = a.clone();
83                 assertNotNull(ac);
84                 assertNotSame(a, ac);
85                 assertEquals(a.getClass(), ac.getClass());
86                 assertEquals(a.length, ac.length);
87                 assertArrayEquals(a, ac);
88         }
89
90         @Test
91         public void testLocked() throws CloneNotSupportedException {
92                 // Test cloning of locked object.
93                 FooCloneable o = new FooCloneable("Locked Test");
94                 synchronized(o) {
95                         Object oc = o.clone();
96                         assertNotNull(oc);
97                         assertTrue(Thread.holdsLock(o));
98                         assertFalse(Thread.holdsLock(oc));
99                 }
100         }
101 }