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
018package org.apache.commons.proxy2.provider;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertNotSame;
023import static org.junit.Assert.fail;
024
025import java.util.Date;
026
027import org.apache.commons.proxy2.exception.ObjectProviderException;
028import org.apache.commons.proxy2.util.AbstractTestCase;
029import org.junit.Test;
030
031public class CloningProviderTest extends AbstractTestCase
032{
033    //**********************************************************************************************************************
034    // Other Methods
035    //**********************************************************************************************************************
036
037    @Test
038    public void testSerialization()
039    {
040        assertSerializable(new CloningProvider<Date>(new Date()));
041    }
042
043    @Test
044    public void testValidCloneable()
045    {
046        final Date now = new Date();
047        final CloningProvider<Date> provider = new CloningProvider<Date>(now);
048        final Date clone1 = provider.getObject();
049        assertEquals(now, clone1);
050        assertNotSame(now, clone1);
051        final Date clone2 = provider.getObject();
052        assertEquals(now, clone2);
053        assertNotSame(now, clone2);
054        assertNotSame(clone2, clone1);
055    }
056
057    @Test
058    public void testWithExceptionThrown()
059    {
060        final CloningProvider<ExceptionCloneable> provider = new CloningProvider<ExceptionCloneable>(
061                new ExceptionCloneable());
062        try
063        {
064            provider.getObject();
065            fail();
066        }
067        catch (ObjectProviderException e)
068        {
069        }
070    }
071
072    @Test(expected = IllegalArgumentException.class)
073    public void testWithInvalidCloneable()
074    {
075        assertNotNull(new CloningProvider<InvalidCloneable>(new InvalidCloneable())); // assert is used to avoid not used warning
076    }
077
078    @Test(expected = IllegalArgumentException.class)
079    public void testWithProtectedCloneMethod()
080    {
081        final CloningProvider<ProtectedCloneable> provider = new CloningProvider<ProtectedCloneable>(
082                new ProtectedCloneable());
083        provider.getObject();
084    }
085
086    //**********************************************************************************************************************
087    // Inner Classes
088    //**********************************************************************************************************************
089
090    public static class ExceptionCloneable implements Cloneable
091    {
092        @Override
093        public Object clone()
094        {
095            throw new RuntimeException("No clone for you!");
096        }
097    }
098
099    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}