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.dynamic;
005:
006: import com.tc.config.schema.MockXmlObject;
007: import com.tc.util.TCAssertionError;
008:
009: import java.math.BigInteger;
010:
011: public class IntXPathBasedConfigItemTest extends
012: XPathBasedConfigItemTestBase {
013:
014: private class SubBean extends MockXmlObject {
015: public BigInteger getBigIntegerValue() {
016: return currentValue;
017: }
018: }
019:
020: private static class DefaultBean extends MockXmlObject {
021: private final BigInteger value;
022:
023: public DefaultBean(BigInteger value) {
024: this .value = value;
025: }
026:
027: public BigInteger getBigIntegerValue() {
028: return this .value;
029: }
030: }
031:
032: private BigInteger currentValue;
033:
034: protected MockXmlObject createSubBean() throws Exception {
035: return new SubBean();
036: }
037:
038: public void setUp() throws Exception {
039: super .setUp();
040:
041: this .currentValue = new BigInteger("147");
042: context.setReturnedHasDefaultFor(false);
043: context.setReturnedIsOptional(false);
044: }
045:
046: public void testConstruction() throws Exception {
047: context.setReturnedIsOptional(true);
048:
049: try {
050: new IntXPathBasedConfigItem(context, xpath);
051: fail("Didn't get TCAE on item that's optional with no default");
052: } catch (TCAssertionError tcae) {
053: // ok
054: }
055: }
056:
057: public void testNoDefault() throws Exception {
058: IntXPathBasedConfigItem item = new IntXPathBasedConfigItem(
059: context, xpath);
060:
061: assertEquals(147, item.getInt());
062: assertEquals(new Integer(147), item.getObject());
063:
064: this .currentValue = new BigInteger("-123854");
065:
066: item = new IntXPathBasedConfigItem(context, xpath);
067: assertEquals(-123854, item.getInt());
068: assertEquals(new Integer(-123854), item.getObject());
069:
070: this .currentValue = new BigInteger(
071: "43289058203953495739457489020092370897586768975042532");
072: item = new IntXPathBasedConfigItem(context, xpath);
073:
074: try {
075: item.getInt();
076: fail("Didn't get TCAE on too-big value");
077: } catch (TCAssertionError tcae) {
078: // ok
079: }
080:
081: try {
082: item.getObject();
083: fail("Didn't get TCAE on too-big value");
084: } catch (TCAssertionError tcae) {
085: // ok
086: }
087:
088: this .currentValue = new BigInteger(
089: "-43289058203953495739457489020092370897586768975042532");
090: item = new IntXPathBasedConfigItem(context, xpath);
091:
092: try {
093: item.getInt();
094: fail("Didn't get TCAE on too-small value");
095: } catch (TCAssertionError tcae) {
096: // ok
097: }
098:
099: try {
100: item.getObject();
101: fail("Didn't get TCAE on too-small value");
102: } catch (TCAssertionError tcae) {
103: // ok
104: }
105: }
106:
107: public void testDefault() throws Exception {
108: context.setReturnedHasDefaultFor(true);
109: context.setReturnedDefaultFor(new DefaultBean(new BigInteger(
110: "42")));
111:
112: this .currentValue = null;
113: IntXPathBasedConfigItem item = new IntXPathBasedConfigItem(
114: context, xpath);
115:
116: assertEquals(42, item.getInt());
117: assertEquals(new Integer(42), item.getObject());
118:
119: this .currentValue = new BigInteger("483290");
120: item = new IntXPathBasedConfigItem(context, xpath);
121:
122: assertEquals(483290, item.getInt());
123: assertEquals(new Integer(483290), item.getObject());
124: }
125:
126: public void testTooBigDefault() throws Exception {
127: context.setReturnedHasDefaultFor(true);
128: context.setReturnedDefaultFor(new DefaultBean(new BigInteger(
129: "424328904823905829058025739572230498074078")));
130:
131: try {
132: new IntXPathBasedConfigItem(context, xpath).defaultValue();
133: fail("Didn't get TCAE on too-big default");
134: } catch (TCAssertionError tcae) {
135: // ok
136: }
137:
138: context.setReturnedDefaultFor(new DefaultBean(new BigInteger(
139: "-424328904823905829058025739572230498074078")));
140:
141: try {
142: new IntXPathBasedConfigItem(context, xpath).defaultValue();
143: fail("Didn't get TCAE on too-small default");
144: } catch (TCAssertionError tcae) {
145: // ok
146: }
147: }
148:
149: }
|