Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / i18n / _XExtendedCalendar.java
blobbd190241c1a6299955af2b705b9b2007ddb83743
1 /*
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 .
18 package ifc.i18n;
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;
32 /**
35 public class _XExtendedCalendar extends MultiMethodTest {
36 public XExtendedCalendar oObj = null;
37 boolean useUSENLocale = false;
38 /**
39 * Load a calendar
41 @Override
42 public void before() throws Exception {
43 Locale[] installed_locales = null;
44 XLocaleData locData = UnoRuntime.queryInterface(
45 XLocaleData.class,
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];
57 useUSENLocale = true;
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);
77 sdf = getSDF("yyyy");
78 expectedStringResult[1] = "AD" + sdf.format(actualDate);
80 sdf = getSDF("MM");
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;
92 sdf = getSDF("MMMM");
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;
104 if (useUSENLocale) {
105 locResult = displayString[i].equals(expectedStringResult[i]);
106 if (!locResult)
107 log.println("getDisplayString() result " + i + ": '" + displayString[i]
108 + "', expected: '" + expectedStringResult[i] + "'");
109 result &= locResult;
111 else { // no defaults for other locales, just expect a String
112 locResult &= displayString[i] != null;
113 if (!locResult)
114 log.println("getDisplayString() result " + i + " was 'null'");
115 result &= locResult;
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);