2013-12-31 19:36:07 +00:00
|
|
|
#include "common/Test.hpp"
|
|
|
|
#include "se/XMLGregorianCalendar.hpp"
|
|
|
|
#include "se/IllegalArgException.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
/* Ordinarily wouldn't bother testing setters and getters, but it's necessary
|
|
|
|
* here because the underlying implementation of of fractional seconds changed
|
|
|
|
* from a pointer to primitive, and is used throughout the class. No good
|
|
|
|
* reason why it was done like this in the first place, other than to
|
|
|
|
* unnecessarily provide a poor analogue of behaviour in the Java version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void testNoFractionalSecondSet()
|
|
|
|
{
|
2014-01-17 01:46:48 +00:00
|
|
|
XMLGC::XMLGregorianCalendar calendar;
|
2013-12-31 19:36:07 +00:00
|
|
|
ASSERT_EQ(calendar.getFractionalSecond(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testSetZeroFractionalSecond()
|
|
|
|
{
|
2014-01-17 01:46:48 +00:00
|
|
|
XMLGC::XMLGregorianCalendar calendar;
|
2013-12-31 19:36:07 +00:00
|
|
|
calendar.setFractionalSecond(0.0);
|
|
|
|
ASSERT_EQ(calendar.getFractionalSecond(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testSetNonZeroFractionalSecond()
|
|
|
|
{
|
2014-01-17 01:46:48 +00:00
|
|
|
XMLGC::XMLGregorianCalendar calendar;
|
2013-12-31 19:36:07 +00:00
|
|
|
calendar.setFractionalSecond(0.123);
|
|
|
|
ASSERT_EQ(calendar.getFractionalSecond(), 0.123);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testSetNegativeFractionalSecond()
|
|
|
|
{
|
2014-01-17 01:46:48 +00:00
|
|
|
XMLGC::XMLGregorianCalendar calendar;
|
2013-12-31 19:36:07 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
calendar.setFractionalSecond(-0.0000001);
|
|
|
|
FAIL("Should have thrown an illegal argument exception");
|
|
|
|
}
|
|
|
|
catch (IllegalArgException& e)
|
|
|
|
{
|
|
|
|
ASSERT_EQ(e.getMessage(), "Invalid fractional seconds");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testSetFractionalSecondToWholeSecond()
|
|
|
|
{
|
2014-01-17 01:46:48 +00:00
|
|
|
XMLGC::XMLGregorianCalendar calendar;
|
2013-12-31 19:36:07 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
calendar.setFractionalSecond(1);
|
|
|
|
FAIL("Should have thrown an illegal argument exception");
|
|
|
|
}
|
|
|
|
catch (IllegalArgException& e)
|
|
|
|
{
|
|
|
|
ASSERT_EQ(e.getMessage(), "Invalid fractional seconds");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Like fractional seconds, it seems that eons were implemented to maintain
|
|
|
|
* parity with the Java class. Given that it leaks memory, and we're not too
|
|
|
|
* concerned with the usability of the usability of this toolkit a billion
|
|
|
|
* years from now, eons have now been removed. The following tests ensure that
|
|
|
|
* the year field works correctly, since it's affected by the change.
|
|
|
|
*/
|
|
|
|
void testValidYearSet()
|
|
|
|
{
|
2014-01-17 01:46:48 +00:00
|
|
|
XMLGC::XMLGregorianCalendar calendar;
|
2013-12-31 19:36:07 +00:00
|
|
|
calendar.setDay(1);
|
|
|
|
calendar.setMonth(3);
|
|
|
|
calendar.setYear(2010);
|
|
|
|
ASSERT_EQ(calendar.isValid(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testInvalidNoYearSet()
|
|
|
|
{
|
2014-01-17 01:46:48 +00:00
|
|
|
XMLGC::XMLGregorianCalendar calendar("2015-04-08T04:23:29.0Z");
|
2013-12-31 19:36:07 +00:00
|
|
|
ASSERT_EQ(calendar.isValid(), true);
|
|
|
|
calendar.setYear(0);
|
|
|
|
ASSERT_EQ(calendar.isValid(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testInvalidNonLeapYear()
|
|
|
|
{
|
2014-01-17 01:46:48 +00:00
|
|
|
XMLGC::XMLGregorianCalendar calendar("2009-02-08T04:23:29.0Z");
|
2013-12-31 19:36:07 +00:00
|
|
|
ASSERT_EQ(calendar.isValid(), true);
|
|
|
|
calendar.setDay(29);
|
|
|
|
ASSERT_EQ(calendar.isValid(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testFormatYear()
|
|
|
|
{
|
2014-01-17 01:46:48 +00:00
|
|
|
XMLGC::XMLGregorianCalendar calendar;
|
2013-12-31 19:36:07 +00:00
|
|
|
calendar.setYear(2015);
|
|
|
|
ASSERT_EQ(calendar.format("%Y"), "2015");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No good reason why we should be allowing negative years, but there was code
|
|
|
|
* to explicitly ensure it was output correctly, so it does appear to be
|
|
|
|
* intended behaviour.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void testValidNegativeYear()
|
|
|
|
{
|
2014-01-17 01:46:48 +00:00
|
|
|
XMLGC::XMLGregorianCalendar calendar;
|
2013-12-31 19:36:07 +00:00
|
|
|
calendar.setYear(-2102);
|
|
|
|
ASSERT_EQ(calendar.isValid(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void testFormatNegativeYear()
|
|
|
|
{
|
2014-01-17 01:46:48 +00:00
|
|
|
XMLGC::XMLGregorianCalendar calendar;
|
2013-12-31 19:36:07 +00:00
|
|
|
calendar.setYear(-2015);
|
|
|
|
ASSERT_EQ(calendar.format("%Y"), "-2015");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
TEST_run(testNoFractionalSecondSet);
|
|
|
|
TEST_run(testSetZeroFractionalSecond);
|
|
|
|
TEST_run(testSetNonZeroFractionalSecond);
|
|
|
|
TEST_run(testSetNegativeFractionalSecond);
|
|
|
|
TEST_run(testSetFractionalSecondToWholeSecond);
|
|
|
|
TEST_run(testValidYearSet);
|
|
|
|
TEST_run(testInvalidNoYearSet);
|
|
|
|
TEST_run(testInvalidNonLeapYear);
|
|
|
|
TEST_run(testFormatYear);
|
|
|
|
TEST_run(testValidNegativeYear);
|
|
|
|
TEST_run(testFormatNegativeYear);
|
|
|
|
return TEST_errorCount();
|
|
|
|
}
|