2 *******************************************************************************
\r
3 * Copyright (c) 2004-2010, International Business Machines
\r
4 * Corporation and others. All Rights Reserved.
\r
5 * Copyright (C) 2010 , Yahoo! Inc.
\r
6 *******************************************************************************
\r
8 package com.ibm.icu.dev.test.format;
\r
10 import java.text.FieldPosition;
\r
11 import java.text.ParsePosition;
\r
13 import com.ibm.icu.dev.test.TestFmwk;
\r
14 import com.ibm.icu.text.SelectFormat;
\r
18 * This class tests the API functionality of the SelectFormat
\r
20 public class SelectFormatAPITest extends TestFmwk {
\r
22 static final String SIMPLE_PATTERN1 = "feminine {feminineVerbValue1} other{otherVerbValue1}";
\r
23 static final String SIMPLE_PATTERN2 = "feminine {feminineVerbValue2} other{otherVerbValue2}";
\r
25 public static void main(String[] args) throws Exception {
\r
26 new SelectFormatAPITest().run(args);
\r
30 * API tests for constructors
\r
32 public void TestConstructors() {
\r
33 log("Inside TestConstructors");
\r
35 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1);
\r
36 assertNotNull("Error: TestConstructors - SelectFormat object constructed "
\r
37 + "with argument constructor is null" , selFmt );
\r
38 } catch (Exception e){
\r
39 errln("Exception encountered in TestConstructors while creating a constructor with argument");
\r
44 * API tests for equals() method
\r
46 public void TestEquals() {
\r
47 log("Inside TestEquals");
\r
48 SelectFormat selFmt1 = null;
\r
50 //Check equality for pattern constructed SelectFormats
\r
52 selFmt1 = new SelectFormat(SIMPLE_PATTERN1);
\r
53 SelectFormat selFmt2 = new SelectFormat(SIMPLE_PATTERN1);
\r
54 assertTrue("Equals test failed while checking equality for "
\r
55 + "pattern constructed SelectFormats ."
\r
56 , selFmt1.equals(selFmt2) );
\r
57 } catch (Exception e){
\r
58 errln("Exception encountered in TestEquals 1 " + e.getMessage());
\r
61 //Check equality for 2 objects
\r
63 Object selFmt3 = new SelectFormat(SIMPLE_PATTERN1);
\r
64 Object selFmt4 = new SelectFormat(SIMPLE_PATTERN1);
\r
65 Object selFmt5 = new SelectFormat(SIMPLE_PATTERN2);
\r
66 assertTrue("Equals test failed while checking equality for object 1."
\r
67 , selFmt3.equals(selFmt4) );
\r
68 assertTrue("Equals test failed while checking equality for object 2."
\r
69 , selFmt1.equals(selFmt3) );
\r
70 assertFalse("Equals test failed while checking equality for object 3."
\r
71 , selFmt3.equals(selFmt5) );
\r
72 } catch (Exception e){
\r
73 errln("Exception encountered in TestEquals 2" + e.getMessage());
\r
78 * API tests for applyPattern() method
\r
80 public void TestApplyPatternToPattern() {
\r
81 log("Inside TestApplyPatternToPattern");
\r
82 SelectFormat selFmt = null;
\r
83 String pattern = "masculine{masculineVerbValue} other{otherVerbValue}";
\r
85 //Check for applyPattern/toPattern
\r
87 selFmt = new SelectFormat(SIMPLE_PATTERN1);
\r
88 selFmt.applyPattern(pattern);
\r
89 assertEquals("Failed in applyPattern,toPattern with unexpected output"
\r
90 , pattern, selFmt.toPattern() );
\r
91 } catch (Exception e){
\r
92 errln("Exception encountered in TestApplyPatternToPattern");
\r
94 //Check for invalid pattern
\r
96 String brokenPattern = "broken }{ pattern";
\r
97 selFmt.applyPattern(brokenPattern);
\r
98 errln("Failed in applyPattern. applyPattern should throw IllegalArgumentException for " + brokenPattern);
\r
99 } catch (IllegalArgumentException e) {
\r
105 * API tests for toString() method
\r
107 public void TestToString(){
\r
108 log("Inside TestToString");
\r
109 SelectFormat selFmt = null;
\r
111 //Check toString for pattern constructed SelectFormat
\r
113 selFmt = new SelectFormat(SIMPLE_PATTERN1);
\r
114 String expected = "pattern='feminine {feminineVerbValue1} other{otherVerbValue1}'";
\r
115 assertEquals("Failed in TestToString with unexpected output 2"
\r
116 , expected, selFmt.toString() );
\r
117 } catch (Exception e){
\r
118 errln("Exception encountered in TestToString 2");
\r
123 * API tests for hashCode() method
\r
125 public void TestHashCode(){
\r
126 log("Inside TestHashCode");
\r
128 //Check hashCode for pattern constructed SelectFormat
\r
130 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1);
\r
131 SelectFormat selFmt1 = new SelectFormat(SIMPLE_PATTERN1);
\r
132 SelectFormat selFmt2 = new SelectFormat(SIMPLE_PATTERN2);
\r
133 assertEquals("Failed in TestHashCode 1 with unexpected output"
\r
134 , selFmt.hashCode(), selFmt1.hashCode() );
\r
135 assertNotEquals("Failed in TestHashCode 2 with unexpected output"
\r
136 , selFmt.hashCode(), selFmt2.hashCode() );
\r
137 } catch (Exception e){
\r
138 errln("Exception encountered in TestHashCode 3 with message : "
\r
145 * API tests for toPattern() method
\r
147 public void TestToPattern(){
\r
148 log("Inside TestToPattern");
\r
149 SelectFormat selFmt = null;
\r
151 //Check toPattern for pattern constructed SelectFormat
\r
153 selFmt = new SelectFormat(SIMPLE_PATTERN1);
\r
154 assertEquals("Failed in TestToPattern 2 with unexpected output"
\r
155 , SIMPLE_PATTERN1, selFmt.toPattern() );
\r
156 } catch (Exception e){
\r
157 errln("Exception encountered in TestToPattern 2 with message : "
\r
163 * API tests for format() method
\r
165 public void TestFormat(){
\r
166 log("Inside TestFormat");
\r
168 //Check format for pattern constructed object
\r
170 SelectFormat selFmt1 = new SelectFormat(SIMPLE_PATTERN1);
\r
171 String expected = "feminineVerbValue1";
\r
172 assertEquals("Failed in TestFormat with unexpected output 1"
\r
174 , selFmt1.format("feminine") );
\r
175 } catch (Exception e){
\r
176 errln("Exception encountered in TestFormat 3: " + e.getMessage() );
\r
179 //Check format with appendTo for pattern constructed object
\r
181 SelectFormat selFmt1 = new SelectFormat(SIMPLE_PATTERN1);
\r
182 StringBuffer expected = new StringBuffer("AppendHere-otherVerbValue1");
\r
183 StringBuffer strBuf = new StringBuffer("AppendHere-");
\r
184 assertEquals("Failed in TestFormat with unexpected output 2"
\r
185 , expected.toString()
\r
186 , (selFmt1.format("other",strBuf, new FieldPosition(0))).toString() );
\r
187 } catch (Exception e){
\r
188 errln("Exception encountered in TestFormat 2: " + e.getMessage() );
\r
193 * API tests for parseObject() method
\r
195 public void TestParseObject(){
\r
196 log("Inside TestToPattern");
\r
198 //Check parseObject
\r
200 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1);
\r
201 selFmt.parseObject("feminine", new ParsePosition(0) );
\r
202 fail("Failed in TestParseObject - UnsupportedOperationException not received");
\r
203 } catch (UnsupportedOperationException e){
\r
204 //Expect this Exception
\r
205 } catch (Exception e){
\r
206 errln("Exception encountered in TestParseObject with message : "
\r