001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: XMLUtilTest.java,v 1.3 2006/09/29 12:32:09 drmlipp Exp $
021: *
022: * $Log: XMLUtilTest.java,v $
023: * Revision 1.3 2006/09/29 12:32:09 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.2 2006/03/08 14:46:41 drmlipp
027: * Synchronized with 1.3.3p5.
028: *
029: * Revision 1.1.1.2.6.1 2005/12/16 09:38:50 drmlipp
030: * Added support for parsing local datetime specifications.
031: *
032: * Revision 1.1.1.2 2003/12/19 13:01:50 drmlipp
033: * Updated to 1.1rc1
034: *
035: * Revision 1.5 2003/09/19 15:18:45 lipp
036: * One more time to check.
037: *
038: * Revision 1.4 2003/09/07 19:40:25 lipp
039: * Extended tests.
040: *
041: * Revision 1.3 2003/09/05 11:16:39 lipp
042: * Added test for duration parsing.
043: *
044: * Revision 1.2 2003/06/27 09:44:13 lipp
045: * Fixed copyright/license information.
046: *
047: * Revision 1.1 2003/03/31 12:43:27 huaiyang
048: * initial.
049: *
050: *
051: *
052: */
053: package util;
054:
055: import java.util.Date;
056: import java.util.TimeZone;
057:
058: import java.text.DateFormat;
059: import java.text.SimpleDateFormat;
060:
061: import de.danet.an.util.Duration;
062: import de.danet.an.util.XMLUtil;
063:
064: import junit.framework.*;
065:
066: /**
067: * Test the class of <code>de.danet.an.util.XMLUtil</code>.
068: */
069: public class XMLUtilTest extends TestCase {
070: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
071: .getLog(XMLUtilTest.class);
072:
073: /**
074: * constructor.
075: */
076: public XMLUtilTest(String name) {
077: super (name);
078: }
079:
080: /**
081: * build a test suite.
082: */
083: public static Test suite() {
084: TestSuite suite = new TestSuite();
085: suite.addTest(new XMLUtilTest("parseDateWithMs"));
086: suite.addTest(new XMLUtilTest("parseDateWithMsAndTimeZone"));
087: suite.addTest(new XMLUtilTest("parseDateWithoutMs"));
088: suite.addTest(new XMLUtilTest("parseDateWithoutMsAndTimeZone"));
089: suite.addTest(new XMLUtilTest("parseDateBeforeZero"));
090: suite.addTest(new XMLUtilTest("parseLocal"));
091: suite.addTest(new XMLUtilTest("parseDuration"));
092: return suite;
093: }
094:
095: public static DateFormat toDateTimeFormatter;
096: static {
097: toDateTimeFormatter = new SimpleDateFormat(
098: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
099: toDateTimeFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
100: }
101:
102: /**
103: * Given date time string with miniseconds included.
104: */
105: public void parseDateWithMs() throws Exception {
106: String dateTime = "2003-03-31T09:04:00.035Z";
107: assertTrue(parsedDateToString(dateTime).equals(
108: "2003-03-31T09:04:00.035Z"));
109: }
110:
111: /**
112: * Given date time string with miniseconds and timezone included.
113: */
114: public void parseDateWithMsAndTimeZone() throws Exception {
115: String dateTime = "2003-03-31T09:04:00.035+02:00";
116: assertTrue(parsedDateToString(dateTime).equals(
117: "2003-03-31T07:04:00.035Z"));
118: }
119:
120: /**
121: * Given date time string without miniseconds included.
122: */
123: public void parseDateWithoutMs() throws Exception {
124: String dateTime = "2003-03-31T09:04:00+02:00";
125: assertTrue(parsedDateToString(dateTime).equals(
126: "2003-03-31T07:04:00.000Z"));
127: }
128:
129: /**
130: * Given date time string without miniseconds and timezone included.
131: */
132: public void parseDateWithoutMsAndTimeZone() throws Exception {
133: String dateTime = "2003-03-31T09:04:00Z";
134: assertTrue(parsedDateToString(dateTime).equals(
135: "2003-03-31T09:04:00.000Z"));
136: }
137:
138: /**
139: * Date BC
140: */
141: public void parseDateBeforeZero() throws Exception {
142: String dateTime = "-332-03-31T09:04:00Z";
143: assertTrue((new SimpleDateFormat("yyyy G")).format(
144: XMLUtil.parseXsdDateTime(dateTime)).equals("0333 BC"));
145: }
146:
147: public void parseLocal() throws Exception {
148: Date now = new Date();
149: String dateTime = (new SimpleDateFormat(
150: "yyyy-MM-dd'T'HH:mm:ss.SSS")).format(now);
151: Date parsed = XMLUtil.parseXsdDateTime(dateTime);
152: assertTrue("Expected: " + now + ", got: " + parsed, parsed
153: .equals(now));
154: }
155:
156: private String parsedDateToString(String dateTime) throws Exception {
157: Date parsedDate = XMLUtil.parseXsdDateTime(dateTime);
158: return toDateTimeFormatter.format(parsedDate);
159: }
160:
161: /**
162: * Parse a duration
163: */
164: public void parseDuration() throws Exception {
165: String duration = "P123Y456M789D";
166: Duration d = XMLUtil.parseXsdDuration(duration);
167: assertTrue(d.getYears() == 123);
168: assertTrue(d.getMonths() == 456);
169: assertTrue(d.getDays() == 789);
170: assertTrue(d.getHours() == 0);
171: assertTrue(d.getMinutes() == 0);
172: assertTrue(d.getSeconds() == 0);
173:
174: duration = "P123Y456M789DT987H654M321.123S";
175: d = XMLUtil.parseXsdDuration(duration);
176: assertTrue(d.getYears() == 123);
177: assertTrue(d.getMonths() == 456);
178: assertTrue(d.getDays() == 789);
179: assertTrue(d.getHours() == 987);
180: assertTrue(d.getMinutes() == 654);
181: assertTrue(d.getSeconds() == (float) 321.123);
182:
183: duration = "PT1M0.1S";
184: d = XMLUtil.parseXsdDuration(duration);
185: assertTrue(d.getYears() == 0);
186: assertTrue(d.getMonths() == 0);
187: assertTrue(d.getDays() == 0);
188: assertTrue(d.getHours() == 0);
189: assertTrue(d.getMinutes() == 1);
190: assertTrue(d.getSeconds() == (float) 0.1);
191: }
192:
193: }
|