1 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 17 public class FromSchemaDefaultValueProviderTest extends TCTestCase { 18 19 private FromSchemaDefaultValueProvider provider; 20 21 public void setUp() throws Exception { 22 this.provider = new FromSchemaDefaultValueProvider(); 23 } 24 25 private void checkHasDefault(String xpath) throws Exception { 26 assertTrue(this.provider.possibleForXPathToHaveDefault(xpath)); 27 assertTrue(this.provider.hasDefault(TestRoot.type, xpath)); 28 } 29 30 public void testDefaultFor() throws Exception { 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 { 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 } 62 } 63 64 public void testPathIntoNowhere() throws Exception { 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 } 71 } 72 73 public void testNonexistentAttribute() throws Exception { 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 } 80 } 81 82 public void testAttributeOfElementWithoutAttributes() throws Exception { 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 } 89 } 90 91 public void testAttributePathIntoNowhere() throws Exception { 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 } 98 } 99 100 public void testIncorrectPath() throws Exception { 101 try { 102 this.provider.defaultFor(TestRoot.type, "element/foo"); 103 fail("Didn't get XMLE on incorrect path"); 104 } catch (XmlException xmle) { 105 } 107 } 108 109 public void testPossibleXPaths() throws Exception { 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 xpath) throws Exception { 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 } 130 } 131 132 public void testNoDefault() throws Exception { 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 { 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 } 147 } 148 149 } 150 | Popular Tags |