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