1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.proxy2.provider;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNotSame;
23 import static org.junit.Assert.fail;
24
25 import java.util.Date;
26
27 import org.apache.commons.proxy2.exception.ObjectProviderException;
28 import org.apache.commons.proxy2.util.AbstractTestCase;
29 import org.junit.Test;
30
31 public class CloningProviderTest extends AbstractTestCase
32 {
33
34
35
36
37 @Test
38 public void testSerialization()
39 {
40 assertSerializable(new CloningProvider<Date>(new Date()));
41 }
42
43 @Test
44 public void testValidCloneable()
45 {
46 final Date now = new Date();
47 final CloningProvider<Date> provider = new CloningProvider<Date>(now);
48 final Date clone1 = provider.getObject();
49 assertEquals(now, clone1);
50 assertNotSame(now, clone1);
51 final Date clone2 = provider.getObject();
52 assertEquals(now, clone2);
53 assertNotSame(now, clone2);
54 assertNotSame(clone2, clone1);
55 }
56
57 @Test
58 public void testWithExceptionThrown()
59 {
60 final CloningProvider<ExceptionCloneable> provider = new CloningProvider<ExceptionCloneable>(
61 new ExceptionCloneable());
62 try
63 {
64 provider.getObject();
65 fail();
66 }
67 catch (ObjectProviderException e)
68 {
69 }
70 }
71
72 @Test(expected = IllegalArgumentException.class)
73 public void testWithInvalidCloneable()
74 {
75 assertNotNull(new CloningProvider<InvalidCloneable>(new InvalidCloneable()));
76 }
77
78 @Test(expected = IllegalArgumentException.class)
79 public void testWithProtectedCloneMethod()
80 {
81 final CloningProvider<ProtectedCloneable> provider = new CloningProvider<ProtectedCloneable>(
82 new ProtectedCloneable());
83 provider.getObject();
84 }
85
86
87
88
89
90 public static class ExceptionCloneable implements Cloneable
91 {
92 @Override
93 public Object clone()
94 {
95 throw new RuntimeException("No clone for you!");
96 }
97 }
98
99 public static class InvalidCloneable implements Cloneable
100 {
101 }
102
103 public static class ProtectedCloneable implements Cloneable
104 {
105 @Override
106 protected Object clone()
107 {
108 return this;
109 }
110 }
111 }