Java Code Examples for javax.xml.datatype.DatatypeFactory#newDurationYearMonth()

The following examples show how to use javax.xml.datatype.DatatypeFactory#newDurationYearMonth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultDurationLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
private javax.xml.datatype.Duration toYearsMonthDuration(DatatypeFactory datatypeFactory, LocalDate date1, LocalDate date2) {
    Period between = Period.between(date2, date1);
    int years = between.getYears();
    int months = between.getMonths();
    if (between.isNegative()) {
        years = - years;
        months = - months;
    }
    return datatypeFactory.newDurationYearMonth(!between.isNegative(), years, months);
}
 
Example 2
Source File: DefaultDurationLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
private Duration toYearsMonthDuration(DatatypeFactory datatypeFactory, LocalDate date1, LocalDate date2) {
    Period between = Period.between(date2, date1);
    int years = between.getYears();
    int months = between.getMonths();
    if (between.isNegative()) {
        years = - years;
        months = - months;
    }
    return datatypeFactory.newDurationYearMonth(!between.isNegative(), years, months);
}
 
Example 3
Source File: Bug6937964Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test() throws DatatypeConfigurationException {
    DatatypeFactory dtf = DatatypeFactory.newInstance();
    Duration d = dtf.newDurationYearMonth("P20Y15M");
    int years = d.getYears();
    System.out.println(d.getYears() == 21 ? "pass" : "fail");
}
 
Example 4
Source File: Bug6937964Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testNewDurationYearMonthLexicalRepresentation() throws DatatypeConfigurationException {
    DatatypeFactory dtf = DatatypeFactory.newInstance();
    Duration d = dtf.newDurationYearMonth("P20Y15M");
    int years = d.getYears();
    Assert.assertTrue(years == 21, "Return value should be normalized");
}
 
Example 5
Source File: Bug6937964Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testNewDurationYearMonthMilliseconds() throws DatatypeConfigurationException {
    DatatypeFactory dtf = DatatypeFactory.newInstance();
    Duration d = dtf.newDurationYearMonth(671976000000L);
    int years = d.getYears();
    System.out.println("Years: " + years);
    Assert.assertTrue(years == 21, "Return value should be normalized");
}
 
Example 6
Source File: Bug6937964Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testNewDurationYearMonthBigInteger() throws DatatypeConfigurationException {
    DatatypeFactory dtf = DatatypeFactory.newInstance();
    BigInteger year = new BigInteger("20");
    BigInteger mon = new BigInteger("15");
    Duration d = dtf.newDurationYearMonth(true, year, mon);
    int years = d.getYears();
    Assert.assertTrue(years == 21, "Return value should be normalized");
}
 
Example 7
Source File: Bug6937964Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testNewDurationYearMonthInt() throws DatatypeConfigurationException {
    DatatypeFactory dtf = DatatypeFactory.newInstance();
    Duration d = dtf.newDurationYearMonth(true, 20, 15);
    int years = d.getYears();
    Assert.assertTrue(years == 21, "Return value should be normalized");
}
 
Example 8
Source File: ValueConverterTest.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertFromJavaDuration() throws DatatypeConfigurationException {
  DatatypeFactory f = DatatypeFactory.newInstance();
  Duration d = f.newDuration(true, 1, 2, 3, 4, 5, 6);
  ValueConverter.convertFromJava(d, processor);
  checkProcessor("duration", "xs:duration", d);

  d = f.newDurationYearMonth(true, 1, 2);
  ValueConverter.convertFromJava(d, processor);
  checkProcessor("year month duration", "xs:yearMonthDuration", d);

  d = f.newDurationDayTime(true, 3, 4, 5, 6);
  ValueConverter.convertFromJava(d, processor);
  checkProcessor("day time duration", "xs:dayTimeDuration", d);
}
 
Example 9
Source File: DatatypeFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test {@link DatatypeFactory.newDurationYearMonth(String
 * lexicalRepresentation)}.
 */
