1 17 package org.apache.ws.jaxme.xs.junit; 18 19 import java.text.ParseException ; 20 import java.util.Calendar ; 21 import java.util.TimeZone ; 22 23 import org.apache.ws.jaxme.xs.util.XsDateFormat; 24 import org.apache.ws.jaxme.xs.util.XsDateTimeFormat; 25 import org.apache.ws.jaxme.xs.util.XsTimeFormat; 26 27 import junit.framework.TestCase; 28 29 34 public class FormatTest extends TestCase { 35 37 public FormatTest(String pName) { 38 super(pName); 39 } 40 41 private Calendar getCalendar(TimeZone pTimeZone) { 42 Calendar cal = Calendar.getInstance(pTimeZone); 43 cal.set(2004, 01-1, 14, 03, 12, 07); 44 cal.set(Calendar.MILLISECOND, 0); 45 return cal; 46 } 47 48 51 public void testFormatDateTime() { 52 Calendar cal = getCalendar(TimeZone.getTimeZone("GMT")); 53 assertEquals(0, cal.get(Calendar.MILLISECOND)); 54 XsDateTimeFormat format = new XsDateTimeFormat(); 55 String got = format.format(cal); 56 String expect = "2004-01-14T03:12:07Z"; 57 assertEquals(expect, got); 58 59 cal = getCalendar(TimeZone.getTimeZone("GMT-03:00")); 60 assertEquals(0, cal.get(Calendar.MILLISECOND)); 61 got = format.format(cal); 62 expect = "2004-01-14T03:12:07-03:00"; 63 assertEquals(expect, got); 64 } 65 66 69 public void testParseDateTime() throws ParseException { 70 String [] dateTimes = new String []{ 71 "2004-01-14T03:12:07.000Z", 72 "2004-01-14T03:12:07", 73 "2004-01-14T03:12:07-00:00", 74 "2004-01-14T03:12:07+00:00", 75 }; 76 XsDateTimeFormat format = new XsDateTimeFormat(); 77 Calendar expect = getCalendar(TimeZone.getTimeZone("GMT")); 78 for (int i = 0; i < dateTimes.length; i++) { 79 Calendar got = (Calendar ) format.parseObject(dateTimes[0]); 80 assertEquals(expect, got); 81 } 82 83 String dateTime = "2004-01-14T03:12:07.000-03:00"; 84 expect = getCalendar(TimeZone.getTimeZone("GMT-03:00")); 85 Calendar got = (Calendar ) format.parseObject(dateTime); 86 assertEquals(expect, got); 87 } 88 89 92 public void testFormatDate() { 93 Calendar cal = getCalendar(TimeZone.getTimeZone("GMT")); 94 assertEquals(0, cal.get(Calendar.MILLISECOND)); 95 XsDateFormat format = new XsDateFormat(); 96 String got = format.format(cal); 97 String expect = "2004-01-14Z"; 98 assertEquals(expect, got); 99 100 cal = getCalendar(TimeZone.getTimeZone("GMT-03:00")); 101 assertEquals(0, cal.get(Calendar.MILLISECOND)); 102 got = format.format(cal); 103 expect = "2004-01-14-03:00"; 104 assertEquals(expect, got); 105 } 106 107 protected void assertEqualDate(Calendar pExpect, Calendar pGot) { 108 assertEquals(pExpect.get(Calendar.YEAR), pGot.get(Calendar.YEAR)); 109 assertEquals(pExpect.get(Calendar.MONTH), pGot.get(Calendar.MONTH)); 110 assertEquals(pExpect.get(Calendar.DAY_OF_MONTH), pGot.get(Calendar.DAY_OF_MONTH)); 111 assertEquals(pExpect.getTimeZone(), pGot.getTimeZone()); 112 } 113 114 protected void assertEqualTime(Calendar pExpect, Calendar pGot) { 115 assertEquals(pExpect.get(Calendar.HOUR_OF_DAY), pGot.get(Calendar.HOUR_OF_DAY)); 116 assertEquals(pExpect.get(Calendar.MINUTE), pGot.get(Calendar.MINUTE)); 117 assertEquals(pExpect.get(Calendar.SECOND), pGot.get(Calendar.SECOND)); 118 assertEquals(pExpect.get(Calendar.MILLISECOND), pGot.get(Calendar.MILLISECOND)); 119 assertEquals(pExpect.getTimeZone(), pGot.getTimeZone()); 120 } 121 122 125 public void testParseDate() throws ParseException { 126 String [] dateTimes = new String []{ 127 "2004-01-14Z", 128 "2004-01-14", 129 "2004-01-14+00:00", 130 "2004-01-14-00:00", 131 }; 132 XsDateFormat format = new XsDateFormat(); 133 Calendar expect = getCalendar(TimeZone.getTimeZone("GMT")); 134 for (int i = 0; i < dateTimes.length; i++) { 135 Calendar got = (Calendar ) format.parseObject(dateTimes[0]); 136 assertEqualDate(expect, got); 137 } 138 139 String dateTime = "2004-01-14-03:00"; 140 expect = getCalendar(TimeZone.getTimeZone("GMT-03:00")); 141 Calendar got = (Calendar ) format.parseObject(dateTime); 142 assertEqualDate(expect, got); 143 } 144 145 148 public void testFormatTime() { 149 Calendar cal = getCalendar(TimeZone.getTimeZone("GMT")); 150 assertEquals(0, cal.get(Calendar.MILLISECOND)); 151 XsTimeFormat format = new XsTimeFormat(); 152 String got = format.format(cal); 153 String expect = "03:12:07Z"; 154 assertEquals(expect, got); 155 156 cal = getCalendar(TimeZone.getTimeZone("GMT-03:00")); 157 assertEquals(0, cal.get(Calendar.MILLISECOND)); 158 got = format.format(cal); 159 expect = "03:12:07-03:00"; 160 assertEquals(expect, got); 161 } 162 163 166 public void testParseTime() throws ParseException { 167 String [] dateTimes = new String []{ 168 "03:12:07.000Z", 169 "03:12:07", 170 "03:12:07-00:00", 171 "03:12:07+00:00", 172 }; 173 XsTimeFormat format = new XsTimeFormat(); 174 Calendar expect = getCalendar(TimeZone.getTimeZone("GMT")); 175 for (int i = 0; i < dateTimes.length; i++) { 176 Calendar got = (Calendar ) format.parseObject(dateTimes[0]); 177 assertEqualTime(expect, got); 178 } 179 180 String dateTime = "03:12:07.000-03:00"; 181 expect = getCalendar(TimeZone.getTimeZone("GMT-03:00")); 182 Calendar got = (Calendar ) format.parseObject(dateTime); 183 assertEqualTime(expect, got); 184 } 185 } 186 | Popular Tags |