Package util

Source Code of util.XMLUtilTest

/*
* 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: XMLUtilTest.java 3205 2009-09-27 17:13:47Z mlipp $
*
* $Log$
* Revision 1.2  2006/03/08 14:46:41  drmlipp
* Synchronized with 1.3.3p5.
*
* Revision 1.1.1.2.6.1  2005/12/16 09:38:50  drmlipp
* Added support for parsing local datetime specifications.
*
* Revision 1.1.1.2  2003/12/19 13:01:50  drmlipp
* Updated to 1.1rc1
*
* Revision 1.5  2003/09/19 15:18:45  lipp
* One more time to check.
*
* Revision 1.4  2003/09/07 19:40:25  lipp
* Extended tests.
*
* Revision 1.3  2003/09/05 11:16:39  lipp
* Added test for duration parsing.
*
* Revision 1.2  2003/06/27 09:44:13  lipp
* Fixed copyright/license information.
*
* Revision 1.1  2003/03/31 12:43:27  huaiyang
* initial.
*
*
*
*/
package util;

import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

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

import junit.framework.*;

/**
* Test the class of <code>de.danet.an.util.XMLUtil</code>.
*/
public class XMLUtilTest extends TestCase {
    private static final org.apache.commons.logging.Log logger
  = org.apache.commons.logging.LogFactory.getLog(XMLUtilTest.class);

    /**
     * constructor.
     */
    public XMLUtilTest(String name) {
  super (name);
    }

    /**
     * build a test suite.
     */
    public static Test suite() {
        TestSuite suite = new TestSuite();
  suite.addTest(new XMLUtilTest("parseDateWithMs"));
  suite.addTest(new XMLUtilTest("parseDateWithMsAndTimeZone"));
  suite.addTest(new XMLUtilTest("parseDateWithoutMs"));
  suite.addTest(new XMLUtilTest("parseDateWithoutMsAndTimeZone"));
  suite.addTest(new XMLUtilTest("parseDateBeforeZero"));
  suite.addTest(new XMLUtilTest("parseLocal"));
  suite.addTest(new XMLUtilTest("parseDuration"));
        return suite;
    }

    public static DateFormat toDateTimeFormatter;
    static {
  toDateTimeFormatter 
      = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  toDateTimeFormatter.setTimeZone (TimeZone.getTimeZone ("GMT"));
    }   
    
    /**
     * Given date time string with miniseconds included.
     */
    public void parseDateWithMs() throws Exception {
  String dateTime = "2003-03-31T09:04:00.035Z";
  assertTrue(parsedDateToString(dateTime).equals
       ("2003-03-31T09:04:00.035Z"));
    }

    /**
     * Given date time string with miniseconds and timezone included.
     */
    public void parseDateWithMsAndTimeZone() throws Exception {
  String dateTime = "2003-03-31T09:04:00.035+02:00";
  assertTrue(parsedDateToString(dateTime).equals
       ("2003-03-31T07:04:00.035Z"));
    }

    /**
     * Given date time string without miniseconds included.
     */
    public void parseDateWithoutMs() throws Exception {
  String dateTime = "2003-03-31T09:04:00+02:00";
  assertTrue(parsedDateToString(dateTime).equals
       ("2003-03-31T07:04:00.000Z"));
    }

    /**
     * Given date time string without miniseconds and timezone included.
     */
    public void parseDateWithoutMsAndTimeZone() throws Exception {
  String dateTime = "2003-03-31T09:04:00Z";
  assertTrue(parsedDateToString(dateTime).equals
       ("2003-03-31T09:04:00.000Z"));
    }

    /**
     * Date BC
     */
    public void parseDateBeforeZero() throws Exception {
  String dateTime = "-332-03-31T09:04:00Z";
  assertTrue ((new SimpleDateFormat ("yyyy G", Locale.US))
        .format (XMLUtil.parseXsdDateTime(dateTime))
        .equals ("0333 BC"));
    }

    public void parseLocal() throws Exception {
  Date now = new Date();
  String dateTime = (new SimpleDateFormat
         ("yyyy-MM-dd'T'HH:mm:ss.SSS")).format(now);
  Date parsed = XMLUtil.parseXsdDateTime (dateTime);
  assertTrue ("Expected: " + now + ", got: " + parsed,
        parsed.equals(now));
    }

    private String parsedDateToString(String dateTime) throws Exception {
  Date parsedDate = XMLUtil.parseXsdDateTime(dateTime);
  return toDateTimeFormatter.format (parsedDate);
    }

    /**
     * Parse a duration
     */
    public void parseDuration() throws Exception {
  String duration = "P123Y456M789D";
  Duration d = XMLUtil.parseXsdDuration (duration);
  assertTrue (d.getYears() == 123);
  assertTrue (d.getMonths() == 456);
  assertTrue (d.getDays() == 789);
  assertTrue (d.getHours() == 0);
  assertTrue (d.getMinutes() == 0);
  assertTrue (d.getSeconds() == 0);

  duration = "P123Y456M789DT987H654M321.123S";
  d = XMLUtil.parseXsdDuration (duration);
  assertTrue (d.getYears() == 123);
  assertTrue (d.getMonths() == 456);
  assertTrue (d.getDays() == 789);
  assertTrue (d.getHours() == 987);
  assertTrue (d.getMinutes() == 654);
  assertTrue (d.getSeconds() == (float)321.123);

  duration = "PT1M0.1S";
  d = XMLUtil.parseXsdDuration (duration);
  assertTrue (d.getYears() == 0);
  assertTrue (d.getMonths() == 0);
  assertTrue (d.getDays() == 0);
  assertTrue (d.getHours() == 0);
  assertTrue (d.getMinutes() == 1);
  assertTrue (d.getSeconds() == (float)0.1);
    }

}
TOP

Related Classes of util.XMLUtilTest

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.