@Test
public final void testNewDurationYearMonthLexicalRepresentation() {

    /**
     * Lexical test values to test.
     */
    final String[] TEST_VALUES_LEXICAL = { null, TEST_VALUE_FAIL, "", TEST_VALUE_FAIL, "-", TEST_VALUE_FAIL, "P", TEST_VALUE_FAIL, "-P", TEST_VALUE_FAIL,
            "P1D", TEST_VALUE_FAIL, "P1Y1M1D", TEST_VALUE_FAIL, "P1M", "P1M", "-P1M", "-P1M", "P1Y", "P1Y", "-P1Y", "-P1Y", "P1Y1M", "P1Y1M", "-P1Y1M",
            "-P1Y1M" };

    DatatypeFactory datatypeFactory = null;
    try {
        datatypeFactory = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException datatypeConfigurationException) {
        Assert.fail(datatypeConfigurationException.toString());
    }

    if (DEBUG) {
        System.err.println("DatatypeFactory created: " + datatypeFactory.toString());
    }

    // test each value
    for (int onTestValue = 0; onTestValue < TEST_VALUES_LEXICAL.length; onTestValue = onTestValue + 2) {

        if (DEBUG) {
            System.err.println("testing value: \"" + TEST_VALUES_LEXICAL[onTestValue] + "\", expecting: \"" + TEST_VALUES_LEXICAL[onTestValue + 1] + "\"");
        }

        try {
            Duration duration = datatypeFactory.newDurationYearMonth(TEST_VALUES_LEXICAL[onTestValue]);

            if (DEBUG) {
                System.err.println("Duration created: \"" + duration.toString() + "\"");
            }

            // was this expected to fail?
            if (TEST_VALUES_LEXICAL[onTestValue + 1].equals(TEST_VALUE_FAIL)) {
                Assert.fail("the value \"" + TEST_VALUES_LEXICAL[onTestValue] + "\" is invalid yet it created the Duration \"" + duration.toString() + "\"");
            }

            // right XMLSchemaType?
            // TODO: enable test, it should pass, it fails with Exception(s)
            // for now due to a bug
            try {
                QName xmlSchemaType = duration.getXMLSchemaType();
                if (!xmlSchemaType.equals(DatatypeConstants.DURATION_YEARMONTH)) {
                    Assert.fail("Duration created with XMLSchemaType of\"" + xmlSchemaType + "\" was expected to be \""
                            + DatatypeConstants.DURATION_YEARMONTH + "\" and has the value \"" + duration.toString() + "\"");
                }
            } catch (IllegalStateException illegalStateException) {
                // TODO; this test really should pass
                System.err.println("Please fix this bug that is being ignored, for now: " + illegalStateException.getMessage());
            }

            // does it have the right value?
            if (!TEST_VALUES_LEXICAL[onTestValue + 1].equals(duration.toString())) {
                Assert.fail("Duration created with \"" + TEST_VALUES_LEXICAL[onTestValue] + "\" was expected to be \""
                        + TEST_VALUES_LEXICAL[onTestValue + 1] + "\" and has the value \"" + duration.toString() + "\"");
            }

            // Duration created with correct value
        } catch (Exception exception) {

            if (DEBUG) {
                System.err.println("Exception in creating duration: \"" + exception.toString() + "\"");
            }

            // was this expected to succed?
            if (!TEST_VALUES_LEXICAL[onTestValue + 1].equals(TEST_VALUE_FAIL)) {
                Assert.fail("the value \"" + TEST_VALUES_LEXICAL[onTestValue] + "\" is valid yet it failed with \"" + exception.toString() + "\"");
            }
            // expected failure
        }
    }
}
 
Example 10
Source File: DatatypeFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test {@link DatatypeFactory.newDurationYearMonth(long milliseconds)}.
 *
 */
@Test
public final void testNewDurationYearMonthMilliseconds() {

    /**
     * Millisecond test values to test.
     */
    final long[] TEST_VALUES_MILLISECONDS = { 0L, 1L, -1L, 2678400000L, // 31
                                                                        // days,
                                                                        // e.g.
                                                                        // 1
                                                                        // month
            -2678400000L, 5270400000L, // 61 days, e.g. 2 months
            -5270400000L, 31622400000L, // 366 days, e.g. 1 year
            -31622400000L, 34300800000L, // 397 days, e.g. 1 year, 1 month
            -34300800000L };

    /**
     * Millisecond test value results of test.
     */
    final String[] TEST_VALUES_MILLISECONDS_RESULTS = { "P0Y0M", "P0Y0M", "P0Y0M", "P0Y1M", "-P0Y1M", "P0Y2M", "-P0Y2M", "P1Y0M", "-P1Y0M", "P1Y1M",
            "-P1Y1M" };

    DatatypeFactory datatypeFactory = null;
    try {
        datatypeFactory = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException datatypeConfigurationException) {
        Assert.fail(datatypeConfigurationException.toString());
    }

    if (DEBUG) {
        System.err.println("DatatypeFactory created: " + datatypeFactory.toString());
    }

    // test each value
    for (int onTestValue = 0; onTestValue < TEST_VALUES_MILLISECONDS.length; onTestValue++) {

        if (DEBUG) {
            System.err.println("testing value: \"" + TEST_VALUES_MILLISECONDS[onTestValue] + "\", expecting: \""
                    + TEST_VALUES_MILLISECONDS_RESULTS[onTestValue] + "\"");
        }

        try {
            Duration duration = datatypeFactory.newDurationYearMonth(TEST_VALUES_MILLISECONDS[onTestValue]);

            if (DEBUG) {
                System.err.println("Duration created: \"" + duration.toString() + "\"");
            }

            // was this expected to fail?
            if (TEST_VALUES_MILLISECONDS_RESULTS[onTestValue].equals(TEST_VALUE_FAIL)) {
                Assert.fail("the value \"" + TEST_VALUES_MILLISECONDS[onTestValue] + "\" is invalid yet it created the Duration \"" + duration.toString()
                        + "\"");
            }

            // right XMLSchemaType?
            QName xmlSchemaType = duration.getXMLSchemaType();
            if (!xmlSchemaType.equals(DatatypeConstants.DURATION_YEARMONTH)) {
                Assert.fail("Duration created with XMLSchemaType of\"" + xmlSchemaType + "\" was expected to be \"" + DatatypeConstants.DURATION_YEARMONTH
                        + "\" and has the value \"" + duration.toString() + "\"");
            }

            // does it have the right value?
            if (!TEST_VALUES_MILLISECONDS_RESULTS[onTestValue].equals(duration.toString())) {
                Assert.fail("Duration created with \"" + TEST_VALUES_MILLISECONDS[onTestValue] + "\" was expected to be \""
                        + TEST_VALUES_MILLISECONDS_RESULTS[onTestValue] + "\" and has the value \"" + duration.toString() + "\"");
            }

            // only YEAR & MONTH should have values
            int days = duration.getDays();
            int hours = duration.getHours();
            int minutes = duration.getMinutes();
            if (days != 0 || hours != 0 || minutes != 0) {
                Assert.fail("xdt:yearMonthDuration created without discarding remaining milliseconds: " + " days = " + days + ", hours = " + hours
                        + ", minutess = " + minutes);
            }

            // Duration created with correct values
        } catch (Exception exception) {

            if (DEBUG) {
                System.err.println("Exception in creating duration: \"" + exception.toString() + "\"");
            }

            // was this expected to succed?
            if (!TEST_VALUES_MILLISECONDS_RESULTS[onTestValue].equals(TEST_VALUE_FAIL)) {
                Assert.fail("the value \"" + TEST_VALUES_MILLISECONDS[onTestValue] + "\" is valid yet it failed with \"" + exception.toString() + "\"");
            }
            // expected failure
        }
    }
}
 
