Package util

Source Code of util.DurParse

/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* $Id: DurParse.java 1607 2006-09-29 12:32:13Z drmlipp $
*
* $Log$
* Revision 1.1.1.1  2003/12/19 13:01:51  drmlipp
* Updated to 1.1rc1
*
* Revision 1.5  2003/09/20 19:26:06  lipp
* Modified complex time value specification handling.
*
* Revision 1.4  2003/09/07 19:41:04  lipp
* Extended tests.
*
* Revision 1.3  2003/09/06 20:36:59  lipp
* Some fixes.
*
* Revision 1.2  2003/09/05 12:38:12  lipp
* Added duration parsing for XPDL.
*
* Revision 1.1  2003/09/04 15:55:46  lipp
* Initial version.
*
*/
package util;

import java.util.Date;

import java.text.ParseException;

import de.danet.an.util.Duration;
import de.danet.an.util.Duration.ValueEvaluator;
import de.danet.an.util.XMLUtil;

import de.danet.an.workflow.util.XPDLUtil;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Zusammenstellung aller Tests.
*/
public class DurParse extends TestCase {

    /**
     * Construct test.
     */
    public DurParse(String name) {
  super (name);
    }

    /**
     * Create test suite.
     */
    public static Test suite() {
        TestSuite suite = new TestSuite();
  suite.addTest(new DurParse("parse1"));
  suite.addTest(new DurParse("durCal"));
        return suite;
    }
    
    /**
     * Do some parsing.
     */
    public void parse1() throws Exception {
  Object o = XPDLUtil.parseDuration ("5 years");
  assertTrue (o instanceof Duration);
  assertTrue (((Duration)o).getYears() == 5);
  assertTrue (((Duration)o).getMonths() == 0);
  assertTrue (((Duration)o).getDays() == 0);
  assertTrue (((Duration)o).getHours() == 0);
  assertTrue (((Duration)o).getMinutes() == 0);
  assertTrue (((Duration)o).getSeconds() == (float)0);

  o = XPDLUtil.parseDuration ("5 years 2 days");
  assertTrue (o instanceof Duration);
  assertTrue (((Duration)o).getYears() == 5);
  assertTrue (((Duration)o).getMonths() == 0);
  assertTrue (((Duration)o).getDays() == 2);
  assertTrue (((Duration)o).getHours() == 0);
  assertTrue (((Duration)o).getMinutes() == 0);
  assertTrue (((Duration)o).getSeconds() == (float)0);

  o = XPDLUtil.parseDuration
      ("5 years x months 2days 3min 35.0sec",
       new ValueEvaluator () {
     public float evaluate (String s) throws ParseException {
         try {
       if (s.equals ("x")) {
           return -1;
       }
       return Float.parseFloat (s);
         } catch (NumberFormatException e) {
       throw new ParseException
           ("Not a number: " + s + ": " + e.getMessage (), 0);
         }
     }
       });
  assertTrue (o instanceof Duration);
  assertTrue (((Duration)o).getYears() == 5);
  assertTrue (((Duration)o).getMonths() == -1);
  assertTrue (((Duration)o).getDays() == 2);
  assertTrue (((Duration)o).getHours() == 0);
  assertTrue (((Duration)o).getMinutes() == 3);
  assertTrue (((Duration)o).getSeconds() == (float)35);

  o = XPDLUtil.parseDuration ("P5Y3M2DT3M35.0S");
  assertTrue (o instanceof Duration);
  assertTrue (((Duration)o).getYears() == 5);
  assertTrue (((Duration)o).getMonths() == 3);
  assertTrue (((Duration)o).getDays() == 2);
  assertTrue (((Duration)o).getHours() == 0);
  assertTrue (((Duration)o).getMinutes() == 3);
  assertTrue (((Duration)o).getSeconds() == (float)35);

  boolean gotEx = false;
  try {
      o = XPDLUtil.parseDuration ("Just garbage");
  } catch (ParseException e) {
      gotEx = true;
  }
  assertTrue (gotEx);

  o = XPDLUtil.parseDuration
      ("some_var days another-var - 2 hrs",
       new ValueEvaluator () {
     public float evaluate (String s) throws ParseException {
         try {
       if (s.equals ("some_var")) {
           return -1;
       }
       if (s.equals ("another-var - 2")) {
           return -2;
       }
       return Float.parseFloat (s);
         } catch (NumberFormatException e) {
       throw new ParseException
           ("Not a number: " + s + ": " + e.getMessage (), 0);
         }
     }
       });
  assertTrue (o instanceof Duration);
  assertTrue (((Duration)o).getYears() == 0);
  assertTrue (((Duration)o).getMonths() == 0);
  assertTrue (((Duration)o).getDays() == -1);
  assertTrue (((Duration)o).getHours() == -2);
  assertTrue (((Duration)o).getMinutes() == 0);
  assertTrue (((Duration)o).getSeconds() == (float)0);

  o = XPDLUtil.parseDuration ("2 hrs 3min");
  assertTrue (o instanceof Duration);
  assertTrue (((Duration)o).getYears() == 0);
  assertTrue (((Duration)o).getMonths() == 0);
  assertTrue (((Duration)o).getDays() == 0);
  assertTrue (((Duration)o).getHours() == 2);
  assertTrue (((Duration)o).getMinutes() == 3);
  assertTrue (((Duration)o).getSeconds() == (float)0);

  gotEx = false;
  try {
      o = XPDLUtil.parseDuration ("fourhrs");
  } catch (ParseException e) {
      gotEx = true;
  }
  assertTrue (gotEx);

  gotEx = false;
  try {
      o = XPDLUtil.parseDuration ("October 1st, 2003");
  } catch (ParseException e) {
      gotEx = true;
  }
  assertTrue (gotEx);

  gotEx = false;
  try {
      o = XPDLUtil.parseDuration ("1.2.2003 11:30");
  } catch (ParseException e) {
      gotEx = true;
  }
  assertTrue (gotEx);

  o = XPDLUtil.parseDuration ("01 Feb 2004 11:30 GMT");
  assertTrue (o instanceof Date);
  assertTrue (((Date)o).getTime () == 1075635000000L);
 
  o = XPDLUtil.parseDuration ("Thu, 4 Sep 03 13:48:35 GMT+01:00");
  assertTrue (o instanceof Date);
  assertTrue (((Date)o).getTime () == 1062679715000L);
 
  o = XPDLUtil.parseDuration ("4 Sep 03 13:48:35 GMT+01:00");
  assertTrue (o instanceof Date);
  assertTrue (((Date)o).getTime () == 1062679715000L);
 
  o = XPDLUtil.parseDuration ("4 Sep 2003 13:48:35 GMT+01:00");
  assertTrue (o instanceof Date);
  assertTrue (((Date)o).getTime () == 1062679715000L);
 
  o = XPDLUtil.parseDuration ("4 Sep 03 13:48 GMT+01:00");
  assertTrue (o instanceof Date);
  assertTrue (((Date)o).getTime () == 1062679680000L);
 
  o = XPDLUtil.parseDuration ("4 Sep 2003 13:48 GMT+01:00");
  assertTrue (o instanceof Date);
  assertTrue (((Date)o).getTime () == 1062679680000L);
 
  o = XPDLUtil.parseDuration ("4 Sep 2003 13:48 CET");
  assertTrue (o instanceof Date);
  assertTrue (((Date)o).getTime () == 1062679680000L);
 
  o = XPDLUtil.parseDuration ("4 Sep 2003 14:48 CEST");
  assertTrue (o instanceof Date);
  assertTrue (((Date)o).getTime () == 1062679680000L);
 
  o = XPDLUtil.parseDuration ("2003-09-04T13:48:00+01:00");
  assertTrue (o instanceof Date);
  assertTrue (((Date)o).getTime () == 1062679680000L);
    }

