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.collections;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.util.NoSuchElementException;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import ch.elca.el4j.util.collections.TransformedList;
30  import ch.elca.el4j.util.collections.helpers.Filter;
31  import ch.elca.el4j.util.collections.helpers.Function;
32  import ch.elca.el4j.util.collections.impl.ExtendedArrayList;
33  
34  // Checkstyle: MagicNumber off
35  // Checkstyle: EmptyBlock off
36  
37  /**
38   * Tests for the extended array list (duh!).
39   *
40   * <script type="text/javascript">printFileStatus
41   *   ("$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/tags/el4j_1_5_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/util/collections/ExtendedListTest.java $",
42   *    "$Revision: 3038 $",
43   *    "$Date: 2008-06-09 12:08:34 +0200 (Mon, 09 Jun 2008) $",
44   *    "$Author: swismer $"
45   * );</script>
46   *
47   * @author Adrian Moos (AMS)
48   */
49  public class ExtendedListTest {
50  	/***/
51  	private ExtendedArrayList<Integer> m_z, m_a, m_b, m_c;
52  	/***/
53  	private ExtendedArrayList<String> m_n;
54  	/***/
55  	private TransformedList<Integer, String> m_s;
56  	
57  	/** {@inheritDoc} */
58  	@Before
59  	public void setUp() {
60  		m_z = new ExtendedArrayList<Integer>(1, 2, 3, 4, 5);
61  		m_a = new ExtendedArrayList<Integer>(1, 2, 3, 4, 6, 5);
62  		m_b = new ExtendedArrayList<Integer>(1, 2, 3, 4, 5, 6);
63  		m_c = new ExtendedArrayList<Integer>(2, 4, 6);
64  		m_s = m_z.mapped(new Function<Integer, String>() {
65  			public String apply(Integer d) {
66  				return d.toString();
67  			}
68  		});
69  		m_n = new ExtendedArrayList<String>("4", "2");
70  	}
71  	
72  	/***/
73  	@Test
74  	public void testEquals() {
75  		assertFalse(m_z.equals(m_a));
76  		assertFalse(m_z.equals(m_b));
77  		assertFalse(m_a.equals(m_b));
78  	}
79  	
80  	/***/
81  	@Test
82  	public void testRemove() {
83  		m_a.remove((Integer) 6);
84  		m_b.remove((Integer) 6);
85  		assertEquals(m_a, m_b);
86  	}
87  	
88  	/***/
89  	@Test
90  	public void testAdd() {
91  		m_z.add(6);
92  		assertEquals(m_z, m_b);
93  	}
94  	
95  	/***/
96  	@Test
97  	public void testOrderLike() {
98  		try {
99  			m_z.orderLike(m_c);
100 			fail("orderLike is required to throw an exception when provided "
101 				+ "with an invalied example, but did not.");
102 		} catch (NoSuchElementException e) { }
103 		m_b = new ExtendedArrayList<Integer>(m_a);
104 		m_a.orderLike(m_c);
105 		assertEquals(m_a.subList(0, 3), m_c);
106 		assertTrue(m_b.containsAll(m_a));
107 	}
108 	
109 	/** Test orderLike on a transformed list. */
110 	@Test
111 	public void testTransformedOrderLike() {
112 		m_s.orderLike(m_n);
113 		assertEquals(
114 			"orderLike is required to propagate to the backing list",
115 			m_z.subList(0, 2),
116 			new ExtendedArrayList<Integer>(4, 2)
117 		);
118 	}
119 	
120 	/***/
121 	@Test
122 	public void testFiltered() {
123 		assertEquals(
124 			m_a.filtered(new Filter<Integer>() {
125 				public boolean accepts(Integer i) {
126 					return i % 2 == 0;
127 				}
128 			}), m_c
129 		);
130 	}
131 }
132 
133 // Checkstyle: MagicNumber on
134 // Checkstyle: EmptyBlock on