xref: /AOO42X/main/qadevOOo/tests/java/ifc/i18n/_XExtendedCalendar.java (revision b0efeae40e43e6d4ccd561d22ec612d42773857b)
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      */
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 <<<<<<< HEAD:main/qadevOOo/tests/java/ifc/i18n/_XExtendedCalendar.java
73 
74 
75 =======
76 
77     @Test
78 >>>>>>> 3309286857 (pre-commit auto remove trailing whitespace from java files (#382)):test/testuno/source/api/i18n/XExtendedCalendarTest.java
79     public void _getDisplayString() {
80         // against regression: the current state is the right one.
81         boolean result = true;
82         String[] displayString = new String[6];
83         // build the defaults with the Java Calendar functions
84         String[] expectedStringResult = new String[6];
85         Calendar cal = new GregorianCalendar();
86         Date actualDate = cal.getTime();
87 
88         SimpleDateFormat sdf = getSDF("yy");
89         expectedStringResult[0] = "AD" + sdf.format(actualDate);
90 
91         sdf = getSDF("yyyy");
92         expectedStringResult[1] = "AD" + sdf.format(actualDate);
93 
94         sdf = getSDF("MM");
95         expectedStringResult[2] = sdf.format(actualDate);
96 
97         int month = cal.get(Calendar.MONTH) + 1;
98         String quarter = "Q1";
99         String longQuarter = "1st quarter";
100         if (month > 3 && month < 7) { quarter = "Q2"; longQuarter = "2nd quarter"; }
101         else if (month > 6 && month < 10) { quarter = "Q3"; longQuarter = "3rd quarter"; }
102         else if (month > 10 && month < 13) {quarter = "Q4"; longQuarter = "4th quarter"; }
103         expectedStringResult[3] = quarter;
104         expectedStringResult[4] = longQuarter;
105 
106         sdf = getSDF("MMMM");
107         expectedStringResult[5] = sdf.format(actualDate);
108 
109         displayString[0] = oObj.getDisplayString(CalendarDisplayCode.SHORT_YEAR_AND_ERA, NativeNumberMode.NATNUM0);
110         displayString[1] = oObj.getDisplayString(CalendarDisplayCode.LONG_YEAR_AND_ERA, NativeNumberMode.NATNUM0);
111         displayString[2] = oObj.getDisplayString(CalendarDisplayCode.LONG_MONTH, NativeNumberMode.NATNUM0);
112         displayString[3] = oObj.getDisplayString(CalendarDisplayCode.SHORT_QUARTER, NativeNumberMode.NATNUM0);
113         displayString[4] = oObj.getDisplayString(CalendarDisplayCode.LONG_QUARTER, NativeNumberMode.NATNUM0);
114         displayString[5] = oObj.getDisplayString(CalendarDisplayCode.LONG_MONTH_NAME, NativeNumberMode.NATNUM0);
115 
116         for (int i=0; i<displayString.length; i++) {
117             boolean locResult = false;
118             if (useUSENLocale) {
119                 locResult = displayString[i].equals(expectedStringResult[i]);
120                 if (!locResult)
121 <<<<<<< HEAD:main/qadevOOo/tests/java/ifc/i18n/_XExtendedCalendar.java
122                     log.println("getDisplayString() result " + i + ": '" + displayString[i]
123 =======
124                     System.out.println("getDisplayString() result " + i + ": '" + displayString[i]
125 >>>>>>> 3309286857 (pre-commit auto remove trailing whitespace from java files (#382)):test/testuno/source/api/i18n/XExtendedCalendarTest.java
126                                         + "', expected: '" + expectedStringResult[i] + "'");
127                 result &= locResult;
128             }
129             else { // no defaults for other locales, just expect a String
130                 locResult &= displayString[i] != null;
131                 if (!locResult)
132 <<<<<<< HEAD:main/qadevOOo/tests/java/ifc/i18n/_XExtendedCalendar.java
133                     log.println("getDisplayString() result " + i + " was 'null'");
134 =======
135                     System.out.println("getDisplayString() result " + i + " was 'null'");
136 >>>>>>> 3309286857 (pre-commit auto remove trailing whitespace from java files (#382)):test/testuno/source/api/i18n/XExtendedCalendarTest.java
137                 result &= locResult;
138             }
139         }
140         tRes.tested("getDisplayString()", result);
141     }
142 
143     private SimpleDateFormat getSDF(String format){
144         if (useUSENLocale) return new SimpleDateFormat(format, java.util.Locale.US);
145         return new SimpleDateFormat(format);
146     }
147 }
148