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.adapter;
018
019import static org.junit.Assert.assertEquals;
020import static org.junit.Assert.assertNotNull;
021import static org.junit.Assert.assertNull;
022import static org.junit.Assert.assertTrue;
023
024import org.apache.commons.functor.BaseFunctorTest;
025import org.apache.commons.functor.Function;
026import org.apache.commons.functor.Procedure;
027import org.apache.commons.functor.core.Constant;
028import org.apache.commons.functor.core.NoOp;
029import org.junit.Test;
030
031/**
032 * @version $Revision: 1508677 $ $Date: 2013-07-31 00:48:02 +0200 (Mi, 31 Jul 2013) $
033 */
034public class TestFunctionProcedure extends BaseFunctorTest {
035
036    // Functor Testing Framework
037    // ------------------------------------------------------------------------
038
039    @Override
040    protected Object makeFunctor() {
041        return new FunctionProcedure<Object>(Constant.of("K"));
042    }
043
044    // Tests
045    // ------------------------------------------------------------------------
046
047    @Test
048    public void testRun() throws Exception {
049        class EvaluateCounter implements Function<Object, Integer> {
050            int count = 0;
051            public Integer evaluate(Object a) { return Integer.valueOf(count++); }
052        }
053        EvaluateCounter counter = new EvaluateCounter();
054        Procedure<Object> p = new FunctionProcedure<Object>(counter);
055        assertEquals(0,counter.count);
056        p.run(null);
057        assertEquals(1,counter.count);
058        p.run("x");
059        assertEquals(2,counter.count);
060    }
061
062    @Test
063    public void testEquals() throws Exception {
064        Procedure<Object> p = new FunctionProcedure<Object>(Constant.of("K"));
065        assertEquals(p,p);
066        assertObjectsAreEqual(p,new FunctionProcedure<Object>(Constant.of("K")));
067        assertObjectsAreNotEqual(p,NoOp.INSTANCE);
068        assertObjectsAreNotEqual(p,new FunctionProcedure<Object>(Constant.of("J")));
069        assertTrue(!p.equals(null));
070    }
071
072    @Test
073    public void testAdaptNull() throws Exception {
074        assertNull(FunctionProcedure.adapt(null));
075    }
076
077    @Test
078    public void testAdapt() throws Exception {
079        assertNotNull(FunctionProcedure.adapt(Constant.of("K")));
080    }
081}