1 16 17 package org.apache.commons.configuration; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import junit.framework.TestCase; 24 import junitx.framework.ListAssert; 25 26 32 public abstract class TestAbstractConfiguration extends TestCase 33 { 34 42 protected abstract AbstractConfiguration getConfiguration(); 43 44 47 protected abstract AbstractConfiguration getEmptyConfiguration(); 48 49 public void testGetProperty() 50 { 51 Configuration config = getConfiguration(); 52 assertEquals("key1", "value1", config.getProperty("key1")); 53 assertEquals("key2", "value2", config.getProperty("key2")); 54 assertNull("key3", config.getProperty("key3")); 55 } 56 57 public void testList() 58 { 59 Configuration config = getConfiguration(); 60 61 List list = config.getList("list"); 62 assertNotNull("list not found", config.getProperty("list")); 63 assertEquals("list size", 2, list.size()); 64 assertTrue("'value1' is not in the list", list.contains("value1")); 65 assertTrue("'value2' is not in the list", list.contains("value2")); 66 } 67 68 public void testAddPropertyDirect() 69 { 70 AbstractConfiguration config = getConfiguration(); 71 config.addPropertyDirect("key3", "value3"); 72 assertEquals("key3", "value3", config.getProperty("key3")); 73 } 74 75 public void testIsEmpty() 76 { 77 Configuration config = getConfiguration(); 78 assertFalse("the configuration is empty", config.isEmpty()); 79 assertTrue("the configuration is not empty", getEmptyConfiguration().isEmpty()); 80 } 81 82 public void testContainsKey() 83 { 84 Configuration config = getConfiguration(); 85 assertTrue("key1 not found", config.containsKey("key1")); 86 assertFalse("key3 found", config.containsKey("key3")); 87 } 88 89 public void testClearProperty() 90 { 91 Configuration config = getConfiguration(); 92 config.clearProperty("key2"); 93 assertFalse("key2 not cleared", config.containsKey("key2")); 94 } 95 96 public void testGetKeys() 97 { 98 Configuration config = getConfiguration(); 99 Iterator keys = config.getKeys(); 100 101 List expectedKeys = new ArrayList (); 102 expectedKeys.add("key1"); 103 expectedKeys.add("key2"); 104 expectedKeys.add("list"); 105 106 assertNotNull("null iterator", keys); 107 assertTrue("empty iterator", keys.hasNext()); 108 109 List actualKeys = new ArrayList (); 110 while (keys.hasNext()) 111 { 112 actualKeys.add(keys.next()); 113 } 114 115 ListAssert.assertEquals("keys", expectedKeys, actualKeys); 116 } 117 118 } 119 | Popular Tags |