1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package org.apache.harmony.tests.java.text;
18
19import java.text.DateFormat;
20import java.text.DateFormatSymbols;
21import java.text.NumberFormat;
22import java.text.ParseException;
23import java.text.SimpleDateFormat;
24import java.util.Calendar;
25import java.util.Date;
26import java.util.Locale;
27
28public class DateFormatTest extends junit.framework.TestCase {
29
30	/**
31	 * @tests java.text.DateFormat#clone()
32	 */
33	public void test_clone() {
34		DateFormat format = DateFormat.getInstance();
35		DateFormat clone = (DateFormat) format.clone();
36		assertTrue("Clone not equal", format.equals(clone));
37		clone.getNumberFormat().setMinimumFractionDigits(123);
38		assertTrue("Clone shares NumberFormat", !format.equals(clone));
39	}
40
41	/**
42	 * @tests java.text.DateFormat#getAvailableLocales()
43	 */
44	public void test_getAvailableLocales() {
45		Locale[] locales = DateFormat.getAvailableLocales();
46		assertTrue("No locales", locales.length > 0);
47		boolean english = false, german = false;
48		for (int i = locales.length; --i >= 0;) {
49			if (locales[i].equals(Locale.ENGLISH))
50				english = true;
51			if (locales[i].equals(Locale.GERMAN))
52				german = true;
53			DateFormat f1 = DateFormat.getDateTimeInstance(DateFormat.SHORT,
54					DateFormat.SHORT, locales[i]);
55			assertTrue("Doesn't work",
56					f1.format(new Date()).getClass() == String.class);
57		}
58		assertTrue("Missing locales", english && german);
59	}
60
61	/**
62	 * @tests java.text.DateFormat#getCalendar()
63	 */
64	public void test_getCalendar() {
65		DateFormat format = DateFormat.getInstance();
66		Calendar cal1 = format.getCalendar();
67		Calendar cal2 = format.getCalendar();
68		assertTrue("Calendars not identical", cal1 == cal2);
69	}
70
71	/**
72	 * @tests java.text.DateFormat#getDateInstance()
73	 */
74	public void test_getDateInstance() {
75		SimpleDateFormat f2 = (SimpleDateFormat) DateFormat.getDateInstance();
76		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
77		assertTrue("Wrong default", f2.equals(DateFormat.getDateInstance(
78				DateFormat.DEFAULT, Locale.getDefault())));
79		assertEquals(f2.getDateFormatSymbols(), new DateFormatSymbols());
80		assertTrue("Doesn't work",
81				f2.format(new Date()).getClass() == String.class);
82	}
83
84	/**
85	 * @tests java.text.DateFormat#getDateInstance(int)
86	 */
87	public void test_getDateInstanceI() {
88		assertTrue("Default not medium",
89				DateFormat.DEFAULT == DateFormat.MEDIUM);
90
91		SimpleDateFormat f2 = (SimpleDateFormat) DateFormat
92				.getDateInstance(DateFormat.SHORT);
93		assertTrue("Wrong class1", f2.getClass() == SimpleDateFormat.class);
94		assertTrue("Wrong default1", f2.equals(DateFormat.getDateInstance(
95				DateFormat.SHORT, Locale.getDefault())));
96		assertTrue("Wrong symbols1", f2.getDateFormatSymbols().equals(
97				new DateFormatSymbols()));
98		assertTrue("Doesn't work1",
99				f2.format(new Date()).getClass() == String.class);
100
101		f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM);
102		assertTrue("Wrong class2", f2.getClass() == SimpleDateFormat.class);
103		assertTrue("Wrong default2", f2.equals(DateFormat.getDateInstance(
104				DateFormat.MEDIUM, Locale.getDefault())));
105		assertTrue("Wrong symbols2", f2.getDateFormatSymbols().equals(
106				new DateFormatSymbols()));
107		assertTrue("Doesn't work2",
108				f2.format(new Date()).getClass() == String.class);
109
110		f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG);
111		assertTrue("Wrong class3", f2.getClass() == SimpleDateFormat.class);
112		assertTrue("Wrong default3", f2.equals(DateFormat.getDateInstance(
113				DateFormat.LONG, Locale.getDefault())));
114		assertTrue("Wrong symbols3", f2.getDateFormatSymbols().equals(
115				new DateFormatSymbols()));
116		assertTrue("Doesn't work3",
117				f2.format(new Date()).getClass() == String.class);
118
119		f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.FULL);
120		assertTrue("Wrong class4", f2.getClass() == SimpleDateFormat.class);
121		assertTrue("Wrong default4", f2.equals(DateFormat.getDateInstance(
122				DateFormat.FULL, Locale.getDefault())));
123		assertTrue("Wrong symbols4", f2.getDateFormatSymbols().equals(
124				new DateFormatSymbols()));
125		assertTrue("Doesn't work4",
126				f2.format(new Date()).getClass() == String.class);
127
128		// regression test for HARMONY-940
129		try {
130			DateFormat.getDateInstance(77);
131            fail("Should throw IAE");
132		} catch (IllegalArgumentException iae) {
133			//expected
134		}
135	}
136
137	/**
138	 * @tests java.text.DateFormat#getDateInstance(int, java.util.Locale)
139	 */
140	public void test_getDateInstanceILjava_util_Locale() {
141		SimpleDateFormat f2 = (SimpleDateFormat) DateFormat.getDateInstance(
142				DateFormat.SHORT, Locale.GERMAN);
143		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
144		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
145				new DateFormatSymbols(Locale.GERMAN)));
146		assertTrue("Doesn't work",
147				f2.format(new Date()).getClass() == String.class);
148
149		f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM,
150				Locale.GERMAN);
151		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
152		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
153				new DateFormatSymbols(Locale.GERMAN)));
154		assertTrue("Doesn't work",
155				f2.format(new Date()).getClass() == String.class);
156
157		f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG,
158				Locale.GERMAN);
159		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
160		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
161				new DateFormatSymbols(Locale.GERMAN)));
162		assertTrue("Doesn't work",
163				f2.format(new Date()).getClass() == String.class);
164
165		f2 = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.FULL,
166				Locale.GERMAN);
167		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
168		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
169				new DateFormatSymbols(Locale.GERMAN)));
170		assertTrue("Doesn't work",
171				f2.format(new Date()).getClass() == String.class);
172
173		// regression test for HARMONY-940
174		try {
175			DateFormat.getDateInstance(77, Locale.GERMAN);
176            fail("Should throw IAE");
177		} catch (IllegalArgumentException iae) {
178			//expected
179		}
180	}
181
182	/**
183	 * @tests java.text.DateFormat#getDateTimeInstance()
184	 */
185	public void test_getDateTimeInstance() {
186		SimpleDateFormat f2 = (SimpleDateFormat) DateFormat
187				.getDateTimeInstance();
188		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
189		assertTrue("Wrong default", f2.equals(DateFormat.getDateTimeInstance(
190				DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.getDefault())));
191		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
192				new DateFormatSymbols()));
193		assertTrue("Doesn't work",
194				f2.format(new Date()).getClass() == String.class);
195	}
196
197	private void testDateTime(int dStyle, int tStyle) {
198		SimpleDateFormat f2 = (SimpleDateFormat) DateFormat
199				.getDateTimeInstance(dStyle, tStyle);
200		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
201		SimpleDateFormat date = (SimpleDateFormat) DateFormat.getDateInstance(
202				dStyle, Locale.getDefault());
203		SimpleDateFormat time = (SimpleDateFormat) DateFormat.getTimeInstance(
204				tStyle, Locale.getDefault());
205		assertEquals(f2.toPattern(), date.toPattern() + " " + time.toPattern());
206		assertEquals(f2.getDateFormatSymbols(), new DateFormatSymbols());
207		assertTrue("Doesn't work",
208				f2.format(new Date()).getClass() == String.class);
209	}
210
211	/**
212	 * @tests java.text.DateFormat#getDateTimeInstance(int, int)
213	 */
214	public void test_getDateTimeInstanceII() {
215		testDateTime(DateFormat.SHORT, DateFormat.SHORT);
216		testDateTime(DateFormat.SHORT, DateFormat.MEDIUM);
217		testDateTime(DateFormat.SHORT, DateFormat.LONG);
218		testDateTime(DateFormat.SHORT, DateFormat.FULL);
219
220		testDateTime(DateFormat.MEDIUM, DateFormat.SHORT);
221		testDateTime(DateFormat.MEDIUM, DateFormat.MEDIUM);
222		testDateTime(DateFormat.MEDIUM, DateFormat.LONG);
223		testDateTime(DateFormat.MEDIUM, DateFormat.FULL);
224
225		testDateTime(DateFormat.LONG, DateFormat.SHORT);
226		testDateTime(DateFormat.LONG, DateFormat.MEDIUM);
227		testDateTime(DateFormat.LONG, DateFormat.LONG);
228		testDateTime(DateFormat.LONG, DateFormat.FULL);
229
230		testDateTime(DateFormat.FULL, DateFormat.SHORT);
231		testDateTime(DateFormat.FULL, DateFormat.MEDIUM);
232		testDateTime(DateFormat.FULL, DateFormat.LONG);
233		testDateTime(DateFormat.FULL, DateFormat.FULL);
234
235		// regression test for HARMONY-940
236		try {
237			DateFormat.getDateTimeInstance(77, 66);
238            fail("Should throw IAE");
239		} catch (IllegalArgumentException iae) {
240			//expected
241		}
242	}
243
244	private void testDateTimeLocale(int dStyle, int tStyle) {
245		SimpleDateFormat f2 = (SimpleDateFormat) DateFormat
246				.getDateTimeInstance(dStyle, tStyle, Locale.GERMAN);
247		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
248		SimpleDateFormat date = (SimpleDateFormat) DateFormat.getDateInstance(
249				dStyle, Locale.GERMAN);
250		SimpleDateFormat time = (SimpleDateFormat) DateFormat.getTimeInstance(
251				tStyle, Locale.GERMAN);
252		assertTrue("Wrong default", f2.toPattern().equals(
253				date.toPattern() + " " + time.toPattern()));
254		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
255				new DateFormatSymbols(Locale.GERMAN)));
256		assertTrue("Doesn't work",
257				f2.format(new Date()).getClass() == String.class);
258	}
259
260	/**
261	 * @tests java.text.DateFormat#getDateTimeInstance(int, int,
262	 *        java.util.Locale)
263	 */
264	public void test_getDateTimeInstanceIILjava_util_Locale() {
265		testDateTimeLocale(DateFormat.SHORT, DateFormat.SHORT);
266		testDateTimeLocale(DateFormat.SHORT, DateFormat.MEDIUM);
267		testDateTimeLocale(DateFormat.SHORT, DateFormat.LONG);
268		testDateTimeLocale(DateFormat.SHORT, DateFormat.FULL);
269
270		testDateTimeLocale(DateFormat.MEDIUM, DateFormat.SHORT);
271		testDateTimeLocale(DateFormat.MEDIUM, DateFormat.MEDIUM);
272		testDateTimeLocale(DateFormat.MEDIUM, DateFormat.LONG);
273		testDateTimeLocale(DateFormat.MEDIUM, DateFormat.FULL);
274
275		testDateTimeLocale(DateFormat.LONG, DateFormat.SHORT);
276		testDateTimeLocale(DateFormat.LONG, DateFormat.MEDIUM);
277		testDateTimeLocale(DateFormat.LONG, DateFormat.LONG);
278		testDateTimeLocale(DateFormat.LONG, DateFormat.FULL);
279
280		testDateTimeLocale(DateFormat.FULL, DateFormat.SHORT);
281		testDateTimeLocale(DateFormat.FULL, DateFormat.MEDIUM);
282		testDateTimeLocale(DateFormat.FULL, DateFormat.LONG);
283		testDateTimeLocale(DateFormat.FULL, DateFormat.FULL);
284
285		// regression test for HARMONY-940
286		try {
287			DateFormat.getDateTimeInstance(77, 66, Locale.GERMAN);
288            fail("Should throw IAE");
289		} catch (IllegalArgumentException iae) {
290			//expected
291		}
292	}
293
294	/**
295	 * @tests java.text.DateFormat#getInstance()
296	 */
297	public void test_getInstance() {
298		SimpleDateFormat f2 = (SimpleDateFormat) DateFormat.getInstance();
299		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
300		assertTrue("Wrong default", f2.equals(DateFormat.getDateTimeInstance(
301				DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault())));
302		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
303				new DateFormatSymbols()));
304		assertTrue("Doesn't work",
305				f2.format(new Date()).getClass() == String.class);
306	}
307
308	/**
309	 * @tests java.text.DateFormat#getNumberFormat()
310	 */
311	public void test_getNumberFormat() {
312		DateFormat format = DateFormat.getInstance();
313		NumberFormat nf1 = format.getNumberFormat();
314		NumberFormat nf2 = format.getNumberFormat();
315		assertTrue("NumberFormats not identical", nf1 == nf2);
316	}
317
318	/**
319	 * @tests java.text.DateFormat#getTimeInstance()
320	 */
321	public void test_getTimeInstance() {
322		SimpleDateFormat f2 = (SimpleDateFormat) DateFormat.getTimeInstance();
323		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
324		assertTrue("Wrong default", f2.equals(DateFormat.getTimeInstance(
325				DateFormat.DEFAULT, Locale.getDefault())));
326		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
327				new DateFormatSymbols()));
328		assertTrue("Doesn't work",
329				f2.format(new Date()).getClass() == String.class);
330	}
331
332	/**
333	 * @tests java.text.DateFormat#getTimeInstance(int)
334	 */
335	public void test_getTimeInstanceI() {
336		SimpleDateFormat f2 = (SimpleDateFormat) DateFormat
337				.getTimeInstance(DateFormat.SHORT);
338		assertTrue("Wrong class1", f2.getClass() == SimpleDateFormat.class);
339		assertTrue("Wrong default1", f2.equals(DateFormat.getTimeInstance(
340				DateFormat.SHORT, Locale.getDefault())));
341		assertTrue("Wrong symbols1", f2.getDateFormatSymbols().equals(
342				new DateFormatSymbols()));
343		assertTrue("Doesn't work1",
344				f2.format(new Date()).getClass() == String.class);
345
346		f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.MEDIUM);
347		assertTrue("Wrong class2", f2.getClass() == SimpleDateFormat.class);
348		assertTrue("Wrong default2", f2.equals(DateFormat.getTimeInstance(
349				DateFormat.MEDIUM, Locale.getDefault())));
350		assertTrue("Wrong symbols2", f2.getDateFormatSymbols().equals(
351				new DateFormatSymbols()));
352		assertTrue("Doesn't work2",
353				f2.format(new Date()).getClass() == String.class);
354
355		f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.LONG);
356		assertTrue("Wrong class3", f2.getClass() == SimpleDateFormat.class);
357		assertTrue("Wrong default3", f2.equals(DateFormat.getTimeInstance(
358				DateFormat.LONG, Locale.getDefault())));
359		assertTrue("Wrong symbols3", f2.getDateFormatSymbols().equals(
360				new DateFormatSymbols()));
361		assertTrue("Doesn't work3",
362				f2.format(new Date()).getClass() == String.class);
363
364		f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.FULL);
365		assertTrue("Wrong class4", f2.getClass() == SimpleDateFormat.class);
366		assertTrue("Wrong default4", f2.equals(DateFormat.getTimeInstance(
367				DateFormat.FULL, Locale.getDefault())));
368		assertTrue("Wrong symbols4", f2.getDateFormatSymbols().equals(
369				new DateFormatSymbols()));
370		assertTrue("Doesn't work4",
371				f2.format(new Date()).getClass() == String.class);
372
373		// regression test for HARMONY-940
374		try {
375			DateFormat.getTimeInstance(77);
376            fail("Should throw IAE");
377		} catch (IllegalArgumentException iae) {
378			//expected
379		}
380	}
381
382	/**
383	 * @tests java.text.DateFormat#getTimeInstance(int, java.util.Locale)
384	 */
385	public void test_getTimeInstanceILjava_util_Locale() {
386		SimpleDateFormat f2 = (SimpleDateFormat) DateFormat.getTimeInstance(
387				DateFormat.SHORT, Locale.GERMAN);
388		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
389		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
390				new DateFormatSymbols(Locale.GERMAN)));
391		assertTrue("Doesn't work",
392				f2.format(new Date()).getClass() == String.class);
393
394		f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.MEDIUM,
395				Locale.GERMAN);
396		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
397		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
398				new DateFormatSymbols(Locale.GERMAN)));
399		assertTrue("Doesn't work",
400				f2.format(new Date()).getClass() == String.class);
401
402		f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.LONG,
403				Locale.GERMAN);
404		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
405		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
406				new DateFormatSymbols(Locale.GERMAN)));
407		assertTrue("Doesn't work",
408				f2.format(new Date()).getClass() == String.class);
409
410		f2 = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.FULL,
411				Locale.GERMAN);
412		assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
413		assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
414				new DateFormatSymbols(Locale.GERMAN)));
415		assertTrue("Doesn't work",
416				f2.format(new Date()).getClass() == String.class);
417
418		// regression test for HARMONY-940
419		try {
420			DateFormat.getTimeInstance(77, Locale.GERMAN);
421            fail("Should throw IAE");
422		} catch (IllegalArgumentException iae) {
423			//expected
424		}
425	}
426
427	/**
428	 * @tests java.text.DateFormat#setCalendar(java.util.Calendar)
429	 */
430	public void test_setCalendarLjava_util_Calendar() {
431		DateFormat format = DateFormat.getInstance();
432		Calendar cal = Calendar.getInstance();
433		format.setCalendar(cal);
434		assertTrue("Not identical Calendar", cal == format.getCalendar());
435	}
436
437	/**
438	 * @tests java.text.DateFormat#setNumberFormat(java.text.NumberFormat)
439	 */
440	public void test_setNumberFormatLjava_text_NumberFormat() {
441		DateFormat format = DateFormat.getInstance();
442		NumberFormat f1 = NumberFormat.getInstance();
443		format.setNumberFormat(f1);
444		assertTrue("Not identical NumberFormat", f1 == format.getNumberFormat());
445	}
446
447	/**
448	 * @tests java.text.DateFormat#parse(String)
449	 */
450	public void test_parse_LString() {
451		DateFormat format = DateFormat.getInstance();
452		try {
453			format.parse("not a Date");
454			fail("should throw ParseException first");
455		} catch (ParseException e) {
456			assertNotNull(e.getMessage());
457		}
458	}
459
460    /**
461     * @tests java.text.DateFormat#setLenient(boolean)
462     */
463    public void test_setLenient() {
464	Date d = null;
465	DateFormat output = new SimpleDateFormat("MM/dd/yy");
466	output.setLenient(false);
467	try {
468	    d = output.parse("01/01/-1");
469	    fail("Should throw ParseException here.");
470	} catch (ParseException e) {}
471    }
472}
473