summaryrefslogtreecommitdiff
path: root/android_icu4j/src/main/tests/android/icu/dev/test/format/IntlTestDateFormatAPIC.java
blob: a5acb752c0394202920badc163a3ff962c74c543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* GENERATED SOURCE. DO NOT MODIFY. */
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
 *******************************************************************************
 * Copyright (C) 2001-2010, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */

/**
 * Port From:   ICU4C v1.8.1 : format : IntlTestDateFormatAPI
 * Source File: $ICU4CRoot/source/test/intltest/dtfmapts.cpp
 **/

package android.icu.dev.test.format;

import java.text.FieldPosition;
import java.text.ParsePosition;
import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import android.icu.dev.test.CoreTestFmwk;
import android.icu.text.DateFormat;
import android.icu.text.DecimalFormat;
import android.icu.text.NumberFormat;
import android.icu.text.SimpleDateFormat;
import android.icu.testsharding.MainTestShard;

/*
 * This is an API test, not a unit test.  It doesn't test very many cases, and doesn't
 * try to test the full functionality.  It just calls each function in the class and
 * verifies that it works on a basic level.
 */
@MainTestShard
@RunWith(JUnit4.class)
public class IntlTestDateFormatAPIC extends CoreTestFmwk {
    /**
     * Test hiding of parse() and format() APIs in the Format hierarchy.
     * We test the entire hierarchy, even though this test is located in
     * the DateFormat API test.
     */
    @Test
    public void TestNameHiding() {

        // N.B.: This test passes if it COMPILES, since it's a test of
        // compile-time name hiding.

        Date dateObj = new Date(0);
        Number numObj = 3.1415926535897932384626433832795;
        StringBuffer strBuffer = new StringBuffer("");
        String str;
        FieldPosition fpos = new FieldPosition(0);
        ParsePosition ppos = new ParsePosition(0);

        // DateFormat calling Format API
        {
            logln("DateFormat");
            DateFormat dateFmt = DateFormat.getInstance();
            if (dateFmt != null) {
                str = dateFmt.format(dateObj);
                strBuffer = dateFmt.format(dateObj, strBuffer, fpos);
            } else {
                errln("FAIL: Can't create DateFormat");
            }
        }

        // SimpleDateFormat calling Format & DateFormat API
        {
            logln("SimpleDateFormat");
            SimpleDateFormat sdf = new SimpleDateFormat();
            // Format API
            str = sdf.format(dateObj);
            strBuffer = sdf.format(dateObj, strBuffer, fpos);
            // DateFormat API
            strBuffer = sdf.format(new Date(0), strBuffer, fpos);
            str = sdf.format(new Date(0));
            try {
                sdf.parse(str);
                sdf.parse(str, ppos);
            } catch (java.text.ParseException pe) {
                System.out.println(pe);
            }
        }

        // NumberFormat calling Format API
        {
            logln("NumberFormat");
            NumberFormat fmt = NumberFormat.getInstance();
            if (fmt != null) {
                str = fmt.format(numObj);
                strBuffer = fmt.format(numObj, strBuffer, fpos);
            } else {
                errln("FAIL: Can't create NumberFormat");
            }
        }

        // DecimalFormat calling Format & NumberFormat API
        {
            logln("DecimalFormat");
            DecimalFormat fmt = new DecimalFormat();
            // Format API
            str = fmt.format(numObj);
            strBuffer = fmt.format(numObj, strBuffer, fpos);
            // NumberFormat API
            str = fmt.format(2.71828);
            str = fmt.format(1234567);
            strBuffer = fmt.format(1.41421, strBuffer, fpos);
            strBuffer = fmt.format(9876543, strBuffer, fpos);
            Number obj = fmt.parse(str, ppos);
            try {
                obj = fmt.parse(str);
                if(obj==null){
                    errln("FAIL: The format object could not parse the string : "+str);
                }
            } catch (java.text.ParseException pe) {
                System.out.println(pe);
            }
        }

        //ICU4J have not the classes ChoiceFormat and MessageFormat
        /*
        // ChoiceFormat calling Format & NumberFormat API
        {
            logln("ChoiceFormat");
            ChoiceFormat fmt = new ChoiceFormat("0#foo|1#foos|2#foos");
            // Format API
            str = fmt.format(numObj);
            strBuffer = fmt.format(numObj, strBuffer, fpos);
            // NumberFormat API
            str = fmt.format(2.71828);
            str = fmt.format(1234567);
            strBuffer = fmt.format(1.41421, strBuffer, fpos);
            strBuffer = fmt.format(9876543, strBuffer, fpos);
            Number obj = fmt.parse(str, ppos);
            try {
                obj = fmt.parse(str);
            } catch (java.text.ParseException pe) {
                System.out.println(pe);
            }
        }


        // MessageFormat calling Format API
        {
            logln("MessageFormat");
            MessageFormat fmt = new MessageFormat("");
            // Format API
            // We use dateObj, which MessageFormat should reject.
            // We're testing name hiding, not the format method.
            try {
                str = fmt.format(dateObj);
            } catch (Exception e) {
                //e.printStackTrace();
            }
            try {
                strBuffer = fmt.format(dateObj, strBuffer, fpos);
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
        */
    }
}