1 17 package org.apache.geronimo.common.propertyeditor; 18 19 import java.beans.PropertyEditor ; 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 23 import junit.framework.TestCase; 24 25 31 public abstract class AbstractCollectionEditorTest extends TestCase { 32 PropertyEditor editor; 33 boolean ordered; 34 35 public void testGetValue_1Item() { 36 String input = "item"; 37 38 editor.setAsText(input); 39 Object output = editor.getValue(); 40 41 assertNotNull(output); 42 checkType(output); 43 44 Collection collection = (Collection ) output; 45 assertEquals(1, collection.size()); 46 assertEquals(input, collection.iterator().next()); 47 } 48 49 public void testGetValue_2Items() { 50 String input = "item1, item2"; 51 52 editor.setAsText(input); 53 Object output = editor.getValue(); 54 55 assertNotNull(output); 56 checkType(output); 57 58 Collection collection = (Collection ) output; 59 checkContents(collection); 60 } 61 62 public void testEmpty() { 63 String input = "[]"; 64 editor.setAsText(input); 65 Object output = editor.getValue(); 66 assertNotNull(output); 67 checkType(output); 68 Collection collection = (Collection ) output; 69 assertEquals(0, collection.size()); 70 } 71 72 private void checkContents(Collection collection) { 73 assertEquals(2, collection.size()); 74 Iterator iterator = collection.iterator(); 75 if (ordered) { 76 assertEquals("item1", iterator.next()); 77 assertEquals("item2", iterator.next()); 78 } else { 79 Object item1 = iterator.next(); 80 Object 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 collection = createCollection(); 88 collection.add("item1"); 89 collection.add("item2"); 90 editor.setValue(collection); 91 String text = editor.getAsText(); 92 editor.setAsText(text); 93 Collection result = (Collection ) editor.getValue(); 94 checkContents(result); 95 } 96 97 protected abstract void checkType(Object output); 98 99 protected abstract Collection createCollection(); 100 } 101 | Popular Tags |