Home | History | Annotate | Download | only in format
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *******************************************************************************
      5  * Copyright (C) 2001-2010, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8  */
      9 
     10 /**
     11  * Port From:   ICU4C v1.8.1 : format : IntlTestDateFormatAPI
     12  * Source File: $ICU4CRoot/source/test/intltest/dtfmapts.cpp
     13  **/
     14 
     15 package com.ibm.icu.dev.test.format;
     16 
     17 import java.text.FieldPosition;
     18 import java.text.ParsePosition;
     19 import java.util.Date;
     20 
     21 import org.junit.Test;
     22 import org.junit.runner.RunWith;
     23 import org.junit.runners.JUnit4;
     24 
     25 import com.ibm.icu.dev.test.TestFmwk;
     26 import com.ibm.icu.text.DateFormat;
     27 import com.ibm.icu.text.DecimalFormat;
     28 import com.ibm.icu.text.NumberFormat;
     29 import com.ibm.icu.text.SimpleDateFormat;
     30 
     31 /*
     32  * This is an API test, not a unit test.  It doesn't test very many cases, and doesn't
     33  * try to test the full functionality.  It just calls each function in the class and
     34  * verifies that it works on a basic level.
     35  */
     36 @RunWith(JUnit4.class)
     37 public class IntlTestDateFormatAPIC extends TestFmwk {
     38     /**
     39      * Test hiding of parse() and format() APIs in the Format hierarchy.
     40      * We test the entire hierarchy, even though this test is located in
     41      * the DateFormat API test.
     42      */
     43     @Test
     44     public void TestNameHiding() {
     45 
     46         // N.B.: This test passes if it COMPILES, since it's a test of
     47         // compile-time name hiding.
     48 
     49         Date dateObj = new Date(0);
     50         Number numObj = new Double(3.1415926535897932384626433832795);
     51         StringBuffer strBuffer = new StringBuffer("");
     52         String str;
     53         FieldPosition fpos = new FieldPosition(0);
     54         ParsePosition ppos = new ParsePosition(0);
     55 
     56         // DateFormat calling Format API
     57         {
     58             logln("DateFormat");
     59             DateFormat dateFmt = DateFormat.getInstance();
     60             if (dateFmt != null) {
     61                 str = dateFmt.format(dateObj);
     62                 strBuffer = dateFmt.format(dateObj, strBuffer, fpos);
     63             } else {
     64                 errln("FAIL: Can't create DateFormat");
     65             }
     66         }
     67 
     68         // SimpleDateFormat calling Format & DateFormat API
     69         {
     70             logln("SimpleDateFormat");
     71             SimpleDateFormat sdf = new SimpleDateFormat();
     72             // Format API
     73             str = sdf.format(dateObj);
     74             strBuffer = sdf.format(dateObj, strBuffer, fpos);
     75             // DateFormat API
     76             strBuffer = sdf.format(new Date(0), strBuffer, fpos);
     77             str = sdf.format(new Date(0));
     78             try {
     79                 sdf.parse(str);
     80                 sdf.parse(str, ppos);
     81             } catch (java.text.ParseException pe) {
     82                 System.out.println(pe);
     83             }
     84         }
     85 
     86         // NumberFormat calling Format API
     87         {
     88             logln("NumberFormat");
     89             NumberFormat fmt = NumberFormat.getInstance();
     90             if (fmt != null) {
     91                 str = fmt.format(numObj);
     92                 strBuffer = fmt.format(numObj, strBuffer, fpos);
     93             } else {
     94                 errln("FAIL: Can't create NumberFormat");
     95             }
     96         }
     97 
     98         // DecimalFormat calling Format & NumberFormat API
     99         {
    100             logln("DecimalFormat");
    101             DecimalFormat fmt = new DecimalFormat();
    102             // Format API
    103             str = fmt.format(numObj);
    104             strBuffer = fmt.format(numObj, strBuffer, fpos);
    105             // NumberFormat API
    106             str = fmt.format(2.71828);
    107             str = fmt.format(1234567);
    108             strBuffer = fmt.format(1.41421, strBuffer, fpos);
    109             strBuffer = fmt.format(9876543, strBuffer, fpos);
    110             Number obj = fmt.parse(str, ppos);
    111             try {
    112                 obj = fmt.parse(str);
    113                 if(obj==null){
    114                     errln("FAIL: The format object could not parse the string : "+str);
    115                 }
    116             } catch (java.text.ParseException pe) {
    117                 System.out.println(pe);
    118             }
    119         }
    120 
    121         //ICU4J have not the classes ChoiceFormat and MessageFormat
    122         /*
    123         // ChoiceFormat calling Format & NumberFormat API
    124         {
    125             logln("ChoiceFormat");
    126             ChoiceFormat fmt = new ChoiceFormat("0#foo|1#foos|2#foos");
    127             // Format API
    128             str = fmt.format(numObj);
    129             strBuffer = fmt.format(numObj, strBuffer, fpos);
    130             // NumberFormat API
    131             str = fmt.format(2.71828);
    132             str = fmt.format(1234567);
    133             strBuffer = fmt.format(1.41421, strBuffer, fpos);
    134             strBuffer = fmt.format(9876543, strBuffer, fpos);
    135             Number obj = fmt.parse(str, ppos);
    136             try {
    137                 obj = fmt.parse(str);
    138             } catch (java.text.ParseException pe) {
    139                 System.out.println(pe);
    140             }
    141         }
    142 
    143 
    144         // MessageFormat calling Format API
    145         {
    146             logln("MessageFormat");
    147             MessageFormat fmt = new MessageFormat("");
    148             // Format API
    149             // We use dateObj, which MessageFormat should reject.
    150             // We're testing name hiding, not the format method.
    151             try {
    152                 str = fmt.format(dateObj);
    153             } catch (Exception e) {
    154                 //e.printStackTrace();
    155             }
    156             try {
    157                 strBuffer = fmt.format(dateObj, strBuffer, fpos);
    158             } catch (Exception e) {
    159                 //e.printStackTrace();
    160             }
    161         }
    162         */
    163     }
    164 }