View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.tests.util.observer;
18  
19  import static org.junit.Assert.assertEquals;
20  
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import ch.elca.el4j.util.observer.ValueObserver;
25  import ch.elca.el4j.util.observer.impl.Computable;
26  import ch.elca.el4j.util.observer.impl.LiveValue;
27  import ch.elca.el4j.util.observer.impl.SettableObservableValue;
28  
29  /**
30   * An observer test.
31   *
32   * @svnLink $Revision: 4010 $;$Date: 2009-12-01 10:59:54 +0100 (Di, 01. Dez 2009) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/util/observer/ObserverTest.java $
33   *
34   * @author Adrian Moos (AMS)
35   */
36  public class ObserverTest implements ValueObserver<Boolean> {
37  	/** the inputs. */
38  	SettableObservableValue<Boolean> m_a, m_b, m_c;
39  	
40  	/** the intermediary nodes/outputs. */
41  	LiveValue<Boolean>  m_e, m_f, m_g;
42  	
43  	/** the computable for {@code g}. */
44  	Computable<Boolean> m_comp;
45  	
46  	/** counts the number of change notifications received. */
47  	int m_notified;
48  
49  	/**
50  	 * {@inheritDoc}
51  	 */
52  	@Before
53  	public void setUp() {
54  		m_a = new SettableObservableValue<Boolean>(false);
55  		m_b = new SettableObservableValue<Boolean>(false);
56  		m_c = new SettableObservableValue<Boolean>(false);
57  //		m_d = new LiveValue<Boolean>() {
58  //			@Override
59  //			protected Boolean is() {
60  //				return m_a.get() != m_b.get();
61  //			}
62  //		};
63  		m_e = new LiveValue<Boolean>() {
64  			@Override
65  			protected Boolean is() {
66  				return m_b.get() != m_c.get();
67  			}
68  		};
69  		m_f = new LiveValue<Boolean>() {
70  			@Override
71  			protected Boolean is() {
72  				return m_a.get() ? m_g.get() : m_b.get();
73  			}
74  		};
75  		m_comp = new Computable<Boolean>() {
76  			public Boolean is() {
77  				return (m_c.get() ? m_f.get() : m_e.get());
78  			}
79  		};
80  		m_g = new LiveValue<Boolean>(m_comp);
81  		m_notified = 0;
82  	}
83  	
84  	/** tests subscription. */
85  	@Test
86  	public void testSubscribe() {
87  		assertEquals(0, m_notified);
88  		m_g.subscribe(this);
89  		assertEquals(1, m_notified);
90  	}
91  	
92  	/** tests silent subscription. */
93  	@Test
94  	public void testSilentSubscribe() {
95  		m_g.subscribeSilently(this);
96  		assertEquals(m_notified, 0);
97  	}
98  
99  	/** tests propagation of changes. */
100 	@Test
101 	public void testInputChange() {
102 		m_g.subscribe(this);
103 		check();
104 		m_a.set(true);
105 		check();
106 		m_b.set(true);
107 		check();
108 		m_a.set(false);
109 		check();
110 		m_b.set(false);
111 		check();
112 		m_c.set(true);
113 		check();
114 	}
115 	
116 	/** asserts that the state is consistent. */
117 	private void check() {
118 		check(m_g.get());
119 	}
120 	
121 	/**
122 	 * asserts that the state is consistent.
123 	 * @param newRef {@code g}'s current value
124 	 */
125 	private void check(Boolean newRef) {
126 		assertEquals(m_notified % 2 == 0, (boolean) newRef);
127 		assertEquals(m_comp.is(), newRef);
128 	}
129 	
130 	/**
131 	 * {@inheritDoc}
132 	 */
133 	public void changed(Boolean newRef) {
134 		m_notified++;
135 		check(newRef);
136 	}
137 }