1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package ifc.i18n;
24 
25 import com.sun.star.i18n.CalendarDisplayCode;
26 import com.sun.star.i18n.NativeNumberMode;
27 import com.sun.star.i18n.XExtendedCalendar;
28 import com.sun.star.i18n.XLocaleData;
29 import com.sun.star.lang.Locale;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.uno.UnoRuntime;
32 import java.text.SimpleDateFormat;
33 import java.util.Calendar;
34 import java.util.Date;
35 import java.util.GregorianCalendar;
36 import lib.MultiMethodTest;
37 
38 /**
39  *
40  */
41 public class _XExtendedCalendar extends MultiMethodTest {
42     public XExtendedCalendar oObj = null;
43     boolean useUSENLocale = false;
44     /**
45      * Load a calendar
46      */
before()47     public void before() {
48         Locale[] installed_locales = null;
49         XLocaleData locData = null;
50         try {
51             locData = (XLocaleData) UnoRuntime.queryInterface(
52                 XLocaleData.class,
53                     ((XMultiServiceFactory)tParam.getMSF()).createInstance(
54                     "com.sun.star.i18n.LocaleData"));
55         } catch (com.sun.star.uno.Exception e) {
56 
57         }
58         installed_locales = locData.getAllInstalledLocaleNames();
59         // use first Locale as fallback, if US-English is not found
60         Locale lo = installed_locales[0];
61         for (int i=0; i<installed_locales.length; i++) {
62             // search for "en" and "US"
63             if (installed_locales[i].Language.equals("en") &&
64                             installed_locales[i].Country.equals("US")) {
65                 lo = installed_locales[i];
66                 useUSENLocale = true;
67             }
68         }
69         log.println("Choose Locale: '" + lo.Language + "', '" + lo.Country + "'");
70         oObj.loadDefaultCalendar(lo);
71     }
72 
73 
_getDisplayString()74     public void _getDisplayString() {
75         // against regression: the current state is the right one.
76         boolean result = true;
77         String[] displayString = new String[6];
78         // build the defaults with the Java Calendar functions
79         String[] expectedStringResult = new String[6];
80         Calendar cal = new GregorianCalendar();
81         Date actualDate = cal.getTime();
82 
83         SimpleDateFormat sdf = getSDF("yy");
84         expectedStringResult[0] = "AD" + sdf.format(actualDate);
85 
86         sdf = getSDF("yyyy");
87         expectedStringResult[1] = "AD" + sdf.format(actualDate);
88 
89         sdf = getSDF("MM");
90         expectedStringResult[2] = sdf.format(actualDate);
91 
92         int month = cal.get(Calendar.MONTH) + 1;
93         String quarter = "Q1";
94         String longQuarter = "1st quarter";
95         if (month > 3 && month < 7) { quarter = "Q2"; longQuarter = "2nd quarter"; }
96         else if (month > 6 && month < 10) { quarter = "Q3"; longQuarter = "3rd quarter"; }
97         else if (month > 10 && month < 13) {quarter = "Q4"; longQuarter = "4th quarter"; }
98         expectedStringResult[3] = quarter;
99         expectedStringResult[4] = longQuarter;
100 
101         sdf = getSDF("MMMM");
102         expectedStringResult[5] = sdf.format(actualDate);
103 
104         displayString[0] = oObj.getDisplayString(CalendarDisplayCode.SHORT_YEAR_AND_ERA, NativeNumberMode.NATNUM0);
105         displayString[1] = oObj.getDisplayString(CalendarDisplayCode.LONG_YEAR_AND_ERA, NativeNumberMode.NATNUM0);
106         displayString[2] = oObj.getDisplayString(CalendarDisplayCode.LONG_MONTH, NativeNumberMode.NATNUM0);
107         displayString[3] = oObj.getDisplayString(CalendarDisplayCode.SHORT_QUARTER, NativeNumberMode.NATNUM0);
108         displayString[4] = oObj.getDisplayString(CalendarDisplayCode.LONG_QUARTER, NativeNumberMode.NATNUM0);
109         displayString[5] = oObj.getDisplayString(CalendarDisplayCode.LONG_MONTH_NAME, NativeNumberMode.NATNUM0);
110 
111         for (int i=0; i<displayString.length; i++) {
112             boolean locResult = false;
113             if (useUSENLocale) {
114                 locResult = displayString[i].equals(expectedStringResult[i]);
115                 if (!locResult)
116                     log.println("getDisplayString() result " + i + ": '" + displayString[i]
117                                         + "', expected: '" + expectedStringResult[i] + "'");
118                 result &= locResult;
119             }
120             else { // no defaults for other locales, just expect a String
121                 locResult &= displayString[i] != null;
122                 if (!locResult)
123                     log.println("getDisplayString() result " + i + " was 'null'");
124                 result &= locResult;
125             }
126         }
127         tRes.tested("getDisplayString()", result);
128     }
129 
getSDF(String format)130     private SimpleDateFormat getSDF(String format){
131         if (useUSENLocale) return new SimpleDateFormat(format, java.util.Locale.US);
132         return new SimpleDateFormat(format);
133     }
134 }
135