1/* GENERATED SOURCE. DO NOT MODIFY. */
2// © 2016 and later: Unicode, Inc. and others.
3// License & terms of use: http://www.unicode.org/copyright.html#License
4/*
5 *******************************************************************************
6 * Copyright (c) 2004-2011, International Business Machines
7 * Corporation and others.  All Rights Reserved.
8 * Copyright (C) 2010 , Yahoo! Inc.
9 *******************************************************************************
10 */
11package android.icu.dev.test.format;
12
13import java.text.FieldPosition;
14import java.text.ParsePosition;
15
16import org.junit.Test;
17
18import android.icu.dev.test.TestFmwk;
19import android.icu.text.SelectFormat;
20
21/**
22 * @author kirtig
23 * This class tests the API functionality of the SelectFormat
24 */
25public class SelectFormatAPITest extends TestFmwk {
26
27    static final String SIMPLE_PATTERN1 = "feminine {feminineVerbValue1} other{otherVerbValue1}";
28    static final String SIMPLE_PATTERN2 = "feminine {feminineVerbValue2} other{otherVerbValue2}";
29
30    /**
31     * API tests for constructors
32     */
33    @Test
34    public void TestConstructors() {
35        SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1);
36        assertNotNull("Error: TestConstructors - SelectFormat object constructed "
37                      + "with argument constructor is null" , selFmt );
38    }
39
40    /**
41     * API tests for equals() method
42     */
43    @Test
44    public void TestEquals() {
45        SelectFormat selFmt1 = null;
46
47        //Check equality for pattern constructed SelectFormats
48        selFmt1 = new SelectFormat(SIMPLE_PATTERN1);
49        SelectFormat selFmt2 = new SelectFormat(SIMPLE_PATTERN1);
50        assertTrue("Equals test failed while checking equality for "
51                   + "pattern constructed SelectFormats ."
52                   , selFmt1.equals(selFmt2) );
53
54        //Check equality for 2 objects
55        Object selFmt3 = new SelectFormat(SIMPLE_PATTERN1);
56        Object selFmt4 = new SelectFormat(SIMPLE_PATTERN1);
57        Object selFmt5 = new SelectFormat(SIMPLE_PATTERN2);
58        assertTrue("Equals test failed while checking equality for object 1."
59                , selFmt3.equals(selFmt4) );
60        assertTrue("Equals test failed while checking equality for object 2."
61                    , selFmt1.equals(selFmt3) );
62        assertFalse("Equals test failed while checking equality for object 3."
63                , selFmt3.equals(selFmt5) );
64    }
65
66    /**
67     * API tests for applyPattern() method
68     */
69    @Test
70    public void TestApplyPatternToPattern() {
71        SelectFormat selFmt = null;
72        String pattern = "masculine{masculineVerbValue} other{otherVerbValue}";
73
74        //Check for applyPattern/toPattern
75        selFmt = new SelectFormat(SIMPLE_PATTERN1);
76        selFmt.applyPattern(pattern);
77        assertEquals("Failed in applyPattern,toPattern with unexpected output"
78                     , pattern,  selFmt.toPattern() );
79
80        //Check for invalid pattern
81        try {
82            String brokenPattern = "broken }{ pattern";
83            selFmt.applyPattern(brokenPattern);
84            errln("Failed in applyPattern.  applyPattern should throw IllegalArgumentException for " + brokenPattern);
85        } catch (IllegalArgumentException e) {
86            // This is OK
87        }
88    }
89
90    /**
91     * API tests for toString() method
92     */
93    @Test
94    public void TestToString(){
95        SelectFormat selFmt = null;
96
97        //Check toString for pattern constructed SelectFormat
98        selFmt = new SelectFormat(SIMPLE_PATTERN1);
99        String expected = "pattern='feminine {feminineVerbValue1} other{otherVerbValue1}'";
100        assertEquals("Failed in TestToString with unexpected output 2"
101                     , expected, selFmt.toString() );
102    }
103
104    /**
105     * API tests for hashCode() method
106     */
107    @Test
108    public void TestHashCode(){
109        //Check hashCode for pattern constructed SelectFormat
110        SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1);
111        SelectFormat selFmt1 = new SelectFormat(SIMPLE_PATTERN1);
112        SelectFormat selFmt2 = new SelectFormat(SIMPLE_PATTERN2);
113        assertEquals("Failed in TestHashCode 1 with unexpected output"
114                     , selFmt.hashCode(), selFmt1.hashCode() );
115        assertNotEquals("Failed in TestHashCode 2 with unexpected output"
116                     , selFmt.hashCode(), selFmt2.hashCode() );
117    }
118
119    /**
120     * API tests for toPattern() method
121     */
122    @Test
123    public void TestToPattern(){
124        SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1);
125        assertEquals("Failed in TestToPattern 2 with unexpected output"
126                     , SIMPLE_PATTERN1, selFmt.toPattern() );
127    }
128
129    /**
130     * API tests for format() method
131     */
132    @Test
133    public void TestFormat(){
134        //Check format for pattern constructed object
135        SelectFormat selFmt1 = new SelectFormat(SIMPLE_PATTERN1);
136        String expected = "feminineVerbValue1";
137        assertEquals("Failed in TestFormat with unexpected output 1"
138                     , expected
139                     , selFmt1.format("feminine") );
140
141        //Check format with appendTo for pattern constructed object
142        expected = "AppendHere-otherVerbValue1";
143        StringBuffer strBuf = new StringBuffer("AppendHere-");
144        assertEquals("Failed in TestFormat with unexpected output 2"
145                     , expected
146                     , (selFmt1.format("other", strBuf, new FieldPosition(0))).toString());
147
148        //Check format throws exception on invalid argument.
149        boolean threwException = false;
150        try {
151            StringBuffer buf = new StringBuffer("AppendHere-");
152            selFmt1.format(Integer.valueOf(0), buf, new FieldPosition(0));
153        } catch (IllegalArgumentException e) {
154            threwException = true;
155        }
156        assertTrue("Did not throw IllegalArgumentException.", threwException);
157    }
158
159    /**
160     * API tests for parseObject() method
161     */
162    @Test
163    public void TestParseObject(){
164        //Check parseObject
165        try {
166            SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1);
167            selFmt.parseObject("feminine", new ParsePosition(0) );
168            fail("Failed in TestParseObject - UnsupportedOperationException not received");
169        } catch (UnsupportedOperationException e){
170            //Expect this Exception
171        }
172    }
173}
174
175