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 18package org.apache.harmony.tests.java.text; 19 20import java.io.ByteArrayInputStream; 21import java.io.ByteArrayOutputStream; 22import java.io.InvalidObjectException; 23import java.io.IOException; 24import java.io.ObjectInputStream; 25import java.io.ObjectOutputStream; 26 27import java.text.DateFormat; 28import java.text.DateFormat.Field; 29import java.util.Calendar; 30 31import junit.framework.TestCase; 32 33public class DataFormatFieldTest extends TestCase{ 34 35 public void test_ConstructorLjava_lang_StringLjava_lang_String() { 36 // Regression for HARMONY-178 37 MyField field = new MyField("day of month", Calendar.ERA); 38 39 assertEquals("field has wrong name", "day of month", field.getName()); 40 assertEquals("field has wrong Calendar field number", Calendar.ERA, 41 field.getCalendarField()); 42 43 DateFormat.Field realField = DateFormat.Field 44 .ofCalendarField(Calendar.ERA); 45 assertSame("Modified calendar field with the same field number", 46 DateFormat.Field.ERA, realField); 47 48 DateFormat.Field realField2 = DateFormat.Field 49 .ofCalendarField(Calendar.DAY_OF_MONTH); 50 assertSame("Modified calendar field with the same field number", 51 DateFormat.Field.DAY_OF_MONTH, realField2); 52 } 53 54 static class MyField extends DateFormat.Field { 55 private static final long serialVersionUID = 1L; 56 57 protected MyField(String fieldName, int calendarField) { 58 super(fieldName, calendarField); 59 } 60 61 protected String getName() { 62 return super.getName(); 63 } 64 } 65 66 /** 67 * @tests java.text.DateFormat$Field#Field(java.lang.String, int) 68 */ 69 public void test_ConstructorLjava_lang_StringI() { 70 MyField field = new MyField("a field", Calendar.DAY_OF_WEEK); 71 72 assertEquals("field has wrong name", "a field", field.getName()); 73 assertEquals("field has wrong Calendar field number", 74 Calendar.DAY_OF_WEEK, field.getCalendarField()); 75 76 DateFormat.Field realField = DateFormat.Field 77 .ofCalendarField(Calendar.DAY_OF_WEEK); 78 assertSame("Modified calendar field with the same field number", 79 DateFormat.Field.DAY_OF_WEEK, realField); 80 } 81 82 /** 83 * @tests java.text.DateFormat$Field#Field(java.lang.String, int) 84 */ 85 public void test_Constructor2() { 86 MyField field = new MyField("day of month", Calendar.ERA); 87 88 assertEquals("field has wrong name", "day of month", field.getName()); 89 assertEquals("field has wrong Calendar field number", Calendar.ERA, 90 field.getCalendarField()); 91 92 DateFormat.Field realField = DateFormat.Field 93 .ofCalendarField(Calendar.ERA); 94 assertSame("Modified calendar field with the same field number", 95 DateFormat.Field.ERA, realField); 96 97 DateFormat.Field realField2 = DateFormat.Field 98 .ofCalendarField(Calendar.DAY_OF_MONTH); 99 assertSame("Modified calendar field with the same field number", 100 DateFormat.Field.DAY_OF_MONTH, realField2); 101 } 102 103 /** 104 * @tests java.text.DateFormat$Field#getCalendarField() 105 */ 106 public void test_getCalendarField() { 107 // Test for method int getCalendarField() 108 assertEquals("Field.AM_PM.getCalendarField() returned the wrong value", 109 Calendar.AM_PM, Field.AM_PM.getCalendarField()); 110 111 // test special cases 112 assertEquals( 113 "Field.TIME_ZONE.getCalendarField() returned the wrong value", 114 -1, Field.TIME_ZONE.getCalendarField()); 115 assertEquals("Field.HOUR0.getCalendarField() returned the wrong value", 116 Calendar.HOUR, Field.HOUR0.getCalendarField()); 117 assertEquals("Field.HOUR1.getCalendarField() returned the wrong value", 118 -1, Field.HOUR1.getCalendarField()); 119 assertEquals( 120 "Field.HOUR_OF_DAY0.getCalendarField() returned the wrong value", 121 Calendar.HOUR_OF_DAY, Field.HOUR_OF_DAY0.getCalendarField()); 122 assertEquals( 123 "Field.HOUR_OF_DAY1.getCalendarField() returned the wrong value", 124 -1, Field.HOUR_OF_DAY1.getCalendarField()); 125 } 126 127 /** 128 * @tests java.text.DateFormat$Field#ofCalendarField(int) 129 */ 130 public void test_ofCalendarFieldI() { 131 // Test for method static java.text.DateFormat.Field 132 // ofCalendarField(int) 133 assertSame("ofCalendarField(Calendar.AM_PM) returned the wrong value", 134 Field.AM_PM, Field.ofCalendarField(Calendar.AM_PM)); 135 136 // test special cases 137 assertSame("ofCalendarField(Calendar.HOUR) returned the wrong value", 138 Field.HOUR0, Field.ofCalendarField(Calendar.HOUR)); 139 assertSame( 140 "ofCalendarField(Calendar.HOUR_OF_DAY) returned the wrong value", 141 Field.HOUR_OF_DAY0, Field.ofCalendarField(Calendar.HOUR_OF_DAY)); 142 143 // test illegal args 144 try { 145 DateFormat.Field.ofCalendarField(-1); 146 fail("Expected IllegalArgumentException for ofCalendarField(-1)"); 147 } catch (IllegalArgumentException e) { 148 } 149 150 try { 151 DateFormat.Field.ofCalendarField(Calendar.FIELD_COUNT); 152 fail("Expected IllegalArgumentException for ofCalendarField(Calendar.FIELD_COUNT)"); 153 } catch (IllegalArgumentException e) { 154 } 155 156 // test Calendar fields that do not have corresponding DateFormat Fields 157 assertNull( 158 "ofCalendarField(Calendar.DST_OFFSET) returned the wrong value", 159 DateFormat.Field.ofCalendarField(Calendar.DST_OFFSET)); 160 assertNull( 161 "ofCalendarField(Calendar.ZONE_OFFSET) returned the wrong value", 162 DateFormat.Field.ofCalendarField(Calendar.ZONE_OFFSET)); 163 } 164 165 /** 166 * @tests java.text.DateFormat$Field#readResolve() 167 */ 168 public void test_readResolve() { 169 // test for method java.lang.Object readResolve() 170 171 // see serialization stress tests: 172 // implemented in 173 // SerializationStressTest4.test_writeObject_NumberFormat_Field() 174 175 ObjectOutputStream out = null; 176 ObjectInputStream in = null; 177 try { 178 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 179 out = new ObjectOutputStream(bytes); 180 181 DateFormat.Field dfield, dfield2; 182 MyField field; 183 184 // a regular instance of DateFormat.Field 185 dfield = DateFormat.Field.MILLISECOND; 186 187 // a subclass instance with null name 188 field = new MyField(null, Calendar.AM_PM); 189 190 out.writeObject(dfield); 191 out.writeObject(field); 192 193 in = new ObjectInputStream(new ByteArrayInputStream(bytes 194 .toByteArray())); 195 196 try { 197 dfield2 = (Field) in.readObject(); 198 assertSame("resolved incorrectly", dfield, dfield2); 199 } catch (IllegalArgumentException e) { 200 fail("Unexpected IllegalArgumentException: " + e); 201 } 202 203 try { 204 in.readObject(); 205 fail("Expected InvalidObjectException for subclass instance with null name"); 206 } catch (InvalidObjectException e) { 207 } 208 209 } catch (IOException e) { 210 fail("unexpected IOException" + e); 211 } catch (ClassNotFoundException e) { 212 fail("unexpected ClassNotFoundException" + e); 213 } finally { 214 try { 215 if (out != null) 216 out.close(); 217 if (in != null) 218 in.close(); 219 } catch (IOException e) { 220 } 221 } 222 } 223} 224