View Javadoc

1   /*
2    *  Copyright 2001-2012 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.format;
17  
18  import java.util.Locale;
19  
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  import org.joda.time.Period;
24  
25  /**
26   * This class is a Junit unit test for PeriodFormat.
27   *
28   * @author Stephen Colebourne
29   */
30  public class TestPeriodFormat extends TestCase {
31  
32      private static final Locale EN = new Locale("en");
33      private static final Locale FR = new Locale("fr");
34      private static final Locale PT = new Locale("pt");
35      private static final Locale ES = new Locale("es");
36      private static final Locale DE = new Locale("de");
37      private static final Locale NL = new Locale("nl");
38      private static final Locale DA = new Locale("da");
39      private static final Locale JA = new Locale("ja");
40  
41      private Locale originalLocale = null;
42  
43      public static void main(String[] args) {
44          junit.textui.TestRunner.run(suite());
45      }
46  
47      public static TestSuite suite() {
48          return new TestSuite(TestPeriodFormat.class);
49      }
50  
51      public TestPeriodFormat(String name) {
52          super(name);
53      }
54  
55      protected void setUp() throws Exception {
56          originalLocale = Locale.getDefault();
57          Locale.setDefault(DE);
58      }
59  
60      protected void tearDown() throws Exception {
61          Locale.setDefault(originalLocale);
62          originalLocale = null;
63      }
64  
65      //-----------------------------------------------------------------------
66      public void testSubclassableConstructor() {
67          PeriodFormat f = new PeriodFormat() {
68              // test constructor is protected
69          };
70          assertNotNull(f);
71      }
72  
73      //-----------------------------------------------------------------------
74      // getDefault()
75      //-----------------------------------------------------------------------
76      public void test_getDefault_formatStandard() {
77          Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
78          assertEquals("1 day, 5 hours, 6 minutes, 7 seconds and 8 milliseconds", PeriodFormat.getDefault().print(p));
79      }
80  
81      //-----------------------------------------------------------------------
82      public void test_getDefault_FormatOneField() {
83          Period p = Period.days(2);
84          assertEquals("2 days", PeriodFormat.getDefault().print(p));
85      }
86  
87      //-----------------------------------------------------------------------
88      public void test_getDefault_formatTwoFields() {
89          Period p = Period.days(2).withHours(5);
90          assertEquals("2 days and 5 hours", PeriodFormat.getDefault().print(p));
91      }
92  
93      //-----------------------------------------------------------------------
94      public void test_getDefault_parseOneField() {
95          Period p = Period.days(2);
96          assertEquals(p, PeriodFormat.getDefault().parsePeriod("2 days"));
97      }
98  
99      //-----------------------------------------------------------------------
100     public void test_getDefault_parseTwoFields() {
101         Period p = Period.days(2).withHours(5);
102         assertEquals(p, PeriodFormat.getDefault().parsePeriod("2 days and 5 hours"));
103     }
104 
105     //-----------------------------------------------------------------------
106     public void test_getDefault_checkRedundantSeparator() {
107         try {
108             PeriodFormat.getDefault().parsePeriod("2 days and 5 hours ");
109             fail("No exception was caught");
110         } catch (Exception e) {
111             assertEquals(IllegalArgumentException.class, e.getClass());
112         }
113     }    
114     
115     //-----------------------------------------------------------------------
116     public void test_getDefault_cached() {
117         assertSame(PeriodFormat.getDefault(), PeriodFormat.getDefault());
118     }
119 
120     //-----------------------------------------------------------------------
121     // wordBased() - default locale (de)
122     //-----------------------------------------------------------------------
123     public void test_wordBased_default() {
124         Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
125         assertEquals("1 Tag, 5 Stunden, 6 Minuten, 7 Sekunden und 8 Millisekunden", PeriodFormat.wordBased().print(p));
126     }
127 
128     //-----------------------------------------------------------------------
129     // wordBased(Locale.FRENCH)
130     //-----------------------------------------------------------------------
131     public void test_wordBased_fr_formatStandard() {
132         Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
133         assertEquals("1 jour, 5 heures, 6 minutes, 7 secondes et 8 millisecondes", PeriodFormat.wordBased(FR).print(p));
134     }
135 
136     //-----------------------------------------------------------------------
137     public void test_wordBased_fr_FormatOneField() {
138         Period p = Period.days(2);
139         assertEquals("2 jours", PeriodFormat.wordBased(FR).print(p));
140     }
141 
142     //-----------------------------------------------------------------------
143     public void test_wordBased_fr_formatTwoFields() {
144         Period p = Period.days(2).withHours(5);
145         assertEquals("2 jours et 5 heures", PeriodFormat.wordBased(FR).print(p));
146     }
147 
148     //-----------------------------------------------------------------------
149     public void test_wordBased_fr_parseOneField() {
150         Period p = Period.days(2);
151         assertEquals(p, PeriodFormat.wordBased(FR).parsePeriod("2 jours"));
152     }
153 
154     //-----------------------------------------------------------------------
155     public void test_wordBased_fr_parseTwoFields() {
156         Period p = Period.days(2).withHours(5);
157         assertEquals(p, PeriodFormat.wordBased(FR).parsePeriod("2 jours et 5 heures"));
158     }
159 
160     //-----------------------------------------------------------------------
161     public void test_wordBased_fr_cached() {
162         assertSame(PeriodFormat.wordBased(FR), PeriodFormat.wordBased(FR));
163     }
164 
165     //-----------------------------------------------------------------------
166     // wordBased(Locale pt)
167     //-----------------------------------------------------------------------
168     public void test_wordBased_pt_formatStandard() {
169         Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
170         assertEquals("1 dia, 5 horas, 6 minutos, 7 segundos e 8 milissegundos", PeriodFormat.wordBased(PT).print(p));
171     }
172 
173     //-----------------------------------------------------------------------
174     public void test_wordBased_pt_FormatOneField() {
175         Period p = Period.days(2);
176         assertEquals("2 dias", PeriodFormat.wordBased(PT).print(p));
177     }
178 
179     //-----------------------------------------------------------------------
180     public void test_wordBased_pt_formatTwoFields() {
181         Period p = Period.days(2).withHours(5);
182         assertEquals("2 dias e 5 horas", PeriodFormat.wordBased(PT).print(p));
183     }
184 
185     //-----------------------------------------------------------------------
186     public void test_wordBased_pt_parseOneField() {
187         Period p = Period.days(2);
188         assertEquals(p, PeriodFormat.wordBased(PT).parsePeriod("2 dias"));
189     }
190 
191     //-----------------------------------------------------------------------
192     public void test_wordBased_pt_parseTwoFields() {
193         Period p = Period.days(2).withHours(5);
194         assertEquals(p, PeriodFormat.wordBased(PT).parsePeriod("2 dias e 5 horas"));
195     }
196 
197     //-----------------------------------------------------------------------
198     public void test_wordBased_pt_cached() {
199         assertSame(PeriodFormat.wordBased(PT), PeriodFormat.wordBased(PT));
200     }
201 
202     //-----------------------------------------------------------------------
203     // wordBased(Locale es)
204     //-----------------------------------------------------------------------
205     public void test_wordBased_es_formatStandard() {
206         Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
207         assertEquals("1 d\u00EDa, 5 horas, 6 minutos, 7 segundos y 8 milisegundos", PeriodFormat.wordBased(ES).print(p));
208     }
209 
210     //-----------------------------------------------------------------------
211     public void test_wordBased_es_FormatOneField() {
212         Period p = Period.days(2);
213         assertEquals("2 d\u00EDas", PeriodFormat.wordBased(ES).print(p));
214     }
215 
216     //-----------------------------------------------------------------------
217     public void test_wordBased_es_formatTwoFields() {
218         Period p = Period.days(2).withHours(5);
219         assertEquals("2 d\u00EDas y 5 horas", PeriodFormat.wordBased(ES).print(p));
220     }
221 
222     //-----------------------------------------------------------------------
223     public void test_wordBased_es_parseOneField() {
224         Period p = Period.days(2);
225         assertEquals(p, PeriodFormat.wordBased(ES).parsePeriod("2 d\u00EDas"));
226     }
227 
228     //-----------------------------------------------------------------------
229     public void test_wordBased_es_parseTwoFields() {
230         Period p = Period.days(2).withHours(5);
231         assertEquals(p, PeriodFormat.wordBased(ES).parsePeriod("2 d\u00EDas y 5 horas"));
232     }
233 
234     //-----------------------------------------------------------------------
235     public void test_wordBased_es_cached() {
236         assertSame(PeriodFormat.wordBased(ES), PeriodFormat.wordBased(ES));
237     }
238 
239     //-----------------------------------------------------------------------
240     // wordBased(Locale de)
241     //-----------------------------------------------------------------------
242     public void test_wordBased_de_formatStandard() {
243         Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
244         assertEquals("1 Tag, 5 Stunden, 6 Minuten, 7 Sekunden und 8 Millisekunden", PeriodFormat.wordBased(DE).print(p));
245     }
246 
247     //-----------------------------------------------------------------------
248     public void test_wordBased_de_FormatOneField() {
249         Period p = Period.days(2);
250         assertEquals("2 Tage", PeriodFormat.wordBased(DE).print(p));
251     }
252 
253     //-----------------------------------------------------------------------
254     public void test_wordBased_de_formatTwoFields() {
255         Period p = Period.days(2).withHours(5);
256         assertEquals("2 Tage und 5 Stunden", PeriodFormat.wordBased(DE).print(p));
257     }
258 
259     //-----------------------------------------------------------------------
260     public void test_wordBased_de_parseOneField() {
261         Period p = Period.days(2);
262         assertEquals(p, PeriodFormat.wordBased(DE).parsePeriod("2 Tage"));
263     }
264 
265     //-----------------------------------------------------------------------
266     public void test_wordBased_de_parseTwoFields() {
267         Period p = Period.days(2).withHours(5);
268         assertEquals(p, PeriodFormat.wordBased(DE).parsePeriod("2 Tage und 5 Stunden"));
269     }
270 
271     //-----------------------------------------------------------------------
272     public void test_wordBased_de_cached() {
273         assertSame(PeriodFormat.wordBased(DE), PeriodFormat.wordBased(DE));
274     }
275 
276     //-----------------------------------------------------------------------
277     // wordBased(Locale nl)
278     //-----------------------------------------------------------------------
279     public void test_wordBased_nl_formatStandard() {
280         Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
281         assertEquals("1 dag, 5 uur, 6 minuten, 7 seconden en 8 milliseconden", PeriodFormat.wordBased(NL).print(p));
282     }
283 
284     //-----------------------------------------------------------------------
285     public void test_wordBased_nl_FormatOneField() {
286         Period p = Period.days(2);
287         assertEquals("2 dagen", PeriodFormat.wordBased(NL).print(p));
288     }
289 
290     //-----------------------------------------------------------------------
291     public void test_wordBased_nl_formatTwoFields() {
292         Period p = Period.days(2).withHours(5);
293         assertEquals("2 dagen en 5 uur", PeriodFormat.wordBased(NL).print(p));
294     }
295 
296     //-----------------------------------------------------------------------
297     public void test_wordBased_nl_parseOneField() {
298         Period p = Period.days(2);
299         assertEquals(p, PeriodFormat.wordBased(NL).parsePeriod("2 dagen"));
300     }
301 
302     //-----------------------------------------------------------------------
303     public void test_wordBased_nl_parseTwoFields() {
304         Period p = Period.days(2).withHours(5);
305         assertEquals(p, PeriodFormat.wordBased(NL).parsePeriod("2 dagen en 5 uur"));
306     }
307 
308     //-----------------------------------------------------------------------
309     public void test_wordBased_nl_cached() {
310         assertSame(PeriodFormat.wordBased(NL), PeriodFormat.wordBased(NL));
311     }
312 
313     //-----------------------------------------------------------------------
314     // wordBased(Locale da)
315     //-----------------------------------------------------------------------
316     public void test_wordBased_da_formatMultiple() {
317         Period p = new Period(2, 3, 4, 2, 5, 6 ,7, 8);
318         assertEquals("2 \u00E5r, 3 m\u00E5neder, 4 uger, 2 dage, 5 timer, 6 minutter, 7 sekunder og 8 millisekunder", PeriodFormat.wordBased(DA).print(p));
319     }
320 
321     //-----------------------------------------------------------------------
322     public void test_wordBased_da_formatSinglular() {
323         Period p = new Period(1, 1, 1, 1, 1, 1, 1, 1);
324         assertEquals("1 \u00E5r, 1 m\u00E5ned, 1 uge, 1 dag, 1 time, 1 minut, 1 sekund og 1 millisekund", PeriodFormat.wordBased(DA).print(p));
325     }
326     
327     //-----------------------------------------------------------------------
328     public void test_wordBased_da_cached() {
329         assertSame(PeriodFormat.wordBased(DA), PeriodFormat.wordBased(DA));
330     }
331     
332     //-----------------------------------------------------------------------
333     // wordBased(Locale ja)
334     //-----------------------------------------------------------------------
335     public void test_wordBased_ja_formatMultiple() {
336         Period p = new Period(2, 3, 4, 2, 5, 6 ,7, 8);
337         assertEquals("2\u5E743\u304B\u67084\u9031\u95932\u65E55\u6642\u95936\u52067\u79D28\u30DF\u30EA\u79D2", PeriodFormat.wordBased(JA).print(p));
338     }
339 
340     //-----------------------------------------------------------------------
341     public void test_wordBased_ja_formatSingular() {
342         Period p = new Period(1, 1, 1, 1, 1, 1, 1, 1);
343         assertEquals("1\u5E741\u304B\u67081\u9031\u95931\u65E51\u6642\u95931\u52061\u79D21\u30DF\u30EA\u79D2", PeriodFormat.wordBased(JA).print(p));
344     }
345 
346     //-----------------------------------------------------------------------
347     public void test_wordBased_ja_cached() {
348         assertSame(PeriodFormat.wordBased(JA), PeriodFormat.wordBased(JA));
349     }
350 
351     //-----------------------------------------------------------------------
352     public void test_wordBased_ja_parseOneField() {
353         Period p = Period.days(2);
354         assertEquals(p, PeriodFormat.wordBased(JA).parsePeriod("2\u65E5"));
355     }
356 
357     //-----------------------------------------------------------------------
358     public void test_wordBased_ja_parseTwoFields() {
359         Period p = Period.days(2).withHours(5);
360         assertEquals(p, PeriodFormat.wordBased(JA).parsePeriod("2\u65E55\u6642\u9593"));
361     }
362     
363     //-----------------------------------------------------------------------
364     public void test_wordBased_ja_checkRedundantSeparator() {
365         try {
366             // Spaces are not valid separators in Japanese
367             PeriodFormat.wordBased(JA).parsePeriod("2\u65E5 ");
368             fail("No exception was caught");
369         } catch (Exception e) {
370             assertEquals(IllegalArgumentException.class, e.getClass());
371         }
372     }   
373     
374     //-----------------------------------------------------------------------
375     // Cross check languages
376     //-----------------------------------------------------------------------
377     public void test_wordBased_fr_from_de() {
378       Locale.setDefault(DE);
379       Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
380       assertEquals("1 jour, 5 heures, 6 minutes, 7 secondes et 8 millisecondes", PeriodFormat.wordBased(FR).print(p));
381   }
382 
383     public void test_wordBased_fr_from_nl() {
384       Locale.setDefault(NL);
385       Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
386       assertEquals("1 jour, 5 heures, 6 minutes, 7 secondes et 8 millisecondes", PeriodFormat.wordBased(FR).print(p));
387   }
388 
389     public void test_wordBased_en_from_de() {
390       Locale.setDefault(DE);
391       Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
392       assertEquals("1 day, 5 hours, 6 minutes, 7 seconds and 8 milliseconds", PeriodFormat.wordBased(EN).print(p));
393   }
394 
395     public void test_wordBased_en_from_nl() {
396       Locale.setDefault(NL);
397       Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
398       assertEquals("1 day, 5 hours, 6 minutes, 7 seconds and 8 milliseconds", PeriodFormat.wordBased(EN).print(p));
399   }
400 
401 }