KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > ConfigObjectTestBase


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
23
24 /**
25  * A base class for all tests of real config objects. Tests derived from this aren't true unit tests, because they use a
26  * bunch of the config infrastructure; they're more like subsystem tests. However, that's more like what we want to do
27  * anyway — we want to make sure these objects hook up all the config stuff correctly so that they work right.
28  */

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 JavaDoc {
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 JavaDoc repositoryBeanClass) throws Exception JavaDoc {
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 JavaDoc oldObject, Object JavaDoc 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 JavaDoc {
78     TcConfigDocument bean = (TcConfigDocument) new TerracottaDomainConfigurationDocumentBeanFactory()
79         .createBean(new ByteArrayInputStream JavaDoc(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 JavaDoc {
84     return this.context;
85   }
86
87   protected final TerracottaConfigBuilder builder() throws Exception JavaDoc {
88     return this.builder;
89   }
90
91   protected final void resetBuilder() throws Exception JavaDoc {
92     this.builder = TerracottaConfigBuilder.newMinimalInstance();
93   }
94
95   protected abstract XmlObject getBeanFromTcConfig(TcConfig config) throws Exception JavaDoc;
96
97 }
98
Popular Tags