2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.i18n
.CalendarDisplayCode
;
21 import com
.sun
.star
.i18n
.NativeNumberMode
;
22 import com
.sun
.star
.i18n
.XExtendedCalendar
;
23 import com
.sun
.star
.i18n
.XLocaleData
;
24 import com
.sun
.star
.lang
.Locale
;
25 import com
.sun
.star
.uno
.UnoRuntime
;
26 import java
.text
.SimpleDateFormat
;
27 import java
.util
.Calendar
;
28 import java
.util
.Date
;
29 import java
.util
.GregorianCalendar
;
30 import lib
.MultiMethodTest
;
35 public class _XExtendedCalendar
extends MultiMethodTest
{
36 public XExtendedCalendar oObj
= null;
37 boolean useUSENLocale
= false;
42 public void before() throws Exception
{
43 Locale
[] installed_locales
= null;
44 XLocaleData locData
= UnoRuntime
.queryInterface(
46 tParam
.getMSF().createInstance(
47 "com.sun.star.i18n.LocaleData"));
49 installed_locales
= locData
.getAllInstalledLocaleNames();
50 // use first Locale as fallback, if US-English is not found
51 Locale lo
= installed_locales
[0];
52 for (int i
=0; i
<installed_locales
.length
; i
++) {
53 // search for "en" and "US"
54 if (installed_locales
[i
].Language
.equals("en") &&
55 installed_locales
[i
].Country
.equals("US")) {
56 lo
= installed_locales
[i
];
60 log
.println("Choose Locale: '" + lo
.Language
+ "', '" + lo
.Country
+ "'");
61 oObj
.loadDefaultCalendar(lo
);
65 public void _getDisplayString() {
66 // against regression: the current state is the right one.
67 boolean result
= true;
68 String
[] displayString
= new String
[6];
69 // build the defaults with the Java Calendar functions
70 String
[] expectedStringResult
= new String
[6];
71 Calendar cal
= new GregorianCalendar();
72 Date actualDate
= cal
.getTime();
74 SimpleDateFormat sdf
= getSDF("yy");
75 expectedStringResult
[0] = "AD" + sdf
.format(actualDate
);
78 expectedStringResult
[1] = "AD" + sdf
.format(actualDate
);
81 expectedStringResult
[2] = sdf
.format(actualDate
);
83 int month
= cal
.get(Calendar
.MONTH
) + 1;
84 String quarter
= "Q1";
85 String longQuarter
= "1st quarter";
86 if (month
> 3 && month
< 7) { quarter
= "Q2"; longQuarter
= "2nd quarter"; }
87 else if (month
> 6 && month
< 10) { quarter
= "Q3"; longQuarter
= "3rd quarter"; }
88 else if (month
> 10 && month
< 13) {quarter
= "Q4"; longQuarter
= "4th quarter"; }
89 expectedStringResult
[3] = quarter
;
90 expectedStringResult
[4] = longQuarter
;
93 expectedStringResult
[5] = sdf
.format(actualDate
);
95 displayString
[0] = oObj
.getDisplayString(CalendarDisplayCode
.SHORT_YEAR_AND_ERA
, NativeNumberMode
.NATNUM0
);
96 displayString
[1] = oObj
.getDisplayString(CalendarDisplayCode
.LONG_YEAR_AND_ERA
, NativeNumberMode
.NATNUM0
);
97 displayString
[2] = oObj
.getDisplayString(CalendarDisplayCode
.LONG_MONTH
, NativeNumberMode
.NATNUM0
);
98 displayString
[3] = oObj
.getDisplayString(CalendarDisplayCode
.SHORT_QUARTER
, NativeNumberMode
.NATNUM0
);
99 displayString
[4] = oObj
.getDisplayString(CalendarDisplayCode
.LONG_QUARTER
, NativeNumberMode
.NATNUM0
);
100 displayString
[5] = oObj
.getDisplayString(CalendarDisplayCode
.LONG_MONTH_NAME
, NativeNumberMode
.NATNUM0
);
102 for (int i
=0; i
<displayString
.length
; i
++) {
103 boolean locResult
= false;
105 locResult
= displayString
[i
].equals(expectedStringResult
[i
]);
107 log
.println("getDisplayString() result " + i
+ ": '" + displayString
[i
]
108 + "', expected: '" + expectedStringResult
[i
] + "'");
111 else { // no defaults for other locales, just expect a String
112 locResult
&= displayString
[i
] != null;
114 log
.println("getDisplayString() result " + i
+ " was 'null'");
118 tRes
.tested("getDisplayString()", result
);
121 private SimpleDateFormat
getSDF(String format
){
122 if (useUSENLocale
) return new SimpleDateFormat(format
, java
.util
.Locale
.US
);
123 return new SimpleDateFormat(format
);