    /**
     * Do some calculations.
     */
    public void durCal() throws Exception {
  Date d = XMLUtil.parseXsdDateTime ("1999-01-31T13:48:00Z");
  assertTrue (XMLUtil.parseXsdDuration ("PT12M").addTo (d)
        .equals (XMLUtil.parseXsdDateTime ("1999-01-31T14:00:00Z")));
  assertTrue (XMLUtil.parseXsdDuration ("PT1H").addTo (d)
        .equals (XMLUtil.parseXsdDateTime ("1999-01-31T14:48:00Z")));
  assertTrue (XMLUtil.parseXsdDuration ("PT47H").addTo (d)
        .equals (XMLUtil.parseXsdDateTime ("1999-02-02T12:48:00Z")));
  assertTrue (XMLUtil.parseXsdDuration ("PT47H12M0.001S").addTo (d)
        .equals (XMLUtil.parseXsdDateTime ("1999-02-02T13:00:00.001Z")));
  assertTrue (XMLUtil.parseXsdDuration ("P3Y25M").addTo (d)
        .equals (XMLUtil.parseXsdDateTime ("2004-02-29T13:48:00Z")));
  assertTrue (XMLUtil.parseXsdDuration ("PT0.1S").addTo
        (XMLUtil.parseXsdDateTime ("1999-12-31T23:59:59.9Z"))
        .equals (XMLUtil.parseXsdDateTime ("2000-01-01T00:00:00Z")));
  assertTrue (XMLUtil.parseXsdDuration ("-PT0.1S").addTo
        (XMLUtil.parseXsdDateTime ("2000-01-01T00:00:00Z"))
        .equals (XMLUtil.parseXsdDateTime ("1999-12-31T23:59:59.9Z")));

  Date base = XMLUtil.parseXsdDateTime ("1999-01-01T00:00:00Z");
  for (int i = 1; i < 17000; i += 17) {
      if (XMLUtil.parseXsdDuration ("PT" + i + "H").addTo
    (base).getTime () != base.getTime() + i * 3600000L) {
    assertTrue
        ("Error for i = " + i + ", still OK: "
         + XMLUtil.parseXsdDuration ("PT"+(i-1)+"H").addTo (base)
         + " not OK: "
         + XMLUtil.parseXsdDuration ("PT" + i + "H").addTo (base),
         false);
      }
  }
    }
}
TOP

Related Classes of util.DurParse

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.