01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.config.schema.beanfactory;
05:
06: import org.apache.xmlbeans.XmlException;
07:
08: import com.tc.config.schema.test.TerracottaConfigBuilder;
09: import com.tc.test.TCTestCase;
10: import com.terracottatech.config.TcConfigDocument;
11:
12: import java.io.ByteArrayInputStream;
13:
14: /**
15: * Unit test for {@link TerracottaDomainConfigurationDocumentBeanFactory}.
16: */
17: public class TerracottaDomainConfigurationDocumentBeanFactoryTest
18: extends TCTestCase {
19:
20: private TerracottaDomainConfigurationDocumentBeanFactory factory;
21:
22: public void setUp() throws Exception {
23: this .factory = new TerracottaDomainConfigurationDocumentBeanFactory();
24: }
25:
26: public void testNormal() throws Exception {
27: TerracottaConfigBuilder builder = TerracottaConfigBuilder
28: .newMinimalInstance();
29: builder.getClient().setLogs("foobar");
30: byte[] xml = builder.toString().getBytes();
31: ByteArrayInputStream stream = new ByteArrayInputStream(xml);
32:
33: BeanWithErrors beanWithErrors = this .factory.createBean(stream,
34: "from test");
35: assertEquals(0, beanWithErrors.errors().length);
36: assertEquals("foobar", ((TcConfigDocument) beanWithErrors
37: .bean()).getTcConfig().getClients().getLogs());
38: }
39:
40: public void testXmlMisparse() throws Exception {
41: TerracottaConfigBuilder builder = TerracottaConfigBuilder
42: .newMinimalInstance();
43: builder.getClient().setLogs("foo <funk>"); // an unclosed tag; the builder intentionally doesn't escape text
44: byte[] xml = builder.toString().getBytes();
45: ByteArrayInputStream stream = new ByteArrayInputStream(xml);
46:
47: try {
48: this .factory.createBean(stream, "from test");
49: fail("Didn't get XmlException on invalid XML");
50: } catch (XmlException xmle) {
51: // ok
52: }
53: }
54:
55: public void testSchemaViolation() throws Exception {
56: TerracottaConfigBuilder builder = TerracottaConfigBuilder
57: .newMinimalInstance();
58: builder.getSystem().setLicenseType("funkiness"); // invalid enumeration value
59: builder.getSystem().setLicenseLocation("foo");
60: byte[] xml = builder.toString().getBytes();
61: ByteArrayInputStream stream = new ByteArrayInputStream(xml);
62:
63: BeanWithErrors beanWithErrors = this .factory.createBean(stream,
64: "from test");
65: assertEquals(1, beanWithErrors.errors().length);
66: assertEquals("from test", beanWithErrors.errors()[0]
67: .getSourceName());
68: assertTrue(beanWithErrors.bean() instanceof TcConfigDocument);
69: }
70: }
|