KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > repository > StandardBeanRepositoryTest


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.repository;
5
6 import org.apache.xmlbeans.SchemaType;
7 import org.apache.xmlbeans.XmlException;
8 import org.apache.xmlbeans.XmlObject;
9 import org.apache.xmlbeans.XmlOptions;
10
11 import com.tc.config.schema.MockSchemaType;
12 import com.tc.config.schema.MockXmlObject;
13 import com.tc.config.schema.listen.MockConfigurationChangeListener;
14 import com.tc.config.schema.validate.MockConfigurationValidator;
15 import com.tc.test.TCTestCase;
16 import com.tc.util.TCAssertionError;
17
18 /**
19  * Unit test for {@link StandardBeanRepository}.
20  */

21 public class StandardBeanRepositoryTest extends TCTestCase {
22
23   private static class MyXmlObject extends MockXmlObject {
24     // DO NOT REMOVE THIS TO FIX THE WARNING. We have to have public static final 'type' fields on both MockXmlObject
25
// and MyXmlObject, because that's the way XMLBeans does it; various classes use reflection to find this, and so
26
// you'll break tests if you change it. If you know of a way to simply get Eclipse to ignore the warning,
27
// please, by all means, do so.
28
public static final SchemaType type = new MockSchemaType();
29
30     private boolean returnedValidate;
31
32     public MyXmlObject() {
33       super();
34       this.returnedValidate = true;
35     }
36
37     public void setReturnedValidate(boolean validate) {
38       this.returnedValidate = validate;
39     }
40
41     public boolean validate(XmlOptions arg0) {
42       return this.returnedValidate;
43     }
44   }
45
46   private StandardBeanRepository repository;
47
48   private MockConfigurationChangeListener listener1;
49   private MockConfigurationChangeListener listener2;
50
51   private MockConfigurationValidator validator1;
52   private MockConfigurationValidator validator2;
53
54   public void setUp() throws Exception JavaDoc {
55     this.repository = new StandardBeanRepository(MyXmlObject.class);
56
57     this.listener1 = new MockConfigurationChangeListener();
58     this.listener2 = new MockConfigurationChangeListener();
59
60     this.validator1 = new MockConfigurationValidator();
61     this.validator2 = new MockConfigurationValidator();
62   }
63
64   public void testConstruction() throws Exception JavaDoc {
65     try {
66       new StandardBeanRepository(null);
67       fail("Didn't get NPE on no required class");
68     } catch (NullPointerException JavaDoc npe) {
69       // ok
70
}
71   }
72
73   public void testComponents() throws Exception JavaDoc {
74     this.repository.ensureBeanIsOfClass(MyXmlObject.class);
75     this.repository.ensureBeanIsOfClass(MockXmlObject.class);
76     this.repository.ensureBeanIsOfClass(Object JavaDoc.class);
77
78     try {
79       this.repository.ensureBeanIsOfClass(String JavaDoc.class);
80       fail("Didn't get TCAE on wrong bean class");
81     } catch (TCAssertionError tcae) {
82       // ok
83
}
84   }
85
86   public void testAll() throws Exception JavaDoc {
87     MyXmlObject bean1 = new MyXmlObject();
88     MyXmlObject bean2 = new MyXmlObject();
89
90     try {
91       this.repository.setBean(bean1, null);
92       fail("Didn't get NPE on no source");
93     } catch (NullPointerException JavaDoc npe) {
94       // ok
95
}
96
97     try {
98       this.repository.setBean(bean1, "");
99       fail("Didn't get IAE on empty source");
100     } catch (IllegalArgumentException JavaDoc iae) {
101       // ok
102
}
103
104     try {
105       this.repository.setBean(bean1, " ");
106       fail("Didn't get IAE on blanksource");
107     } catch (IllegalArgumentException JavaDoc iae) {
108       // ok
109
}
110
111     try {
112       this.repository.setBean(new MockXmlObject(), "foobar");
113       fail("Didn't get TCAE on wrong class");
114     } catch (TCAssertionError tcae) {
115       // ok
116
}
117
118     try {
119       this.repository.addListener(null);
120       fail("Didn't get NPE on no listener");
121     } catch (NullPointerException JavaDoc npe) {
122       // ok
123
}
124
125     try {
126       this.repository.addValidator(null);
127       fail("Didn't get NPE on no validator");
128     } catch (NullPointerException JavaDoc npe) {
129       // ok
130
}
131
132     this.repository.setBean(bean1, "foobar");
133     assertSame(bean1, this.repository.bean());
134
135     this.repository = new StandardBeanRepository(MyXmlObject.class);
136
137     this.repository.addListener(listener1);
138     this.repository.addListener(listener2);
139     this.repository.addValidator(validator1);
140     this.repository.addValidator(validator2);
141
142     checkNoListeners();
143
144     this.repository.setBean(bean1, "foobar");
145     checkListeners(null, bean1);
146
147     this.repository.setBean(bean2, "baz");
148     checkListeners(bean1, bean2);
149
150     this.repository.setBean(null, "bonk");
151     checkListeners(bean2, null);
152
153     this.repository.setBean(bean2, "bonk");
154     checkListeners(null, bean2);
155
156     bean1.setReturnedValidate(false);
157     try {
158       this.repository.setBean(bean1, "foo");
159       fail("Didn't get XmlException on failed schema validation");
160     } catch (XmlException xmle) {
161       // ok
162
}
163     assertSame(bean2, this.repository.bean());
164     checkNoListeners();
165
166     bean1.setReturnedValidate(true);
167     validator1.setThrownException(new XmlException("fooBAR"));
168     try {
169       this.repository.setBean(bean1, "quux");
170       fail("Didn't get XmlException on failed validator validation");
171     } catch (XmlException xmle) {
172       assertContains("fooBAR", xmle.getMessage());
173     }
174     assertSame(bean2, this.repository.bean());
175     checkNoListeners();
176
177     validator1.setThrownException(null);
178     validator2.setThrownException(null);
179
180     this.repository.setBean(bean1, "Whatever");
181     assertSame(bean1, this.repository.bean());
182     checkListeners(bean2, bean1);
183   }
184
185   private void checkNoListeners() {
186     assertEquals(0, this.listener1.getNumConfigurationChangeds());
187     assertEquals(0, this.listener2.getNumConfigurationChangeds());
188   }
189
190   private void checkListeners(XmlObject expectedOld, XmlObject expectedNew) {
191     assertEquals(1, this.listener1.getNumConfigurationChangeds());
192     assertSame(expectedOld, this.listener1.getLastOldConfig());
193     assertSame(expectedNew, this.listener1.getLastNewConfig());
194
195     assertEquals(1, this.listener2.getNumConfigurationChangeds());
196     assertSame(expectedOld, this.listener2.getLastOldConfig());
197     assertSame(expectedNew, this.listener2.getLastNewConfig());
198
199     this.listener1.reset();
200     this.listener2.reset();
201   }
202
203 }
204
Popular Tags