1// © 2016 and later: Unicode, Inc. and others. 2// License & terms of use: http://www.unicode.org/copyright.html#License 3/* 4 ******************************************************************************* 5 * Copyright (c) 2004-2011, International Business Machines 6 * Corporation and others. All Rights Reserved. 7 * Copyright (C) 2010 , Yahoo! Inc. 8 ******************************************************************************* 9 */ 10package com.ibm.icu.dev.test.format; 11 12import java.text.FieldPosition; 13import java.text.ParsePosition; 14 15import org.junit.Test; 16import org.junit.runner.RunWith; 17import org.junit.runners.JUnit4; 18 19import com.ibm.icu.dev.test.TestFmwk; 20import com.ibm.icu.text.SelectFormat; 21 22/** 23 * @author kirtig 24 * This class tests the API functionality of the SelectFormat 25 */ 26@RunWith(JUnit4.class) 27public class SelectFormatAPITest extends TestFmwk { 28 29 static final String SIMPLE_PATTERN1 = "feminine {feminineVerbValue1} other{otherVerbValue1}"; 30 static final String SIMPLE_PATTERN2 = "feminine {feminineVerbValue2} other{otherVerbValue2}"; 31 32 /** 33 * API tests for constructors 34 */ 35 @Test 36 public void TestConstructors() { 37 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1); 38 assertNotNull("Error: TestConstructors - SelectFormat object constructed " 39 + "with argument constructor is null" , selFmt ); 40 } 41 42 /** 43 * API tests for equals() method 44 */ 45 @Test 46 public void TestEquals() { 47 SelectFormat selFmt1 = null; 48 49 //Check equality for pattern constructed SelectFormats 50 selFmt1 = new SelectFormat(SIMPLE_PATTERN1); 51 SelectFormat selFmt2 = new SelectFormat(SIMPLE_PATTERN1); 52 assertTrue("Equals test failed while checking equality for " 53 + "pattern constructed SelectFormats ." 54 , selFmt1.equals(selFmt2) ); 55 56 //Check equality for 2 objects 57 Object selFmt3 = new SelectFormat(SIMPLE_PATTERN1); 58 Object selFmt4 = new SelectFormat(SIMPLE_PATTERN1); 59 Object selFmt5 = new SelectFormat(SIMPLE_PATTERN2); 60 assertTrue("Equals test failed while checking equality for object 1." 61 , selFmt3.equals(selFmt4) ); 62 assertTrue("Equals test failed while checking equality for object 2." 63 , selFmt1.equals(selFmt3) ); 64 assertFalse("Equals test failed while checking equality for object 3." 65 , selFmt3.equals(selFmt5) ); 66 } 67 68 /** 69 * API tests for applyPattern() method 70 */ 71 @Test 72 public void TestApplyPatternToPattern() { 73 SelectFormat selFmt = null; 74 String pattern = "masculine{masculineVerbValue} other{otherVerbValue}"; 75 76 //Check for applyPattern/toPattern 77 selFmt = new SelectFormat(SIMPLE_PATTERN1); 78 selFmt.applyPattern(pattern); 79 assertEquals("Failed in applyPattern,toPattern with unexpected output" 80 , pattern, selFmt.toPattern() ); 81 82 //Check for invalid pattern 83 try { 84 String brokenPattern = "broken }{ pattern"; 85 selFmt.applyPattern(brokenPattern); 86 errln("Failed in applyPattern. applyPattern should throw IllegalArgumentException for " + brokenPattern); 87 } catch (IllegalArgumentException e) { 88 // This is OK 89 } 90 } 91 92 /** 93 * API tests for toString() method 94 */ 95 @Test 96 public void TestToString(){ 97 SelectFormat selFmt = null; 98 99 //Check toString for pattern constructed SelectFormat 100 selFmt = new SelectFormat(SIMPLE_PATTERN1); 101 String expected = "pattern='feminine {feminineVerbValue1} other{otherVerbValue1}'"; 102 assertEquals("Failed in TestToString with unexpected output 2" 103 , expected, selFmt.toString() ); 104 } 105 106 /** 107 * API tests for hashCode() method 108 */ 109 @Test 110 public void TestHashCode(){ 111 //Check hashCode for pattern constructed SelectFormat 112 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1); 113 SelectFormat selFmt1 = new SelectFormat(SIMPLE_PATTERN1); 114 SelectFormat selFmt2 = new SelectFormat(SIMPLE_PATTERN2); 115 assertEquals("Failed in TestHashCode 1 with unexpected output" 116 , selFmt.hashCode(), selFmt1.hashCode() ); 117 assertNotEquals("Failed in TestHashCode 2 with unexpected output" 118 , selFmt.hashCode(), selFmt2.hashCode() ); 119 } 120 121 /** 122 * API tests for toPattern() method 123 */ 124 @Test 125 public void TestToPattern(){ 126 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1); 127 assertEquals("Failed in TestToPattern 2 with unexpected output" 128 , SIMPLE_PATTERN1, selFmt.toPattern() ); 129 } 130 131 /** 132 * API tests for format() method 133 */ 134 @Test 135 public void TestFormat(){ 136 //Check format for pattern constructed object 137 SelectFormat selFmt1 = new SelectFormat(SIMPLE_PATTERN1); 138 String expected = "feminineVerbValue1"; 139 assertEquals("Failed in TestFormat with unexpected output 1" 140 , expected 141 , selFmt1.format("feminine") ); 142 143 //Check format with appendTo for pattern constructed object 144 expected = "AppendHere-otherVerbValue1"; 145 StringBuffer strBuf = new StringBuffer("AppendHere-"); 146 assertEquals("Failed in TestFormat with unexpected output 2" 147 , expected 148 , (selFmt1.format("other", strBuf, new FieldPosition(0))).toString()); 149 150 //Check format throws exception on invalid argument. 151 boolean threwException = false; 152 try { 153 StringBuffer buf = new StringBuffer("AppendHere-"); 154 selFmt1.format(Integer.valueOf(0), buf, new FieldPosition(0)); 155 } catch (IllegalArgumentException e) { 156 threwException = true; 157 } 158 assertTrue("Did not throw IllegalArgumentException.", threwException); 159 } 160 161 /** 162 * API tests for parseObject() method 163 */ 164 @Test 165 public void TestParseObject(){ 166 //Check parseObject 167 try { 168 SelectFormat selFmt = new SelectFormat(SIMPLE_PATTERN1); 169 selFmt.parseObject("feminine", new ParsePosition(0) ); 170 fail("Failed in TestParseObject - UnsupportedOperationException not received"); 171 } catch (UnsupportedOperationException e){ 172 //Expect this Exception 173 } 174 } 175} 176 177