001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.xjc.dv;
019:
020: import java.math.BigDecimal;
021: import java.math.BigInteger;
022:
023: import javax.xml.bind.DatatypeConverter;
024: import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
025: import javax.xml.namespace.QName;
026:
027: import org.apache.cxf.configuration.foo.Foo;
028: import org.apache.ws.jaxme.impl.DatatypeConverterImpl;
029: import org.junit.Assert;
030: import org.junit.Test;
031:
032: public class DefaultValueTest extends Assert {
033:
034: @Test
035: public void testFooDefaultValues() throws Exception {
036:
037: DatatypeConverter
038: .setDatatypeConverter(new DatatypeConverterImpl());
039:
040: Foo foo = new org.apache.cxf.configuration.foo.ObjectFactory()
041: .createFoo();
042:
043: // verify default values
044:
045: assertAttributeValuesWithoutDefault(foo);
046: assertDefaultAttributeValues(foo);
047: assertDefaultElementValues(foo);
048: }
049:
050: private void assertDefaultAttributeValues(Foo foo) {
051: assertEquals("Unexpected value for attribute stringAttr",
052: "hello", foo.getStringAttr());
053: assertTrue("Unexpected value for attribute booleanAttr", foo
054: .isBooleanAttr());
055: assertEquals("Unexpected value for attribute integerAttr",
056: new BigInteger("111"), foo.getIntegerAttr());
057: assertEquals("Unexpected value for attribute intAttr", 112, foo
058: .getIntAttr());
059: assertEquals("Unexpected value for attribute longAttr", 113L,
060: foo.getLongAttr());
061: assertEquals("Unexpected value for attribute shortAttr", 114,
062: foo.getShortAttr());
063: assertEquals("Unexpected value for attribute decimalAttr",
064: new BigDecimal("115"), foo.getDecimalAttr());
065: assertEquals("Unexpected value for attribute floatAttr",
066: new Float(116F), foo.getFloatAttr());
067: assertEquals("Unexpected value for attribute doubleAttr",
068: new Double(117D), foo.getDoubleAttr());
069: assertEquals("Unexpected value for attribute byteAttr", 118,
070: foo.getByteAttr());
071:
072: byte[] expected = DatatypeConverter.parseBase64Binary("wxyz");
073: byte[] effective = foo.getBase64BinaryAttr();
074:
075: assertEquals("Unexpected value for attribute base64BinaryAttr",
076: expected.length, effective.length);
077: for (int i = 0; i < expected.length; i++) {
078: assertEquals(
079: "Unexpected value for attribute base64BinaryAttr",
080: expected[i], effective[i]);
081: }
082:
083: expected = new HexBinaryAdapter().unmarshal("aaaa");
084: effective = foo.getHexBinaryAttr();
085: assertEquals("Unexpected value for attribute hexBinaryAttr",
086: expected.length, effective.length);
087: for (int i = 0; i < expected.length; i++) {
088: assertEquals(
089: "Unexpected value for attribute hexBinaryAttr",
090: expected[i], effective[i]);
091: }
092:
093: QName qn = foo.getQnameAttr();
094: assertEquals("Unexpected value for attribute qnameAttr",
095: "http://www.w3.org/2001/XMLSchema", qn
096: .getNamespaceURI());
097: assertEquals("Unexpected value for attribute qnameAttr",
098: "schema", qn.getLocalPart());
099:
100: assertEquals("Unexpected value for attribute unsignedIntAttr",
101: 119L, foo.getUnsignedIntAttr());
102: assertEquals(
103: "Unexpected value for attribute unsignedShortAttr",
104: 120, foo.getUnsignedShortAttr());
105: assertEquals("Unexpected value for attribute unsignedByteAttr",
106: 121, foo.getUnsignedByteAttr());
107:
108: assertEquals("Unexpected value for attribute durationAttr", 3,
109: foo.getDurationAttr().getSeconds());
110: assertEquals("Unexpected value for attribute durationAttr", 0,
111: foo.getDurationAttr().getHours());
112: }
113:
114: /**
115: * @param foo
116: */
117: private void assertAttributeValuesWithoutDefault(Foo foo) {
118: assertNull(
119: "Unexpected value for attribute stringAttrNoDefault",
120: foo.getStringAttrNoDefault());
121: assertNull(
122: "Unexpected value for attribute booleanAttrNoDefault",
123: foo.isBooleanAttrNoDefault());
124: assertNull(
125: "Unexpected value for attribute integerAttrNoDefault",
126: foo.getIntegerAttrNoDefault());
127: assertNull("Unexpected value for attribute intAttrNoDefault",
128: foo.getIntAttrNoDefault());
129: assertNull("Unexpected value for attribute longAttrNoDefault",
130: foo.getLongAttrNoDefault());
131: assertNull("Unexpected value for attribute shortAttrNoDefault",
132: foo.getShortAttrNoDefault());
133: assertNull(
134: "Unexpected value for attribute decimalAttrNoDefault",
135: foo.getDecimalAttrNoDefault());
136: assertNull("Unexpected value for attribute floatAttrNoDefault",
137: foo.getFloatAttrNoDefault());
138: assertNull(
139: "Unexpected value for attribute doubleAttrNoDefault",
140: foo.getDoubleAttrNoDefault());
141: assertNull("Unexpected value for attribute byteAttrNoDefault",
142: foo.getByteAttrNoDefault());
143:
144: assertNull(
145: "Unexpected value for attribute base64BinaryAttrNoDefault",
146: foo.getBase64BinaryAttrNoDefault());
147: assertNull(
148: "Unexpected value for attribute hexBinaryAttrNoDefault",
149: foo.getHexBinaryAttrNoDefault());
150:
151: assertNull("Unexpected value for attribute qnameAttrNoDefault",
152: foo.getQnameAttrNoDefault());
153:
154: assertNull(
155: "Unexpected value for attribute unsignedIntAttrNoDefault",
156: foo.getUnsignedIntAttrNoDefault());
157: assertNull(
158: "Unexpected value for attribute unsignedShortAttrNoDefault",
159: foo.getUnsignedShortAttrNoDefault());
160: assertNull(
161: "Unexpected value for attribute unsignedByteAttrNoDefault",
162: foo.getUnsignedByteAttrNoDefault());
163: assertNull(
164: "Unexpected value for attribute durationAttrNoDefault",
165: foo.getDurationAttrNoDefault());
166: }
167:
168: private void assertDefaultElementValues(Foo foo) {
169: assertEquals("Unexpected value for element stringElem",
170: "hello", foo.getStringElem());
171: assertTrue("Unexpected value for element booleanElem", foo
172: .isBooleanElem());
173: assertEquals("Unexpected value for element integerElem",
174: new BigInteger("11"), foo.getIntegerElem());
175: assertEquals("Unexpected value for element intElem",
176: new Integer(12), foo.getIntElem());
177: assertEquals("Unexpected value for element longElem", new Long(
178: 13L), foo.getLongElem());
179: assertEquals("Unexpected value for element shortElem",
180: new Short((short) 14), foo.getShortElem());
181: assertEquals("Unexpected value for element decimalElem",
182: new BigDecimal("15"), foo.getDecimalElem());
183: assertEquals("Unexpected value for element floatElem",
184: new Float(16F), foo.getFloatElem());
185: assertEquals("Unexpected value for element doubleElem",
186: new Double(17D), foo.getDoubleElem());
187: assertEquals("Unexpected value for element byteElem", new Byte(
188: (byte) 18), foo.getByteElem());
189:
190: byte[] expected = DatatypeConverter
191: .parseBase64Binary("abcdefgh");
192: byte[] effective = foo.getBase64BinaryElem();
193:
194: assertEquals("Unexpected value for element base64BinaryElem",
195: expected.length, effective.length);
196: for (int i = 0; i < expected.length; i++) {
197: assertEquals(
198: "Unexpected value for element base64BinaryElem",
199: expected[i], effective[i]);
200: }
201:
202: expected = new HexBinaryAdapter().unmarshal("ffff");
203: effective = foo.getHexBinaryElem();
204: assertEquals("Unexpected value for element hexBinaryElem",
205: expected.length, effective.length);
206: for (int i = 0; i < expected.length; i++) {
207: assertEquals("Unexpected value for element hexBinaryElem",
208: expected[i], effective[i]);
209: }
210:
211: QName qn = foo.getQnameElem();
212: assertEquals("Unexpected value for element qnameElem",
213: "http://www.w3.org/2001/XMLSchema", qn
214: .getNamespaceURI());
215: assertEquals("Unexpected value for element qnameElem",
216: "string", qn.getLocalPart());
217:
218: assertEquals("Unexpected value for element unsignedIntElem",
219: new Long(19L), foo.getUnsignedIntElem());
220: assertEquals("Unexpected value for element unsignedShortElem",
221: new Integer(20), foo.getUnsignedShortElem());
222: assertEquals("Unexpected value for element unsignedByteElem",
223: new Short((short) 21), foo.getUnsignedByteElem());
224:
225: assertEquals("Unexpected value for element durationElem", 0,
226: foo.getDurationElem().getSeconds());
227: assertEquals("Unexpected value for element durationElem", 3,
228: foo.getDurationElem().getHours());
229: }
230:
231: }
|