1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package org.apache.harmony.tests.java.text; 18 19import java.io.ByteArrayInputStream; 20import java.io.ByteArrayOutputStream; 21import java.io.ObjectInputStream; 22import java.io.ObjectOutputStream; 23import java.text.DateFormatSymbols; 24import java.util.Arrays; 25import java.util.Locale; 26 27public class DateFormatSymbolsTest extends junit.framework.TestCase { 28 29 private DateFormatSymbols dfs; 30 31 /** 32 * @tests java.text.DateFormatSymbols#DateFormatSymbols() 33 */ 34 public void test_Constructor() { 35 // Test for method java.text.DateFormatSymbols() 36 // Used in tests 37 new DateFormatSymbols(); 38 } 39 40 /** 41 * @tests java.text.DateFormatSymbols#DateFormatSymbols(java.util.Locale) 42 */ 43 public void test_ConstructorLjava_util_Locale() { 44 // Test for method java.text.DateFormatSymbols(java.util.Locale) 45 new DateFormatSymbols(new Locale("en", "us")); 46 } 47 48 /** 49 * @tests java.text.DateFormatSymbols#getAvailableLocales() 50 */ 51 public void test_getAvailableLocales_no_provider() throws Exception { 52 Locale[] locales = DateFormatSymbols.getAvailableLocales(); 53 assertNotNull(locales); 54 // must contain Locale.US 55 boolean flag = false; 56 for (Locale locale : locales) { 57 if (locale.equals(Locale.US)) { 58 flag = true; 59 break; 60 } 61 } 62 assertTrue(flag); 63 } 64 65 /** 66 * @tests java.text.DateFormatSymbols#getInstance() 67 */ 68 public void test_getInstance() { 69 DateFormatSymbols.getInstance(); 70 assertEquals(new DateFormatSymbols(), DateFormatSymbols.getInstance()); 71 assertEquals(new DateFormatSymbols(Locale.getDefault()), 72 DateFormatSymbols.getInstance()); 73 74 assertNotSame(DateFormatSymbols.getInstance(), DateFormatSymbols.getInstance()); 75 } 76 77 public void test_getInstanceLjava_util_Locale() { 78 try { 79 DateFormatSymbols.getInstance(null); 80 fail(); 81 } catch (NullPointerException expected) { 82 } 83 84 assertEquals(new DateFormatSymbols(Locale.GERMANY), DateFormatSymbols.getInstance(Locale.GERMANY)); 85 86 Locale locale = new Locale("not exist language", "not exist country"); 87 DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale); 88 assertEquals(DateFormatSymbols.getInstance(Locale.ROOT), symbols); 89 } 90 91 /** 92 * @tests java.text.DateFormatSymbols#clone() 93 */ 94 public void test_clone() { 95 // Test for method java.lang.Object java.text.DateFormatSymbols.clone() 96 DateFormatSymbols symbols = new DateFormatSymbols(); 97 DateFormatSymbols clone = (DateFormatSymbols) symbols.clone(); 98 assertTrue("Not equal", symbols.equals(clone)); 99 } 100 101 /** 102 * @tests java.text.DateFormatSymbols#equals(java.lang.Object) 103 */ 104 public void test_equalsLjava_lang_Object() { 105 // Test for method boolean 106 // java.text.DateFormatSymbols.equals(java.lang.Object) 107 assertTrue("Equal object returned true", dfs.equals(dfs.clone())); 108 dfs.setLocalPatternChars("KKKKKKKKK"); 109 assertTrue("Un-Equal objects returned false", !dfs 110 .equals(new DateFormatSymbols())); 111 } 112 113 /** 114 * @tests java.text.DateFormatSymbols#getAmPmStrings() 115 */ 116 public void test_getAmPmStrings() { 117 // Test for method java.lang.String [] 118 // java.text.DateFormatSymbols.getAmPmStrings() 119 String[] retVal = dfs.getAmPmStrings(); 120 String[] val = { "AM", "PM" }; 121 if (retVal.length != val.length) 122 fail("Returned wrong array"); 123 for (int i = 0; i < val.length; i++) 124 assertTrue("Array values do not match", retVal[i].equals(val[i])); 125 } 126 127 /** 128 * @tests java.text.DateFormatSymbols#getEras() 129 */ 130 public void test_getEras() { 131 // Test for method java.lang.String [] 132 // java.text.DateFormatSymbols.getEras() 133 String[] retVal = dfs.getEras(); 134 String[] val = { "BC", "AD" }; 135 if (retVal.length != val.length) 136 fail("Returned wrong array"); 137 for (int i = 0; i < val.length; i++) 138 assertTrue("Array values do not match", retVal[i].equals(val[i])); 139 } 140 141 /** 142 * @tests java.text.DateFormatSymbols#getMonths() 143 */ 144 public void test_getMonths() { 145 // Test for method java.lang.String [] 146 // java.text.DateFormatSymbols.getMonths() 147 String[] retVal = dfs.getMonths(); 148 String[] val = { "January", "February", "March", "April", "May", 149 "June", "July", "August", "September", "October", "November", 150 "December"}; 151 assertEquals("Returned wrong array: ", val.length, retVal.length); 152 for (int i = 0; i < val.length; i++) 153 assertTrue("Array values do not match", retVal[i].equals(val[i])); 154 } 155 156 /** 157 * @tests java.text.DateFormatSymbols#getShortMonths() 158 */ 159 public void test_getShortMonths() { 160 // Test for method java.lang.String [] 161 // java.text.DateFormatSymbols.getShortMonths() 162 String[] retVal = dfs.getShortMonths(); 163 String[] val = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 164 "Aug", "Sep", "Oct", "Nov", "Dec"}; 165 assertEquals("Returned wrong array: ", val.length, retVal.length); 166 for (int i = 0; i < val.length; i++) 167 assertTrue("Array values do not match", retVal[i].equals(val[i])); 168 } 169 170 /** 171 * @tests java.text.DateFormatSymbols#getShortWeekdays() 172 */ 173 public void test_getShortWeekdays() { 174 // Test for method java.lang.String [] 175 // java.text.DateFormatSymbols.getShortWeekdays() 176 String[] retVal = dfs.getShortWeekdays(); 177 String[] val = { "", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; 178 if (retVal.length != val.length) 179 fail("Returned wrong array"); 180 for (int i = 0; i < val.length; i++) 181 assertTrue("Array values do not match", retVal[i].equals(val[i])); 182 } 183 184 /** 185 * @tests java.text.DateFormatSymbols#getWeekdays() 186 */ 187 public void test_getWeekdays() { 188 // Test for method java.lang.String [] 189 // java.text.DateFormatSymbols.getWeekdays() 190 String[] retVal = dfs.getWeekdays(); 191 String[] val = { "", "Sunday", "Monday", "Tuesday", "Wednesday", 192 "Thursday", "Friday", "Saturday" }; 193 if (retVal.length != val.length) 194 fail("Returned wrong array"); 195 for (int i = 0; i < val.length; i++) 196 assertTrue("Array values do not match", retVal[i].equals(val[i])); 197 } 198 199 /** 200 * @tests java.text.DateFormatSymbols#getZoneStrings() 201 */ 202 public void test_getZoneStrings() { 203 // Test for method java.lang.String [][] 204 // java.text.DateFormatSymbols.getZoneStrings() 205 String[][] val = { { "XX", "XX", "XX", "XX", "XX" }, 206 { "YY", "YY", "YY", "YY", "YY" } }; 207 dfs.setZoneStrings(val); 208 String[][] retVal = dfs.getZoneStrings(); 209 if (retVal.length != val.length) 210 fail("Returned wrong array"); 211 for (int i = 0; i < val.length; i++) 212 assertTrue("Failed to set strings", Arrays 213 .equals(retVal[i], val[i])); 214 } 215 216 /** 217 * @tests java.text.DateFormatSymbols#hashCode() 218 */ 219 public void test_hashCode() { 220 // Test for method int java.text.DateFormatSymbols.hashCode() 221 int hc1 = dfs.hashCode(); 222 int hc2 = dfs.hashCode(); 223 assertTrue("hashCode() returned inconsistent number : " + hc1 + " - " + hc2, hc1 == hc2); 224 225 assertTrue("hashCode() returns different values for equal() objects", 226 dfs.hashCode() == dfs.clone().hashCode()); 227 } 228 229 /** 230 * @tests java.text.DateFormatSymbols#setAmPmStrings(java.lang.String[]) 231 */ 232 public void test_setAmPmStrings$Ljava_lang_String() { 233 // Test for method void 234 // java.text.DateFormatSymbols.setAmPmStrings(java.lang.String []) 235 String[] val = { "XX", "YY" }; 236 dfs.setAmPmStrings(val); 237 String[] retVal = dfs.getAmPmStrings(); 238 if (retVal.length != val.length) 239 fail("Returned wrong array"); 240 for (int i = 0; i < val.length; i++) 241 assertTrue("Failed to set strings", retVal[i].equals(val[i])); 242 } 243 244 /** 245 * @tests java.text.DateFormatSymbols#setEras(java.lang.String[]) 246 */ 247 public void test_setEras$Ljava_lang_String() { 248 // Test for method void 249 // java.text.DateFormatSymbols.setEras(java.lang.String []) 250 String[] val = { "XX", "YY" }; 251 dfs.setEras(val); 252 String[] retVal = dfs.getEras(); 253 if (retVal.length != val.length) 254 fail("Returned wrong array"); 255 for (int i = 0; i < val.length; i++) 256 assertTrue("Failed to set strings", retVal[i].equals(val[i])); 257 } 258 259 public void test_setLocalPatternCharsLjava_lang_String() { 260 String patternChars = "GyMZZkHmsSEHHFwWahKz"; 261 dfs.setLocalPatternChars(patternChars); 262 assertEquals(patternChars, dfs.getLocalPatternChars()); 263 264 try { 265 // Regression for HARMONY-466 266 new DateFormatSymbols().setLocalPatternChars(null); 267 fail(); 268 } catch (NullPointerException expected) { 269 } 270 } 271 272 /** 273 * @tests java.text.DateFormatSymbols#setMonths(java.lang.String[]) 274 */ 275 public void test_setMonths$Ljava_lang_String() { 276 // Test for method void 277 // java.text.DateFormatSymbols.setMonths(java.lang.String []) 278 String[] val = { "XX", "YY" }; 279 dfs.setMonths(val); 280 String[] retVal = dfs.getMonths(); 281 assertTrue("Return is identical", retVal != dfs.getMonths()); 282 if (retVal.length != val.length) 283 fail("Returned wrong array"); 284 for (int i = 0; i < val.length; i++) 285 assertTrue("Failed to set strings", retVal[i].equals(val[i])); 286 } 287 288 /** 289 * @tests java.text.DateFormatSymbols#setShortMonths(java.lang.String[]) 290 */ 291 public void test_setShortMonths$Ljava_lang_String() { 292 // Test for method void 293 // java.text.DateFormatSymbols.setShortMonths(java.lang.String []) 294 String[] val = { "XX", "YY" }; 295 dfs.setShortMonths(val); 296 String[] retVal = dfs.getShortMonths(); 297 assertTrue("Return is identical", retVal != dfs.getShortMonths()); 298 if (retVal.length != val.length) 299 fail("Returned wrong array"); 300 for (int i = 0; i < val.length; i++) 301 assertTrue("Failed to set strings", retVal[i].equals(val[i])); 302 } 303 304 /** 305 * @tests java.text.DateFormatSymbols#setShortWeekdays(java.lang.String[]) 306 */ 307 public void test_setShortWeekdays$Ljava_lang_String() { 308 // Test for method void 309 // java.text.DateFormatSymbols.setShortWeekdays(java.lang.String []) 310 String[] val = { "XX", "YY" }; 311 dfs.setShortWeekdays(val); 312 String[] retVal = dfs.getShortWeekdays(); 313 assertTrue("Return is identical", retVal != dfs.getShortWeekdays()); 314 if (retVal.length != val.length) 315 fail("Returned wrong array"); 316 for (int i = 0; i < val.length; i++) 317 assertTrue("Failed to set strings", retVal[i].equals(val[i])); 318 } 319 320 /** 321 * @tests java.text.DateFormatSymbols#setWeekdays(java.lang.String[]) 322 */ 323 public void test_setWeekdays$Ljava_lang_String() { 324 // Test for method void 325 // java.text.DateFormatSymbols.setWeekdays(java.lang.String []) 326 String[] val = { "XX", "YY" }; 327 dfs.setWeekdays(val); 328 String[] retVal = dfs.getWeekdays(); 329 assertTrue("Return is identical", retVal != dfs.getWeekdays()); 330 if (retVal.length != val.length) 331 fail("Returned wrong array"); 332 for (int i = 0; i < val.length; i++) 333 assertTrue("Failed to set strings", retVal[i].equals(val[i])); 334 } 335 336 /** 337 * @tests java.text.DateFormatSymbols#setZoneStrings(java.lang.String[][]) 338 */ 339 public void test_setZoneStrings$$Ljava_lang_String() { 340 // Test for method void 341 // java.text.DateFormatSymbols.setZoneStrings(java.lang.String [][]) 342 String[][] val = { { "XX", "XX", "XX", "XX", "XX" }, 343 { "YY", "YY", "YY", "YY", "YY" } }; 344 dfs.setZoneStrings(val); 345 String[][] retVal = dfs.getZoneStrings(); 346 assertTrue("get returns identical", retVal != dfs.getZoneStrings()); 347 assertTrue("get[0] returns identical", retVal[0] != dfs 348 .getZoneStrings()[0]); 349 assertTrue("get returned identical", retVal != val); 350 assertEquals("Returned wrong array", val.length, retVal.length); 351 for (int i = 0; i < val.length; i++) 352 assertTrue("Failed to set strings: " + retVal[i], Arrays.equals( 353 retVal[i], val[i])); 354 } 355 356 /** 357 * @tests java.text.DateFormatSymbols#setZoneStrings(java.lang.String[][]) 358 * 359 * Tests setting zone strings to invalid values 360 * Regression for HARMONY-6337 361 */ 362 public void test_setZoneStrings_invalid() { 363 // failing cases 364 String[][] val1 = null; 365 try { 366 dfs.setZoneStrings(val1); 367 fail("Attempt to set zone strings a null array should throw NullPointerException"); 368 } catch (NullPointerException e) { 369 // expected 370 } 371 372 String[][] val2 = { { "XX", "XX" }, { "YY", "YY" } }; 373 try { 374 dfs.setZoneStrings(val2); 375 fail("Attempt to set zone strings to a 2D array that contains one or more " 376 + "rows of length less than 5 should throw IllegalArgumentException"); 377 } catch (IllegalArgumentException e) { 378 // expected because each subarray has length < 5 379 } 380 381 String[][] val3 = { { "a", "b", "c", "d", "e" }, 382 { "a", "b", "c", "d", "e" }, 383 { "a", "b", "c", "d" }, 384 { "a", "b", "c", "d", "e" } }; 385 try { 386 dfs.setZoneStrings(val3); 387 fail("Attempt to set zone strings to a 2D array that contains one or more " 388 + "rows of length less than 5 should throw IllegalArgumentException"); 389 } catch (IllegalArgumentException e) { 390 // expected because each subarray has length < 5 391 } 392 } 393 394 /** 395 * Sets up the fixture, for example, open a network connection. This method 396 * is called before a test is executed. 397 */ 398 protected void setUp() { 399 dfs = new DateFormatSymbols(new Locale("en", "us")); 400 } 401 402 // Test serialization mechanism of DateFormatSymbols 403 public void test_serialization() throws Exception { 404 DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRANCE); 405 String[][] zoneStrings = symbols.getZoneStrings(); 406 assertNotNull(zoneStrings); 407 408 // serialize 409 ByteArrayOutputStream byteOStream = new ByteArrayOutputStream(); 410 ObjectOutputStream objectOStream = new ObjectOutputStream(byteOStream); 411 objectOStream.writeObject(symbols); 412 413 // and deserialize 414 ObjectInputStream objectIStream = new ObjectInputStream( 415 new ByteArrayInputStream(byteOStream.toByteArray())); 416 DateFormatSymbols symbolsD = (DateFormatSymbols) objectIStream 417 .readObject(); 418 419 String[][] zoneStringsD = symbolsD.getZoneStrings(); 420 assertNotNull(zoneStringsD); 421 assertEquals(symbols, symbolsD); 422 } 423} 424