Java Code Examples for org.apache.commons.lang3.SystemUtils#isJavaVersionAtLeast()

The following examples show how to use org.apache.commons.lang3.SystemUtils#isJavaVersionAtLeast() . 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: VersionUtils.java    From rug-cli with GNU General Public License v3.0 6 votes vote down vote up
public static void validateJdkVersion() {
    boolean warn = false;
    if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8)) {
        // We need 1.8.0_111 at least
        String version = SystemUtils.JAVA_VERSION;
        int ix = version.lastIndexOf('_');
        if (ix > 0) {
            int patch = Integer.valueOf(version.substring(ix + 1));
            if (patch < 111) {
                warn = true;
            }
        }
    }
    else {
        // Not even Java 1.8
        warn = true;
    }

    if (warn) {
        new Log(VersionUtils.class).info(Style.yellow(
                "Please update your Java™ Runtime Environment (JRE) from %s to version 1.8.0_111 or newer.",
                SystemUtils.JAVA_VERSION));
    }
}
 
Example 2
Source File: ScriptRunnerFactory.java    From citeproc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link ScriptRunner} instance according to the
 * type set with {@link #setRunnerType(RunnerType)}
 * @return the script runner
 */
public static ScriptRunner createRunner() {
    RunnerType t = runner.get();

    switch (t) {
        case AUTO:
            if (supportsV8()) {
                return createV8Runner();
            }
            if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_11)) {
                return createGraalRunner();
            }
            // fall back to JRE
            return createJreRunner();

        case JRE:
            return createJreRunner();

        case V8:
            return createV8Runner();

        case GRAALJS:
            return createGraalRunner();

        default:
            throw new RuntimeException("Invalid runner type");
    }
}
 
Example 3
Source File: ExceptionUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testIsNestedThrowable_Throwable() {
    assertEquals(true, ExceptionUtils.isNestedThrowable(new SQLException()));
    assertEquals(true, ExceptionUtils.isNestedThrowable(new InvocationTargetException(new Exception())));
    assertEquals(true, ExceptionUtils.isNestedThrowable(new NestableRuntimeException()));
    assertEquals(true, ExceptionUtils.isNestedThrowable(withCause));
    assertEquals(true, ExceptionUtils.isNestedThrowable(nested));
    if (SystemUtils.isJavaVersionAtLeast(140)) {
        assertEquals(true, ExceptionUtils.isNestedThrowable(withoutCause));
        assertEquals(true, ExceptionUtils.isNestedThrowable(new Throwable()));
    } else {
        assertEquals(false, ExceptionUtils.isNestedThrowable(withoutCause));
        assertEquals(false, ExceptionUtils.isNestedThrowable(new Throwable()));
    }
}
 
Example 4
Source File: NumberUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testCreateNumber() {
    // a lot of things can go wrong
    assertEquals("createNumber(String) 1 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5"));
    assertEquals("createNumber(String) 2 failed", new Integer("12345"), NumberUtils.createNumber("12345"));
    assertEquals("createNumber(String) 3 failed", new Double("1234.5"), NumberUtils.createNumber("1234.5D"));
    assertEquals("createNumber(String) 3 failed", new Double("1234.5"), NumberUtils.createNumber("1234.5d"));
    assertEquals("createNumber(String) 4 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5F"));
    assertEquals("createNumber(String) 4 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5f"));
    assertEquals("createNumber(String) 5 failed", new Long(Integer.MAX_VALUE + 1L), NumberUtils.createNumber(""
        + (Integer.MAX_VALUE + 1L)));
    assertEquals("createNumber(String) 6 failed", new Long(12345), NumberUtils.createNumber("12345L"));
    assertEquals("createNumber(String) 6 failed", new Long(12345), NumberUtils.createNumber("12345l"));
    assertEquals("createNumber(String) 7 failed", new Float("-1234.5"), NumberUtils.createNumber("-1234.5"));
    assertEquals("createNumber(String) 8 failed", new Integer("-12345"), NumberUtils.createNumber("-12345"));
    assertTrue("createNumber(String) 9 failed", 0xFADE == NumberUtils.createNumber("0xFADE").intValue());
    assertTrue("createNumber(String) 10 failed", -0xFADE == NumberUtils.createNumber("-0xFADE").intValue());
    assertEquals("createNumber(String) 11 failed", new Double("1.1E200"), NumberUtils.createNumber("1.1E200"));
    assertEquals("createNumber(String) 12 failed", new Float("1.1E20"), NumberUtils.createNumber("1.1E20"));
    assertEquals("createNumber(String) 13 failed", new Double("-1.1E200"), NumberUtils.createNumber("-1.1E200"));
    assertEquals("createNumber(String) 14 failed", new Double("1.1E-200"), NumberUtils.createNumber("1.1E-200"));
    assertEquals("createNumber(null) failed", null, NumberUtils.createNumber(null));
    assertEquals("createNumber(String) failed", new BigInteger("12345678901234567890"), NumberUtils
            .createNumber("12345678901234567890L"));

    // jdk 1.2 doesn't support this. unsure about jdk 1.2.2
    if (SystemUtils.isJavaVersionAtLeast(1.3f)) {
        assertEquals("createNumber(String) 15 failed", new BigDecimal("1.1E-700"), NumberUtils
                .createNumber("1.1E-700F"));
    }
    assertEquals("createNumber(String) 16 failed", new Long("10" + Integer.MAX_VALUE), NumberUtils
            .createNumber("10" + Integer.MAX_VALUE + "L"));
    assertEquals("createNumber(String) 17 failed", new Long("10" + Integer.MAX_VALUE), NumberUtils
            .createNumber("10" + Integer.MAX_VALUE));
    assertEquals("createNumber(String) 18 failed", new BigInteger("10" + Long.MAX_VALUE), NumberUtils
            .createNumber("10" + Long.MAX_VALUE));
}
 
