1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XExtendedCalendar.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 import com
.sun
.star
.i18n
.CalendarDisplayCode
;
33 import com
.sun
.star
.i18n
.NativeNumberMode
;
34 import com
.sun
.star
.i18n
.XExtendedCalendar
;
35 import com
.sun
.star
.i18n
.XLocaleData
;
36 import com
.sun
.star
.lang
.Locale
;
37 import com
.sun
.star
.lang
.XMultiServiceFactory
;
38 import com
.sun
.star
.uno
.UnoRuntime
;
39 import java
.text
.SimpleDateFormat
;
40 import java
.util
.Calendar
;
41 import java
.util
.Date
;
42 import java
.util
.GregorianCalendar
;
43 import lib
.MultiMethodTest
;
48 public class _XExtendedCalendar
extends MultiMethodTest
{
49 public XExtendedCalendar oObj
= null;
50 boolean useUSENLocale
= false;
54 public void before() {
55 Locale
[] installed_locales
= null;
56 XLocaleData locData
= null;
58 locData
= (XLocaleData
) UnoRuntime
.queryInterface(
60 ((XMultiServiceFactory
)tParam
.getMSF()).createInstance(
61 "com.sun.star.i18n.LocaleData"));
62 } catch (com
.sun
.star
.uno
.Exception e
) {
65 installed_locales
= locData
.getAllInstalledLocaleNames();
66 // use first Locale as fallback, if US-English is not found
67 Locale lo
= installed_locales
[0];
68 for (int i
=0; i
<installed_locales
.length
; i
++) {
69 // search for "en" and "US"
70 if (installed_locales
[i
].Language
.equals("en") &&
71 installed_locales
[i
].Country
.equals("US")) {
72 lo
= installed_locales
[i
];
76 log
.println("Choose Locale: '" + lo
.Language
+ "', '" + lo
.Country
+ "'");
77 oObj
.loadDefaultCalendar(lo
);
81 public void _getDisplayString() {
82 // against regression: the current state is the right one.
83 boolean result
= true;
84 String
[] displayString
= new String
[6];
85 // build the defaults with the Java Calendar functions
86 String
[] expectedStringResult
= new String
[6];
87 Calendar cal
= new GregorianCalendar();
88 Date actualDate
= cal
.getTime();
90 SimpleDateFormat sdf
= getSDF("yy");
91 expectedStringResult
[0] = "AD" + sdf
.format(actualDate
);
94 expectedStringResult
[1] = "AD" + sdf
.format(actualDate
);
97 expectedStringResult
[2] = sdf
.format(actualDate
);
99 int month
= cal
.get(Calendar
.MONTH
) + 1;
100 String quarter
= "Q1";
101 String longQuarter
= "1st quarter";
102 if (month
> 3 && month
< 7) { quarter
= "Q2"; longQuarter
= "2nd quarter"; }
103 else if (month
> 6 && month
< 10) { quarter
= "Q3"; longQuarter
= "3rd quarter"; }
104 else if (month
> 10 && month
< 13) {quarter
= "Q4"; longQuarter
= "4th quarter"; }
105 expectedStringResult
[3] = quarter
;
106 expectedStringResult
[4] = longQuarter
;
108 sdf
= getSDF("MMMM");
109 expectedStringResult
[5] = sdf
.format(actualDate
);
111 displayString
[0] = oObj
.getDisplayString(CalendarDisplayCode
.SHORT_YEAR_AND_ERA
, NativeNumberMode
.NATNUM0
);
112 displayString
[1] = oObj
.getDisplayString(CalendarDisplayCode
.LONG_YEAR_AND_ERA
, NativeNumberMode
.NATNUM0
);
113 displayString
[2] = oObj
.getDisplayString(CalendarDisplayCode
.LONG_MONTH
, NativeNumberMode
.NATNUM0
);
114 displayString
[3] = oObj
.getDisplayString(CalendarDisplayCode
.SHORT_QUARTER
, NativeNumberMode
.NATNUM0
);
115 displayString
[4] = oObj
.getDisplayString(CalendarDisplayCode
.LONG_QUARTER
, NativeNumberMode
.NATNUM0
);
116 displayString
[5] = oObj
.getDisplayString(CalendarDisplayCode
.LONG_MONTH_NAME
, NativeNumberMode
.NATNUM0
);
118 for (int i
=0; i
<displayString
.length
; i
++) {
119 boolean locResult
= false;
121 locResult
= displayString
[i
].equals(expectedStringResult
[i
]);
123 log
.println("getDisplayString() result " + i
+ ": '" + displayString
[i
]
124 + "', expected: '" + expectedStringResult
[i
] + "'");
127 else { // no defaults for other locales, just expect a String
128 locResult
&= displayString
[i
] != null;
130 log
.println("getDisplayString() result " + i
+ " was 'null'");
134 tRes
.tested("getDisplayString()", result
);
137 private SimpleDateFormat
getSDF(String format
){
138 if (useUSENLocale
) return new SimpleDateFormat(format
, java
.util
.Locale
.US
);
139 return new SimpleDateFormat(format
);