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.defaults;
005:
006: import org.apache.xmlbeans.XmlBoolean;
007: import org.apache.xmlbeans.XmlException;
008: import org.apache.xmlbeans.XmlInteger;
009: import org.apache.xmlbeans.XmlString;
010:
011: import com.tc.test.TCTestCase;
012: import com.terracottatech.configTest.TestRootDocument.TestRoot;
013:
014: /**
015: * Unit test for {@link FromSchemaDefaultValueProvider}.
016: */
017: public class FromSchemaDefaultValueProviderTest extends TCTestCase {
018:
019: private FromSchemaDefaultValueProvider provider;
020:
021: public void setUp() throws Exception {
022: this .provider = new FromSchemaDefaultValueProvider();
023: }
024:
025: private void checkHasDefault(String xpath) throws Exception {
026: assertTrue(this .provider.possibleForXPathToHaveDefault(xpath));
027: assertTrue(this .provider.hasDefault(TestRoot.type, xpath));
028: }
029:
030: public void testDefaultFor() throws Exception {
031: checkHasDefault("element/inner-3");
032: assertEquals(19235, ((XmlInteger) this .provider.defaultFor(
033: TestRoot.type, "element/inner-3")).getBigIntegerValue()
034: .intValue());
035:
036: checkHasDefault("element/inner-4/complex-2");
037: assertEquals(423456, ((XmlInteger) this .provider.defaultFor(
038: TestRoot.type, "element/inner-4/complex-2"))
039: .getBigIntegerValue().intValue());
040:
041: checkHasDefault("element/inner-4/complex-4");
042: assertEquals(true, ((XmlBoolean) this .provider.defaultFor(
043: TestRoot.type, "element/inner-4/complex-4"))
044: .getBooleanValue());
045:
046: checkHasDefault("element/inner-4/complex-5");
047: assertEquals("FUNKiness",
048: ((XmlString) this .provider.defaultFor(TestRoot.type,
049: "element/inner-4/complex-5")).getStringValue());
050:
051: checkHasDefault("element/@attr1");
052: assertEquals("funk", ((XmlString) this .provider.defaultFor(
053: TestRoot.type, "element/@attr1")).getStringValue());
054:
055: checkHasDefault("element/@attr2");
056: assertEquals(1795, ((XmlInteger) this .provider.defaultFor(
057: TestRoot.type, "element/@attr2")).getBigIntegerValue()
058: .intValue());
059: }
060:
061: public void testInvalidXPath() throws Exception {
062: try {
063: this .provider.defaultFor(TestRoot.type,
064: "element/inner-4/--foo");
065: fail("Didn't get XMLE on attempt at attribute");
066: } catch (XmlException xmle) {
067: // ok
068: }
069: }
070:
071: public void testPathIntoNowhere() throws Exception {
072: try {
073: this .provider.defaultFor(TestRoot.type,
074: "element/inner-4/complex-2/foo");
075: fail("Didn't get XMLE on path into nowhere");
076: } catch (XmlException xmle) {
077: // ok
078: }
079: }
080:
081: public void testNonexistentAttribute() throws Exception {
082: try {
083: this .provider.defaultFor(TestRoot.type,
084: "element/@attrnonexistent");
085: fail("Didn't get XMLE on path into nowhere");
086: } catch (XmlException xmle) {
087: // ok
088: }
089: }
090:
091: public void testAttributeOfElementWithoutAttributes()
092: throws Exception {
093: try {
094: this .provider.defaultFor(TestRoot.type,
095: "element/inner-4/@attr");
096: fail("Didn't get XMLE on path into nowhere");
097: } catch (XmlException xmle) {
098: // ok
099: }
100: }
101:
102: public void testAttributePathIntoNowhere() throws Exception {
103: try {
104: this .provider.defaultFor(TestRoot.type,
105: "element/inner-4/complex-2/@foo");
106: fail("Didn't get XMLE on path into nowhere");
107: } catch (XmlException xmle) {
108: // ok
109: }
110: }
111:
112: public void testIncorrectPath() throws Exception {
113: try {
114: this .provider.defaultFor(TestRoot.type, "element/foo");
115: fail("Didn't get XMLE on incorrect path");
116: } catch (XmlException xmle) {
117: // ok
118: }
119: }
120:
121: public void testPossibleXPaths() throws Exception {
122: assertTrue(this .provider.possibleForXPathToHaveDefault("foo"));
123: assertTrue(this .provider
124: .possibleForXPathToHaveDefault("foo/bar"));
125: assertTrue(this .provider
126: .possibleForXPathToHaveDefault("foo/bar/baz"));
127:
128: assertFalse(this .provider
129: .possibleForXPathToHaveDefault("foo/-bar"));
130: assertFalse(this .provider
131: .possibleForXPathToHaveDefault("/foo/bar"));
132: assertFalse(this .provider
133: .possibleForXPathToHaveDefault("foo/b*ar"));
134: assertFalse(this .provider
135: .possibleForXPathToHaveDefault("foo/*"));
136: assertFalse(this .provider
137: .possibleForXPathToHaveDefault("foo/bar/../baz"));
138: }
139:
140: private void checkNoDefault(String xpath) throws Exception {
141: assertTrue(this .provider.possibleForXPathToHaveDefault(xpath));
142: assertFalse(this .provider.hasDefault(TestRoot.type, xpath));
143: try {
144: this .provider.defaultFor(TestRoot.type, xpath);
145: fail("Didn't get XMLE on element with no default ('"
146: + xpath + "')");
147: } catch (XmlException xmle) {
148: // ok
149: }
150: }
151:
152: public void testNoDefault() throws Exception {
153: checkNoDefault("element/inner-1");
154: checkNoDefault("element/inner-4/complex-1");
155: checkNoDefault("element/inner-4/complex-3");
156: checkNoDefault("element/@attr3");
157: checkNoDefault("element/@attr4");
158: }
159:
160: public void testComplexType() throws Exception {
161: try {
162: this .provider.defaultFor(TestRoot.type, "element/inner-4");
163: fail("Didn't get XMLE on complex-typed element");
164: } catch (XmlException xmle) {
165: // ok
166: }
167: }
168:
169: }
|