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