1// © 2016 and later: Unicode, Inc. and others. 2// License & terms of use: http://www.unicode.org/copyright.html#License 3/***************************************************************************************** 4 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved 5 * (C) Copyright IBM Corp. 1996-2012 - All Rights Reserved 6 * 7 * The original version of this source code and documentation is copyrighted and 8 * owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are 9 * provided under terms of a License Agreement between Taligent and Sun. This 10 * technology is protected by multiple US and International patents. This notice and 11 * attribution to Taligent may not be removed. 12 * Taligent is a registered trademark of Taligent, Inc. 13 **/ 14 15/** 16 * Port From: JDK 1.4b1 : java.text.Format.IntlTestSimpleDateFormatAPI 17 * Source File: java/text/format/IntlTestSimpleDateFormatAPI.java 18 **/ 19 20package com.ibm.icu.dev.test.format; 21 22import java.text.FieldPosition; 23import java.text.Format; 24import java.text.ParseException; 25import java.text.ParsePosition; 26import java.util.Date; 27import java.util.Locale; 28 29import org.junit.Test; 30import org.junit.runner.RunWith; 31import org.junit.runners.JUnit4; 32 33import com.ibm.icu.dev.test.TestFmwk; 34import com.ibm.icu.text.DateFormatSymbols; 35import com.ibm.icu.text.SimpleDateFormat; 36 37/** 38* @test 1.4 98/03/06 39* @summary test International Simple Date Format API 40*/ 41@RunWith(JUnit4.class) 42public class IntlTestSimpleDateFormatAPI extends TestFmwk 43{ 44 // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage. 45 @Test 46 public void TestAPI() 47 { 48 logln("SimpleDateFormat API test---"); logln(""); 49 50 Locale.setDefault(Locale.ENGLISH); 51 52 // ======= Test constructors 53 54 logln("Testing SimpleDateFormat constructors"); 55 56 SimpleDateFormat def = new SimpleDateFormat(); 57 58 final String pattern = new String("yyyy.MM.dd G 'at' hh:mm:ss z"); 59 SimpleDateFormat pat = new SimpleDateFormat(pattern); 60 61 SimpleDateFormat pat_fr = new SimpleDateFormat(pattern, Locale.FRENCH); 62 63 DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRENCH); 64 65 SimpleDateFormat cust1 = new SimpleDateFormat(pattern, symbols); 66 67 // ======= Test clone() and equality 68 69 logln("Testing clone(), assignment and equality operators"); 70 71 Format clone = (Format) def.clone(); 72 if( ! clone.equals(def) ) { 73 errln("ERROR: Format clone or equals failed"); 74 } 75 76 // ======= Test various format() methods 77 78 logln("Testing various format() methods"); 79 80 Date d = new Date((long)837039928046.0); 81 82 StringBuffer res1 = new StringBuffer(); 83 StringBuffer res2 = new StringBuffer(); 84 FieldPosition pos1 = new FieldPosition(0); 85 FieldPosition pos2 = new FieldPosition(0); 86 87 res1 = def.format(d, res1, pos1); 88 logln( "" + d.getTime() + " formatted to " + res1); 89 90 res2 = cust1.format(d, res2, pos2); 91 logln("" + d.getTime() + " formatted to " + res2); 92 93 // ======= Test parse() 94 95 logln("Testing parse()"); 96 97 String text = new String("02/03/76, 2:50 AM, CST"); 98 Date result1 = new Date(); 99 Date result2 = new Date(); 100 ParsePosition pos= new ParsePosition(0); 101 result1 = def.parse(text, pos); 102 logln(text + " parsed into " + result1); 103 104 try { 105 result2 = def.parse(text); 106 } 107 catch (ParseException e) { 108 errln("ERROR: parse() failed"); 109 } 110 logln(text + " parsed into " + result2); 111 112 // ======= Test getters and setters 113 114 logln("Testing getters and setters"); 115 116 final DateFormatSymbols syms = pat.getDateFormatSymbols(); 117 def.setDateFormatSymbols(syms); 118 pat_fr.setDateFormatSymbols(syms); 119 if( ! pat.getDateFormatSymbols().equals(def.getDateFormatSymbols()) ) { 120 errln("ERROR: set DateFormatSymbols() failed"); 121 } 122 123 /* 124 DateFormatSymbols has not the method getTwoDigitStartDate(); 125 //Date startDate = null; //The variable is never used 126 try { 127// startDate = pat.getTwoDigitStartDate(); 128 } 129 catch (Exception e) { 130 errln("ERROR: getTwoDigitStartDate() failed"); 131 } 132 133 try { 134// pat_fr.setTwoDigitStartDate(startDate); 135 } 136 catch (Exception e) { 137 errln("ERROR: setTwoDigitStartDate() failed"); 138 }*/ 139 140 // ======= Test applyPattern() 141 142 logln("Testing applyPattern()"); 143 144 String p1 = new String("yyyy.MM.dd G 'at' hh:mm:ss z"); 145 logln("Applying pattern " + p1); 146 pat.applyPattern(p1); 147 148 String s2 = pat.toPattern(); 149 logln("Extracted pattern is " + s2); 150 if( ! s2.equals(p1) ) { 151 errln("ERROR: toPattern() result did not match pattern applied"); 152 } 153 154 logln("Applying pattern " + p1); 155 pat.applyLocalizedPattern(p1); 156 String s3 = pat.toLocalizedPattern(); 157 logln("Extracted pattern is " + s3); 158 if( ! s3.equals(p1) ) { 159 errln("ERROR: toLocalizedPattern() result did not match pattern applied"); 160 } 161 162 // ======= Test for Ticket 5684 (Parsing patterns with 'Y' and 'e' 163 logln("Testing parse()"); 164 165 String p2 = new String("YYYY'W'wwe"); 166 logln("Applying pattern " + p2); 167 pat.applyPattern(p2); 168 Date dt = pat.parse("2007W014", new ParsePosition(0)); 169 if (dt == null) { 170 errln("ERROR: Parsing failed using 'Y' and 'e'"); 171 } 172 173 174 // ======= Test getStaticClassID() 175 176// logln("Testing instanceof"); 177 178// try { 179// DateFormat test = new SimpleDateFormat(); 180 181// if (! (test instanceof SimpleDateFormat)) { 182// errln("ERROR: instanceof failed"); 183// } 184// } 185// catch (Exception e) { 186// errln("ERROR: Couldn't create a SimpleDateFormat"); 187// } 188 } 189 190 // Jitterbug 4451, for coverage 191 @Test 192 public void TestCoverage(){ 193 class StubDateFormat extends SimpleDateFormat{ 194 /** 195 * For serialization 196 */ 197 private static final long serialVersionUID = 8460897119491427934L; 198 199 public void run(){ 200 if (!zeroPaddingNumber(12, 4, 6).equals("0012")){ 201 errln("SimpleDateFormat(zeroPaddingNumber(long , int , int )"); 202 } 203 } 204 } 205 new StubDateFormat().run(); 206 } 207} 208