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