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: DurParse.java,v 1.2 2006/09/29 12:32:09 drmlipp Exp $
021: *
022: * $Log: DurParse.java,v $
023: * Revision 1.2 2006/09/29 12:32:09 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.1.1.1 2003/12/19 13:01:51 drmlipp
027: * Updated to 1.1rc1
028: *
029: * Revision 1.5 2003/09/20 19:26:06 lipp
030: * Modified complex time value specification handling.
031: *
032: * Revision 1.4 2003/09/07 19:41:04 lipp
033: * Extended tests.
034: *
035: * Revision 1.3 2003/09/06 20:36:59 lipp
036: * Some fixes.
037: *
038: * Revision 1.2 2003/09/05 12:38:12 lipp
039: * Added duration parsing for XPDL.
040: *
041: * Revision 1.1 2003/09/04 15:55:46 lipp
042: * Initial version.
043: *
044: */
045: package util;
046:
047: import java.util.Date;
048:
049: import java.text.ParseException;
050:
051: import de.danet.an.util.Duration;
052: import de.danet.an.util.Duration.ValueEvaluator;
053: import de.danet.an.util.XMLUtil;
054:
055: import de.danet.an.workflow.util.XPDLUtil;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: /**
062: * Zusammenstellung aller Tests.
063: */
064: public class DurParse extends TestCase {
065:
066: /**
067: * Construct test.
068: */
069: public DurParse(String name) {
070: super (name);
071: }
072:
073: /**
074: * Create test suite.
075: */
076: public static Test suite() {
077: TestSuite suite = new TestSuite();
078: suite.addTest(new DurParse("parse1"));
079: suite.addTest(new DurParse("durCal"));
080: return suite;
081: }
082:
083: /**
084: * Do some parsing.
085: */
086: public void parse1() throws Exception {
087: Object o = XPDLUtil.parseDuration("5 years");
088: assertTrue(o instanceof Duration);
089: assertTrue(((Duration) o).getYears() == 5);
090: assertTrue(((Duration) o).getMonths() == 0);
091: assertTrue(((Duration) o).getDays() == 0);
092: assertTrue(((Duration) o).getHours() == 0);
093: assertTrue(((Duration) o).getMinutes() == 0);
094: assertTrue(((Duration) o).getSeconds() == (float) 0);
095:
096: o = XPDLUtil.parseDuration("5 years 2 days");
097: assertTrue(o instanceof Duration);
098: assertTrue(((Duration) o).getYears() == 5);
099: assertTrue(((Duration) o).getMonths() == 0);
100: assertTrue(((Duration) o).getDays() == 2);
101: assertTrue(((Duration) o).getHours() == 0);
102: assertTrue(((Duration) o).getMinutes() == 0);
103: assertTrue(((Duration) o).getSeconds() == (float) 0);
104:
105: o = XPDLUtil.parseDuration(
106: "5 years x months 2days 3min 35.0sec",
107: new ValueEvaluator() {
108: public float evaluate(String s)
109: throws ParseException {
110: try {
111: if (s.equals("x")) {
112: return -1;
113: }
114: return Float.parseFloat(s);
115: } catch (NumberFormatException e) {
116: throw new ParseException("Not a number: "
117: + s + ": " + e.getMessage(), 0);
118: }
119: }
120: });
121: assertTrue(o instanceof Duration);
122: assertTrue(((Duration) o).getYears() == 5);
123: assertTrue(((Duration) o).getMonths() == -1);
124: assertTrue(((Duration) o).getDays() == 2);
125: assertTrue(((Duration) o).getHours() == 0);
126: assertTrue(((Duration) o).getMinutes() == 3);
127: assertTrue(((Duration) o).getSeconds() == (float) 35);
128:
129: o = XPDLUtil.parseDuration("P5Y3M2DT3M35.0S");
130: assertTrue(o instanceof Duration);
131: assertTrue(((Duration) o).getYears() == 5);
132: assertTrue(((Duration) o).getMonths() == 3);
133: assertTrue(((Duration) o).getDays() == 2);
134: assertTrue(((Duration) o).getHours() == 0);
135: assertTrue(((Duration) o).getMinutes() == 3);
136: assertTrue(((Duration) o).getSeconds() == (float) 35);
137:
138: boolean gotEx = false;
139: try {
140: o = XPDLUtil.parseDuration("Just garbage");
141: } catch (ParseException e) {
142: gotEx = true;
143: }
144: assertTrue(gotEx);
145:
146: o = XPDLUtil.parseDuration("some_var days another-var - 2 hrs",
147: new ValueEvaluator() {
148: public float evaluate(String s)
149: throws ParseException {
150: try {
151: if (s.equals("some_var")) {
152: return -1;
153: }
154: if (s.equals("another-var - 2")) {
155: return -2;
156: }
157: return Float.parseFloat(s);
158: } catch (NumberFormatException e) {
159: throw new ParseException("Not a number: "
160: + s + ": " + e.getMessage(), 0);
161: }
162: }
163: });
164: assertTrue(o instanceof Duration);
165: assertTrue(((Duration) o).getYears() == 0);
166: assertTrue(((Duration) o).getMonths() == 0);
167: assertTrue(((Duration) o).getDays() == -1);
168: assertTrue(((Duration) o).getHours() == -2);
169: assertTrue(((Duration) o).getMinutes() == 0);
170: assertTrue(((Duration) o).getSeconds() == (float) 0);
171:
172: o = XPDLUtil.parseDuration("2 hrs 3min");
173: assertTrue(o instanceof Duration);
174: assertTrue(((Duration) o).getYears() == 0);
175: assertTrue(((Duration) o).getMonths() == 0);
176: assertTrue(((Duration) o).getDays() == 0);
177: assertTrue(((Duration) o).getHours() == 2);
178: assertTrue(((Duration) o).getMinutes() == 3);
179: assertTrue(((Duration) o).getSeconds() == (float) 0);
180:
181: gotEx = false;
182: try {
183: o = XPDLUtil.parseDuration("fourhrs");
184: } catch (ParseException e) {
185: gotEx = true;
186: }
187: assertTrue(gotEx);
188:
189: gotEx = false;
190: try {
191: o = XPDLUtil.parseDuration("October 1st, 2003");
192: } catch (ParseException e) {
193: gotEx = true;
194: }
195: assertTrue(gotEx);
196:
197: gotEx = false;
198: try {
199: o = XPDLUtil.parseDuration("1.2.2003 11:30");
200: } catch (ParseException e) {
201: gotEx = true;
202: }
203: assertTrue(gotEx);
204:
205: o = XPDLUtil.parseDuration("01 Feb 2004 11:30 GMT");
206: assertTrue(o instanceof Date);
207: assertTrue(((Date) o).getTime() == 1075635000000L);
208:
209: o = XPDLUtil.parseDuration("Thu, 4 Sep 03 13:48:35 GMT+01:00");
210: assertTrue(o instanceof Date);
211: assertTrue(((Date) o).getTime() == 1062679715000L);
212:
213: o = XPDLUtil.parseDuration("4 Sep 03 13:48:35 GMT+01:00");
214: assertTrue(o instanceof Date);
215: assertTrue(((Date) o).getTime() == 1062679715000L);
216:
217: o = XPDLUtil.parseDuration("4 Sep 2003 13:48:35 GMT+01:00");
218: assertTrue(o instanceof Date);
219: assertTrue(((Date) o).getTime() == 1062679715000L);
220:
221: o = XPDLUtil.parseDuration("4 Sep 03 13:48 GMT+01:00");
222: assertTrue(o instanceof Date);
223: assertTrue(((Date) o).getTime() == 1062679680000L);
224:
225: o = XPDLUtil.parseDuration("4 Sep 2003 13:48 GMT+01:00");
226: assertTrue(o instanceof Date);
227: assertTrue(((Date) o).getTime() == 1062679680000L);
228:
229: o = XPDLUtil.parseDuration("4 Sep 2003 13:48 CET");
230: assertTrue(o instanceof Date);
231: assertTrue(((Date) o).getTime() == 1062679680000L);
232:
233: o = XPDLUtil.parseDuration("4 Sep 2003 14:48 CEST");
234: assertTrue(o instanceof Date);
235: assertTrue(((Date) o).getTime() == 1062679680000L);
236:
237: o = XPDLUtil.parseDuration("2003-09-04T13:48:00+01:00");
238: assertTrue(o instanceof Date);
239: assertTrue(((Date) o).getTime() == 1062679680000L);
240: }
241:
242: /**
243: * Do some calculations.
244: */
245: public void durCal() throws Exception {
246: Date d = XMLUtil.parseXsdDateTime("1999-01-31T13:48:00Z");
247: assertTrue(XMLUtil.parseXsdDuration("PT12M").addTo(d).equals(
248: XMLUtil.parseXsdDateTime("1999-01-31T14:00:00Z")));
249: assertTrue(XMLUtil.parseXsdDuration("PT1H").addTo(d).equals(
250: XMLUtil.parseXsdDateTime("1999-01-31T14:48:00Z")));
251: assertTrue(XMLUtil.parseXsdDuration("PT47H").addTo(d).equals(
252: XMLUtil.parseXsdDateTime("1999-02-02T12:48:00Z")));
253: assertTrue(XMLUtil
254: .parseXsdDuration("PT47H12M0.001S")
255: .addTo(d)
256: .equals(
257: XMLUtil
258: .parseXsdDateTime("1999-02-02T13:00:00.001Z")));
259: assertTrue(XMLUtil.parseXsdDuration("P3Y25M").addTo(d).equals(
260: XMLUtil.parseXsdDateTime("2004-02-29T13:48:00Z")));
261: assertTrue(XMLUtil
262: .parseXsdDuration("PT0.1S")
263: .addTo(
264: XMLUtil
265: .parseXsdDateTime("1999-12-31T23:59:59.9Z"))
266: .equals(
267: XMLUtil
268: .parseXsdDateTime("2000-01-01T00:00:00Z")));
269: assertTrue(XMLUtil
270: .parseXsdDuration("-PT0.1S")
271: .addTo(XMLUtil.parseXsdDateTime("2000-01-01T00:00:00Z"))
272: .equals(
273: XMLUtil
274: .parseXsdDateTime("1999-12-31T23:59:59.9Z")));
275:
276: Date base = XMLUtil.parseXsdDateTime("1999-01-01T00:00:00Z");
277: for (int i = 1; i < 17000; i += 17) {
278: if (XMLUtil.parseXsdDuration("PT" + i + "H").addTo(base)
279: .getTime() != base.getTime() + i * 3600000L) {
280: assertTrue("Error for i = "
281: + i
282: + ", still OK: "
283: + XMLUtil
284: .parseXsdDuration("PT" + (i - 1) + "H")
285: .addTo(base)
286: + " not OK: "
287: + XMLUtil.parseXsdDuration("PT" + i + "H")
288: .addTo(base), false);
289: }
290: }
291: }
292: }
|