Example 5
Source File: ExceptionUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testIsThrowableNested() {
    if (SystemUtils.isJavaVersionAtLeast(140)) {
        assertEquals(true, ExceptionUtils.isThrowableNested());
    } else {
        assertEquals(false, ExceptionUtils.isThrowableNested());
    }
}
 
Example 6
Source File: ExceptionUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testSetCause() {
    Exception cause = new ExceptionWithoutCause();
    assertEquals(true, ExceptionUtils.setCause(new ExceptionWithCause(null), cause));
    if (SystemUtils.isJavaVersionAtLeast(140)) {
        assertEquals(true, ExceptionUtils.setCause(new ExceptionWithoutCause(), cause));
    }
}
 
Example 7
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void assertPatternsEqual(String message, String expected, String actual) {
    if (SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) {
        assertEquals(message, expected, actual);
    }
}
 
Example 8
Source File: DateUtilsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests for LANG-59
 *
 * see http://issues.apache.org/jira/browse/LANG-59
 */
@Test
public void testTruncateLang59() throws Exception {
    if (!SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) {
        this.warn("WARNING: Test for LANG-59 not run since the current version is " + SystemUtils.JAVA_SPECIFICATION_VERSION);
        return;
    }

    // Set TimeZone to Mountain Time
    TimeZone MST_MDT = TimeZone.getTimeZone("MST7MDT");
    TimeZone.setDefault(MST_MDT);
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
    format.setTimeZone(MST_MDT);

    Date oct31_01MDT = new Date(1099206000000L); 

    Date oct31MDT             = new Date(oct31_01MDT.getTime()       - 3600000L); // - 1 hour
    Date oct31_01_02MDT       = new Date(oct31_01MDT.getTime()       + 120000L);  // + 2 minutes
    Date oct31_01_02_03MDT    = new Date(oct31_01_02MDT.getTime()    + 3000L);    // + 3 seconds
    Date oct31_01_02_03_04MDT = new Date(oct31_01_02_03MDT.getTime() + 4L);       // + 4 milliseconds

    assertEquals("Check 00:00:00.000", "2004-10-31 00:00:00.000 MDT", format.format(oct31MDT));
    assertEquals("Check 01:00:00.000", "2004-10-31 01:00:00.000 MDT", format.format(oct31_01MDT));
    assertEquals("Check 01:02:00.000", "2004-10-31 01:02:00.000 MDT", format.format(oct31_01_02MDT));
    assertEquals("Check 01:02:03.000", "2004-10-31 01:02:03.000 MDT", format.format(oct31_01_02_03MDT));
    assertEquals("Check 01:02:03.004", "2004-10-31 01:02:03.004 MDT", format.format(oct31_01_02_03_04MDT));

    // ------- Demonstrate Problem -------
    Calendar gval = Calendar.getInstance();
    gval.setTime(new Date(oct31_01MDT.getTime()));
    gval.set(Calendar.MINUTE, gval.get(Calendar.MINUTE)); // set minutes to the same value
    assertEquals("Demonstrate Problem", gval.getTime().getTime(), oct31_01MDT.getTime() + 3600000L);

    // ---------- Test Truncate ----------
    assertEquals("Truncate Calendar.MILLISECOND",
            oct31_01_02_03_04MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MILLISECOND));

    assertEquals("Truncate Calendar.SECOND",
               oct31_01_02_03MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.SECOND));

    assertEquals("Truncate Calendar.MINUTE",
                  oct31_01_02MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MINUTE));

    assertEquals("Truncate Calendar.HOUR_OF_DAY",
                     oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));

    assertEquals("Truncate Calendar.HOUR",
                     oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR));

    assertEquals("Truncate Calendar.DATE",
                        oct31MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.DATE));


    // ---------- Test Round (down) ----------
    assertEquals("Round Calendar.MILLISECOND",
            oct31_01_02_03_04MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MILLISECOND));

    assertEquals("Round Calendar.SECOND",
               oct31_01_02_03MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.SECOND));

    assertEquals("Round Calendar.MINUTE",
                  oct31_01_02MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MINUTE));

    assertEquals("Round Calendar.HOUR_OF_DAY",
                     oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));

    assertEquals("Round Calendar.HOUR",
                     oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR));

    assertEquals("Round Calendar.DATE",
                        oct31MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.DATE));

    // restore default time zone
    TimeZone.setDefault(defaultZone);
}
 
