1 4 package com.tc.config.schema; 5 6 import org.apache.xmlbeans.XmlObject; 7 8 import com.tc.config.schema.beanfactory.TerracottaDomainConfigurationDocumentBeanFactory; 9 import com.tc.config.schema.context.ConfigContext; 10 import com.tc.config.schema.context.StandardConfigContext; 11 import com.tc.config.schema.defaults.DefaultValueProvider; 12 import com.tc.config.schema.defaults.FromSchemaDefaultValueProvider; 13 import com.tc.config.schema.dynamic.ConfigItem; 14 import com.tc.config.schema.dynamic.MockConfigItemListener; 15 import com.tc.config.schema.repository.StandardBeanRepository; 16 import com.tc.config.schema.test.TerracottaConfigBuilder; 17 import com.tc.test.TCTestCase; 18 import com.tc.util.Assert; 19 import com.terracottatech.config.TcConfigDocument; 20 import com.terracottatech.config.TcConfigDocument.TcConfig; 21 22 import java.io.ByteArrayInputStream ; 23 24 29 public abstract class ConfigObjectTestBase extends TCTestCase { 30 31 private StandardBeanRepository repository; 32 private TerracottaConfigBuilder builder; 33 private ConfigContext context; 34 35 private MockConfigItemListener listener1; 36 private MockConfigItemListener listener2; 37 38 public void setUp() throws Exception { 39 throw Assert.failure("You must specify the repository bean class via a call to super.setUp(Class)."); 40 } 41 42 public void setUp(Class repositoryBeanClass) throws Exception { 43 this.repository = new StandardBeanRepository(repositoryBeanClass); 44 45 DefaultValueProvider provider = new FromSchemaDefaultValueProvider(); 46 this.context = new StandardConfigContext(this.repository, provider, new MockIllegalConfigurationChangeHandler(), null); 47 48 this.builder = new TerracottaConfigBuilder(); 49 50 this.listener1 = new MockConfigItemListener(); 51 this.listener2 = new MockConfigItemListener(); 52 } 53 54 protected final void addListeners(ConfigItem item) { 55 item.addListener(this.listener1); 56 item.addListener(this.listener2); 57 } 58 59 protected final void checkListener(Object oldObject, Object newObject) { 60 assertEquals(1, this.listener1.getNumValueChangeds()); 61 assertEquals(oldObject, this.listener1.getLastOldValue()); 62 assertEquals(newObject, this.listener1.getLastNewValue()); 63 assertEquals(1, this.listener2.getNumValueChangeds()); 64 assertEquals(oldObject, this.listener2.getLastOldValue()); 65 assertEquals(newObject, this.listener2.getLastNewValue()); 66 this.listener1.reset(); 67 this.listener2.reset(); 68 } 69 70 protected final void checkNoListener() { 71 assertEquals(0, this.listener1.getNumValueChangeds()); 72 assertEquals(0, this.listener2.getNumValueChangeds()); 73 this.listener1.reset(); 74 this.listener2.reset(); 75 } 76 77 public void setConfig() throws Exception { 78 TcConfigDocument bean = (TcConfigDocument) new TerracottaDomainConfigurationDocumentBeanFactory() 79 .createBean(new ByteArrayInputStream (this.builder.toString().getBytes()), "for test").bean(); 80 this.repository.setBean(getBeanFromTcConfig(bean.getTcConfig()), "from test config builder"); 81 } 82 83 protected final ConfigContext context() throws Exception { 84 return this.context; 85 } 86 87 protected final TerracottaConfigBuilder builder() throws Exception { 88 return this.builder; 89 } 90 91 protected final void resetBuilder() throws Exception { 92 this.builder = TerracottaConfigBuilder.newMinimalInstance(); 93 } 94 95 protected abstract XmlObject getBeanFromTcConfig(TcConfig config) throws Exception ; 96 97 } 98 | Popular Tags |