Search in sources :

Example 76 with ParsePosition

use of java.text.ParsePosition in project poi by apache.

the class TestPackageCoreProperties method compareProperties.

private void compareProperties(OPCPackage p) throws InvalidFormatException {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT);
    df.setTimeZone(LocaleUtil.TIMEZONE_UTC);
    Date expectedDate = df.parse("2007-05-12T08:00:00Z", new ParsePosition(0));
    // Gets the core properties
    PackageProperties props = p.getPackageProperties();
    assertEquals("MyCategory", props.getCategoryProperty().getValue());
    assertEquals("MyContentStatus", props.getContentStatusProperty().getValue());
    assertEquals("MyContentType", props.getContentTypeProperty().getValue());
    assertEquals(expectedDate, props.getCreatedProperty().getValue());
    assertEquals("MyCreator", props.getCreatorProperty().getValue());
    assertEquals("MyDescription", props.getDescriptionProperty().getValue());
    assertEquals("MyIdentifier", props.getIdentifierProperty().getValue());
    assertEquals("MyKeywords", props.getKeywordsProperty().getValue());
    assertEquals("MyLanguage", props.getLanguageProperty().getValue());
    assertEquals("Julien Chable", props.getLastModifiedByProperty().getValue());
    assertEquals(expectedDate, props.getLastPrintedProperty().getValue());
    assertEquals(expectedDate, props.getModifiedProperty().getValue());
    assertEquals("2", props.getRevisionProperty().getValue());
    assertEquals("MySubject", props.getSubjectProperty().getValue());
    assertEquals("MyTitle", props.getTitleProperty().getValue());
    assertEquals("2", props.getVersionProperty().getValue());
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 77 with ParsePosition

use of java.text.ParsePosition in project robovm by robovm.

the class NumberFormatTest method test_small_BigInteger_gets_longValue.

// NumberFormat.format(Object, StringBuffer, FieldPosition) guarantees it calls longValue for
// any BigInteger with a bitLength strictly less than 64.
public void test_small_BigInteger_gets_longValue() throws Exception {
    class MyNumberFormat extends NumberFormat {

        public StringBuffer format(double value, StringBuffer b, FieldPosition f) {
            b.append("double");
            return b;
        }

        public StringBuffer format(long value, StringBuffer b, FieldPosition f) {
            b.append("long");
            return b;
        }

        public Number parse(String string, ParsePosition p) {
            throw new UnsupportedOperationException();
        }
    }
    NumberFormat nf = new MyNumberFormat();
    assertEquals("long", nf.format(BigInteger.valueOf(Long.MAX_VALUE)));
    assertEquals("double", nf.format(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE)));
    assertEquals("long", nf.format(BigInteger.valueOf(Long.MIN_VALUE)));
    assertEquals("double", nf.format(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE)));
}
Also used : FieldPosition(java.text.FieldPosition) ParsePosition(java.text.ParsePosition) NumberFormat(java.text.NumberFormat)

Example 78 with ParsePosition

use of java.text.ParsePosition in project robovm by robovm.

the class OldMessageFormatTest method test_parseLjava_lang_StringLjava_text_ParsePosition.

public void test_parseLjava_lang_StringLjava_text_ParsePosition() {
    ParsePosition pos = new ParsePosition(2);
    MessageFormat mf = new MessageFormat("{0}; {0}; {0}");
    String parse = "a; b; c";
    try {
        mf.parse(parse, null);
        fail("NullPointerException was not thrown.");
    } catch (NullPointerException npe) {
    //expected
    }
    try {
        mf.parse(null, pos);
    } catch (NullPointerException npe) {
        fail("NullPointerException was thrown.");
    }
}
Also used : MessageFormat(java.text.MessageFormat) Support_MessageFormat(tests.support.Support_MessageFormat) ParsePosition(java.text.ParsePosition)

Example 79 with ParsePosition

use of java.text.ParsePosition in project robovm by robovm.

the class OldDateFormatTest method test_parseObjectLjava_lang_StringLjava_text_ParsePosition.