Example 9
Source File: NumberUtilsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCreateNumber() {
    // a lot of things can go wrong
    assertEquals("createNumber(String) 1 failed", Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5"));
    assertEquals("createNumber(String) 2 failed", Integer.valueOf("12345"), NumberUtils.createNumber("12345"));
    assertEquals("createNumber(String) 3 failed", Double.valueOf("1234.5"), NumberUtils.createNumber("1234.5D"));
    assertEquals("createNumber(String) 3 failed", Double.valueOf("1234.5"), NumberUtils.createNumber("1234.5d"));
    assertEquals("createNumber(String) 4 failed", Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5F"));
    assertEquals("createNumber(String) 4 failed", Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5f"));
    assertEquals("createNumber(String) 5 failed", Long.valueOf(Integer.MAX_VALUE + 1L), NumberUtils.createNumber(""
        + (Integer.MAX_VALUE + 1L)));
    assertEquals("createNumber(String) 6 failed", Long.valueOf(12345), NumberUtils.createNumber("12345L"));
    assertEquals("createNumber(String) 6 failed", Long.valueOf(12345), NumberUtils.createNumber("12345l"));
    assertEquals("createNumber(String) 7 failed", Float.valueOf("-1234.5"), NumberUtils.createNumber("-1234.5"));
    assertEquals("createNumber(String) 8 failed", Integer.valueOf("-12345"), NumberUtils.createNumber("-12345"));
    assertTrue("createNumber(String) 9a failed", 0xFADE == NumberUtils.createNumber("0xFADE").intValue());
    assertTrue("createNumber(String) 9b failed", 0xFADE == NumberUtils.createNumber("0Xfade").intValue());
    assertTrue("createNumber(String) 10a failed", -0xFADE == NumberUtils.createNumber("-0xFADE").intValue());
    assertTrue("createNumber(String) 10b failed", -0xFADE == NumberUtils.createNumber("-0Xfade").intValue());
    assertEquals("createNumber(String) 11 failed", Double.valueOf("1.1E200"), NumberUtils.createNumber("1.1E200"));
    assertEquals("createNumber(String) 12 failed", Float.valueOf("1.1E20"), NumberUtils.createNumber("1.1E20"));
    assertEquals("createNumber(String) 13 failed", Double.valueOf("-1.1E200"), NumberUtils.createNumber("-1.1E200"));
    assertEquals("createNumber(String) 14 failed", Double.valueOf("1.1E-200"), NumberUtils.createNumber("1.1E-200"));
    assertEquals("createNumber(null) failed", null, NumberUtils.createNumber(null));
    assertEquals("createNumber(String) failed", new BigInteger("12345678901234567890"), NumberUtils
            .createNumber("12345678901234567890L"));

    // jdk 1.2 doesn't support this. unsure about jdk 1.2.2
    if (SystemUtils.isJavaVersionAtLeast(JAVA_1_3)) {
        assertEquals("createNumber(String) 15 failed", new BigDecimal("1.1E-700"), NumberUtils
                .createNumber("1.1E-700F"));
    }
    assertEquals("createNumber(String) 16 failed", Long.valueOf("10" + Integer.MAX_VALUE), NumberUtils
            .createNumber("10" + Integer.MAX_VALUE + "L"));
    assertEquals("createNumber(String) 17 failed", Long.valueOf("10" + Integer.MAX_VALUE), NumberUtils
            .createNumber("10" + Integer.MAX_VALUE));
    assertEquals("createNumber(String) 18 failed", new BigInteger("10" + Long.MAX_VALUE), NumberUtils
            .createNumber("10" + Long.MAX_VALUE));

    // LANG-521
    assertEquals("createNumber(String) LANG-521 failed", Float.valueOf("2."), NumberUtils.createNumber("2."));

    // LANG-638
    assertFalse("createNumber(String) succeeded", checkCreateNumber("1eE"));

    // LANG-693
    assertEquals("createNumber(String) LANG-693 failed", Double.valueOf(Double.MAX_VALUE), NumberUtils
                .createNumber("" + Double.MAX_VALUE));
}
 
Example 10
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void assertPatternsEqual(final String message, final String expected, final String actual) {
    if (SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) {
        assertEquals(message, expected, actual);
    }
}
 
Example 11
Source File: DateUtilsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests for LANG-59
 *
 * see http://issues.apache.org/jira/browse/LANG-59
 */
@Test
public void testTruncateLang59() throws Exception {
    if (!SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) {
        this.warn("WARNING: Test for LANG-59 not run since the current version is " + SystemUtils.JAVA_SPECIFICATION_VERSION);
        return;
    }

    // Set TimeZone to Mountain Time
    final TimeZone MST_MDT = TimeZone.getTimeZone("MST7MDT");
    TimeZone.setDefault(MST_MDT);
    final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
    format.setTimeZone(MST_MDT);

    final Date oct31_01MDT = new Date(1099206000000L); 

    final Date oct31MDT             = new Date(oct31_01MDT.getTime()       - 3600000L); // - 1 hour
    final Date oct31_01_02MDT       = new Date(oct31_01MDT.getTime()       + 120000L);  // + 2 minutes
    final Date oct31_01_02_03MDT    = new Date(oct31_01_02MDT.getTime()    + 3000L);    // + 3 seconds
    final Date oct31_01_02_03_04MDT = new Date(oct31_01_02_03MDT.getTime() + 4L);       // + 4 milliseconds

    assertEquals("Check 00:00:00.000", "2004-10-31 00:00:00.000 MDT", format.format(oct31MDT));
    assertEquals("Check 01:00:00.000", "2004-10-31 01:00:00.000 MDT", format.format(oct31_01MDT));
    assertEquals("Check 01:02:00.000", "2004-10-31 01:02:00.000 MDT", format.format(oct31_01_02MDT));
    assertEquals("Check 01:02:03.000", "2004-10-31 01:02:03.000 MDT", format.format(oct31_01_02_03MDT));
    assertEquals("Check 01:02:03.004", "2004-10-31 01:02:03.004 MDT", format.format(oct31_01_02_03_04MDT));

    // ------- Demonstrate Problem -------
    final Calendar gval = Calendar.getInstance();
    gval.setTime(new Date(oct31_01MDT.getTime()));
    gval.set(Calendar.MINUTE, gval.get(Calendar.MINUTE)); // set minutes to the same value
    assertEquals("Demonstrate Problem", gval.getTime().getTime(), oct31_01MDT.getTime() + 3600000L);

    // ---------- Test Truncate ----------
    assertEquals("Truncate Calendar.MILLISECOND",
            oct31_01_02_03_04MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MILLISECOND));

    assertEquals("Truncate Calendar.SECOND",
               oct31_01_02_03MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.SECOND));

    assertEquals("Truncate Calendar.MINUTE",
                  oct31_01_02MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MINUTE));

    assertEquals("Truncate Calendar.HOUR_OF_DAY",
                     oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));

    assertEquals("Truncate Calendar.HOUR",
                     oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR));

    assertEquals("Truncate Calendar.DATE",
                        oct31MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.DATE));


    // ---------- Test Round (down) ----------
    assertEquals("Round Calendar.MILLISECOND",
            oct31_01_02_03_04MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MILLISECOND));

    assertEquals("Round Calendar.SECOND",
               oct31_01_02_03MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.SECOND));

    assertEquals("Round Calendar.MINUTE",
                  oct31_01_02MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MINUTE));

    assertEquals("Round Calendar.HOUR_OF_DAY",
                     oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));

    assertEquals("Round Calendar.HOUR",
                     oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR));

    assertEquals("Round Calendar.DATE",
                        oct31MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.DATE));

    // restore default time zone
    TimeZone.setDefault(defaultZone);
}
 
