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 org.apache.commons.proxy.exception.ObjectProviderException;
21  import org.apache.commons.proxy.util.AbstractTestCase;
22  
23  import java.util.Date;
24  
25  public class TestCloningProvider extends AbstractTestCase
26  {
27  //**********************************************************************************************************************
28  // Other Methods
29  //**********************************************************************************************************************
30  
31      public void testSerialization()
32      {
33          assertSerializable(new CloningProvider(new Date()));
34      }
35  
36      public void testValidCloneable()
37      {
38          final Date now = new Date();
39          final CloningProvider provider = new CloningProvider(now);
40          final Date clone1 = ( Date ) provider.getObject();
41          assertEquals(now, clone1);
42          assertNotSame(now, clone1);
43          final Date clone2 = ( Date ) provider.getObject();
44          assertEquals(now, clone2);
45          assertNotSame(now, clone2);
46          assertNotSame(clone2, clone1);
47      }
48  
49      public void testWithExceptionThrown()
50      {
51          final CloningProvider provider = new CloningProvider(new ExceptionCloneable());
52          try
53          {
54              provider.getObject();
55              fail();
56          }
57          catch( ObjectProviderException e )
58          {
59          }
60      }
61  
62      public void testWithInvalidCloneable()
63      {
64          final CloningProvider provider = new CloningProvider(new InvalidCloneable());
65          try
66          {
67              provider.getObject();
68              fail();
69          }
70          catch( ObjectProviderException e )
71          {
72          }
73      }
74  
75      public void testWithPrivateCloneMethod()
76      {
77          final CloningProvider provider = new CloningProvider(new PrivateCloneable());
78          try
79          {
80              provider.getObject();
81              fail();
82          }
83          catch( ObjectProviderException e )
84          {
85          }
86      }
87  
88  //**********************************************************************************************************************
89  // Inner Classes
90  //**********************************************************************************************************************
91  
92      public static class ExceptionCloneable implements Cloneable
93      {
94          public Object clone()
95          {
96              throw new RuntimeException("No clone for you!");
97          }
98      }
99  
100     public static class InvalidCloneable implements Cloneable
101     {
102     }
103 
104     public static class PrivateCloneable implements Cloneable
105     {
106         protected Object clone()
107         {
108             return this;
109         }
110     }
111 }