Example 11
Source File: Bug6937964Test.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public final void testNewDurationYearMonthLexicalRepresentation1() {

    /**
     * Lexical test values to test.
     */
    final String[] TEST_VALUES_LEXICAL = { "P13M", "P1Y1M", "-P13M", "-P1Y1M", "P1Y", "P1Y", "-P1Y", "-P1Y", "P1Y25M", "P3Y1M", "-P1Y25M", "-P3Y1M" };

    DatatypeFactory datatypeFactory = null;
    try {
        datatypeFactory = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException datatypeConfigurationException) {
        Assert.fail(datatypeConfigurationException.toString());
    }

    if (DEBUG) {
        System.err.println("DatatypeFactory created: " + datatypeFactory.toString());
    }

    // test each value
    for (int onTestValue = 0; onTestValue < TEST_VALUES_LEXICAL.length; onTestValue = onTestValue + 2) {

        if (DEBUG) {
            System.err.println("testing value: \"" + TEST_VALUES_LEXICAL[onTestValue] + "\", expecting: \"" + TEST_VALUES_LEXICAL[onTestValue + 1] + "\"");
        }

        try {
            Duration duration = datatypeFactory.newDurationYearMonth(TEST_VALUES_LEXICAL[onTestValue]);

            if (DEBUG) {
                System.err.println("Duration created: \"" + duration.toString() + "\"");
            }

            // was this expected to fail?
            if (TEST_VALUES_LEXICAL[onTestValue + 1].equals(TEST_VALUE_FAIL)) {
                Assert.fail("the value \"" + TEST_VALUES_LEXICAL[onTestValue] + "\" is invalid yet it created the Duration \"" + duration.toString() + "\"");
            }

            // right XMLSchemaType?
            // TODO: enable test, it should pass, it fails with Exception(s)
            // for now due to a bug
            try {
                QName xmlSchemaType = duration.getXMLSchemaType();
                if (!xmlSchemaType.equals(DatatypeConstants.DURATION_YEARMONTH)) {
                    Assert.fail("Duration created with XMLSchemaType of\"" + xmlSchemaType + "\" was expected to be \""
                            + DatatypeConstants.DURATION_YEARMONTH + "\" and has the value \"" + duration.toString() + "\"");
                }
            } catch (IllegalStateException illegalStateException) {
                // TODO; this test really should pass
                System.err.println("Please fix this bug that is being ignored, for now: " + illegalStateException.getMessage());
            }

            // does it have the right value?
            if (!TEST_VALUES_LEXICAL[onTestValue + 1].equals(duration.toString())) {
                Assert.fail("Duration created with \"" + TEST_VALUES_LEXICAL[onTestValue] + "\" was expected to be \""
                        + TEST_VALUES_LEXICAL[onTestValue + 1] + "\" and has the value \"" + duration.toString() + "\"");
            }

            // Duration created with correct value
        } catch (Exception exception) {

            if (DEBUG) {
                System.err.println("Exception in creating duration: \"" + exception.toString() + "\"");
            }

            // was this expected to succed?
            if (!TEST_VALUES_LEXICAL[onTestValue + 1].equals(TEST_VALUE_FAIL)) {
                Assert.fail("the value \"" + TEST_VALUES_LEXICAL[onTestValue] + "\" is valid yet it failed with \"" + exception.toString() + "\"");
            }
            // expected failure
        }
    }
}