1   /*
2    * Copyright 2002-2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.binding.value.support;
17  
18  import org.springframework.binding.value.CommitTrigger;
19  import org.springframework.binding.value.CommitTriggerListener;
20  
21  import junit.framework.TestCase;
22  
23  /**
24   * Tests class {@link CommitTrigger}.
25   * 
26   * @author Oliver Hutchison
27   */
28  public class CommitTriggerTests extends TestCase {
29  
30      public void testCommitTrigger() {
31          CommitTrigger ct = new CommitTrigger();
32          ct.commit();
33          ct.revert();
34  
35          TestCommitTriggerListener l = new TestCommitTriggerListener();
36          ct.addCommitTriggerListener(l);
37          assertEquals(0, l.commits);
38          assertEquals(0, l.reverts);
39  
40          ct.commit();
41          assertEquals(1, l.commits);
42          ct.commit();
43          assertEquals(2, l.commits);
44          assertEquals(0, l.reverts);
45          ct.revert();
46          assertEquals(2, l.commits);
47          assertEquals(1, l.reverts);
48          
49          ct.removeCommitTriggerListener(l);
50          
51          ct.commit();
52          assertEquals(2, l.commits);
53          ct.revert();
54          assertEquals(1, l.reverts);        
55      }
56  
57      private class TestCommitTriggerListener implements CommitTriggerListener {
58  
59          public int commits;
60  
61          int reverts;
62  
63          public void commit() {
64              commits++;
65          }
66  
67          public void revert() {
68              reverts++;
69          }
70      }
71  }