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 java.text.DateFormat; 20import java.text.FieldPosition; 21 22public class FieldPositionTest extends junit.framework.TestCase { 23 24 /** 25 * @tests java.text.FieldPosition#FieldPosition(int) 26 */ 27 public void test_ConstructorI() { 28 // Test for constructor java.text.FieldPosition(int) 29 FieldPosition fpos = new FieldPosition(DateFormat.MONTH_FIELD); 30 assertEquals("Test1: Constructor failed to set field identifier!", 31 DateFormat.MONTH_FIELD, fpos.getField()); 32 assertNull("Constructor failed to set field attribute!", fpos 33 .getFieldAttribute()); 34 } 35 36 /** 37 * @tests java.text.FieldPosition#FieldPosition(java.text.Format$Field) 38 */ 39 public void test_ConstructorLjava_text_Format$Field() { 40 // Test for constructor java.text.FieldPosition(Format.Field) 41 FieldPosition fpos = new FieldPosition(DateFormat.Field.MONTH); 42 assertSame("Constructor failed to set field attribute!", 43 DateFormat.Field.MONTH, fpos.getFieldAttribute()); 44 assertEquals("Test1: Constructor failed to set field identifier!", -1, 45 fpos.getField()); 46 } 47 48 /** 49 * @tests java.text.FieldPosition#FieldPosition(java.text.Format$Field, int) 50 */ 51 public void test_ConstructorLjava_text_Format$FieldI() { 52 // Test for constructor java.text.FieldPosition(Format.Field, int) 53 FieldPosition fpos = new FieldPosition(DateFormat.Field.MONTH, 54 DateFormat.MONTH_FIELD); 55 assertSame("Constructor failed to set field attribute!", 56 DateFormat.Field.MONTH, fpos.getFieldAttribute()); 57 assertEquals("Test1: Constructor failed to set field identifier!", 58 DateFormat.MONTH_FIELD, fpos.getField()); 59 60 // test special cases 61 FieldPosition fpos2 = new FieldPosition(DateFormat.Field.HOUR1, 62 DateFormat.HOUR1_FIELD); 63 assertSame("Constructor failed to set field attribute!", 64 DateFormat.Field.HOUR1, fpos2.getFieldAttribute()); 65 assertEquals("Test2: Constructor failed to set field identifier!", 66 DateFormat.HOUR1_FIELD, fpos2.getField()); 67 68 FieldPosition fpos3 = new FieldPosition(DateFormat.Field.TIME_ZONE, 69 DateFormat.MONTH_FIELD); 70 assertSame("Constructor failed to set field attribute!", 71 DateFormat.Field.TIME_ZONE, fpos3.getFieldAttribute()); 72 assertEquals("Test3: Constructor failed to set field identifier!", 73 DateFormat.MONTH_FIELD, fpos3.getField()); 74 } 75 76 /** 77 * @tests java.text.FieldPosition#equals(java.lang.Object) 78 */ 79 public void test_equalsLjava_lang_Object() { 80 // Test for method boolean 81 // java.text.FieldPosition.equals(java.lang.Object) 82 FieldPosition fpos = new FieldPosition(1); 83 FieldPosition fpos1 = new FieldPosition(1); 84 assertTrue("Identical objects were not equal!", fpos.equals(fpos1)); 85 86 FieldPosition fpos2 = new FieldPosition(2); 87 assertTrue("Objects with a different ID should not be equal!", !fpos 88 .equals(fpos2)); 89 90 fpos.setBeginIndex(1); 91 fpos1.setBeginIndex(2); 92 assertTrue("Objects with a different beginIndex were still equal!", 93 !fpos.equals(fpos1)); 94 fpos1.setBeginIndex(1); 95 fpos1.setEndIndex(2); 96 assertTrue("Objects with a different endIndex were still equal!", !fpos 97 .equals(fpos1)); 98 99 FieldPosition fpos3 = new FieldPosition(DateFormat.Field.ERA, 1); 100 assertTrue("Objects with a different attribute should not be equal!", 101 !fpos.equals(fpos3)); 102 FieldPosition fpos4 = new FieldPosition(DateFormat.Field.AM_PM, 1); 103 assertTrue("Objects with a different attribute should not be equal!", 104 !fpos3.equals(fpos4)); 105 } 106 107 /** 108 * @tests java.text.FieldPosition#getBeginIndex() 109 */ 110 public void test_getBeginIndex() { 111 // Test for method int java.text.FieldPosition.getBeginIndex() 112 FieldPosition fpos = new FieldPosition(1); 113 fpos.setEndIndex(3); 114 fpos.setBeginIndex(2); 115 assertEquals("getBeginIndex should have returned 2", 116 2, fpos.getBeginIndex()); 117 } 118 119 /** 120 * @tests java.text.FieldPosition#getEndIndex() 121 */ 122 public void test_getEndIndex() { 123 // Test for method int java.text.FieldPosition.getEndIndex() 124 FieldPosition fpos = new FieldPosition(1); 125 fpos.setBeginIndex(2); 126 fpos.setEndIndex(3); 127 assertEquals("getEndIndex should have returned 3", 128 3, fpos.getEndIndex()); 129 } 130 131 /** 132 * @tests java.text.FieldPosition#getField() 133 */ 134 public void test_getField() { 135 // Test for method int java.text.FieldPosition.getField() 136 FieldPosition fpos = new FieldPosition(65); 137 assertEquals("FieldPosition(65) should have caused getField to return 65", 138 65, fpos.getField()); 139 FieldPosition fpos2 = new FieldPosition(DateFormat.Field.MINUTE); 140 assertEquals("FieldPosition(DateFormat.Field.MINUTE) should have caused getField to return -1", 141 -1, fpos2.getField()); 142 } 143 144 /** 145 * @tests java.text.FieldPosition#getFieldAttribute() 146 */ 147 public void test_getFieldAttribute() { 148 // Test for method int java.text.FieldPosition.getFieldAttribute() 149 FieldPosition fpos = new FieldPosition(DateFormat.Field.TIME_ZONE); 150 assertTrue( 151 "FieldPosition(DateFormat.Field.TIME_ZONE) should have caused getFieldAttribute to return DateFormat.Field.TIME_ZONE", 152 fpos.getFieldAttribute() == DateFormat.Field.TIME_ZONE); 153 154 FieldPosition fpos2 = new FieldPosition(DateFormat.TIMEZONE_FIELD); 155 assertNull( 156 "FieldPosition(DateFormat.TIMEZONE_FIELD) should have caused getFieldAttribute to return null", 157 fpos2.getFieldAttribute()); 158 } 159 160 /** 161 * @tests java.text.FieldPosition#hashCode() 162 */ 163 public void test_hashCode() { 164 // Test for method int java.text.FieldPosition.hashCode() 165 FieldPosition fpos = new FieldPosition(1); 166 fpos.setBeginIndex(5); 167 fpos.setEndIndex(110); 168 fpos.hashCode(); 169 170 FieldPosition fpos2 = new FieldPosition( 171 DateFormat.Field.DAY_OF_WEEK_IN_MONTH); 172 fpos2.setBeginIndex(5); 173 fpos2.setEndIndex(110); 174 fpos2.hashCode(); 175 } 176 177 /** 178 * @tests java.text.FieldPosition#setBeginIndex(int) 179 */ 180 public void test_setBeginIndexI() { 181 // Test for method void java.text.FieldPosition.setBeginIndex(int) 182 FieldPosition fpos = new FieldPosition(1); 183 fpos.setBeginIndex(2); 184 fpos.setEndIndex(3); 185 assertEquals("beginIndex should have been set to 2", 186 2, fpos.getBeginIndex()); 187 } 188 189 /** 190 * @tests java.text.FieldPosition#setEndIndex(int) 191 */ 192 public void test_setEndIndexI() { 193 // Test for method void java.text.FieldPosition.setEndIndex(int) 194 FieldPosition fpos = new FieldPosition(1); 195 fpos.setEndIndex(3); 196 fpos.setBeginIndex(2); 197 assertEquals("EndIndex should have been set to 3", 198 3, fpos.getEndIndex()); 199 } 200 201 /** 202 * @tests java.text.FieldPosition#toString() 203 */ 204 public void test_toString() { 205 // Test for method java.lang.String java.text.FieldPosition.toString() 206 FieldPosition fpos = new FieldPosition(1); 207 fpos.setBeginIndex(2); 208 fpos.setEndIndex(3); 209 assertEquals( 210 "ToString returned the wrong value:", 211 "java.text.FieldPosition[attribute=null, field=1, beginIndex=2, endIndex=3]", 212 fpos.toString()); 213 214 FieldPosition fpos2 = new FieldPosition(DateFormat.Field.ERA); 215 fpos2.setBeginIndex(4); 216 fpos2.setEndIndex(5); 217 assertEquals("ToString returned the wrong value:", 218 "java.text.FieldPosition[attribute=" + DateFormat.Field.ERA 219 + ", field=-1, beginIndex=4, endIndex=5]", fpos2 220 .toString()); 221 } 222 223 /** 224 * Sets up the fixture, for example, open a network connection. This method 225 * is called before a test is executed. 226 */ 227 protected void setUp() { 228 } 229 230 /** 231 * Tears down the fixture, for example, close a network connection. This 232 * method is called after a test is executed. 233 */ 234 protected void tearDown() { 235 } 236} 237