1   /*
2    * Copyright 2002-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.clazz.reflect;
17  
18  import java.util.ArrayList;
19  import java.util.ConcurrentModificationException;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.commons.clazz.reflect.common.ReflectedListProperty;
24  
25  /**
26   * 
27   * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
28   * @version $Id: ReflectedListPropertyTestCommon.java 155436 2005-02-26 13:17:48Z dirkv $
29   */
30  public abstract class ReflectedListPropertyTestCommon
31      extends ReflectedClazzTestSupport 
32  {
33      protected ReflectedListProperty intArrayProperty;
34      protected ReflectedListProperty stringProperty;
35  
36      /**
37       * Constructor for ReflectedPropertyMapTest.
38       * @param name Is the test method name
39       */
40      public ReflectedListPropertyTestCommon(String name) {
41          super(name);
42      }
43  
44      public void setUp() throws Exception {
45          super.setUp();
46          intArrayProperty =
47              (ReflectedListProperty) clazz.getProperty("intArray");
48          stringProperty = (ReflectedListProperty) clazz.getProperty("string");
49      }
50  
51      public void testSizeWithArray() {
52          populateIntArray();
53  
54          List list = (List) intArrayProperty.get(instance);
55          int count = list.size();
56  
57          assertTrace("getting size with count getter", "getIntArray()");
58          assertEquals(3, count);
59      }
60  
61      public void testSizeWithEmpty() {
62          List list = (List) intArrayProperty.get(instance);
63          int count = list.size();
64  
65          assertTrace("getting size", "getIntArray()");
66          assertEquals(0, count);
67      }
68  
69      public void testIsEmptyWithArray() {
70          List list = (List) intArrayProperty.get(instance);
71          boolean empty = list.isEmpty();
72          assertTrace("empty", "getIntArray()");
73          assertTrue(empty);
74  
75          populateIntArray();
76          //        list = (List)intArrayProperty.get(instance);
77          empty = list.isEmpty();
78          assertTrace("not empty", "getIntArray()");
79          assertTrue(!empty);
80      }
81  
82      public void testContainsWithArraySuccess() {
83          populateIntArray();
84  
85          List list = (List) intArrayProperty.get(instance);
86          boolean contains = list.contains(new Integer(2));
87          assertTrace("contains", "getIntArray()");
88          assertTrue(contains);
89      }
90  
91      public void testContainsWithArrayFailure() {
92          populateIntArray();
93  
94          List list = (List) intArrayProperty.get(instance);
95          boolean contains = list.contains(new Integer(371));
96          assertTrace("does not contain", "getIntArray()");
97          assertTrue(!contains);
98      }
99  
100     public void testIteratorWithArray() {
101         populateIntArray();
102 
103         List list = (List) intArrayProperty.get(instance);
104         Iterator it = list.iterator();
105         ArrayList result = new ArrayList();
106         while (it.hasNext()) {
107             result.add(it.next());
108         }
109 
110         assertTrace("iterator", "getIntArray()");
111 
112         ArrayList expected = new ArrayList();
113         expected.add(new Integer(1));
114         expected.add(new Integer(2));
115         expected.add(new Integer(3));
116 
117         assertEquals("iterator", expected, result);
118     }
119 
120     /*
121      * Test for Object[] toArray()
122      */
123     public void testToArray() {
124     }
125 
126     /*
127      * Test for Object[] toArray(Object[])
128      */
129     public void testToArrayObjectArray() {
130     }
131 
132     /*
133      * Test for boolean add(Object)
134      */
135     public void testAddObjectWithArrayStartEmpty() {
136         List list = (List) intArrayProperty.get(instance);
137         list.add(new Integer(1));
138 
139         assertTrace("add", "getIntArray()", "setIntArray(int[])");
140 
141         ArrayList expected = new ArrayList();
142         expected.add(new Integer(1));
143         assertEquals("result of add via list", expected, list);
144 
145         int array[] = instance.getIntArray();
146         assertEquals("result of add directly (length)", 1, array.length);
147         assertEquals("result of add directly (value)", 1, array[0]);
148     }
149 
150     /*
151      * Test for boolean add(Object)
152      */
153     public void testAddObjectWithArrayStartPopulated() {
154         populateIntArray();
155 
156         List list = (List) intArrayProperty.get(instance);
157         list.add(new Integer(4));
158 
159         assertTrace("add", "getIntArray()", "setIntArray(int[])");
160 
161 //      System.err.println("LIST: " + list);
162         ArrayList expected = new ArrayList();
163         expected.add(new Integer(1));
164         expected.add(new Integer(2));
165         expected.add(new Integer(3));
166         expected.add(new Integer(4));
167         assertEquals("result of add via list", expected, list);
168 
169         int array[] = instance.getIntArray();
170         assertEquals("result of add directly (length)", 4, array.length);
171         assertEquals("result of add directly (value)", 4, array[3]);
172     }
173 
174     /*
175      * Test for boolean add(Object)
176      */
177     public void testConcurrentModificationException() {
178         populateIntArray();
179 
180         boolean ex = false;
181         List list = (List) intArrayProperty.get(instance);
182         Iterator it = list.iterator();
183         it.next();
184         list.add(new Integer(4));
185         try {
186             it.next();
187         }
188         catch (ConcurrentModificationException e) {
189             ex = true;
190         }
191 
192         assertTrue("concurrent mod exception", ex);
193     }
194 
195     /*
196      * Test for boolean remove(Object)
197      */
198     public void testRemoveObjectFromArray() {
199         populateIntArray();
200 
201         List list = (List) intArrayProperty.get(instance);
202         list.remove(new Integer(2));
203 
204         assertTrace("remove", new String[] { "getIntArray()", // For iteration
205             "getIntArray()", // For removal
206             "setIntArray(int[])" }); // New shorter array
207 
208         ArrayList expected = new ArrayList();
209         expected.add(new Integer(1));
210         expected.add(new Integer(3));
211         assertEquals("result of remove via list", expected, list);
212 
213         int array[] = instance.getIntArray();
214         assertEquals("result of remove directly (length)", 2, array.length);
215     }
216 
217     /*
218      * Test for Object remove(int)
219      */
220     public void testRemoveWithIndexFromArray() {
221         populateIntArray();
222 
223         List list = (List) intArrayProperty.get(instance);
224         list.remove(1);
225 
226         assertTrace("remove", "getIntArray()", "setIntArray(int[])");
227 
228         ArrayList expected = new ArrayList();
229         expected.add(new Integer(1));
230         expected.add(new Integer(3));
231         assertEquals("result of remove via list", expected, list);
232 
233         int array[] = instance.getIntArray();
234         assertEquals("result of remove directly (length)", 2, array.length);
235     }
236 
237     public void testContainsAll() {
238     }
239 
240     /*
241      * Test for boolean addAll(Collection)
242      */
243     public void testAddAllCollection() {
244     }
245 
246     /*
247      * Test for boolean addAll(int, Collection)
248      */
249     public void testAddAllICollection() {
250     }
251 
252     public void testRemoveAll() {
253     }
254 
255     public void testRetainAll() {
256     }
257 
258     public void testClear() {
259     }
260 
261     public void testGetWithArray() {
262         populateIntArray();
263 
264         List list = (List) intArrayProperty.get(instance);
265         Object value = list.get(1);
266 
267         assertTrace("getting value with direct getter", "getIntArray()");
268         assertEquals(new Integer(2), value);
269     }
270 
271     public void testGetWithList() {
272         populateStringList();
273 
274         List list = (List) stringProperty.get(instance);
275         Object value = list.get(1);
276 
277         assertTrace(
278             "getting value with parameterized getter",
279             "getString(int)");
280 
281         assertEquals("bar", value);
282     }
283 
284     public void testSetWithArray() {
285         populateIntArray();
286 
287         List list = (List) intArrayProperty.get(instance);
288         list.set(1, new Integer(7));
289 
290         assertTrace("setting value with direct getter", "getIntArray()");
291         assertEquals(7, instance.getIntArray()[1]);
292     }
293 
294     public void testSetWithList() {
295         populateStringList();
296 
297         List list = (List) stringProperty.get(instance);
298         list.set(1, "biz");
299 
300         assertTrace(
301             "setting value with parameterized setter",
302             "getString(int)",
303             "setString(int,String)");
304 
305         assertEquals("biz", instance.getString(1));
306     }
307 
308     public void testIndexOf() {
309     }
310 
311     public void testLastIndexOf() {
312     }
313 
314     /*
315      * Test for ListIterator listIterator()
316      */
317     public void testListIterator() {
318     }
319 
320     /*
321      * Test for ListIterator listIterator(int)
322      */
323     public void testListIteratorI() {
324     }
325 
326     public void testSubList() {
327     }
328 
329     /**
330      * We are supposed to get different results for standard and extended models
331      * when the corresponding property does not have a <code>get</code> method
332      * that returns the whole list. The extended model can still take care of
333      * this as long as there is a <code>get<i>Foo</i>Count()</code> method.
334      */
335     public abstract void testSizeWithList();
336     public abstract void testIsEmptyWithList();
337     public abstract void testContainsWithListSuccess();
338     public abstract void testContainsWithListFailure();
339     public abstract void testIteratorWithList();
340     public abstract void testAddObjectWithListStartEmpty();
341     public abstract void testAddObjectWithListStartPopulated();
342     public abstract void testAddIndexedWithList();
343 
344     public void populateIntArray() {
345         instance.setIntArray(new int[] { 1, 2, 3 });
346         instance.resetTrace();
347     }
348 
349     public void populateStringList() {
350         instance.addString("foo");
351         instance.addString("bar");
352         instance.addString("baz");
353         instance.resetTrace();
354     }
355 }