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