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 java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.functor.BaseFunctorTest;
25  import org.apache.commons.functor.UnaryProcedure;
26  import org.apache.commons.functor.core.NoOp;
27  import org.junit.Test;
28  
29  /**
30   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
31   * @author Rodney Waldhoff
32   */
33  @SuppressWarnings("unchecked")
34  public class TestUnarySequence extends BaseFunctorTest {
35  
36      // Functor Testing Framework
37      // ------------------------------------------------------------------------
38  
39      protected Object makeFunctor() {
40          return new UnarySequence(new NoOp(),new NoOp());
41      }
42  
43      // Tests
44      // ------------------------------------------------------------------------
45  
46      @Test
47      public void testRunZero() throws Exception {
48          UnarySequence seq = new UnarySequence();
49          seq.run(null);
50          seq.run("xyzzy");
51      }
52  
53      @Test
54      public void testRunOne() throws Exception {
55          RunCounter counter = new RunCounter();
56          UnarySequence seq = new UnarySequence(counter);
57          assertEquals(0,counter.count);
58          seq.run(null);
59          assertEquals(1,counter.count);
60          seq.run("xyzzy");
61          assertEquals(2,counter.count);
62      }
63  
64      @Test
65      public void testRunTwo() throws Exception {
66          RunCounter[] counter = { new RunCounter(), new RunCounter() };
67          UnarySequence seq = new UnarySequence(counter[0],counter[1]);
68          assertEquals(0,counter[0].count);
69          assertEquals(0,counter[1].count);
70          seq.run(null);
71          assertEquals(1,counter[0].count);
72          assertEquals(1,counter[1].count);
73          seq.run("xyzzy");
74          assertEquals(2,counter[0].count);
75          assertEquals(2,counter[1].count);
76      }
77  
78      @Test
79      public void testThen() throws Exception {
80          List list = new ArrayList();
81          UnarySequence seq = new UnarySequence();
82          seq.run(null);
83          for (int i=0;i<10;i++) {
84              RunCounter counter = new RunCounter();
85              seq.then(counter);
86              list.add(counter);
87              seq.run("xyzzy");
88              for (int j=0;j<list.size();j++) {
89                  assertEquals(list.size()-j,(((RunCounter)(list.get(j))).count));
90              }
91          }
92      }
93  
94      @Test
95      public void testEquals() throws Exception {
96          UnarySequence p = new UnarySequence();
97          assertEquals(p,p);
98          UnarySequence q = new UnarySequence();
99          assertObjectsAreEqual(p,q);
100 
101         for (int i=0;i<3;i++) {
102             p.then(new NoOp());
103             assertObjectsAreNotEqual(p,q);
104             q.then(new NoOp());
105             assertObjectsAreEqual(p,q);
106             p.then(new UnarySequence(new NoOp(),new NoOp()));
107             assertObjectsAreNotEqual(p,q);
108             q.then(new UnarySequence(new NoOp(),new NoOp()));
109             assertObjectsAreEqual(p,q);
110         }
111 
112         assertObjectsAreNotEqual(p,new NoOp());
113     }
114 
115     // Classes
116     // ------------------------------------------------------------------------
117 
118     static class RunCounter implements UnaryProcedure {
119         public void run(Object that) {
120             count++;
121         }
122         public int count = 0;
123     }
124 }