001: /**
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */package com.tc.config.schema;
005:
006: import org.apache.xmlbeans.XmlObject;
007:
008: import com.tc.config.schema.beanfactory.TerracottaDomainConfigurationDocumentBeanFactory;
009: import com.tc.config.schema.context.ConfigContext;
010: import com.tc.config.schema.context.StandardConfigContext;
011: import com.tc.config.schema.defaults.DefaultValueProvider;
012: import com.tc.config.schema.defaults.FromSchemaDefaultValueProvider;
013: import com.tc.config.schema.dynamic.ConfigItem;
014: import com.tc.config.schema.dynamic.MockConfigItemListener;
015: import com.tc.config.schema.repository.StandardBeanRepository;
016: import com.tc.config.schema.test.TerracottaConfigBuilder;
017: import com.tc.test.TCTestCase;
018: import com.tc.util.Assert;
019: import com.terracottatech.config.TcConfigDocument;
020: import com.terracottatech.config.TcConfigDocument.TcConfig;
021:
022: import java.io.ByteArrayInputStream;
023:
024: /**
025: * A base class for all tests of real config objects. Tests derived from this aren't true unit tests, because they use a
026: * bunch of the config infrastructure; they're more like subsystem tests. However, that's more like what we want to do
027: * anyway — we want to make sure these objects hook up all the config stuff correctly so that they work right.
028: */
029: public abstract class ConfigObjectTestBase extends TCTestCase {
030:
031: private StandardBeanRepository repository;
032: private TerracottaConfigBuilder builder;
033: private ConfigContext context;
034:
035: private MockConfigItemListener listener1;
036: private MockConfigItemListener listener2;
037:
038: public void setUp() throws Exception {
039: throw Assert
040: .failure("You must specify the repository bean class via a call to super.setUp(Class).");
041: }
042:
043: public void setUp(Class repositoryBeanClass) throws Exception {
044: this .repository = new StandardBeanRepository(
045: repositoryBeanClass);
046:
047: DefaultValueProvider provider = new FromSchemaDefaultValueProvider();
048: this .context = new StandardConfigContext(this .repository,
049: provider, new MockIllegalConfigurationChangeHandler(),
050: null);
051:
052: this .builder = TerracottaConfigBuilder.newMinimalInstance();
053:
054: this .listener1 = new MockConfigItemListener();
055: this .listener2 = new MockConfigItemListener();
056: }
057:
058: protected final void addListeners(ConfigItem item) {
059: item.addListener(this .listener1);
060: item.addListener(this .listener2);
061: }
062:
063: protected final void checkListener(Object oldObject,
064: Object newObject) {
065: assertEquals(1, this .listener1.getNumValueChangeds());
066: assertEquals(oldObject, this .listener1.getLastOldValue());
067: assertEquals(newObject, this .listener1.getLastNewValue());
068: assertEquals(1, this .listener2.getNumValueChangeds());
069: assertEquals(oldObject, this .listener2.getLastOldValue());
070: assertEquals(newObject, this .listener2.getLastNewValue());
071: this .listener1.reset();
072: this .listener2.reset();
073: }
074:
075: protected final void checkNoListener() {
076: assertEquals(0, this .listener1.getNumValueChangeds());
077: assertEquals(0, this .listener2.getNumValueChangeds());
078: this .listener1.reset();
079: this .listener2.reset();
080: }
081:
082: public void setConfig() throws Exception {
083: TcConfigDocument bean = (TcConfigDocument) new TerracottaDomainConfigurationDocumentBeanFactory()
084: .createBean(
085: new ByteArrayInputStream(this .builder
086: .toString().getBytes()), "for test")
087: .bean();
088: this .repository.setBean(
089: getBeanFromTcConfig(bean.getTcConfig()),
090: "from test config builder");
091: }
092:
093: protected final ConfigContext context() throws Exception {
094: return this .context;
095: }
096:
097: protected final TerracottaConfigBuilder builder() throws Exception {
098: return this .builder;
099: }
100:
101: protected final void resetBuilder() throws Exception {
102: this .builder = TerracottaConfigBuilder.newMinimalInstance();
103: }
104:
105: protected abstract XmlObject getBeanFromTcConfig(TcConfig config)
106: throws Exception;
107:
108: }
|