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   * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +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/collections/ExtendedListTest.java $
41   *
42   * @author Adrian Moos (AMS)
43   */
44  public class ExtendedListTest {
45  	/***/
46  	private ExtendedArrayList<Integer> m_z, m_a, m_b, m_c;
47  	/***/
48  	private ExtendedArrayList<String> m_n;
49  	/***/
50  	private TransformedList<Integer, String> m_s;
51  	
52  	/** {@inheritDoc} */
53  	@Before
54  	public void setUp() {
55  		m_z = new ExtendedArrayList<Integer>(1, 2, 3, 4, 5);
56  		m_a = new ExtendedArrayList<Integer>(1, 2, 3, 4, 6, 5);
57  		m_b = new ExtendedArrayList<Integer>(1, 2, 3, 4, 5, 6);
58  		m_c = new ExtendedArrayList<Integer>(2, 4, 6);
59  		m_s = m_z.mapped(new Function<Integer, String>() {
60  			public String apply(Integer d) {
61  				return d.toString();
62  			}
63  		});
64  		m_n = new ExtendedArrayList<String>("4", "2");
65  	}
66  	
67  	/***/
68  	@Test
69  	public void testEquals() {
70  		assertFalse(m_z.equals(m_a));
71  		assertFalse(m_z.equals(m_b));
72  		assertFalse(m_a.equals(m_b));
73  	}
74  	
75  	/***/
76  	@Test
77  	public void testRemove() {
78  		m_a.remove((Integer) 6);
79  		m_b.remove((Integer) 6);
80  		assertEquals(m_a, m_b);
81  	}
82  	
83  	/***/
84  	@Test
85  	public void testAdd() {
86  		m_z.add(6);
87  		assertEquals(m_z, m_b);
88  	}
89  	
90  	/***/
91  	@Test
92  	public void testOrderLike() {
93  		try {
94  			m_z.orderLike(m_c);
95  			fail("orderLike is required to throw an exception when provided "
96  				+ "with an invalied example, but did not.");
97  		} catch (NoSuchElementException e) { }
98  		m_b = new ExtendedArrayList<Integer>(m_a);
99  		m_a.orderLike(m_c);
100 		assertEquals(m_a.subList(0, 3), m_c);
101 		assertTrue(m_b.containsAll(m_a));
102 	}
103 	
104 	/** Test orderLike on a transformed list. */
105 	@Test
106 	public void testTransformedOrderLike() {
107 		m_s.orderLike(m_n);
108 		assertEquals(
109 			"orderLike is required to propagate to the backing list",
110 			m_z.subList(0, 2),
111 			new ExtendedArrayList<Integer>(4, 2)
112 		);
113 	}
114 	
115 	/***/
116 	@Test
117 	public void testFiltered() {
118 		assertEquals(
119 			m_a.filtered(new Filter<Integer>() {
120 				public boolean accepts(Integer i) {
121 					return i % 2 == 0;
122 				}
123 			}), m_c
124 		);
125 	}
126 }
127 
128 // Checkstyle: MagicNumber on
129 // Checkstyle: EmptyBlock on