2 *******************************************************************************
3 * Copyright (c) 2004-2010, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 * Copyright (C) 2010 , Yahoo! Inc.
6 *******************************************************************************
8 package com.ibm.icu.dev.test.format;
10 import java.text.FieldPosition;
11 import java.text.ParsePosition;
13 import com.ibm.icu.dev.test.TestFmwk;
14 import com.ibm.icu.text.SelectFormat;
18 * This class tests the API functionality of the SelectFormat
20 public class SelectFormatAPITest extends TestFmwk {
22 static final String SIMPLE_PATTERN1 = "feminine {feminineVerbValue1} other{otherVerbValue1}";
23 static final String SIMPLE_PATTERN2 = "feminine {feminineVerbValue2} other{otherVerbValue2}";
25 public static void main(String[] args) throws Exception {
26 new SelectFormatAPITest().run(args);
30 * API tests for constructors
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 );
39 * API tests for equals() method
41 public void TestEquals() {
42 SelectFormat selFmt1 = null;
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) );
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) );
64 * API tests for applyPattern() method
66 public void TestApplyPatternToPattern() {
67 SelectFormat selFmt = null;
68 String pattern = "masculine{masculineVerbValue} other{otherVerbValue}";
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() );
76 //Check for invalid pattern
78 String brokenPattern = "broken }{ pattern";
79 selFmt.applyPattern(brokenPattern);
80 errln("Failed in applyPattern. applyPattern should throw IllegalArgumentException for " + brokenPattern);
81 } catch (IllegalArgumentException e) {
87 * API tests for toString() method
89 public void TestToString(){
90 SelectFormat selFmt = null;
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() );
100 * API tests for hashCode() method
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() );
114 * API tests for toPattern() method
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() );
123 * API tests for format() method
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"
131 , selFmt1.format("feminine") );
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"
138 , (selFmt1.format("other", strBuf, new FieldPosition(0))).toString());
142 * API tests for parseObject() method
144 public void TestParseObject(){
147 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1);
148 selFmt.parseObject("feminine", new ParsePosition(0) );
149 fail("Failed in TestParseObject - UnsupportedOperationException not received");
150 } catch (UnsupportedOperationException e){
151 //Expect this Exception