001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time.convert;
017:
018: import java.lang.reflect.Constructor;
019: import java.lang.reflect.Modifier;
020: import java.util.Calendar;
021: import java.util.GregorianCalendar;
022:
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.joda.time.DateMidnight;
027: import org.joda.time.DateTime;
028: import org.joda.time.ReadWritableDateTime;
029: import org.joda.time.ReadWritableInstant;
030: import org.joda.time.ReadableDateTime;
031: import org.joda.time.ReadableInstant;
032:
033: /**
034: * This class is a JUnit test for ConverterSet.
035: * Mostly for coverage.
036: *
037: * @author Stephen Colebourne
038: */
039: public class TestConverterSet extends TestCase {
040:
041: private static final Converter c1 = new Converter() {
042: public Class getSupportedType() {
043: return Boolean.class;
044: }
045: };
046: private static final Converter c2 = new Converter() {
047: public Class getSupportedType() {
048: return Character.class;
049: }
050: };
051: private static final Converter c3 = new Converter() {
052: public Class getSupportedType() {
053: return Byte.class;
054: }
055: };
056: private static final Converter c4 = new Converter() {
057: public Class getSupportedType() {
058: return Short.class;
059: }
060: };
061: private static final Converter c4a = new Converter() {
062: public Class getSupportedType() {
063: return Short.class;
064: }
065: };
066: private static final Converter c5 = new Converter() {
067: public Class getSupportedType() {
068: return Integer.class;
069: }
070: };
071:
072: public static void main(String[] args) {
073: junit.textui.TestRunner.run(suite());
074: }
075:
076: public static TestSuite suite() {
077: return new TestSuite(TestConverterSet.class);
078: }
079:
080: public TestConverterSet(String name) {
081: super (name);
082: }
083:
084: //-----------------------------------------------------------------------
085: public void testClass() throws Exception {
086: Class cls = ConverterSet.class;
087: assertEquals(false, Modifier.isPublic(cls.getModifiers()));
088: assertEquals(false, Modifier.isProtected(cls.getModifiers()));
089: assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
090:
091: assertEquals(1, cls.getDeclaredConstructors().length);
092: Constructor con = cls.getDeclaredConstructors()[0];
093: assertEquals(false, Modifier.isPublic(con.getModifiers()));
094: assertEquals(false, Modifier.isProtected(con.getModifiers()));
095: assertEquals(false, Modifier.isPrivate(con.getModifiers()));
096: }
097:
098: //-----------------------------------------------------------------------
099: public void testBigHashtable() {
100: Converter[] array = new Converter[] { c1, c2, c3, c4, };
101: ConverterSet set = new ConverterSet(array);
102: set.select(Boolean.class);
103: set.select(Character.class);
104: set.select(Byte.class);
105: set.select(Short.class);
106: set.select(Integer.class);
107: set.select(Long.class);
108: set.select(Float.class);
109: set.select(Double.class);
110: set.select(null);
111: set.select(Calendar.class);
112: set.select(GregorianCalendar.class);
113: set.select(DateTime.class);
114: set.select(DateMidnight.class);
115: set.select(ReadableInstant.class);
116: set.select(ReadableDateTime.class);
117: set.select(ReadWritableInstant.class); // 16
118: set.select(ReadWritableDateTime.class);
119: set.select(DateTime.class);
120: assertEquals(4, set.size());
121: }
122:
123: //-----------------------------------------------------------------------
124: public void testAddNullRemoved1() {
125: Converter[] array = new Converter[] { c1, c2, c3, c4, };
126: ConverterSet set = new ConverterSet(array);
127: ConverterSet result = set.add(c5, null);
128: assertEquals(4, set.size());
129: assertEquals(5, result.size());
130: }
131:
132: public void testAddNullRemoved2() {
133: Converter[] array = new Converter[] { c1, c2, c3, c4, };
134: ConverterSet set = new ConverterSet(array);
135: ConverterSet result = set.add(c4, null);
136: assertSame(set, result);
137: }
138:
139: public void testAddNullRemoved3() {
140: Converter[] array = new Converter[] { c1, c2, c3, c4, };
141: ConverterSet set = new ConverterSet(array);
142: ConverterSet result = set.add(c4a, null);
143: assertTrue(set != result);
144: assertEquals(4, set.size());
145: assertEquals(4, result.size());
146: }
147:
148: //-----------------------------------------------------------------------
149: public void testRemoveNullRemoved1() {
150: Converter[] array = new Converter[] { c1, c2, c3, c4, };
151: ConverterSet set = new ConverterSet(array);
152: ConverterSet result = set.remove(c3, null);
153: assertEquals(4, set.size());
154: assertEquals(3, result.size());
155: }
156:
157: public void testRemoveNullRemoved2() {
158: Converter[] array = new Converter[] { c1, c2, c3, c4, };
159: ConverterSet set = new ConverterSet(array);
160: ConverterSet result = set.remove(c5, null);
161: assertSame(set, result);
162: }
163:
164: //-----------------------------------------------------------------------
165: public void testRemoveBadIndex1() {
166: Converter[] array = new Converter[] { c1, c2, c3, c4, };
167: ConverterSet set = new ConverterSet(array);
168: try {
169: set.remove(200, null);
170: fail();
171: } catch (IndexOutOfBoundsException ex) {
172: }
173: assertEquals(4, set.size());
174: }
175:
176: public void testRemoveBadIndex2() {
177: Converter[] array = new Converter[] { c1, c2, c3, c4, };
178: ConverterSet set = new ConverterSet(array);
179: try {
180: set.remove(-1, null);
181: fail();
182: } catch (IndexOutOfBoundsException ex) {
183: }
184: assertEquals(4, set.size());
185: }
186:
187: }
|