1 4 package com.tc.config.schema.context; 5 6 import com.tc.config.schema.MockIllegalConfigurationChangeHandler; 7 import com.tc.config.schema.MockSchemaType; 8 import com.tc.config.schema.MockXmlObject; 9 import com.tc.config.schema.defaults.MockDefaultValueProvider; 10 import com.tc.config.schema.dynamic.BooleanConfigItem; 11 import com.tc.config.schema.dynamic.ConfigItem; 12 import com.tc.config.schema.dynamic.FileConfigItem; 13 import com.tc.config.schema.dynamic.IntConfigItem; 14 import com.tc.config.schema.dynamic.MockConfigItem; 15 import com.tc.config.schema.dynamic.MockListeningConfigItem; 16 import com.tc.config.schema.dynamic.StringArrayConfigItem; 17 import com.tc.config.schema.dynamic.StringConfigItem; 18 import com.tc.config.schema.dynamic.XPathBasedConfigItem; 19 import com.tc.config.schema.repository.MockBeanRepository; 20 import com.tc.test.TCTestCase; 21 22 25 public class StandardConfigContextTest extends TCTestCase { 26 27 private MockSchemaType schemaType; 28 private MockBeanRepository beanRepository; 29 private MockDefaultValueProvider defaultValueProvider; 30 private MockIllegalConfigurationChangeHandler illegalConfigurationChangeHandler; 31 32 private ConfigContext context; 33 34 public void setUp() throws Exception { 35 this.schemaType = new MockSchemaType(); 36 this.beanRepository = new MockBeanRepository(); 37 this.beanRepository.setReturnedRootBeanSchemaType(this.schemaType); 38 this.defaultValueProvider = new MockDefaultValueProvider(); 39 this.illegalConfigurationChangeHandler = new MockIllegalConfigurationChangeHandler(); 40 41 this.context = new StandardConfigContext(this.beanRepository, this.defaultValueProvider, 42 this.illegalConfigurationChangeHandler, null); 43 } 44 45 public void testEnsureRepositoryProvides() throws Exception { 46 this.beanRepository.setExceptionOnEnsureBeanIsOfClass(null); 47 48 this.context.ensureRepositoryProvides(Number .class); 49 assertEquals(1, this.beanRepository.getNumEnsureBeanIsOfClasses()); 50 assertEquals(Number .class, this.beanRepository.getLastClass()); 51 this.beanRepository.reset(); 52 53 RuntimeException exception = new RuntimeException ("foo"); 54 this.beanRepository.setExceptionOnEnsureBeanIsOfClass(exception); 55 56 try { 57 this.context.ensureRepositoryProvides(Object .class); 58 fail("Didn't get expected exception"); 59 } catch (RuntimeException re) { 60 assertSame(exception, re); 61 assertEquals(1, this.beanRepository.getNumEnsureBeanIsOfClasses()); 62 assertEquals(Object .class, this.beanRepository.getLastClass()); 63 } 64 } 65 66 public void testConstruction() throws Exception { 67 try { 68 new StandardConfigContext(null, this.defaultValueProvider, this.illegalConfigurationChangeHandler, null); 69 fail("Didn't get NPE on no bean repository"); 70 } catch (NullPointerException npe) { 71 } 73 74 try { 75 new StandardConfigContext(this.beanRepository, null, this.illegalConfigurationChangeHandler, null); 76 fail("Didn't get NPE on no default value provider"); 77 } catch (NullPointerException npe) { 78 } 80 81 try { 82 new StandardConfigContext(this.beanRepository, this.defaultValueProvider, null, null); 83 fail("Didn't get NPE on no illegal configuration change handler"); 84 } catch (NullPointerException npe) { 85 } 87 } 88 89 public void testHasDefaultFor() throws Exception { 90 this.defaultValueProvider.setReturnedPossibleForXPathToHaveDefault(false); 91 this.defaultValueProvider.setReturnedHasDefault(false); 92 93 assertFalse(this.context.hasDefaultFor("foobar/baz")); 94 assertEquals(1, this.defaultValueProvider.getNumPossibleForXPathToHaveDefaults()); 95 assertEquals("foobar/baz", this.defaultValueProvider.getLastPossibleForXPathToHaveDefaultsXPath()); 96 assertEquals(0, this.defaultValueProvider.getNumHasDefaults()); 97 98 this.defaultValueProvider.reset(); 99 this.defaultValueProvider.setReturnedPossibleForXPathToHaveDefault(true); 100 this.defaultValueProvider.setReturnedHasDefault(false); 101 102 assertFalse(this.context.hasDefaultFor("foobar/baz")); 103 assertEquals(1, this.defaultValueProvider.getNumPossibleForXPathToHaveDefaults()); 104 assertEquals("foobar/baz", this.defaultValueProvider.getLastPossibleForXPathToHaveDefaultsXPath()); 105 assertEquals(1, this.defaultValueProvider.getNumHasDefaults()); 106 assertSame(this.schemaType, this.defaultValueProvider.getLastHasDefaultsSchemaType()); 107 assertEquals("foobar/baz", this.defaultValueProvider.getLastHasDefaultsXPath()); 108 109 this.defaultValueProvider.reset(); 110 this.defaultValueProvider.setReturnedPossibleForXPathToHaveDefault(false); 111 this.defaultValueProvider.setReturnedHasDefault(true); 112 113 assertFalse(this.context.hasDefaultFor("foobar/baz")); 114 assertEquals(1, this.defaultValueProvider.getNumPossibleForXPathToHaveDefaults()); 115 assertEquals("foobar/baz", this.defaultValueProvider.getLastPossibleForXPathToHaveDefaultsXPath()); 116 assertEquals(0, this.defaultValueProvider.getNumHasDefaults()); 117 118 this.defaultValueProvider.reset(); 119 this.defaultValueProvider.setReturnedPossibleForXPathToHaveDefault(true); 120 this.defaultValueProvider.setReturnedHasDefault(true); 121 122 assertTrue(this.context.hasDefaultFor("foobar/baz")); 123 assertEquals(1, this.defaultValueProvider.getNumPossibleForXPathToHaveDefaults()); 124 assertEquals("foobar/baz", this.defaultValueProvider.getLastPossibleForXPathToHaveDefaultsXPath()); 125 assertEquals(1, this.defaultValueProvider.getNumHasDefaults()); 126 assertSame(this.schemaType, this.defaultValueProvider.getLastHasDefaultsSchemaType()); 127 assertEquals("foobar/baz", this.defaultValueProvider.getLastHasDefaultsXPath()); 128 } 129 130 public void testDefaultFor() throws Exception { 131 MockXmlObject object = new MockXmlObject(); 132 this.defaultValueProvider.setReturnedDefaultFor(object); 133 134 assertSame(object, this.context.defaultFor("foobar/baz")); 135 assertEquals(1, this.defaultValueProvider.getNumDefaultFors()); 136 assertSame(this.schemaType, this.defaultValueProvider.getLastBaseType()); 137 assertEquals("foobar/baz", this.defaultValueProvider.getLastXPath()); 138 } 139 140 public void testIsOptional() throws Exception { 141 this.defaultValueProvider.setReturnedIsOptional(false); 142 143 assertFalse(this.context.isOptional("foobar/baz")); 144 assertEquals(1, this.defaultValueProvider.getNumIsOptionals()); 145 assertSame(this.schemaType, this.defaultValueProvider.getLastBaseType()); 146 assertEquals("foobar/baz", this.defaultValueProvider.getLastXPath()); 147 } 148 149 public void testBean() throws Exception { 150 MockXmlObject object = new MockXmlObject(); 151 this.beanRepository.setReturnedBean(object); 152 153 assertSame(object, this.context.bean()); 154 assertEquals(1, this.beanRepository.getNumBeans()); 155 } 156 157 public void testItemCreated() throws Exception { 158 MockConfigItem item = new MockConfigItem(); 159 160 this.context.itemCreated(item); 161 assertEquals(0, this.beanRepository.getNumAddListeners()); 162 163 MockListeningConfigItem listeningItem = new MockListeningConfigItem(); 164 165 this.context.itemCreated(listeningItem); 166 assertEquals(1, this.beanRepository.getNumAddListeners()); 167 assertSame(listeningItem, this.beanRepository.getLastListener()); 168 } 169 170 public void testItems() throws Exception { 171 checkItem(this.context.intItem("foobar/baz"), "foobar/baz", IntConfigItem.class, null); 172 checkItem(this.context.stringItem("foobar/baz"), "foobar/baz", StringConfigItem.class, null); 173 checkItem(this.context.stringArrayItem("foobar/baz"), "foobar/baz", StringArrayConfigItem.class, null); 174 checkItem(this.context.fileItem("foobar/baz"), "foobar/baz", FileConfigItem.class, null); 175 checkItem(this.context.booleanItem("foobar/baz"), "foobar/baz", BooleanConfigItem.class, null); 176 checkItem(this.context.booleanItem("foobar/baz", true), "foobar/baz", BooleanConfigItem.class, Boolean.TRUE); 177 checkItem(this.context.booleanItem("foobar/baz", false), "foobar/baz", BooleanConfigItem.class, Boolean.FALSE); 178 } 179 180 private void checkItem(ConfigItem item, String xpath, Class expectedClass, Object expectedDefaultValue) { 181 assertTrue(expectedClass.isInstance(item)); 182 assertEquals(xpath, ((XPathBasedConfigItem) item).xpath()); 183 assertSame(this.context, ((XPathBasedConfigItem) item).context()); 184 assertEquals(expectedDefaultValue, ((XPathBasedConfigItem) item).defaultValue()); 185 } 186 187 } 188 | Popular Tags |