1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package org.apache.harmony.text.tests.java.text; 18 19import dalvik.annotation.TestTargets; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTargetNew; 22import dalvik.annotation.TestTargetClass; 23 24import java.text.DateFormat; 25import java.text.FieldPosition; 26 27@TestTargetClass(FieldPosition.class) 28public class FieldPositionTest extends junit.framework.TestCase { 29 30 /** 31 * @tests java.text.FieldPosition#FieldPosition(int) 32 */ 33 @TestTargetNew( 34 level = TestLevel.COMPLETE, 35 notes = "", 36 method = "FieldPosition", 37 args = {int.class} 38 ) 39 public void test_ConstructorI() { 40 // Test for constructor java.text.FieldPosition(int) 41 FieldPosition fpos = new FieldPosition(DateFormat.MONTH_FIELD); 42 assertEquals("Test1: Constructor failed to set field identifier!", 43 DateFormat.MONTH_FIELD, fpos.getField()); 44 assertNull("Constructor failed to set field attribute!", fpos 45 .getFieldAttribute()); 46 } 47 48 /** 49 * @tests java.text.FieldPosition#FieldPosition(java.text.Format$Field) 50 */ 51 @TestTargetNew( 52 level = TestLevel.COMPLETE, 53 notes = "", 54 method = "FieldPosition", 55 args = {java.text.Format.Field.class} 56 ) 57 public void test_ConstructorLjava_text_Format$Field() { 58 // Test for constructor java.text.FieldPosition(Format.Field) 59 FieldPosition fpos = new FieldPosition(DateFormat.Field.MONTH); 60 assertSame("Constructor failed to set field attribute!", 61 DateFormat.Field.MONTH, fpos.getFieldAttribute()); 62 assertEquals("Test1: Constructor failed to set field identifier!", -1, 63 fpos.getField()); 64 } 65 66 /** 67 * @tests java.text.FieldPosition#FieldPosition(java.text.Format$Field, int) 68 */ 69 @TestTargetNew( 70 level = TestLevel.COMPLETE, 71 notes = "", 72 method = "FieldPosition", 73 args = {java.text.Format.Field.class, int.class} 74 ) 75 public void test_ConstructorLjava_text_Format$FieldI() { 76 // Test for constructor java.text.FieldPosition(Format.Field, int) 77 FieldPosition fpos = new FieldPosition(DateFormat.Field.MONTH, 78 DateFormat.MONTH_FIELD); 79 assertSame("Constructor failed to set field attribute!", 80 DateFormat.Field.MONTH, fpos.getFieldAttribute()); 81 assertEquals("Test1: Constructor failed to set field identifier!", 82 DateFormat.MONTH_FIELD, fpos.getField()); 83 84 // test special cases 85 FieldPosition fpos2 = new FieldPosition(DateFormat.Field.HOUR1, 86 DateFormat.HOUR1_FIELD); 87 assertSame("Constructor failed to set field attribute!", 88 DateFormat.Field.HOUR1, fpos2.getFieldAttribute()); 89 assertEquals("Test2: Constructor failed to set field identifier!", 90 DateFormat.HOUR1_FIELD, fpos2.getField()); 91 92 FieldPosition fpos3 = new FieldPosition(DateFormat.Field.TIME_ZONE, 93 DateFormat.MONTH_FIELD); 94 assertSame("Constructor failed to set field attribute!", 95 DateFormat.Field.TIME_ZONE, fpos3.getFieldAttribute()); 96 assertEquals("Test3: Constructor failed to set field identifier!", 97 DateFormat.MONTH_FIELD, fpos3.getField()); 98 } 99 100 /** 101 * @tests java.text.FieldPosition#equals(java.lang.Object) 102 */ 103 @TestTargetNew( 104 level = TestLevel.COMPLETE, 105 notes = "", 106 method = "equals", 107 args = {java.lang.Object.class} 108 ) 109 public void test_equalsLjava_lang_Object() { 110 // Test for method boolean 111 // java.text.FieldPosition.equals(java.lang.Object) 112 FieldPosition fpos = new FieldPosition(1); 113 FieldPosition fpos1 = new FieldPosition(1); 114 assertTrue("Identical objects were not equal!", fpos.equals(fpos1)); 115 116 FieldPosition fpos2 = new FieldPosition(2); 117 assertTrue("Objects with a different ID should not be equal!", !fpos 118 .equals(fpos2)); 119 120 fpos.setBeginIndex(1); 121 fpos1.setBeginIndex(2); 122 assertTrue("Objects with a different beginIndex were still equal!", 123 !fpos.equals(fpos1)); 124 fpos1.setBeginIndex(1); 125 fpos1.setEndIndex(2); 126 assertTrue("Objects with a different endIndex were still equal!", !fpos 127 .equals(fpos1)); 128 129 FieldPosition fpos3 = new FieldPosition(DateFormat.Field.ERA, 1); 130 assertTrue("Objects with a different attribute should not be equal!", 131 !fpos.equals(fpos3)); 132 FieldPosition fpos4 = new FieldPosition(DateFormat.Field.AM_PM, 1); 133 assertTrue("Objects with a different attribute should not be equal!", 134 !fpos3.equals(fpos4)); 135 } 136 137 /** 138 * @tests java.text.FieldPosition#getBeginIndex() 139 */ 140 @TestTargetNew( 141 level = TestLevel.COMPLETE, 142 notes = "", 143 method = "getBeginIndex", 144 args = {} 145 ) 146 public void test_getBeginIndex() { 147 // Test for method int java.text.FieldPosition.getBeginIndex() 148 FieldPosition fpos = new FieldPosition(1); 149 fpos.setEndIndex(3); 150 fpos.setBeginIndex(2); 151 assertEquals("getBeginIndex should have returned 2", 2, fpos 152 .getBeginIndex()); 153 } 154 155 /** 156 * @tests java.text.FieldPosition#getEndIndex() 157 */ 158 @TestTargetNew( 159 level = TestLevel.COMPLETE, 160 notes = "", 161 method = "getEndIndex", 162 args = {} 163 ) 164 public void test_getEndIndex() { 165 // Test for method int java.text.FieldPosition.getEndIndex() 166 FieldPosition fpos = new FieldPosition(1); 167 fpos.setBeginIndex(2); 168 fpos.setEndIndex(3); 169 assertEquals("getEndIndex should have returned 3", 3, fpos 170 .getEndIndex()); 171 } 172 173 /** 174 * @tests java.text.FieldPosition#getField() 175 */ 176 @TestTargetNew( 177 level = TestLevel.COMPLETE, 178 notes = "", 179 method = "getField", 180 args = {} 181 ) 182 public void test_getField() { 183 // Test for method int java.text.FieldPosition.getField() 184 FieldPosition fpos = new FieldPosition(65); 185 assertEquals( 186 "FieldPosition(65) should have caused getField to return 65", 187 65, fpos.getField()); 188 FieldPosition fpos2 = new FieldPosition(DateFormat.Field.MINUTE); 189 assertEquals( 190 "FieldPosition(DateFormat.Field.MINUTE) should have caused getField to return -1", 191 -1, fpos2.getField()); 192 } 193 194 /** 195 * @tests java.text.FieldPosition#getFieldAttribute() 196 */ 197 @TestTargetNew( 198 level = TestLevel.COMPLETE, 199 notes = "", 200 method = "getFieldAttribute", 201 args = {} 202 ) 203 public void test_getFieldAttribute() { 204 // Test for method int java.text.FieldPosition.getFieldAttribute() 205 FieldPosition fpos = new FieldPosition(DateFormat.Field.TIME_ZONE); 206 assertTrue( 207 "FieldPosition(DateFormat.Field.TIME_ZONE) should have caused getFieldAttribute to return DateFormat.Field.TIME_ZONE", 208 fpos.getFieldAttribute() == DateFormat.Field.TIME_ZONE); 209 210 FieldPosition fpos2 = new FieldPosition(DateFormat.TIMEZONE_FIELD); 211 assertNull( 212 "FieldPosition(DateFormat.TIMEZONE_FIELD) should have caused getFieldAttribute to return null", 213 fpos2.getFieldAttribute()); 214 } 215 216 /** 217 * @tests java.text.FieldPosition#hashCode() 218 */ 219 @TestTargetNew( 220 level = TestLevel.COMPLETE, 221 notes = "", 222 method = "hashCode", 223 args = {} 224 ) 225 public void test_hashCode() { 226 // Test for method int java.text.FieldPosition.hashCode() 227 FieldPosition fpos1 = new FieldPosition(1); 228 FieldPosition fpos2 = new FieldPosition(1); 229 assertTrue("test 1: hash codes are not equal for equal objects.", 230 fpos1.hashCode() == fpos2.hashCode()); 231 fpos1.setBeginIndex(5); 232 fpos1.setEndIndex(110); 233 assertTrue("test 2: hash codes are equal for non equal objects.", 234 fpos1.hashCode() != fpos2.hashCode()); 235 fpos2.setBeginIndex(5); 236 fpos2.setEndIndex(110); 237 assertTrue("test 3: hash codes are not equal for equal objects.", 238 fpos1.hashCode() == fpos2.hashCode()); 239 240 FieldPosition fpos3 = new FieldPosition( 241 DateFormat.Field.DAY_OF_WEEK_IN_MONTH); 242 243 assertTrue("test 4: hash codes are equal for non equal objects.", 244 fpos2.hashCode() != fpos3.hashCode()); 245 } 246 247 /** 248 * @tests java.text.FieldPosition#setBeginIndex(int) 249 */ 250 @TestTargetNew( 251 level = TestLevel.COMPLETE, 252 notes = "", 253 method = "setBeginIndex", 254 args = {int.class} 255 ) 256 public void test_setBeginIndexI() { 257 // Test for method void java.text.FieldPosition.setBeginIndex(int) 258 FieldPosition fpos = new FieldPosition(1); 259 fpos.setBeginIndex(2); 260 fpos.setEndIndex(3); 261 assertEquals("beginIndex should have been set to 2", 2, fpos 262 .getBeginIndex()); 263 264 fpos.setBeginIndex(Integer.MAX_VALUE); 265 assertEquals("beginIndex should have been set to Integer.MAX_VALUE", 266 Integer.MAX_VALUE, fpos.getBeginIndex()); 267 268 fpos.setBeginIndex(-1); 269 assertEquals("beginIndex should have been set to -1", 270 -1, fpos.getBeginIndex()); 271 } 272 273 /** 274 * @tests java.text.FieldPosition#setEndIndex(int) 275 */ 276 @TestTargetNew( 277 level = TestLevel.COMPLETE, 278 notes = "", 279 method = "setEndIndex", 280 args = {int.class} 281 ) 282 public void test_setEndIndexI() { 283 // Test for method void java.text.FieldPosition.setEndIndex(int) 284 FieldPosition fpos = new FieldPosition(1); 285 fpos.setEndIndex(3); 286 fpos.setBeginIndex(2); 287 assertEquals("EndIndex should have been set to 3", 3, fpos 288 .getEndIndex()); 289 290 fpos.setEndIndex(Integer.MAX_VALUE); 291 assertEquals("endIndex should have been set to Integer.MAX_VALUE", 292 Integer.MAX_VALUE, fpos.getEndIndex()); 293 294 fpos.setEndIndex(-1); 295 assertEquals("endIndex should have been set to -1", 296 -1, fpos.getEndIndex()); 297 } 298 299 /** 300 * @tests java.text.FieldPosition#toString() 301 */ 302 @TestTargetNew( 303 level = TestLevel.COMPLETE, 304 notes = "", 305 method = "toString", 306 args = {} 307 ) 308 public void test_toString() { 309 // Test for method java.lang.String java.text.FieldPosition.toString() 310 FieldPosition fpos = new FieldPosition(1); 311 fpos.setBeginIndex(2); 312 fpos.setEndIndex(3); 313 // string format is not specified 314 assertNotNull( 315 "toString returned null", 316 fpos.toString()); 317 318 FieldPosition fpos2 = new FieldPosition(DateFormat.Field.ERA); 319 fpos2.setBeginIndex(4); 320 fpos2.setEndIndex(5); 321 assertNotNull("ToString returned the wrong value:", 322 fpos2.toString()); 323 } 324 325 /** 326 * Sets up the fixture, for example, open a network connection. This method 327 * is called before a test is executed. 328 */ 329 protected void setUp() { 330 } 331 332 /** 333 * Tears down the fixture, for example, close a network connection. This 334 * method is called after a test is executed. 335 */ 336 protected void tearDown() { 337 } 338} 339