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 api.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 com.sun.star.uno.XComponentContext; 33 import org.junit.After; 34 import org.junit.AfterClass; 35 import org.junit.Before; 36 import org.junit.BeforeClass; 37 import org.junit.Assert; 38 import org.junit.Test; 39 import java.text.SimpleDateFormat; 40 import java.util.Calendar; 41 import java.util.Date; 42 import java.util.GregorianCalendar; 43 import org.openoffice.test.uno.UnoApp; 44 45 46 /** 47 * 48 */ 49 public class XExtendedCalendarTest { 50 private static final UnoApp app = new UnoApp(); 51 52 private XComponentContext xContext = null; 53 public XExtendedCalendar oObj = null; 54 boolean useUSENLocale = false; 55 56 // setup and close connections 57 @BeforeClass setUpConnection()58 public static void setUpConnection() throws Exception 59 { 60 app.start(); 61 } 62 63 @AfterClass tearDownConnection()64 public static void tearDownConnection() throws InterruptedException, com.sun.star.uno.Exception 65 { 66 app.close(); 67 } 68 69 /** 70 * Load a calendar 71 */ 72 @Before before()73 public void before() throws Exception { 74 xContext = app.getComponentContext(); 75 Locale[] installed_locales = null; 76 XLocaleData locData = null; 77 locData = UnoRuntime.queryInterface( 78 XLocaleData.class, 79 xContext.getServiceManager().createInstanceWithContext("com.sun.star.i18n.LocaleData", xContext) 80 ); 81 oObj = UnoRuntime.queryInterface( 82 XExtendedCalendar.class, 83 xContext.getServiceManager().createInstanceWithContext("com.sun.star.i18n.LocaleCalendar", xContext) 84 ); 85 installed_locales = locData.getAllInstalledLocaleNames(); 86 // use first Locale as fallback, if US-English is not found 87 Locale lo = installed_locales[0]; 88 for (int i=0; i<installed_locales.length; i++) { 89 // search for "en" and "US" 90 if (installed_locales[i].Language.equals("en") && 91 installed_locales[i].Country.equals("US")) { 92 lo = installed_locales[i]; 93 useUSENLocale = true; 94 } 95 } 96 System.out.println("Choose Locale: '" + lo.Language + "', '" + lo.Country + "'"); 97 oObj.loadDefaultCalendar(lo); 98 } 99 100 @Test _getDisplayString()101 public void _getDisplayString() { 102 // against regression: the current state is the right one. 103 boolean result = true; 104 String[] displayString = new String[6]; 105 // build the defaults with the Java Calendar functions 106 String[] expectedStringResult = new String[6]; 107 Calendar cal = new GregorianCalendar(); 108 Date actualDate = cal.getTime(); 109 110 SimpleDateFormat sdf = getSDF("yy"); 111 expectedStringResult[0] = "AD" + sdf.format(actualDate); 112 113 sdf = getSDF("yyyy"); 114 expectedStringResult[1] = "AD" + sdf.format(actualDate); 115 116 sdf = getSDF("MM"); 117 expectedStringResult[2] = sdf.format(actualDate); 118 119 int month = cal.get(Calendar.MONTH) + 1; 120 String quarter = "Q1"; 121 String longQuarter = "1st quarter"; 122 if (4 <= month && month <= 6) { quarter = "Q2"; longQuarter = "2nd quarter"; } 123 else if (7 <= month && month <= 9) { quarter = "Q3"; longQuarter = "3rd quarter"; } 124 else if (10 <= month && month <= 12) {quarter = "Q4"; longQuarter = "4th quarter"; } 125 expectedStringResult[3] = quarter; 126 expectedStringResult[4] = longQuarter; 127 128 sdf = getSDF("MMMM"); 129 expectedStringResult[5] = sdf.format(actualDate); 130 131 displayString[0] = oObj.getDisplayString(CalendarDisplayCode.SHORT_YEAR_AND_ERA, NativeNumberMode.NATNUM0); 132 displayString[1] = oObj.getDisplayString(CalendarDisplayCode.LONG_YEAR_AND_ERA, NativeNumberMode.NATNUM0); 133 displayString[2] = oObj.getDisplayString(CalendarDisplayCode.LONG_MONTH, NativeNumberMode.NATNUM0); 134 displayString[3] = oObj.getDisplayString(CalendarDisplayCode.SHORT_QUARTER, NativeNumberMode.NATNUM0); 135 displayString[4] = oObj.getDisplayString(CalendarDisplayCode.LONG_QUARTER, NativeNumberMode.NATNUM0); 136 displayString[5] = oObj.getDisplayString(CalendarDisplayCode.LONG_MONTH_NAME, NativeNumberMode.NATNUM0); 137 138 for (int i=0; i<displayString.length; i++) { 139 boolean locResult = false; 140 if (useUSENLocale) { 141 locResult = displayString[i].equals(expectedStringResult[i]); 142 if (!locResult) 143 System.out.println("getDisplayString() result " + i + ": '" + displayString[i] 144 + "', expected: '" + expectedStringResult[i] + "'"); 145 result &= locResult; 146 } 147 else { // no defaults for other locales, just expect a String 148 locResult &= displayString[i] != null; 149 if (!locResult) 150 System.out.println("getDisplayString() result " + i + " was 'null'"); 151 result &= locResult; 152 } 153 } 154 Assert.assertTrue("getDisplayString()", result); 155 } 156 getSDF(String format)157 private SimpleDateFormat getSDF(String format){ 158 if (useUSENLocale) return new SimpleDateFormat(format, java.util.Locale.US); 159 return new SimpleDateFormat(format); 160 } 161 } 162