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.Procedure;
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 TestSequence extends BaseFunctorTest {
35  
36      // Functor Testing Framework
37      // ------------------------------------------------------------------------
38  
39      protected Object makeFunctor() {
40          return new Sequence(new NoOp(),new NoOp());
41      }
42  
43      // Tests
44      // ------------------------------------------------------------------------
45  
46      @Test
47      public void testRunZero() throws Exception {
48          Sequence seq = new Sequence();
49          seq.run();
50      }
51  
52      @Test
53      public void testRunOne() throws Exception {
54          RunCounter counter = new RunCounter();
55          Sequence seq = new Sequence(counter);
56          assertEquals(0,counter.count);
57          seq.run();
58          assertEquals(1,counter.count);
59      }
60  
61      @Test
62      public void testRunTwo() throws Exception {
63          RunCounter[] counter = { new RunCounter(), new RunCounter() };
64          Sequence seq = new Sequence(counter[0],counter[1]);
65          assertEquals(0,counter[0].count);
66          assertEquals(0,counter[1].count);
67          seq.run();
68          assertEquals(1,counter[0].count);
69          assertEquals(1,counter[1].count);
70      }
71  
72      @Test
73      public void testThen() throws Exception {
74          List list = new ArrayList();
75          Sequence seq = new Sequence();
76          seq.run();
77          for (int i=0;i<10;i++) {
78              RunCounter counter = new RunCounter();
79              seq.then(counter);
80              list.add(counter);
81              seq.run();
82              for (int j=0;j<list.size();j++) {
83                  assertEquals(list.size()-j,(((RunCounter)(list.get(j))).count));
84              }
85          }
86      }
87  
88      @Test
89      public void testEquals() throws Exception {
90          Sequence p = new Sequence();
91          assertEquals(p,p);
92          Sequence q = new Sequence();
93          assertObjectsAreEqual(p,q);
94  
95          for (int i=0;i<3;i++) {
96              p.then(new NoOp());
97              assertObjectsAreNotEqual(p,q);
98              q.then(new NoOp());
99              assertObjectsAreEqual(p,q);
100             p.then(new Sequence(new NoOp(),new NoOp()));
101             assertObjectsAreNotEqual(p,q);
102             q.then(new Sequence(new NoOp(),new NoOp()));
103             assertObjectsAreEqual(p,q);
104         }
105 
106         assertObjectsAreNotEqual(p,new NoOp());
107     }
108 
109     // Classes
110     // ------------------------------------------------------------------------
111 
112     static class RunCounter implements Procedure {
113         public void run() {
114             count++;
115         }
116         public int count = 0;
117     }
118 }