Example 12
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void assertPatternsEqual(String message, String expected, String actual) {
    if (SystemUtils.isJavaVersionAtLeast(1.4f)) {
        assertEquals(message, expected, actual);
    }
}
 
Example 13
Source File: DateUtilsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests for LANG-59
 *
 * see http://issues.apache.org/jira/browse/LANG-59
 */
public void testTruncateLang59() throws Exception {
    if (!SystemUtils.isJavaVersionAtLeast(1.4f)) {
        this.warn("WARNING: Test for LANG-59 not run since the current version is " + SystemUtils.JAVA_VERSION);
        return;
    }

    // Set TimeZone to Mountain Time
    TimeZone MST_MDT = TimeZone.getTimeZone("MST7MDT");
    TimeZone.setDefault(MST_MDT);
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
    format.setTimeZone(MST_MDT);

    Date oct31_01MDT = new Date(1099206000000L); 

    Date oct31MDT             = new Date(oct31_01MDT.getTime()       - 3600000L); // - 1 hour
    Date oct31_01_02MDT       = new Date(oct31_01MDT.getTime()       + 120000L);  // + 2 minutes
    Date oct31_01_02_03MDT    = new Date(oct31_01_02MDT.getTime()    + 3000L);    // + 3 seconds
    Date oct31_01_02_03_04MDT = new Date(oct31_01_02_03MDT.getTime() + 4L);       // + 4 milliseconds

    assertEquals("Check 00:00:00.000", "2004-10-31 00:00:00.000 MDT", format.format(oct31MDT));
    assertEquals("Check 01:00:00.000", "2004-10-31 01:00:00.000 MDT", format.format(oct31_01MDT));
    assertEquals("Check 01:02:00.000", "2004-10-31 01:02:00.000 MDT", format.format(oct31_01_02MDT));
    assertEquals("Check 01:02:03.000", "2004-10-31 01:02:03.000 MDT", format.format(oct31_01_02_03MDT));
    assertEquals("Check 01:02:03.004", "2004-10-31 01:02:03.004 MDT", format.format(oct31_01_02_03_04MDT));

    // ------- Demonstrate Problem -------
    Calendar gval = Calendar.getInstance();
    gval.setTime(new Date(oct31_01MDT.getTime()));
    gval.set(Calendar.MINUTE, gval.get(Calendar.MINUTE)); // set minutes to the same value
    assertEquals("Demonstrate Problem", gval.getTime().getTime(), oct31_01MDT.getTime() + 3600000L);

    // ---------- Test Truncate ----------
    assertEquals("Truncate Calendar.MILLISECOND",
            oct31_01_02_03_04MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MILLISECOND));

    assertEquals("Truncate Calendar.SECOND",
               oct31_01_02_03MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.SECOND));

    assertEquals("Truncate Calendar.MINUTE",
                  oct31_01_02MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MINUTE));

    assertEquals("Truncate Calendar.HOUR_OF_DAY",
                     oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));

    assertEquals("Truncate Calendar.HOUR",
                     oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR));

    assertEquals("Truncate Calendar.DATE",
                        oct31MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.DATE));


    // ---------- Test Round (down) ----------
    assertEquals("Round Calendar.MILLISECOND",
            oct31_01_02_03_04MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MILLISECOND));

    assertEquals("Round Calendar.SECOND",
               oct31_01_02_03MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.SECOND));

    assertEquals("Round Calendar.MINUTE",
                  oct31_01_02MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MINUTE));

    assertEquals("Round Calendar.HOUR_OF_DAY",
                     oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));

    assertEquals("Round Calendar.HOUR",
                     oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR));

    assertEquals("Round Calendar.DATE",
                        oct31MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.DATE));

    // restore default time zone
    TimeZone.setDefault(defaultZone);
}
 
