001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.config.schema.context;
005:
006: import com.tc.config.schema.MockIllegalConfigurationChangeHandler;
007: import com.tc.config.schema.MockSchemaType;
008: import com.tc.config.schema.MockXmlObject;
009: import com.tc.config.schema.defaults.MockDefaultValueProvider;
010: import com.tc.config.schema.dynamic.BooleanConfigItem;
011: import com.tc.config.schema.dynamic.ConfigItem;
012: import com.tc.config.schema.dynamic.FileConfigItem;
013: import com.tc.config.schema.dynamic.IntConfigItem;
014: import com.tc.config.schema.dynamic.MockConfigItem;
015: import com.tc.config.schema.dynamic.MockListeningConfigItem;
016: import com.tc.config.schema.dynamic.StringArrayConfigItem;
017: import com.tc.config.schema.dynamic.StringConfigItem;
018: import com.tc.config.schema.dynamic.XPathBasedConfigItem;
019: import com.tc.config.schema.repository.MockBeanRepository;
020: import com.tc.test.TCTestCase;
021:
022: /**
023: * Unit test for {@link StandardConfigContext}.
024: */
025: public class StandardConfigContextTest extends TCTestCase {
026:
027: private MockSchemaType schemaType;
028: private MockBeanRepository beanRepository;
029: private MockDefaultValueProvider defaultValueProvider;
030: private MockIllegalConfigurationChangeHandler illegalConfigurationChangeHandler;
031:
032: private ConfigContext context;
033:
034: public void setUp() throws Exception {
035: this .schemaType = new MockSchemaType();
036: this .beanRepository = new MockBeanRepository();
037: this .beanRepository
038: .setReturnedRootBeanSchemaType(this .schemaType);
039: this .defaultValueProvider = new MockDefaultValueProvider();
040: this .illegalConfigurationChangeHandler = new MockIllegalConfigurationChangeHandler();
041:
042: this .context = new StandardConfigContext(this .beanRepository,
043: this .defaultValueProvider,
044: this .illegalConfigurationChangeHandler, null);
045: }
046:
047: public void testEnsureRepositoryProvides() throws Exception {
048: this .beanRepository.setExceptionOnEnsureBeanIsOfClass(null);
049:
050: this .context.ensureRepositoryProvides(Number.class);
051: assertEquals(1, this .beanRepository
052: .getNumEnsureBeanIsOfClasses());
053: assertEquals(Number.class, this .beanRepository.getLastClass());
054: this .beanRepository.reset();
055:
056: RuntimeException exception = new RuntimeException("foo");
057: this .beanRepository
058: .setExceptionOnEnsureBeanIsOfClass(exception);
059:
060: try {
061: this .context.ensureRepositoryProvides(Object.class);
062: fail("Didn't get expected exception");
063: } catch (RuntimeException re) {
064: assertSame(exception, re);
065: assertEquals(1, this .beanRepository
066: .getNumEnsureBeanIsOfClasses());
067: assertEquals(Object.class, this .beanRepository
068: .getLastClass());
069: }
070: }
071:
072: public void testConstruction() throws Exception {
073: try {
074: new StandardConfigContext(null, this .defaultValueProvider,
075: this .illegalConfigurationChangeHandler, null);
076: fail("Didn't get NPE on no bean repository");
077: } catch (NullPointerException npe) {
078: // ok
079: }
080:
081: try {
082: new StandardConfigContext(this .beanRepository, null,
083: this .illegalConfigurationChangeHandler, null);
084: fail("Didn't get NPE on no default value provider");
085: } catch (NullPointerException npe) {
086: // ok
087: }
088:
089: try {
090: new StandardConfigContext(this .beanRepository,
091: this .defaultValueProvider, null, null);
092: fail("Didn't get NPE on no illegal configuration change handler");
093: } catch (NullPointerException npe) {
094: // ok
095: }
096: }
097:
098: public void testHasDefaultFor() throws Exception {
099: this .defaultValueProvider
100: .setReturnedPossibleForXPathToHaveDefault(false);
101: this .defaultValueProvider.setReturnedHasDefault(false);
102:
103: assertFalse(this .context.hasDefaultFor("foobar/baz"));
104: assertEquals(1, this .defaultValueProvider
105: .getNumPossibleForXPathToHaveDefaults());
106: assertEquals("foobar/baz", this .defaultValueProvider
107: .getLastPossibleForXPathToHaveDefaultsXPath());
108: assertEquals(0, this .defaultValueProvider.getNumHasDefaults());
109:
110: this .defaultValueProvider.reset();
111: this .defaultValueProvider
112: .setReturnedPossibleForXPathToHaveDefault(true);
113: this .defaultValueProvider.setReturnedHasDefault(false);
114:
115: assertFalse(this .context.hasDefaultFor("foobar/baz"));
116: assertEquals(1, this .defaultValueProvider
117: .getNumPossibleForXPathToHaveDefaults());
118: assertEquals("foobar/baz", this .defaultValueProvider
119: .getLastPossibleForXPathToHaveDefaultsXPath());
120: assertEquals(1, this .defaultValueProvider.getNumHasDefaults());
121: assertSame(this .schemaType, this .defaultValueProvider
122: .getLastHasDefaultsSchemaType());
123: assertEquals("foobar/baz", this .defaultValueProvider
124: .getLastHasDefaultsXPath());
125:
126: this .defaultValueProvider.reset();
127: this .defaultValueProvider
128: .setReturnedPossibleForXPathToHaveDefault(false);
129: this .defaultValueProvider.setReturnedHasDefault(true);
130:
131: assertFalse(this .context.hasDefaultFor("foobar/baz"));
132: assertEquals(1, this .defaultValueProvider
133: .getNumPossibleForXPathToHaveDefaults());
134: assertEquals("foobar/baz", this .defaultValueProvider
135: .getLastPossibleForXPathToHaveDefaultsXPath());
136: assertEquals(0, this .defaultValueProvider.getNumHasDefaults());
137:
138: this .defaultValueProvider.reset();
139: this .defaultValueProvider
140: .setReturnedPossibleForXPathToHaveDefault(true);
141: this .defaultValueProvider.setReturnedHasDefault(true);
142:
143: assertTrue(this .context.hasDefaultFor("foobar/baz"));
144: assertEquals(1, this .defaultValueProvider
145: .getNumPossibleForXPathToHaveDefaults());
146: assertEquals("foobar/baz", this .defaultValueProvider
147: .getLastPossibleForXPathToHaveDefaultsXPath());
148: assertEquals(1, this .defaultValueProvider.getNumHasDefaults());
149: assertSame(this .schemaType, this .defaultValueProvider
150: .getLastHasDefaultsSchemaType());
151: assertEquals("foobar/baz", this .defaultValueProvider
152: .getLastHasDefaultsXPath());
153: }
154:
155: public void testDefaultFor() throws Exception {
156: MockXmlObject object = new MockXmlObject();
157: this .defaultValueProvider.setReturnedDefaultFor(object);
158:
159: assertSame(object, this .context.defaultFor("foobar/baz"));
160: assertEquals(1, this .defaultValueProvider.getNumDefaultFors());
161: assertSame(this .schemaType, this .defaultValueProvider
162: .getLastBaseType());
163: assertEquals("foobar/baz", this .defaultValueProvider
164: .getLastXPath());
165: }
166:
167: public void testIsOptional() throws Exception {
168: this .defaultValueProvider.setReturnedIsOptional(false);
169:
170: assertFalse(this .context.isOptional("foobar/baz"));
171: assertEquals(1, this .defaultValueProvider.getNumIsOptionals());
172: assertSame(this .schemaType, this .defaultValueProvider
173: .getLastBaseType());
174: assertEquals("foobar/baz", this .defaultValueProvider
175: .getLastXPath());
176: }
177:
178: public void testBean() throws Exception {
179: MockXmlObject object = new MockXmlObject();
180: this .beanRepository.setReturnedBean(object);
181:
182: assertSame(object, this .context.bean());
183: assertEquals(1, this .beanRepository.getNumBeans());
184: }
185:
186: public void testItemCreated() throws Exception {
187: MockConfigItem item = new MockConfigItem();
188:
189: this .context.itemCreated(item);
190: assertEquals(0, this .beanRepository.getNumAddListeners());
191:
192: MockListeningConfigItem listeningItem = new MockListeningConfigItem();
193:
194: this .context.itemCreated(listeningItem);
195: assertEquals(1, this .beanRepository.getNumAddListeners());
196: assertSame(listeningItem, this .beanRepository.getLastListener());
197: }
198:
199: public void testItems() throws Exception {
200: checkItem(this .context.intItem("foobar/baz"), "foobar/baz",
201: IntConfigItem.class, null);
202: checkItem(this .context.stringItem("foobar/baz"), "foobar/baz",
203: StringConfigItem.class, null);
204: checkItem(this .context.stringArrayItem("foobar/baz"),
205: "foobar/baz", StringArrayConfigItem.class, null);
206: checkItem(this .context.fileItem("foobar/baz"), "foobar/baz",
207: FileConfigItem.class, null);
208: checkItem(this .context.booleanItem("foobar/baz"), "foobar/baz",
209: BooleanConfigItem.class, null);
210: checkItem(this .context.booleanItem("foobar/baz", true),
211: "foobar/baz", BooleanConfigItem.class, Boolean.TRUE);
212: checkItem(this .context.booleanItem("foobar/baz", false),
213: "foobar/baz", BooleanConfigItem.class, Boolean.FALSE);
214: }
215:
216: private void checkItem(ConfigItem item, String xpath,
217: Class expectedClass, Object expectedDefaultValue) {
218: assertTrue(expectedClass.isInstance(item));
219: assertEquals(xpath, ((XPathBasedConfigItem) item).xpath());
220: assertSame(this .context, ((XPathBasedConfigItem) item)
221: .context());
222: assertEquals(expectedDefaultValue,
223: ((XPathBasedConfigItem) item).defaultValue());
224: }
225:
226: }
|