/**
     * java.text.DateFormat#parseObject(String, ParsePosition) Test of
     *        method java.text.DateFormat#parseObject(String, ParsePosition).
     *        Case 1: Try to parse correct data string. Case 2: Try to parse
     *        partialy correct data string. Case 3: Try to use argument
     *        ParsePosition as null.
     */
public void test_parseObjectLjava_lang_StringLjava_text_ParsePosition() {
    DateFormat df = DateFormat.getInstance();
    try {
        // case 1: Try to parse correct data string.
        Date current = new Date();
        ParsePosition pp = new ParsePosition(0);
        int parseIndex = pp.getIndex();
        Date result = (Date) df.parseObject(df.format(current), pp);
        assertEquals("Dates are different.", current.getDate(), result.getDate());
        assertEquals("Days are different.", current.getDay(), result.getDay());
        assertEquals("Months are different.", current.getMonth(), result.getMonth());
        assertEquals("Years are different.", current.getYear(), result.getYear());
        assertEquals("Hours are different", current.getHours(), result.getHours());
        assertEquals("Minutes are diffetrent,", current.getMinutes(), result.getMinutes());
        assertTrue("Parse operation return null", result != null);
        assertTrue("ParseIndex is incorrect", pp.getIndex() != parseIndex);
        // case 2: Try to parse partially correct data string.
        pp.setIndex(0);
        char[] cur = df.format(current).toCharArray();
        cur[cur.length / 2] = 'Z';
        String partialCorrect = new String(cur);
        result = (Date) df.parseObject(partialCorrect, pp);
        assertTrue("Parse operation return not-null", result == null);
        assertTrue("ParseIndex is incorrect", pp.getIndex() == 0);
        assertTrue("ParseErrorIndex is incorrect", pp.getErrorIndex() == cur.length / 2);
        pp.setIndex(2);
        char[] curDate = df.format(current).toCharArray();
        char[] newArray = new char[curDate.length + pp.getIndex()];
        for (int i = 0; i < curDate.length; i++) {
            newArray[i + pp.getIndex()] = curDate[i];
        }
        result = (Date) df.parseObject(new String(newArray), pp);
        //assertEquals(current, result);
        assertEquals("Dates are different.", current.getDate(), result.getDate());
        assertEquals("Days are different.", current.getDay(), result.getDay());
        assertEquals("Months are different.", current.getMonth(), result.getMonth());
        assertEquals("Years are different.", current.getYear(), result.getYear());
        assertEquals("Hours are different", current.getHours(), result.getHours());
        assertEquals("Minutes are diffetrent,", current.getMinutes(), result.getMinutes());
        // case 3: Try to use argument ParsePosition as null.
        try {
            df.parseObject(df.format(current), null);
            fail("Expected NullPointerException was not thrown");
        } catch (NullPointerException e) {
        // expected
        }
        assertNull(df.parseObject("test", pp));
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Date(java.util.Date) ParseException(java.text.ParseException) ParsePosition(java.text.ParsePosition)

Example 80 with ParsePosition

use of java.text.ParsePosition in project robovm by robovm.

the class OldDecimalFormatTest method test_parseLjava_lang_StringLjava_text_ParsePosition.

public void test_parseLjava_lang_StringLjava_text_ParsePosition() {
    DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ENGLISH);
    ParsePosition pos = new ParsePosition(0);
    Number result = format.parse("9223372036854775807", pos);
    assertTrue("Wrong result type for Long.MAX_VALUE", result.getClass() == Long.class);
    assertEquals("Wrong result Long.MAX_VALUE", Long.MAX_VALUE, result.longValue());
    pos = new ParsePosition(0);
    result = format.parse("-9223372036854775808", pos);
    assertTrue("Wrong result type for Long.MIN_VALUE", result.getClass() == Long.class);
    assertTrue("Wrong result Long.MIN_VALUE: " + result.longValue(), result.longValue() == Long.MIN_VALUE);
    pos = new ParsePosition(0);
    result = format.parse("9223372036854775808", pos);
    assertTrue("Wrong result type for Long.MAX_VALUE+1", result.getClass() == Double.class);
    assertEquals("Wrong result Long.MAX_VALUE + 1", (double) Long.MAX_VALUE + 1, result.doubleValue());
    pos = new ParsePosition(0);
    result = format.parse("-9223372036854775809", pos);
    assertTrue("Wrong result type for Long.MIN_VALUE - 1", result.getClass() == Double.class);
    assertEquals("Wrong result Long.MIN_VALUE - 1", (double) Long.MIN_VALUE - 1, result.doubleValue());
    pos = new ParsePosition(0);
    result = format.parse("18446744073709551629", pos);
    assertTrue("Wrong result type for overflow", result.getClass() == Double.class);
    assertEquals("Wrong result for overflow", 18446744073709551629d, result.doubleValue());
    pos = new ParsePosition(0);
    result = format.parse("42325917317067571199", pos);
    assertTrue("Wrong result type for overflow a: " + result, result.getClass() == Double.class);
    assertTrue("Wrong result for overflow a: " + result, result.doubleValue() == 42325917317067571199d);
    pos = new ParsePosition(0);
    result = format.parse("4232591731706757119E1", pos);
    assertTrue("Wrong result type for overflow b: " + result, result.getClass() == Double.class);
    assertEquals("Wrong result for overflow b: " + result, 42325917317067571190d, result.doubleValue());
    pos = new ParsePosition(0);
    result = format.parse(".42325917317067571199E20", pos);
    assertTrue("Wrong result type for overflow c: " + result, result.getClass() == Double.class);
    assertTrue("Wrong result for overflow c: " + result, result.doubleValue() == 42325917317067571199d);
    pos = new ParsePosition(0);
    result = format.parse("922337203685477580.9E1", pos);
    assertTrue("Wrong result type for overflow d: " + result, result.getClass() == Double.class);
    assertTrue("Wrong result for overflow d: " + result, result.doubleValue() == 9223372036854775809d);
    pos = new ParsePosition(0);
    result = format.parse("9.223372036854775809E18", pos);
    assertTrue("Wrong result type for overflow e: " + result, result.getClass() == Double.class);
    assertTrue("Wrong result for overflow e: " + result, result.doubleValue() == 9223372036854775809d);
    // test parse with multipliers
    format.setMultiplier(100);
    result = format.parse("9223372036854775807", new ParsePosition(0));
    assertEquals("Wrong result type multiplier 100: " + result, Long.class, result.getClass());
    // RI on windows and linux both answer with a slightly rounded result
    assertTrue("Wrong result for multiplier 100: " + result, result.longValue() == 92233720368547760L);
    format.setMultiplier(1000);
    result = format.parse("9223372036854775807", new ParsePosition(0));
    assertTrue("Wrong result type multiplier 1000: " + result, result.getClass() == Long.class);
    assertTrue("Wrong result for multiplier 1000: " + result, result.longValue() == 9223372036854776L);
    format.setMultiplier(10000);
    result = format.parse("9223372036854775807", new ParsePosition(0));
    assertTrue("Wrong result type multiplier 10000: " + result, result.getClass() == Double.class);
    assertTrue("Wrong result for multiplier 10000: " + result, result.doubleValue() == 922337203685477.5807d);
}
Also used : Support_DecimalFormat(tests.support.Support_DecimalFormat) DecimalFormat(java.text.DecimalFormat) ParsePosition(java.text.ParsePosition)

Aggregations

ParsePosition (java.text.ParsePosition)694 Test (org.junit.Test)250 Date (java.util.Date)213 TemporalAccessor (java.time.temporal.TemporalAccessor)163 SimpleDateFormat (java.text.SimpleDateFormat)117 DateTimeFormatter (java.time.format.DateTimeFormatter)94 Test (org.testng.annotations.Test)88 ParseException (java.text.ParseException)71 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)49 SimpleDateFormat (android.icu.text.SimpleDateFormat)39 FieldPosition (java.text.FieldPosition)32 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)25 Calendar (android.icu.util.Calendar)23 ULocale (android.icu.util.ULocale)23 NumberFormat (java.text.NumberFormat)23 Calendar (java.util.Calendar)23 Format (java.text.Format)21 DecimalFormat (java.text.DecimalFormat)19 GregorianCalendar (android.icu.util.GregorianCalendar)18 JapaneseCalendar (android.icu.util.JapaneseCalendar)17