Example 14
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void assertPatternsEqual(String message, String expected, String actual) {
    if (SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) {
        assertEquals(message, expected, actual);
    }
}
 
Example 15
Source File: DateUtilsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests for LANG-59
 *
 * see http://issues.apache.org/jira/browse/LANG-59
 */
@Test
public void testTruncateLang59() throws Exception {
    if (!SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) {
        this.warn("WARNING: Test for LANG-59 not run since the current version is " + SystemUtils.JAVA_SPECIFICATION_VERSION);
        return;
    }

    // Set TimeZone to Mountain Time
    TimeZone MST_MDT = TimeZone.getTimeZone("MST7MDT");
    TimeZone.setDefault(MST_MDT);
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
    format.setTimeZone(MST_MDT);

    Date oct31_01MDT = new Date(1099206000000L); 

    Date oct31MDT             = new Date(oct31_01MDT.getTime()       - 3600000L); // - 1 hour
    Date oct31_01_02MDT       = new Date(oct31_01MDT.getTime()       + 120000L);  // + 2 minutes
    Date oct31_01_02_03MDT    = new Date(oct31_01_02MDT.getTime()    + 3000L);    // + 3 seconds
    Date oct31_01_02_03_04MDT = new Date(oct31_01_02_03MDT.getTime() + 4L);       // + 4 milliseconds

    assertEquals("Check 00:00:00.000", "2004-10-31 00:00:00.000 MDT", format.format(oct31MDT));
    assertEquals("Check 01:00:00.000", "2004-10-31 01:00:00.000 MDT", format.format(oct31_01MDT));
    assertEquals("Check 01:02:00.000", "2004-10-31 01:02:00.000 MDT", format.format(oct31_01_02MDT));
    assertEquals("Check 01:02:03.000", "2004-10-31 01:02:03.000 MDT", format.format(oct31_01_02_03MDT));
    assertEquals("Check 01:02:03.004", "2004-10-31 01:02:03.004 MDT", format.format(oct31_01_02_03_04MDT));

    // ------- Demonstrate Problem -------
    Calendar gval = Calendar.getInstance();
    gval.setTime(new Date(oct31_01MDT.getTime()));
    gval.set(Calendar.MINUTE, gval.get(Calendar.MINUTE)); // set minutes to the same value
    assertEquals("Demonstrate Problem", gval.getTime().getTime(), oct31_01MDT.getTime() + 3600000L);

    // ---------- Test Truncate ----------
    assertEquals("Truncate Calendar.MILLISECOND",
            oct31_01_02_03_04MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MILLISECOND));

    assertEquals("Truncate Calendar.SECOND",
               oct31_01_02_03MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.SECOND));

    assertEquals("Truncate Calendar.MINUTE",
                  oct31_01_02MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MINUTE));

    assertEquals("Truncate Calendar.HOUR_OF_DAY",
                     oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));

    assertEquals("Truncate Calendar.HOUR",
                     oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR));

    assertEquals("Truncate Calendar.DATE",
                        oct31MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.DATE));


    // ---------- Test Round (down) ----------
    assertEquals("Round Calendar.MILLISECOND",
            oct31_01_02_03_04MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MILLISECOND));

    assertEquals("Round Calendar.SECOND",
               oct31_01_02_03MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.SECOND));

    assertEquals("Round Calendar.MINUTE",
                  oct31_01_02MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MINUTE));

    assertEquals("Round Calendar.HOUR_OF_DAY",
                     oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));

    assertEquals("Round Calendar.HOUR",
                     oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR));

    assertEquals("Round Calendar.DATE",
                        oct31MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.DATE));

    // restore default time zone
    TimeZone.setDefault(defaultZone);
}
 
