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 */
017package org.apache.commons.functor;
018
019import static org.junit.Assert.assertEquals;
020import static org.junit.Assert.assertNotNull;
021import static org.junit.Assert.assertTrue;
022
023import org.junit.Test;
024
025/**
026 * @version $Revision: 1541658 $ $Date: 2013-11-13 19:54:05 +0100 (Mi, 13 Nov 2013) $
027 */
028public abstract class BaseFunctorTest {
029
030    // Framework
031    // ------------------------------------------------------------------------
032
033    protected abstract Object makeFunctor() throws Exception;
034
035    // Tests
036    // ------------------------------------------------------------------------
037
038    @Test
039    public final void testObjectEquals() throws Exception {
040        Object obj = makeFunctor();
041        assertEquals("equals must be reflexive", obj, obj);
042        assertEquals("hashCode must be reflexive", obj.hashCode(), obj.hashCode());
043        assertTrue(!obj.equals(null)); // should be able to compare to null
044
045        Object obj2 = makeFunctor();
046        if (obj.equals(obj2)) {
047            assertEquals("equals implies hash equals", obj.hashCode(), obj2.hashCode());
048            assertEquals("equals must be symmetric", obj2, obj);
049        } else {
050            assertTrue("equals must be symmetric", !obj2.equals(obj));
051        }
052
053        assertTrue("a functor is not equal to an integer", !obj.equals(Integer.valueOf(1)));
054    }
055
056    @Test
057    public void testToStringIsOverridden() throws Exception {
058        Object obj = makeFunctor();
059        assertNotNull("toString should never return null", obj.toString());
060        assertTrue(obj.getClass().getName() + " should override toString(), found \"" + obj.toString() + "\"", !obj
061            .toString().equals(objectToString(obj)));
062    }
063
064    // protected utils
065    // ------------------------------------------------------------------------
066
067    public static void assertObjectsAreEqual(Object a, Object b) {
068        assertEquals(a, b);
069        assertEquals(b, a);
070        assertEquals(a.hashCode(), b.hashCode());
071        assertEquals(a.toString(), b.toString()); // not strictly required
072    }
073
074    public static void assertObjectsAreNotEqual(Object a, Object b) {
075        assertTrue(!a.equals(b));
076        assertTrue(!b.equals(a));
077        assertTrue(a.hashCode() != b.hashCode()); // not strictly required
078        assertTrue(!a.toString().equals(b.toString())); // not strictly required
079    }
080
081    // private utils
082    // ------------------------------------------------------------------------
083    private String objectToString(Object obj) {
084        return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
085    }
086}