Home | History | Annotate | Download | only in text
      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  */
     17 package org.apache.harmony.tests.java.text;
     18 
     19 import java.text.DateFormat;
     20 import java.text.FieldPosition;
     21 
     22 public 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 	public void test_hashCode() {
    161 	  // Test for method int java.text.FieldPosition.hashCode()
    162 	  FieldPosition fpos1 = new FieldPosition(1);
    163 	  FieldPosition fpos2 = new FieldPosition(1);
    164 	  assertTrue("test 1: hash codes are not equal for equal objects.",
    165 	             fpos1.hashCode() == fpos2.hashCode());
    166 	  fpos1.setBeginIndex(5);
    167 	  fpos1.setEndIndex(110);
    168 	  assertTrue("test 2: hash codes are equal for non equal objects.",
    169 	             fpos1.hashCode() != fpos2.hashCode());
    170 	  fpos2.setBeginIndex(5);
    171 	  fpos2.setEndIndex(110);
    172 	  assertTrue("test 3: hash codes are not equal for equal objects.",
    173 	             fpos1.hashCode() == fpos2.hashCode());
    174 
    175 	  FieldPosition fpos3 = new FieldPosition(
    176 	      DateFormat.Field.DAY_OF_WEEK_IN_MONTH);
    177 
    178 	  assertTrue("test 4: hash codes are equal for non equal objects.",
    179 	             fpos2.hashCode() != fpos3.hashCode());
    180 	}
    181 
    182 	public void test_setBeginIndexI() {
    183 	  FieldPosition fpos = new FieldPosition(1);
    184 	  fpos.setBeginIndex(2);
    185 	  fpos.setEndIndex(3);
    186 	  assertEquals("beginIndex should have been set to 2", 2, fpos
    187 	                   .getBeginIndex());
    188 
    189 	  fpos.setBeginIndex(Integer.MAX_VALUE);
    190 	  assertEquals("beginIndex should have been set to Integer.MAX_VALUE",
    191 	               Integer.MAX_VALUE, fpos.getBeginIndex());
    192 
    193 	  fpos.setBeginIndex(-1);
    194 	  assertEquals("beginIndex should have been set to -1",
    195 	               -1, fpos.getBeginIndex());
    196 	}
    197 
    198 	public void test_setEndIndexI() {
    199 	  FieldPosition fpos = new FieldPosition(1);
    200 	  fpos.setEndIndex(3);
    201 	  fpos.setBeginIndex(2);
    202 	  assertEquals("EndIndex should have been set to 3", 3, fpos
    203 	                   .getEndIndex());
    204 
    205 	  fpos.setEndIndex(Integer.MAX_VALUE);
    206 	  assertEquals("endIndex should have been set to Integer.MAX_VALUE",
    207 	               Integer.MAX_VALUE, fpos.getEndIndex());
    208 
    209 	  fpos.setEndIndex(-1);
    210 	  assertEquals("endIndex should have been set to -1",
    211 	               -1, fpos.getEndIndex());
    212 	}
    213 
    214 	/**
    215 	 * @tests java.text.FieldPosition#toString()
    216 	 */
    217 	public void test_toString() {
    218 		// Test for method java.lang.String java.text.FieldPosition.toString()
    219 		FieldPosition fpos = new FieldPosition(1);
    220 		fpos.setBeginIndex(2);
    221 		fpos.setEndIndex(3);
    222 		assertEquals(
    223 				"ToString returned the wrong value:",
    224 				"java.text.FieldPosition[field=1,attribute=null,beginIndex=2,endIndex=3]",
    225 				fpos.toString());
    226 
    227 		FieldPosition fpos2 = new FieldPosition(DateFormat.Field.ERA);
    228 		fpos2.setBeginIndex(4);
    229 		fpos2.setEndIndex(5);
    230 		assertEquals("ToString returned the wrong value:",
    231 				"java.text.FieldPosition[field=-1,attribute=" + DateFormat.Field.ERA
    232 						+ ",beginIndex=4,endIndex=5]", fpos2.toString());
    233 	}
    234 
    235 	/**
    236 	 * Sets up the fixture, for example, open a network connection. This method
    237 	 * is called before a test is executed.
    238 	 */
    239 	protected void setUp() {
    240 	}
    241 
    242 	/**
    243 	 * Tears down the fixture, for example, close a network connection. This
    244 	 * method is called after a test is executed.
    245 	 */
    246 	protected void tearDown() {
    247 	}
    248 }
    249