Example 16
Source File: NumberUtilsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCreateNumber() {
    // a lot of things can go wrong
    assertEquals("createNumber(String) 1 failed", Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5"));
    assertEquals("createNumber(String) 2 failed", Integer.valueOf("12345"), NumberUtils.createNumber("12345"));
    assertEquals("createNumber(String) 3 failed", Double.valueOf("1234.5"), NumberUtils.createNumber("1234.5D"));
    assertEquals("createNumber(String) 3 failed", Double.valueOf("1234.5"), NumberUtils.createNumber("1234.5d"));
    assertEquals("createNumber(String) 4 failed", Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5F"));
    assertEquals("createNumber(String) 4 failed", Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5f"));
    assertEquals("createNumber(String) 5 failed", Long.valueOf(Integer.MAX_VALUE + 1L), NumberUtils.createNumber(""
        + (Integer.MAX_VALUE + 1L)));
    assertEquals("createNumber(String) 6 failed", Long.valueOf(12345), NumberUtils.createNumber("12345L"));
    assertEquals("createNumber(String) 6 failed", Long.valueOf(12345), NumberUtils.createNumber("12345l"));
    assertEquals("createNumber(String) 7 failed", Float.valueOf("-1234.5"), NumberUtils.createNumber("-1234.5"));
    assertEquals("createNumber(String) 8 failed", Integer.valueOf("-12345"), NumberUtils.createNumber("-12345"));
    assertTrue("createNumber(String) 9a failed", 0xFADE == NumberUtils.createNumber("0xFADE").intValue());
    assertTrue("createNumber(String) 9b failed", 0xFADE == NumberUtils.createNumber("0Xfade").intValue());
    assertTrue("createNumber(String) 10a failed", -0xFADE == NumberUtils.createNumber("-0xFADE").intValue());
    assertTrue("createNumber(String) 10b failed", -0xFADE == NumberUtils.createNumber("-0Xfade").intValue());
    assertEquals("createNumber(String) 11 failed", Double.valueOf("1.1E200"), NumberUtils.createNumber("1.1E200"));
    assertEquals("createNumber(String) 12 failed", Float.valueOf("1.1E20"), NumberUtils.createNumber("1.1E20"));
    assertEquals("createNumber(String) 13 failed", Double.valueOf("-1.1E200"), NumberUtils.createNumber("-1.1E200"));
    assertEquals("createNumber(String) 14 failed", Double.valueOf("1.1E-200"), NumberUtils.createNumber("1.1E-200"));
    assertEquals("createNumber(null) failed", null, NumberUtils.createNumber(null));
    assertEquals("createNumber(String) failed", new BigInteger("12345678901234567890"), NumberUtils
            .createNumber("12345678901234567890L"));

    // jdk 1.2 doesn't support this. unsure about jdk 1.2.2
    if (SystemUtils.isJavaVersionAtLeast(JAVA_1_3)) {
        assertEquals("createNumber(String) 15 failed", new BigDecimal("1.1E-700"), NumberUtils
                .createNumber("1.1E-700F"));
    }
    assertEquals("createNumber(String) 16 failed", Long.valueOf("10" + Integer.MAX_VALUE), NumberUtils
            .createNumber("10" + Integer.MAX_VALUE + "L"));
    assertEquals("createNumber(String) 17 failed", Long.valueOf("10" + Integer.MAX_VALUE), NumberUtils
            .createNumber("10" + Integer.MAX_VALUE));
    assertEquals("createNumber(String) 18 failed", new BigInteger("10" + Long.MAX_VALUE), NumberUtils
            .createNumber("10" + Long.MAX_VALUE));

    // LANG-521
    assertEquals("createNumber(String) LANG-521 failed", Float.valueOf("2."), NumberUtils.createNumber("2."));

    // LANG-638
    assertFalse("createNumber(String) succeeded", checkCreateNumber("1eE"));

    // LANG-693
    assertEquals("createNumber(String) LANG-693 failed", Double.valueOf(Double.MAX_VALUE), NumberUtils
                .createNumber("" + Double.MAX_VALUE));
    
    // LANG-822
    // ensure that the underlying negative number would create a BigDecimal
    final Number bigNum = NumberUtils.createNumber("-1.1E-700F");
    assertEquals(BigDecimal.class,bigNum.getClass());
    assertNotNull(bigNum);

    // Check that the code fails to create a valid number when preceeded by -- rather than -
    try {
        NumberUtils.createNumber("--1.1E-700F");
        fail("Expected NumberFormatException");
    } catch (NumberFormatException nfe) {
        // expected
    }
}
 
Example 17
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private void assertPatternsEqual(String message, String expected, String actual) {
    if (SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) {
        assertEquals(message, expected, actual);
    }
}
 
Example 18
Source File: DateUtilsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests for LANG-59
 *
 * see http://issues.apache.org/jira/browse/LANG-59
 */
public void testTruncateLang59() throws Exception {
    if (!SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) {
        this.warn("WARNING: Test for LANG-59 not run since the current version is " + SystemUtils.JAVA_SPECIFICATION_VERSION);
        return;
    }

    // Set TimeZone to Mountain Time
    TimeZone MST_MDT = TimeZone.getTimeZone("MST7MDT");
    TimeZone.setDefault(MST_MDT);
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
    format.setTimeZone(MST_MDT);

    Date oct31_01MDT = new Date(1099206000000L); 

    Date oct31MDT             = new Date(oct31_01MDT.getTime()       - 3600000L); // - 1 hour
    Date oct31_01_02MDT       = new Date(oct31_01MDT.getTime()       + 120000L);  // + 2 minutes
    Date oct31_01_02_03MDT    = new Date(oct31_01_02MDT.getTime()    + 3000L);    // + 3 seconds
    Date oct31_01_02_03_04MDT = new Date(oct31_01_02_03MDT.getTime() + 4L);       // + 4 milliseconds

    assertEquals("Check 00:00:00.000", "2004-10-31 00:00:00.000 MDT", format.format(oct31MDT));
    assertEquals("Check 01:00:00.000", "2004-10-31 01:00:00.000 MDT", format.format(oct31_01MDT));
    assertEquals("Check 01:02:00.000", "2004-10-31 01:02:00.000 MDT", format.format(oct31_01_02MDT));
    assertEquals("Check 01:02:03.000", "2004-10-31 01:02:03.000 MDT", format.format(oct31_01_02_03MDT));
    assertEquals("Check 01:02:03.004", "2004-10-31 01:02:03.004 MDT", format.format(oct31_01_02_03_04MDT));

    // ------- Demonstrate Problem -------
    Calendar gval = Calendar.getInstance();
    gval.setTime(new Date(oct31_01MDT.getTime()));
    gval.set(Calendar.MINUTE, gval.get(Calendar.MINUTE)); // set minutes to the same value
    assertEquals("Demonstrate Problem", gval.getTime().getTime(), oct31_01MDT.getTime() + 3600000L);

    // ---------- Test Truncate ----------
    assertEquals("Truncate Calendar.MILLISECOND",
            oct31_01_02_03_04MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MILLISECOND));

    assertEquals("Truncate Calendar.SECOND",
               oct31_01_02_03MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.SECOND));

    assertEquals("Truncate Calendar.MINUTE",
                  oct31_01_02MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.MINUTE));

    assertEquals("Truncate Calendar.HOUR_OF_DAY",
                     oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));

    assertEquals("Truncate Calendar.HOUR",
                     oct31_01MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.HOUR));

    assertEquals("Truncate Calendar.DATE",
                        oct31MDT, DateUtils.truncate(oct31_01_02_03_04MDT, Calendar.DATE));


    // ---------- Test Round (down) ----------
    assertEquals("Round Calendar.MILLISECOND",
            oct31_01_02_03_04MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MILLISECOND));

    assertEquals("Round Calendar.SECOND",
               oct31_01_02_03MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.SECOND));

    assertEquals("Round Calendar.MINUTE",
                  oct31_01_02MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.MINUTE));

    assertEquals("Round Calendar.HOUR_OF_DAY",
                     oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR_OF_DAY));

    assertEquals("Round Calendar.HOUR",
                     oct31_01MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.HOUR));

    assertEquals("Round Calendar.DATE",
                        oct31MDT, DateUtils.round(oct31_01_02_03_04MDT, Calendar.DATE));

    // restore default time zone
    TimeZone.setDefault(defaultZone);
}
 
