View Javadoc

1   /*
2    *  Copyright 2001-2005 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.time.convert;
17  
18  import java.lang.reflect.Constructor;
19  import java.lang.reflect.Modifier;
20  import java.util.Calendar;
21  import java.util.GregorianCalendar;
22  
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  import org.joda.time.DateMidnight;
27  import org.joda.time.DateTime;
28  import org.joda.time.ReadWritableDateTime;
29  import org.joda.time.ReadWritableInstant;
30  import org.joda.time.ReadableDateTime;
31  import org.joda.time.ReadableInstant;
32  
33  /**
34   * This class is a JUnit test for ConverterSet.
35   * Mostly for coverage.
36   *
37   * @author Stephen Colebourne
38   */
39  public class TestConverterSet extends TestCase {
40  
41      private static final Converter c1 = new Converter() {
42          public Class getSupportedType() {return Boolean.class;}
43      };
44      private static final Converter c2 = new Converter() {
45          public Class getSupportedType() {return Character.class;}
46      };
47      private static final Converter c3 = new Converter() {
48          public Class getSupportedType() {return Byte.class;}
49      };
50      private static final Converter c4 = new Converter() {
51          public Class getSupportedType() {return Short.class;}
52      };
53      private static final Converter c4a = new Converter() {
54          public Class getSupportedType() {return Short.class;}
55      };
56      private static final Converter c5 = new Converter() {
57          public Class getSupportedType() {return Integer.class;}
58      };
59      
60      public static void main(String[] args) {
61          junit.textui.TestRunner.run(suite());
62      }
63  
64      public static TestSuite suite() {
65          return new TestSuite(TestConverterSet.class);
66      }
67  
68      public TestConverterSet(String name) {
69          super(name);
70      }
71  
72      //-----------------------------------------------------------------------
73      public void testClass() throws Exception {
74          Class cls = ConverterSet.class;
75          assertEquals(false, Modifier.isPublic(cls.getModifiers()));
76          assertEquals(false, Modifier.isProtected(cls.getModifiers()));
77          assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
78          
79          assertEquals(1, cls.getDeclaredConstructors().length);
80          Constructor con = cls.getDeclaredConstructors()[0];
81          assertEquals(false, Modifier.isPublic(con.getModifiers()));
82          assertEquals(false, Modifier.isProtected(con.getModifiers()));
83          assertEquals(false, Modifier.isPrivate(con.getModifiers()));
84      }
85  
86      //-----------------------------------------------------------------------
87      public void testBigHashtable() {
88          Converter[] array = new Converter[] {
89              c1, c2, c3, c4,
90          };
91          ConverterSet set = new ConverterSet(array);
92          set.select(Boolean.class);
93          set.select(Character.class);
94          set.select(Byte.class);
95          set.select(Short.class);
96          set.select(Integer.class);
97          set.select(Long.class);
98          set.select(Float.class);
99          set.select(Double.class);
100         set.select(null);
101         set.select(Calendar.class);
102         set.select(GregorianCalendar.class);
103         set.select(DateTime.class);
104         set.select(DateMidnight.class);
105         set.select(ReadableInstant.class);
106         set.select(ReadableDateTime.class);
107         set.select(ReadWritableInstant.class);  // 16
108         set.select(ReadWritableDateTime.class);
109         set.select(DateTime.class);
110         assertEquals(4, set.size());
111     }
112 
113     //-----------------------------------------------------------------------
114     public void testAddNullRemoved1() {
115         Converter[] array = new Converter[] {
116             c1, c2, c3, c4,
117         };
118         ConverterSet set = new ConverterSet(array);
119         ConverterSet result = set.add(c5, null);
120         assertEquals(4, set.size());
121         assertEquals(5, result.size());
122     }
123 
124     public void testAddNullRemoved2() {
125         Converter[] array = new Converter[] {
126             c1, c2, c3, c4,
127         };
128         ConverterSet set = new ConverterSet(array);
129         ConverterSet result = set.add(c4, null);
130         assertSame(set, result);
131     }
132 
133     public void testAddNullRemoved3() {
134         Converter[] array = new Converter[] {
135             c1, c2, c3, c4,
136         };
137         ConverterSet set = new ConverterSet(array);
138         ConverterSet result = set.add(c4a, null);
139         assertTrue(set != result);
140         assertEquals(4, set.size());
141         assertEquals(4, result.size());
142     }
143 
144     //-----------------------------------------------------------------------
145     public void testRemoveNullRemoved1() {
146         Converter[] array = new Converter[] {
147             c1, c2, c3, c4,
148         };
149         ConverterSet set = new ConverterSet(array);
150         ConverterSet result = set.remove(c3, null);
151         assertEquals(4, set.size());
152         assertEquals(3, result.size());
153     }
154 
155     public void testRemoveNullRemoved2() {
156         Converter[] array = new Converter[] {
157             c1, c2, c3, c4,
158         };
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[] {
167             c1, c2, c3, c4,
168         };
169         ConverterSet set = new ConverterSet(array);
170         try {
171             set.remove(200, null);
172             fail();
173         } catch (IndexOutOfBoundsException ex) {}
174         assertEquals(4, set.size());
175     }
176 
177     public void testRemoveBadIndex2() {
178         Converter[] array = new Converter[] {
179             c1, c2, c3, c4,
180         };
181         ConverterSet set = new ConverterSet(array);
182         try {
183             set.remove(-1, null);
184             fail();
185         } catch (IndexOutOfBoundsException ex) {}
186         assertEquals(4, set.size());
187     }
188 
189 }