KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > defaults > FromSchemaDefaultValueProviderTest


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.defaults;
5
6 import org.apache.xmlbeans.XmlBoolean;
7 import org.apache.xmlbeans.XmlException;
8 import org.apache.xmlbeans.XmlInteger;
9 import org.apache.xmlbeans.XmlString;
10
11 import com.tc.test.TCTestCase;
12 import com.terracottatech.configTest.TestRootDocument.TestRoot;
13
14 /**
15  * Unit test for {@link FromSchemaDefaultValueProvider}.
16  */

17 public class FromSchemaDefaultValueProviderTest extends TCTestCase {
18
19   private FromSchemaDefaultValueProvider provider;
20
21   public void setUp() throws Exception JavaDoc {
22     this.provider = new FromSchemaDefaultValueProvider();
23   }
24
25   private void checkHasDefault(String JavaDoc xpath) throws Exception JavaDoc {
26     assertTrue(this.provider.possibleForXPathToHaveDefault(xpath));
27     assertTrue(this.provider.hasDefault(TestRoot.type, xpath));
28   }
29
30   public void testDefaultFor() throws Exception JavaDoc {
31     checkHasDefault("element/inner-3");
32     assertEquals(19235, ((XmlInteger) this.provider.defaultFor(TestRoot.type, "element/inner-3")).getBigIntegerValue()
33         .intValue());
34
35     checkHasDefault("element/inner-4/complex-2");
36     assertEquals(423456, ((XmlInteger) this.provider.defaultFor(TestRoot.type, "element/inner-4/complex-2"))
37         .getBigIntegerValue().intValue());
38
39     checkHasDefault("element/inner-4/complex-4");
40     assertEquals(true, ((XmlBoolean) this.provider.defaultFor(TestRoot.type, "element/inner-4/complex-4"))
41         .getBooleanValue());
42
43     checkHasDefault("element/inner-4/complex-5");
44     assertEquals("FUNKiness", ((XmlString) this.provider.defaultFor(TestRoot.type, "element/inner-4/complex-5"))
45         .getStringValue());
46
47     checkHasDefault("element/@attr1");
48     assertEquals("funk", ((XmlString) this.provider.defaultFor(TestRoot.type, "element/@attr1")).getStringValue());
49
50     checkHasDefault("element/@attr2");
51     assertEquals(1795, ((XmlInteger) this.provider.defaultFor(TestRoot.type, "element/@attr2")).getBigIntegerValue()
52         .intValue());
53   }
54
55   public void testInvalidXPath() throws Exception JavaDoc {
56     try {
57       this.provider.defaultFor(TestRoot.type, "element/inner-4/--foo");
58       fail("Didn't get XMLE on attempt at attribute");
59     } catch (XmlException xmle) {
60       // ok
61
}
62   }
63
64   public void testPathIntoNowhere() throws Exception JavaDoc {
65     try {
66       this.provider.defaultFor(TestRoot.type, "element/inner-4/complex-2/foo");
67       fail("Didn't get XMLE on path into nowhere");
68     } catch (XmlException xmle) {
69       // ok
70
}
71   }
72
73   public void testNonexistentAttribute() throws Exception JavaDoc {
74     try {
75       this.provider.defaultFor(TestRoot.type, "element/@attrnonexistent");
76       fail("Didn't get XMLE on path into nowhere");
77     } catch (XmlException xmle) {
78       // ok
79
}
80   }
81
82   public void testAttributeOfElementWithoutAttributes() throws Exception JavaDoc {
83     try {
84       this.provider.defaultFor(TestRoot.type, "element/inner-4/@attr");
85       fail("Didn't get XMLE on path into nowhere");
86     } catch (XmlException xmle) {
87       // ok
88
}
89   }
90
91   public void testAttributePathIntoNowhere() throws Exception JavaDoc {
92     try {
93       this.provider.defaultFor(TestRoot.type, "element/inner-4/complex-2/@foo");
94       fail("Didn't get XMLE on path into nowhere");
95     } catch (XmlException xmle) {
96       // ok
97
}
98   }
99
100   public void testIncorrectPath() throws Exception JavaDoc {
101     try {
102       this.provider.defaultFor(TestRoot.type, "element/foo");
103       fail("Didn't get XMLE on incorrect path");
104     } catch (XmlException xmle) {
105       // ok
106
}
107   }
108
109   public void testPossibleXPaths() throws Exception JavaDoc {
110     assertTrue(this.provider.possibleForXPathToHaveDefault("foo"));
111     assertTrue(this.provider.possibleForXPathToHaveDefault("foo/bar"));
112     assertTrue(this.provider.possibleForXPathToHaveDefault("foo/bar/baz"));
113
114     assertFalse(this.provider.possibleForXPathToHaveDefault("foo/-bar"));
115     assertFalse(this.provider.possibleForXPathToHaveDefault("/foo/bar"));
116     assertFalse(this.provider.possibleForXPathToHaveDefault("foo/b*ar"));
117     assertFalse(this.provider.possibleForXPathToHaveDefault("foo/*"));
118     assertFalse(this.provider.possibleForXPathToHaveDefault("foo/bar/../baz"));
119   }
120
121   private void checkNoDefault(String JavaDoc xpath) throws Exception JavaDoc {
122     assertTrue(this.provider.possibleForXPathToHaveDefault(xpath));
123     assertFalse(this.provider.hasDefault(TestRoot.type, xpath));
124     try {
125       this.provider.defaultFor(TestRoot.type, xpath);
126       fail("Didn't get XMLE on element with no default ('" + xpath + "')");
127     } catch (XmlException xmle) {
128       // ok
129
}
130   }
131
132   public void testNoDefault() throws Exception JavaDoc {
133     checkNoDefault("element/inner-1");
134     checkNoDefault("element/inner-4/complex-1");
135     checkNoDefault("element/inner-4/complex-3");
136     checkNoDefault("element/@attr3");
137     checkNoDefault("element/@attr4");
138   }
139
140   public void testComplexType() throws Exception JavaDoc {
141     try {
142       this.provider.defaultFor(TestRoot.type, "element/inner-4");
143       fail("Didn't get XMLE on complex-typed element");
144     } catch (XmlException xmle) {
145       // ok
146
}
147   }
148
149 }
150
Popular Tags