📄 datetimetestcase.java
字号:
/* Sesame - Storage and Querying architecture for RDF and RDF Schema
* Copyright (C) 2005 ECCA - eTourism Competence Center Austria
*
* Contact:
* ECCA - eTourism Competence Center Austria
* Technikerstrasse 21a
* ICT-Technologiepark
* 6020 Innsbruck
* Austria
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.openrdf.util.xml;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* This class provides JUnit test cases for
* <code>org.openrdf.util.xml.datatypes.DateTime</code> class
*
* @version 0.2 2005-07-10
* @author Thomas Beer
* @see org.openrdf.util.xml.datatypes.DateTime
*/
public class DateTimeTestcase extends TestCase {
private static final String[] VALID_DATES = {
"0001-01-01T00:00:00",
"0001-01-01T00:00:00.0",
"0001-01-01T00:00:00Z",
"0001-01-01T00:00:00.0Z",
"0001-01-01T00:00:00+00:00",
"0001-01-01T00:00:00.0+00:00",
"0001-01-01T00:00:00.0-00:00",
"0001-01-01T00:00:00.0+14:00",
"0001-01-01T00:00:00.0-14:00",
"-0001-01-01T00:00:00",
"1234-12-31T23:59:59",
"1234-12-31T24:00:00",
"12345-12-31T24:00:00",
"5000000000-12-31T24:00:00", // year > Integer.MAX_VALUE
"-5000000000-12-31T24:00:00", // year < Integer.MIN_VALUE
"1234-12-31T24:00:00.1234567890",
"2004-02-29T00:00:00" // leap year
};
private static final String[] INVALID_DATES = {
"foo",
"Mon, 11 Jul 2005 09:22:29 +0200",
"0001-01-01T00:00",
"0001-13-01T00:00.00",
"0001-01-32T00:00.00",
"0001-02-30T00:00.00",
"2005-02-29T00:00:00",
"0001-01-01T25:00.00",
"0001-01-01T00:61.00",
"0001-01-01T00:00.61",
"0001-01-01T00:00.00+15:00",
"0001-01-01T00:00.00-15:00",
"001-01-01T00:00:00.0",
"0001-1-01T00:00:00.0",
"0001-01-1T00:00:00.0",
"0001-01-01T0:00:00.0",
"0001-01-01T00:0:00.0",
"0001-01-01T00:00:0.0",
"0001/01-01T00:00:00.0",
"0001-01/01T00:00:00.0",
"0001-01-01t00:00:00.0",
"0001-01-01T00.00:00.0",
"0001-01-01T00:00.00.0",
"0001-01-01T00:00:00:0",
"0001-01-01T00:00.00+0:00",
"0001-01-01T00:00.00+00:0",
"0001-jan-01T00:00:00",
"0001-01-01T00:00:00+00:00Z",
"0001-01-01T24:01:00",
"0001-01-01T24:00:01",
"00001-01-01T00:00:00",
"0001-001-01T00:00:00",
"0001-01-001T00:00:00",
"0001-01-01T000:00:00",
"0001-01-01T00:000:00",
"0001-01-01T00:00:000",
"0001-01-01T00:00:000",
"0001-01-01T00:00:00z",
"0001-01-01T00:00:00+05",
"0001-01-01T00:00:00+0500",
"0001-01-01T00:00:00GMT",
"0001-01-01T00:00:00PST",
"0001-01-01T00:00:00GMT+05",
"0000-01-01T00:00:00",
"0000-01-01T00:00:00",
"-0000-01-01T00:00:00",
"+0001-01-01T00:00:00"
};
private static final String[][] NORMALIZED_DATES = {
{"0001-01-01T00:00:00", "0001-01-01T00:00:00"},
{"0001-01-01T00:00:00.0", "0001-01-01T00:00:00"},
{"0001-01-01T00:00:00Z", "0001-01-01T00:00:00Z"},
{"0001-01-01T00:00:00.0Z", "0001-01-01T00:00:00Z"},
{"0001-01-01T00:00:00+00:00", "0001-01-01T00:00:00Z"},
{"0001-01-01T00:00:00-00:00", "0001-01-01T00:00:00Z"},
{"0001-01-01T00:00:00.0+00:00", "0001-01-01T00:00:00Z"},
{"0001-01-01T00:00:00.0-00:00", "0001-01-01T00:00:00Z"},
{"0001-01-01T00:00:00-14:00", "0001-01-01T14:00:00Z"},
{"0001-01-01T00:00:00+14:00", "-0001-12-31T10:00:00Z"},
{"1234-12-31T24:00:00", "1235-01-01T00:00:00"}
};
private static final String[][] EQUAL_DATES = {
{"2005-01-29T00:00:00", "2005-01-29T00:00:00"},
{"2005-01-29T00:00:00", "2005-01-28T24:00:00"},
{"2005-05-01T00:00:00.2+02:00", "2005-05-01T00:00:00.2+02:00"}
};
private static final String[][] COMPARISON_DATES = {
{"-0005-02-27T00:00:00", "2005-02-28T24:00:00"},
{"2005-02-28T24:00:00", "2005-03-27T00:00:00"},
{"2005-01-28T24:00:00", "2005-02-29T00:00:00.4"},
{"2005-02-29T00:00:00.1", "2005-02-29T00:00:00.4"},
{"2005-02-29T00:00:00", "2005-02-29T00:00:00.1"},
{"2005-02-29T00:00:00", "2005-02-29T00:00:00.2"},
{"2005-05-01T00:00:00.2+02:00", "2005-05-01T00:00:00.2+01:00"},
{"2005-05-01T00:00:00.2-01:00", "2005-05-01T00:00:00.2-02:00"},
{"2005-05-01T00:00:00+02:00", "2005-05-01T00:00:00.2+01:00"},
{"2005-05-01T00:00:00-01:00", "2005-05-01T00:00:00.2-02:00"},
{"2005-05-01T00:00:00-01:00", "2005-05-01T00:00:00-02:00"},
{"2005-05-01T00:00:00-01:00", "2005-05-01T00:00:00-02:00"},
{"-2005-05-01T00:00:00.2+02:00", "2005-05-01T00:00:00.2+02:00"},
{"2005-05-01T00:00:00.2+02:00", "20005-05-01T00:00:00.2+02:00"}
};
public static Test suite() {
TestSuite suite = new TestSuite();
for (int i = 0; i < VALID_DATES.length; i++) {
suite.addTest( new ValidDateTest(VALID_DATES[i]) );
}
for (int i = 0; i < INVALID_DATES.length; i++) {
suite.addTest( new InvalidDateTest(INVALID_DATES[i]) );
}
for (int i = 0; i < NORMALIZED_DATES.length; i++) {
suite.addTest( new NormalizedDateTest(NORMALIZED_DATES[i][0], NORMALIZED_DATES[i][1]) );
}
for (int i = 0; i < EQUAL_DATES.length; i++) {
suite.addTest( new EqualDateTest(EQUAL_DATES[i][0], EQUAL_DATES[i][1]) );
}
for (int i = 0; i < COMPARISON_DATES.length; i++) {
suite.addTest( new CompareDateTest(COMPARISON_DATES[i][0], COMPARISON_DATES[i][1]) );
}
return suite;
}
private static void _logError(String errMsg) {
System.err.println("[ERROR]: " + errMsg);
}
/*
* run test
*/
public static void main(String[] args) {
TestRunner.run(new TestSuite(DateTimeTestcase.class));
}
/*-------------------------------------------------+
| Inner classes for testing specific functionality |
+-------------------------------------------------*/
private static class ValidDateTest extends TestCase {
private String _dateString;
public ValidDateTest(String dateString) {
super("valid: " + dateString);
_dateString = dateString;
}
protected void runTest() {
if (!XmlDatatypeUtil.isValidDateTime(_dateString)) {
String errMsg = "string should be valid but is not: " + _dateString;
_logError(errMsg);
fail(errMsg);
}
}
}
private static class InvalidDateTest extends TestCase {
private String _dateString;
public InvalidDateTest(String dateString) {
super("invalid: " + dateString);
_dateString = dateString;
}
protected void runTest() {
if (XmlDatatypeUtil.isValidDateTime(_dateString)) {
String errMsg = "string should be invalid but is not: " + _dateString;
_logError(errMsg);
fail(errMsg);
}
}
}
private static class NormalizedDateTest extends TestCase {
private String _input;
private String _expected;
public NormalizedDateTest(String input, String expected) {
super("normalize: " + input + " --> " + expected);
_input = input;
_expected = expected;
}
protected void runTest() {
String normalized = XmlDatatypeUtil.normalizeDateTime(_input);
if (!_expected.equals(normalized)) {
String errMsg = "normalizing " + _input + " should produce " +
_expected + " but was " + normalized;
_logError(errMsg);
fail(errMsg);
}
}
}
private static class EqualDateTest extends TestCase {
private String _dateString1;
private String _dateString2;
public EqualDateTest(String dateString1, String dateString2) {
super(dateString1 + " == " + dateString2);
_dateString1 = dateString1;
_dateString2 = dateString2;
}
protected void runTest() {
int result = XmlDatatypeUtil.compareDateTime(_dateString1, _dateString2);
if (result != 0) {
String errMsg = _dateString1 + " and " + _dateString2 +
" should be equals but are not (result=" + result + ")";
_logError(errMsg);
fail(errMsg);
}
}
}
private static class CompareDateTest extends TestCase {
private String _dateString1;
private String _dateString2;
public CompareDateTest(String dateString1, String dateString2) {
super(dateString1 + " < " + dateString2);
_dateString1 = dateString1;
_dateString2 = dateString2;
}
protected void runTest() {
int result = XmlDatatypeUtil.compareDateTime(_dateString1, _dateString2);
if (result >= 0) {
String errMsg = _dateString1 + " should be smaller than " + _dateString2 +
" but is not (result=" + result + ")";
_logError(errMsg);
fail(errMsg);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -