001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.proxy.provider;
019    
020    import junit.framework.TestCase;
021    import org.apache.commons.proxy.exception.ObjectProviderException;
022    
023    import java.util.Date;
024    
025    public class TestCloningProvider extends TestCase
026    {
027        public void testValidCloneable()
028        {
029            final Date now = new Date();
030            final CloningProvider provider = new CloningProvider( now );
031            final Date clone1 = ( Date ) provider.getObject();
032            assertEquals( now, clone1 );
033            assertNotSame( now, clone1 );
034            final Date clone2 = ( Date )provider.getObject();
035            assertEquals( now, clone2 );
036            assertNotSame( now, clone2 );
037            assertNotSame( clone2, clone1 );
038        }
039    
040        public void testWithPrivateCloneMethod()
041        {
042            final CloningProvider provider = new CloningProvider( new PrivateCloneable() );
043            try
044            {
045                provider.getObject();
046                fail();
047            }
048            catch( ObjectProviderException e )
049            {
050            }
051        }
052        
053        public void testWithInvalidCloneable()
054        {
055            final CloningProvider provider = new CloningProvider( new InvalidCloneable() );
056            try
057            {
058                provider.getObject();
059                fail();
060            }
061            catch( ObjectProviderException e )
062            {
063            }
064        }
065    
066        public void testWithExceptionThrown()
067        {
068            final CloningProvider provider = new CloningProvider( new ExceptionCloneable() );
069            try
070            {
071                provider.getObject();
072                fail();
073            }
074            catch( ObjectProviderException e )
075            {
076            }
077        }
078    
079        public static class InvalidCloneable implements Cloneable
080        {
081        }
082    
083        public static class PrivateCloneable implements Cloneable
084        {
085            protected Object clone()
086            {
087                return this;
088            }
089        }
090        
091        public static class ExceptionCloneable implements Cloneable
092        {
093            public Object clone()
094            {
095                throw new RuntimeException( "No clone for you!" );
096            }
097        }
098    }