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 com.ibm.icu.dev.test.TestFmwk;
\r
11 import com.ibm.icu.text.SelectFormat;
\r
15 * This class does the unit testing for the SelectFormat
\r
17 public class SelectFormatUnitTest extends TestFmwk {
\r
19 static final String SIMPLE_PATTERN = "feminine {feminineVerbValue} other{otherVerbValue}";
\r
21 public static void main(String[] args) throws Exception {
\r
22 new SelectFormatUnitTest().run(args);
\r
26 * Unit tests for pattern syntax
\r
28 public void TestPatternSyntax() {
\r
29 log("Inside TestPatternSyntax");
\r
31 String checkSyntaxData[] = {
\r
32 "odd{foo} odd{bar} other{foobar}",
\r
33 "odd{foo} other{bar} other{foobar}",
\r
35 "1odd{foo} other{bar}",
\r
36 "odd{foo},other{bar}",
\r
37 "od d{foo} other{bar}",
\r
38 "odd{foo}{foobar}other{foo}",
\r
39 "odd{foo1}other{foo2}}",
\r
40 "odd{foo1}other{{foo2}",
\r
41 "odd{fo{o1}other{foo2}}"
\r
44 String expectedErrorMsgs[] = {
\r
45 "Duplicate keyword error.",
\r
46 "Duplicate keyword error.",
\r
47 "Pattern syntax error. Value for case \"other\" was not defined. ",
\r
48 "Pattern syntax error.",
\r
49 "Pattern syntax error.",
\r
50 "Pattern syntax error.",
\r
51 "Pattern syntax error.",
\r
52 "Pattern syntax error.",
\r
53 "Pattern syntax error.",
\r
54 "Pattern syntax error. Value for case \"other\" was not defined. ",
\r
57 //Test SelectFormat pattern syntax
\r
59 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN);
\r
60 for (int i=0; i<checkSyntaxData.length; ++i) {
\r
62 selFmt.applyPattern(checkSyntaxData[i]);
\r
63 errln("\nERROR: Unexpected result - SelectFormat Unit Test failed "
\r
64 + "to detect syntax error with pattern: "+checkSyntaxData[i]);
\r
65 } catch (IllegalArgumentException e){
\r
66 assertEquals("Error:TestPatternSyntax failed with unexpected"
\r
67 + " error message for pattern: " + checkSyntaxData[i] ,
\r
68 expectedErrorMsgs[i], e.getMessage() );
\r
72 } catch (Exception e){
\r
73 errln("Exception encountered in TestPatternSyntax ");
\r
78 * Unit tests for invalid keywords
\r
80 public void TestInvalidKeyword() {
\r
81 //Test formatting with invalid keyword
\r
82 log("Inside TestInvalidKeyword");
\r
84 String keywords[] = {
\r
85 "9Keyword-_", //Starts with a digit
\r
86 "-Keyword-_", //Starts with a hyphen
\r
87 "_Keyword-_", //Starts with an underscore
\r
88 "\\u00E9Keyword-_", //Starts with non-ASCII character
\r
89 "Key*word-_", //Contains a sepial character not allowed
\r
90 "*Keyword-_" //Starts with a sepial character not allowed
\r
93 String expected = "Invalid formatting argument.";
\r
94 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN);
\r
95 for (int i = 0; i< 6; i++ ){
\r
97 selFmt.format( keywords[i]);
\r
98 fail("Error:TestInvalidKeyword failed to detect invalid keyword "
\r
99 + "for keyword: " + keywords[i] );
\r
100 } catch (IllegalArgumentException e){
\r
101 assertEquals("Error:TestInvalidKeyword failed with unexpected "
\r
102 +"error message for keyword: " + keywords[i]
\r
103 , expected , e.getMessage() );
\r
105 } catch (Exception e){
\r
106 errln("ERROR:TestInvalidKeyword failed with an invalid keyword: "
\r
107 + keywords[i] + " with exception: " + e.getMessage() );
\r
114 * API tests for applyPattern and format
\r
116 public void TestApplyFormat() {
\r
117 //Test applying and formatting with various pattern
\r
118 log("Inside TestApplyFormat");
\r
120 String patternTestData[] = {
\r
121 "fem {femValue} other{even}",
\r
122 "other{odd or even}",
\r
123 "odd{The number {0, number, integer} is odd.}other{The number {0, number, integer} is even.}",
\r
124 "odd{The number {1} is odd}other{The number {1} is even}"
\r
127 String formatArgs[] = {
\r
133 String expFormatResult[][] = {
\r
145 "The number {0, number, integer} is even.",
\r
146 "The number {0, number, integer} is even.",
\r
147 "The number {0, number, integer} is odd.",
\r
150 "The number {1} is even",
\r
151 "The number {1} is even",
\r
152 "The number {1} is odd",
\r
156 log("SelectFormat Unit test: Testing applyPattern() and format() ...");
\r
157 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN);
\r
159 for (int i=0; i<patternTestData.length; ++i) {
\r
161 selFmt.applyPattern(patternTestData[i]);
\r
162 } catch (Exception e){
\r
163 errln("ERROR: SelectFormat Unit Test failed to apply pattern- "
\r
164 + patternTestData[i] );
\r
168 //Format with the keyword array
\r
169 for (int j=0; j<3; j++) {
\r
170 assertEquals("ERROR: SelectFormat Unit test failed in format() with unexpected result", selFmt.format(formatArgs[j]) ,expFormatResult[i][j] );
\r