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 com.ibm.icu.dev.test.TestFmwk; 11import com.ibm.icu.text.SelectFormat; 12 13/** 14 * @author kirtig 15 * This class does the unit testing for the SelectFormat 16 */ 17public class SelectFormatUnitTest extends TestFmwk { 18 19 static final String SIMPLE_PATTERN = "feminine {feminineVerbValue} other{otherVerbValue}"; 20 21 public static void main(String[] args) throws Exception { 22 new SelectFormatUnitTest().run(args); 23 } 24 25 /** 26 * Unit tests for pattern syntax 27 */ 28 public void TestPatternSyntax() { 29 String checkSyntaxData[] = { 30 "odd{foo}", 31 "*odd{foo} other{bar}", 32 "odd{foo},other{bar}", 33 "od d{foo} other{bar}", 34 "odd{foo}{foobar}other{foo}", 35 "odd{foo1}other{foo2}}", 36 "odd{foo1}other{{foo2}", 37 "odd{fo{o1}other{foo2}}" 38 }; 39 40 //Test SelectFormat pattern syntax 41 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN); 42 for (int i=0; i<checkSyntaxData.length; ++i) { 43 try { 44 selFmt.applyPattern(checkSyntaxData[i]); 45 errln("\nERROR: Unexpected result - SelectFormat Unit Test failed " 46 + "to detect syntax error with pattern: "+checkSyntaxData[i]); 47 } catch (IllegalArgumentException e){ 48 // ok 49 continue; 50 } 51 } 52 53 // ICU 4.8 does not check for duplicate keywords any more. 54 selFmt.applyPattern("odd{foo} odd{bar} other{foobar}"); 55 assertEquals("should use first occurrence of the 'odd' keyword", "foo", selFmt.format("odd")); 56 selFmt.applyPattern("odd{foo} other{bar} other{foobar}"); 57 assertEquals("should use first occurrence of the 'other' keyword", "bar", selFmt.format("other")); 58 } 59 60 /** 61 * Unit tests for invalid keywords 62 */ 63 public void TestInvalidKeyword() { 64 // Test formatting with invalid keyword: 65 // one which contains Pattern_Syntax or Pattern_White_Space. 66 String keywords[] = { 67 "9Keyword-_", 68 "-Keyword-_", 69 "_Keyword-_", 70 "\\u00E9Keyword-_", 71 "Key word", 72 " Keyword", 73 "Keyword ", 74 "Key*word-_", 75 "*Keyword-_" 76 }; 77 78 String expected = "Invalid formatting argument."; 79 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN); 80 for (int i = 0; i< 6; i++ ){ 81 try { 82 selFmt.format( keywords[i]); 83 fail("Error:TestInvalidKeyword failed to detect invalid keyword " 84 + "for keyword: " + keywords[i] ); 85 } catch (IllegalArgumentException e){ 86 assertEquals("Error:TestInvalidKeyword failed with unexpected " 87 +"error message for keyword: " + keywords[i] 88 , expected , e.getMessage() ); 89 continue; 90 } 91 } 92 93 } 94 95 /** 96 * API tests for applyPattern and format 97 */ 98 public void TestApplyFormat() { 99 //Test applying and formatting with various pattern 100 String patternTestData[] = { 101 "fem {femValue} other{even}", 102 "other{odd or even}", 103 "odd{The number {0, number, integer} is odd.}other{The number {0, number, integer} is even.}", 104 "odd{The number {1} is odd}other{The number {1} is even}" 105 }; 106 107 String formatArgs[] = { 108 "fem", 109 "other", 110 "odd" 111 }; 112 113 String expFormatResult[][] = { 114 { 115 "femValue", 116 "even", 117 "even", 118 }, 119 { 120 "odd or even", 121 "odd or even", 122 "odd or even", 123 }, 124 { 125 "The number {0, number, integer} is even.", 126 "The number {0, number, integer} is even.", 127 "The number {0, number, integer} is odd.", 128 }, 129 { 130 "The number {1} is even", 131 "The number {1} is even", 132 "The number {1} is odd", 133 } 134 }; 135 136 log("SelectFormat Unit test: Testing applyPattern() and format() ..."); 137 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN); 138 139 for (int i=0; i<patternTestData.length; ++i) { 140 try { 141 selFmt.applyPattern(patternTestData[i]); 142 } catch (IllegalArgumentException e){ 143 errln("ERROR: SelectFormat Unit Test failed to apply pattern- " 144 + patternTestData[i] ); 145 continue; 146 } 147 148 //Format with the keyword array 149 for (int j=0; j<3; j++) { 150 assertEquals("ERROR: SelectFormat Unit test failed in format() with unexpected result", selFmt.format(formatArgs[j]) ,expFormatResult[i][j] ); 151 } 152 } 153 } 154 155} 156 157