Example 19
Source File: NumberUtilsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testCreateNumber() {
    // a lot of things can go wrong
    assertEquals("createNumber(String) 1 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5"));
    assertEquals("createNumber(String) 2 failed", new Integer("12345"), NumberUtils.createNumber("12345"));
    assertEquals("createNumber(String) 3 failed", new Double("1234.5"), NumberUtils.createNumber("1234.5D"));
    assertEquals("createNumber(String) 3 failed", new Double("1234.5"), NumberUtils.createNumber("1234.5d"));
    assertEquals("createNumber(String) 4 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5F"));
    assertEquals("createNumber(String) 4 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5f"));
    assertEquals("createNumber(String) 5 failed", new Long(Integer.MAX_VALUE + 1L), NumberUtils.createNumber(""
        + (Integer.MAX_VALUE + 1L)));
    assertEquals("createNumber(String) 6 failed", new Long(12345), NumberUtils.createNumber("12345L"));
    assertEquals("createNumber(String) 6 failed", new Long(12345), NumberUtils.createNumber("12345l"));
    assertEquals("createNumber(String) 7 failed", new Float("-1234.5"), NumberUtils.createNumber("-1234.5"));
    assertEquals("createNumber(String) 8 failed", new Integer("-12345"), NumberUtils.createNumber("-12345"));
    assertTrue("createNumber(String) 9 failed", 0xFADE == NumberUtils.createNumber("0xFADE").intValue());
    assertTrue("createNumber(String) 10 failed", -0xFADE == NumberUtils.createNumber("-0xFADE").intValue());
    assertEquals("createNumber(String) 11 failed", new Double("1.1E200"), NumberUtils.createNumber("1.1E200"));
    assertEquals("createNumber(String) 12 failed", new Float("1.1E20"), NumberUtils.createNumber("1.1E20"));
    assertEquals("createNumber(String) 13 failed", new Double("-1.1E200"), NumberUtils.createNumber("-1.1E200"));
    assertEquals("createNumber(String) 14 failed", new Double("1.1E-200"), NumberUtils.createNumber("1.1E-200"));
    assertEquals("createNumber(null) failed", null, NumberUtils.createNumber(null));
    assertEquals("createNumber(String) failed", new BigInteger("12345678901234567890"), NumberUtils
            .createNumber("12345678901234567890L"));

    // jdk 1.2 doesn't support this. unsure about jdk 1.2.2
    if (SystemUtils.isJavaVersionAtLeast(JAVA_1_3)) {
        assertEquals("createNumber(String) 15 failed", new BigDecimal("1.1E-700"), NumberUtils
                .createNumber("1.1E-700F"));
    }
    assertEquals("createNumber(String) 16 failed", new Long("10" + Integer.MAX_VALUE), NumberUtils
            .createNumber("10" + Integer.MAX_VALUE + "L"));
    assertEquals("createNumber(String) 17 failed", new Long("10" + Integer.MAX_VALUE), NumberUtils
            .createNumber("10" + Integer.MAX_VALUE));
    assertEquals("createNumber(String) 18 failed", new BigInteger("10" + Long.MAX_VALUE), NumberUtils
            .createNumber("10" + Long.MAX_VALUE));

    // LANG-521
    assertEquals("createNumber(String) LANG-521 failed", new Float("2."), NumberUtils.createNumber("2."));

    // LANG-638
    assertFalse("createNumber(String) succeeded", checkCreateNumber("1eE"));
}
 
Example 20
Source File: IntegrationTestBase.java    From jarjar with Apache License 2.0 2 votes vote down vote up
/**
 * Returns true if the system java version is at least as great as the input java version.
 *
 * @param version The java version, formated as %d.%d (eg, 1.8).
 * @return
 */
public boolean javaVersionIsAtLeast(String version) {
  String enumName = "JAVA_" + version.replace('.', '_');
  return SystemUtils.isJavaVersionAtLeast(JavaVersion.valueOf(enumName));
}