KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > common > propertyeditor > AbstractCollectionEditorTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.common.propertyeditor;
18
19 import java.beans.PropertyEditor JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import junit.framework.TestCase;
24
25 /**
26  * Abstract superclass for collection editor tests. Implement the two abstract methods and initialize
27  * editor and ordered in the setUp() method.
28  *
29  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
30  */

31 public abstract class AbstractCollectionEditorTest extends TestCase {
32     PropertyEditor JavaDoc editor;
33     boolean ordered;
34
35     public void testGetValue_1Item() {
36         String JavaDoc input = "item";
37
38         editor.setAsText(input);
39         Object JavaDoc output = editor.getValue();
40
41         assertNotNull(output);
42         checkType(output);
43
44         Collection JavaDoc collection = (Collection JavaDoc) output;
45         assertEquals(1, collection.size());
46         assertEquals(input, collection.iterator().next());
47     }
48
49     public void testGetValue_2Items() {
50         String JavaDoc input = "item1, item2";
51
52         editor.setAsText(input);
53         Object JavaDoc output = editor.getValue();
54
55         assertNotNull(output);
56         checkType(output);
57
58         Collection JavaDoc collection = (Collection JavaDoc) output;
59         checkContents(collection);
60     }
61
62     public void testEmpty() {
63         String JavaDoc input = "[]";
64         editor.setAsText(input);
65         Object JavaDoc output = editor.getValue();
66         assertNotNull(output);
67         checkType(output);
68         Collection JavaDoc collection = (Collection JavaDoc) output;
69         assertEquals(0, collection.size());
70     }
71
72     private void checkContents(Collection JavaDoc collection) {
73         assertEquals(2, collection.size());
74         Iterator JavaDoc iterator = collection.iterator();
75         if (ordered) {
76             assertEquals("item1", iterator.next());
77             assertEquals("item2", iterator.next());
78         } else {
79             Object JavaDoc item1 = iterator.next();
80             Object JavaDoc item2 = iterator.next();
81             assertTrue((item1.equals("item1") && item2.equals("item2")) || (item1.equals("item2") && item2.equals("item1")));
82         }
83
84     }
85
86     public void testRoundTrip() {
87         Collection JavaDoc collection = createCollection();
88         collection.add("item1");
89         collection.add("item2");
90         editor.setValue(collection);
91         String JavaDoc text = editor.getAsText();
92         editor.setAsText(text);
93         Collection JavaDoc result = (Collection JavaDoc) editor.getValue();
94         checkContents(result);
95     }
96
97     protected abstract void checkType(Object JavaDoc output);
98
99     protected abstract Collection JavaDoc createCollection();
100 }
101
Popular Tags