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.repository;
005:
006: import org.apache.xmlbeans.SchemaType;
007: import org.apache.xmlbeans.XmlException;
008: import org.apache.xmlbeans.XmlObject;
009: import org.apache.xmlbeans.XmlOptions;
010:
011: import com.tc.config.schema.MockSchemaType;
012: import com.tc.config.schema.MockXmlObject;
013: import com.tc.config.schema.listen.MockConfigurationChangeListener;
014: import com.tc.config.schema.validate.MockConfigurationValidator;
015: import com.tc.test.TCTestCase;
016: import com.tc.util.TCAssertionError;
017:
018: /**
019: * Unit test for {@link StandardBeanRepository}.
020: */
021: public class StandardBeanRepositoryTest extends TCTestCase {
022:
023: private static class MyXmlObject extends MockXmlObject {
024: // DO NOT REMOVE THIS TO FIX THE WARNING. We have to have public static final 'type' fields on both MockXmlObject
025: // and MyXmlObject, because that's the way XMLBeans does it; various classes use reflection to find this, and so
026: // you'll break tests if you change it. If you know of a way to simply get Eclipse to ignore the warning,
027: // please, by all means, do so.
028: public static final SchemaType type = new MockSchemaType();
029:
030: private boolean returnedValidate;
031:
032: public MyXmlObject() {
033: super ();
034: this .returnedValidate = true;
035: }
036:
037: public void setReturnedValidate(boolean validate) {
038: this .returnedValidate = validate;
039: }
040:
041: public boolean validate(XmlOptions arg0) {
042: return this .returnedValidate;
043: }
044: }
045:
046: private StandardBeanRepository repository;
047:
048: private MockConfigurationChangeListener listener1;
049: private MockConfigurationChangeListener listener2;
050:
051: private MockConfigurationValidator validator1;
052: private MockConfigurationValidator validator2;
053:
054: public void setUp() throws Exception {
055: this .repository = new StandardBeanRepository(MyXmlObject.class);
056:
057: this .listener1 = new MockConfigurationChangeListener();
058: this .listener2 = new MockConfigurationChangeListener();
059:
060: this .validator1 = new MockConfigurationValidator();
061: this .validator2 = new MockConfigurationValidator();
062: }
063:
064: public void testConstruction() throws Exception {
065: try {
066: new StandardBeanRepository(null);
067: fail("Didn't get NPE on no required class");
068: } catch (NullPointerException npe) {
069: // ok
070: }
071: }
072:
073: public void testComponents() throws Exception {
074: this .repository.ensureBeanIsOfClass(MyXmlObject.class);
075: this .repository.ensureBeanIsOfClass(MockXmlObject.class);
076: this .repository.ensureBeanIsOfClass(Object.class);
077:
078: try {
079: this .repository.ensureBeanIsOfClass(String.class);
080: fail("Didn't get TCAE on wrong bean class");
081: } catch (TCAssertionError tcae) {
082: // ok
083: }
084: }
085:
086: public void testAll() throws Exception {
087: MyXmlObject bean1 = new MyXmlObject();
088: MyXmlObject bean2 = new MyXmlObject();
089:
090: try {
091: this .repository.setBean(bean1, null);
092: fail("Didn't get NPE on no source");
093: } catch (NullPointerException npe) {
094: // ok
095: }
096:
097: try {
098: this .repository.setBean(bean1, "");
099: fail("Didn't get IAE on empty source");
100: } catch (IllegalArgumentException iae) {
101: // ok
102: }
103:
104: try {
105: this .repository.setBean(bean1, " ");
106: fail("Didn't get IAE on blanksource");
107: } catch (IllegalArgumentException 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 npe) {
122: // ok
123: }
124:
125: try {
126: this .repository.addValidator(null);
127: fail("Didn't get NPE on no validator");
128: } catch (NullPointerException 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,
191: XmlObject expectedNew) {
192: assertEquals(1, this .listener1.getNumConfigurationChangeds());
193: assertSame(expectedOld, this .listener1.getLastOldConfig());
194: assertSame(expectedNew, this .listener1.getLastNewConfig());
195:
196: assertEquals(1, this.listener2.getNumConfigurationChangeds());
197: assertSame(expectedOld, this.listener2.getLastOldConfig());
198: assertSame(expectedNew, this.listener2.getLastNewConfig());
199:
200: this.listener1.reset();
201: this.listener2.reset();
202: }
203:
204: }
|