1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  }