merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / i18n / _XExtendedCalendar.java
blob79ad0e3bd82d8878ae1e7c06ba1d3751691216a0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XExtendedCalendar.java,v $
10 * $Revision: 1.8 $
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 ************************************************************************/
30 package ifc.i18n;
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;
45 /**
48 public class _XExtendedCalendar extends MultiMethodTest {
49 public XExtendedCalendar oObj = null;
50 boolean useUSENLocale = false;
51 /**
52 * Load a calendar
54 public void before() {
55 Locale[] installed_locales = null;
56 XLocaleData locData = null;
57 try {
58 locData = (XLocaleData) UnoRuntime.queryInterface(
59 XLocaleData.class,
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];
73 useUSENLocale = true;
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);
93 sdf = getSDF("yyyy");
94 expectedStringResult[1] = "AD" + sdf.format(actualDate);
96 sdf = getSDF("MM");
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;
120 if (useUSENLocale) {
121 locResult = displayString[i].equals(expectedStringResult[i]);
122 if (!locResult)
123 log.println("getDisplayString() result " + i + ": '" + displayString[i]
124 + "', expected: '" + expectedStringResult[i] + "'");
125 result &= locResult;
127 else { // no defaults for other locales, just expect a String
128 locResult &= displayString[i] != null;
129 if (!locResult)
130 log.println("getDisplayString() result " + i + " was 'null'");
131 result &= locResult;
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);