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  import static org.junit.Assert.fail;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import ch.elca.el4j.util.observer.ObservableValue;
26  import ch.elca.el4j.util.observer.ValueObserver;
27  import ch.elca.el4j.util.observer.impl.Computable;
28  import ch.elca.el4j.util.observer.impl.LiveValue;
29  import ch.elca.el4j.util.observer.impl.SettableObservableValue;
30  
31  /**
32   * Another test case for the observer package.
33   *
34   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$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/AnotherObserverTest.java $
35   *
36   * @author Adrian Moos (AMS)
37   */
38  public class AnotherObserverTest implements ValueObserver<Integer> {
39  	
40  	/** the size of this test. Must be in 1..32 */
41  	static final int SIZE = 30;
42  	
43  	
44  	/** the first value. */
45  	SettableObservableValue<Integer> m_origin;
46  	
47  	/** the list of all values. */
48  	ObservableValue<Integer>[] m_values;
49  	
50  	/** the last value. */
51  	ObservableValue<Integer> m_last;
52  	
53  	/** counts the number of change notifications received. */
54  	int m_notified;
55  	
56  	/**
57  	 * {@inheritDoc}
58  	 */
59  	@SuppressWarnings("unchecked")
60  	@Before
61  	public void setUp() {
62  		m_origin = new SettableObservableValue<Integer>(0);
63  
64  		m_values = (ObservableValue<Integer>[]) new ObservableValue[SIZE];
65  		m_values[0] = m_origin;
66  		for (int i = 1; i < m_values.length; i++) {
67  			Sum s = new Sum();
68  			s.m_i = i;
69  			m_values[i] = new LiveValue(s);
70  		}
71  
72  		m_last = m_values[m_values.length - 1];
73  		
74  		m_notified = 0;
75  	}
76  	
77  	/***/
78  	class Sum implements Computable<Integer> {
79  		/***/
80  		int m_i;
81  		
82  		/**
83  		 * returns the sum of all values with index smaller {@code i}.
84  		 * @return see above
85  		 */
86  		public Integer is() {
87  			int result = 0;
88  			for (int j = 0; j < m_i; j++) {
89  				result += m_values[j].get();
90  			}
91  			return result;
92  		}
93  	}
94  
95  	/**
96  	 * {@inheritDoc}
97  	 */
98  	public void changed(Integer newRef) {
99  		m_notified++;
100 	}
101 	
102 	/** tests update propagation. */
103 	@Test
104 	public void testUpdate() {
105 		assertEquals("to do" , 0, m_notified);
106 
107 		m_last.subscribe(this);
108 		assertEquals(0, (int) m_last.get());
109 		assertEquals(1, m_notified);
110 		
111 		m_origin.set(1);
112 		assertEquals(2, m_notified);
113 		assertEquals(
114 			1 << (m_values.length - 2),
115 			(int) m_last.get()
116 		);
117 	}
118 	
119 	/** tests unsubscription. */
120 	@Test
121 	public void testUnsubscribe() {
122 		m_last.subscribe(new ValueObserver<Integer>() {
123 			public void changed(Integer newRef) {
124 				if (m_origin.get() == 0) {
125 					m_last.unsubscribe(this);
126 				} else {
127 					fail("Notification received after unsubscription.");
128 				}
129 			}
130 		});
131 		
132 		m_origin.set(1);
133 	}
134 }