]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/format/SelectFormatUnitTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / format / SelectFormatUnitTest.java
1 /*\r
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
7  */\r
8 package com.ibm.icu.dev.test.format;\r
9 \r
10 import com.ibm.icu.dev.test.TestFmwk;\r
11 import com.ibm.icu.text.SelectFormat;\r
12 \r
13 /**\r
14  * @author kirtig \r
15  * This class does the unit testing for the SelectFormat\r
16  */\r
17 public class SelectFormatUnitTest extends TestFmwk {\r
18   \r
19     static final String SIMPLE_PATTERN = "feminine {feminineVerbValue} other{otherVerbValue}";\r
20 \r
21     public static void main(String[] args) throws Exception {\r
22         new SelectFormatUnitTest().run(args);\r
23     }\r
24   \r
25     /**\r
26      * Unit tests for pattern syntax\r
27      */\r
28     public void TestPatternSyntax() {\r
29         log("Inside TestPatternSyntax");\r
30 \r
31         String checkSyntaxData[] = {\r
32             "odd{foo} odd{bar} other{foobar}",\r
33             "odd{foo} other{bar} other{foobar}",\r
34             "odd{foo}",\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
42         };\r
43 \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
55         };\r
56 \r
57         //Test SelectFormat pattern syntax\r
58         try {\r
59             SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN);\r
60             for (int i=0; i<checkSyntaxData.length; ++i) {\r
61                 try {\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
69                     continue;\r
70                 }\r
71             }\r
72         } catch (Exception e){\r
73             errln("Exception encountered in TestPatternSyntax ");\r
74         }\r
75     }\r
76 \r
77     /**\r
78      * Unit tests for invalid keywords \r
79      */\r
80     public void TestInvalidKeyword() {\r
81         //Test formatting with invalid keyword\r
82         log("Inside TestInvalidKeyword");\r
83 \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
91         };\r
92 \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
96             try {\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
104                 continue;\r
105             } catch (Exception e){\r
106                 errln("ERROR:TestInvalidKeyword failed with an invalid keyword: "\r
107                      + keywords[i] + " with exception: " + e.getMessage() );\r
108             }\r
109         }\r
110 \r
111     }\r
112 \r
113     /**\r
114      * API tests for  applyPattern and format\r
115      */\r
116     public void TestApplyFormat() {\r
117         //Test applying and formatting with various pattern\r
118         log("Inside TestApplyFormat");\r
119 \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
125         };\r
126 \r
127         String formatArgs[] = {\r
128             "fem",\r
129             "other",\r
130             "odd"\r
131         };\r
132 \r
133         String expFormatResult[][] = {\r
134             {\r
135                 "femValue",\r
136                 "even",\r
137                 "even",\r
138             },\r
139             {\r
140                 "odd or even",\r
141             "odd or even",\r
142             "odd or even",\r
143             },\r
144             {\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
148             },\r
149             {\r
150                 "The number {1} is even",\r
151                 "The number {1} is even",\r
152                 "The number {1} is odd",\r
153             }\r
154         };\r
155 \r
156         log("SelectFormat Unit test: Testing  applyPattern() and format() ...");\r
157         SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN); \r
158 \r
159         for (int i=0; i<patternTestData.length; ++i) {\r
160             try {\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
165                 continue;\r
166             }\r
167 \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
171             }\r
172         }\r
173     }\r
174 \r
175 }\r
176 \r