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  package org.apache.commons.functor.core.composite;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  
22  import org.apache.commons.functor.BaseFunctorTest;
23  import org.apache.commons.functor.Procedure;
24  import org.apache.commons.functor.core.Constant;
25  import org.apache.commons.functor.core.Identity;
26  import org.apache.commons.functor.core.NoOp;
27  import org.junit.Test;
28  
29  /**
30   * @version $Revision: 1508677 $ $Date: 2013-07-31 00:48:02 +0200 (Mi, 31 Jul 2013) $
31   */
32  public class TestConditionalProcedure extends BaseFunctorTest {
33  
34      // Functor Testing Framework
35      // ------------------------------------------------------------------------
36  
37      @Override
38      protected Object makeFunctor() {
39          return new ConditionalProcedure<Object>(
40              Constant.TRUE,
41              NoOp.INSTANCE,
42              NoOp.INSTANCE);
43      }
44  
45      // Tests
46      // ------------------------------------------------------------------------
47  
48      @Test
49      public void testRun() throws Exception {
50          RunCounter left = new RunCounter();
51          RunCounter right = new RunCounter();
52          ConditionalProcedure<Object> p = new ConditionalProcedure<Object>(
53              Identity.INSTANCE,
54              left,
55              right);
56          assertEquals(0,left.count);
57          assertEquals(0,right.count);
58          p.run(Boolean.TRUE);
59          assertEquals(1,left.count);
60          assertEquals(0,right.count);
61          p.run(Boolean.FALSE);
62          assertEquals(1,left.count);
63          assertEquals(1,right.count);
64          p.run(Boolean.TRUE);
65          assertEquals(2,left.count);
66          assertEquals(1,right.count);
67      }
68  
69      @Test
70      public void testEquals() throws Exception {
71          ConditionalProcedure<Object> p = new ConditionalProcedure<Object>(
72              Identity.INSTANCE,
73              NoOp.INSTANCE,
74              NoOp.INSTANCE);
75          assertEquals(p,p);
76          assertObjectsAreEqual(p,new ConditionalProcedure<Object>(
77              Identity.INSTANCE,
78              NoOp.INSTANCE,
79              NoOp.INSTANCE));
80          assertObjectsAreNotEqual(p,new ConditionalProcedure<Object>(
81              Constant.TRUE,
82              NoOp.INSTANCE,
83              NoOp.INSTANCE));
84          assertObjectsAreNotEqual(p,new ConditionalProcedure<Object>(
85              Identity.INSTANCE,
86              new RunCounter(),
87              NoOp.INSTANCE));
88          assertObjectsAreNotEqual(p,new ConditionalProcedure<Object>(
89              Identity.INSTANCE,
90              NoOp.INSTANCE,
91              new RunCounter()));
92          assertObjectsAreNotEqual(p, new ConditionalProcedure<Object>(
93              Constant.TRUE,
94              new RunCounter()));
95          assertTrue(!p.equals(null));
96      }
97  
98      // Classes
99      // ------------------------------------------------------------------------
100 
101     static class RunCounter implements Procedure<Object> {
102         public void run(Object obj) {
103             count++;
104         }
105         public int count = 0;
106     }
107 }