View Javadoc
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.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      // Other Methods
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())); // assert is used to avoid not used warning
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      // Inner Classes
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 }