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 24 package api.i18n; 25 26 import com.sun.star.i18n.KNumberFormatType; 27 import com.sun.star.i18n.KNumberFormatUsage; 28 import com.sun.star.i18n.NumberFormatCode; 29 import com.sun.star.i18n.NumberFormatIndex; 30 import com.sun.star.i18n.XNumberFormatCode; 31 import com.sun.star.lang.Locale; 32 import com.sun.star.uno.UnoRuntime; 33 import com.sun.star.uno.XComponentContext; 34 import org.junit.After; 35 import org.junit.AfterClass; 36 import org.junit.Before; 37 import org.junit.BeforeClass; 38 import org.junit.Assert; 39 import org.junit.Test; 40 import org.openoffice.test.uno.UnoApp; 41 42 43 /** 44 * Testing <code>com.sun.star.i18n.XNumberFormatCode</code> 45 * interface methods: 46 * <ul> 47 * <li><code> getDefault() </code></li> 48 * <li><code> getFormatCode() </code></li> 49 * <li><code> getAllFormatCode() </code></li> 50 * <li><code> getAllFormatCodes() </code></li> 51 * </ul><p> 52 * Test is <b> NOT </b> multithread compliant. <p> 53 * @see com.sun.star.i18n.XNumberFormatCode 54 */ 55 public class XNumberFormatCodeTest { 56 private static final UnoApp app = new UnoApp(); 57 58 private XComponentContext xContext = null; 59 public XNumberFormatCode oObj = null; 60 public String[] languages = new String[] 61 {"de","en","es","fr","ko","ko","zh"}; 62 public String[] countries = new String[] 63 {"DE","US","ES","FR","KR","KR","CN"}; 64 65 // setup and close connections 66 @BeforeClass setUpConnection()67 public static void setUpConnection() throws Exception 68 { 69 app.start(); 70 } 71 72 @AfterClass tearDownConnection()73 public static void tearDownConnection() throws InterruptedException, com.sun.star.uno.Exception 74 { 75 app.close(); 76 } 77 78 @Before before()79 public void before() throws Exception { 80 xContext = app.getComponentContext(); 81 oObj = UnoRuntime.queryInterface( 82 XNumberFormatCode.class, 83 xContext.getServiceManager().createInstanceWithContext("com.sun.star.i18n.NumberFormatCodeMapper", xContext) 84 ); 85 } 86 87 88 /** 89 * Test calls the method twice with two different format types as 90 * parameters for each locale. Result is checked after every call.<p> 91 * Has <b> OK </b> status if both times returned structure's field 'Code' 92 * does not equal to empty string. 93 */ 94 @Test _getDefault()95 public void _getDefault() { 96 boolean res = true; 97 NumberFormatCode nfc = null; 98 99 for (int i=0;i<7;i++) { 100 nfc = oObj.getDefault(KNumberFormatType.SHORT, 101 KNumberFormatUsage.DATE, getLocale(i)); 102 String str = nfc.Code; 103 if (str.equals("")) { 104 System.out.println("'NumberFormat.code.equals(\"\") = true' for" 105 + " language: " + languages[i]); 106 System.out.println("Usage: oObj.getDefault(KNumberFormatType.SHORT," 107 + " KNumberFormatUsage.DATE,new Locale(" + languages[i] 108 + "," + countries[i] + ",\"\");"); 109 } 110 res &= !str.equals(""); 111 112 nfc = oObj.getDefault(KNumberFormatType.LONG, 113 KNumberFormatUsage.DATE,getLocale(i)); 114 str = nfc.Code; 115 if (str.equals("")) { 116 System.out.println("'NumberFormat.code.equals(\"\") = true' for " 117 + "language: " + languages[i]); 118 System.out.println("Usage: oObj.getDefault(KNumberFormatType.LONG," 119 + " KNumberFormatUsage.DATE,new Locale(" + languages[i] 120 + "," + countries[i] + ",\"\");"); 121 } 122 res &= ( ! str.equals("") ); 123 } 124 Assert.assertTrue("getDefault()", res); 125 } 126 127 /** 128 * Test calls the method twice for each locale with two different arguments. 129 * After every call result is checked.<p> 130 * Has <b> OK </b> status if both times returned structure's field 'Code' 131 * does not equal to a empty string. 132 */ 133 @Test _getFormatCode()134 public void _getFormatCode() { 135 boolean res = true; 136 NumberFormatCode nfc = null; 137 138 for (int i=0;i<7;i++) { 139 nfc = oObj.getFormatCode 140 (NumberFormatIndex.DATE_SYSTEM_SHORT,getLocale(i)); 141 res &= ( ! nfc.Code.equals("") ); 142 nfc = oObj.getFormatCode 143 (NumberFormatIndex.DATE_SYSTEM_LONG,getLocale(i)); 144 res &= ( ! nfc.Code.equals("") ); 145 } 146 Assert.assertTrue("getFormatCode()", res); 147 } 148 149 /** 150 * Test calls the method twice with two different arguments for each locale. 151 * After every call result is checked.<p> 152 * Has <b> OK </b> status if both times returned array's length does not 153 * equal to zero. 154 */ 155 @Test _getAllFormatCode()156 public void _getAllFormatCode() { 157 boolean res = true; 158 NumberFormatCode[] nfc = null; 159 160 for (int i=0;i<7;i++) { 161 nfc = oObj.getAllFormatCode(KNumberFormatUsage.DATE, getLocale(i)); 162 res &= ( nfc.length != 0 ); 163 nfc = oObj.getAllFormatCode(KNumberFormatUsage.TIME, getLocale(i)); 164 res &= ( nfc.length != 0 ); 165 } 166 Assert.assertTrue("getAllFormatCode()", res); 167 } 168 169 /** 170 * Test calls the method for each locale. <p> 171 * Has <b> OK </b> status if returned array's length does not equal to zero. 172 */ 173 @Test _getAllFormatCodes()174 public void _getAllFormatCodes() { 175 boolean res = true; 176 NumberFormatCode[] nfc = null; 177 178 for (int i=0;i<7;i++) { 179 nfc = oObj.getAllFormatCodes(getLocale(i)); 180 res &= ( nfc.length != 0 ); 181 } 182 Assert.assertTrue("getAllFormatCodes()", res); 183 } 184 185 /** 186 * Method returns locale for a given language and country. 187 * @param k index of needed locale. 188 * @return Locale by the index from arrays defined above 189 */ getLocale(int k)190 public Locale getLocale(int k) { 191 return new Locale(languages[k], countries[k], ""); 192 } 193 194 195 196 } // end XNumberFormatCode 197