< prev index next >

test/jdk/java/time/test/java/time/format/TestFractionPrinterParser.java

Print this page
rev 56231 : [mq]: 8230136
   1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  42  *    and/or other materials provided with the distribution.
  43  *
  44  *  * Neither the name of JSR-310 nor the names of its contributors
  45  *    may be used to endorse or promote products derived from this software
  46  *    without specific prior written permission.
  47  *
  48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59  */
  60 package test.java.time.format;
  61 

  62 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
  63 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
  64 import static org.testng.Assert.assertEquals;
  65 import static org.testng.Assert.fail;
  66 
  67 import java.text.ParsePosition;
  68 import java.time.DateTimeException;
  69 import java.time.LocalTime;
  70 import java.time.format.DateTimeFormatter;
  71 import java.time.temporal.TemporalAccessor;
  72 import java.time.temporal.TemporalField;
  73 
  74 import org.testng.annotations.DataProvider;
  75 import org.testng.annotations.Test;
  76 import test.java.time.temporal.MockFieldValue;
  77 
  78 /**
  79  * Test FractionPrinterParser.


  80  */
  81 @Test
  82 public class TestFractionPrinterParser extends AbstractTestPrinterParser {
  83 
  84     private DateTimeFormatter getFormatter(TemporalField field, int minWidth, int maxWidth, boolean decimalPoint) {
  85         return builder.appendFraction(field, minWidth, maxWidth, decimalPoint).toFormatter(locale).withDecimalStyle(decimalStyle);
  86     }
  87 
  88     //-----------------------------------------------------------------------
  89     // print
  90     //-----------------------------------------------------------------------
  91     @Test(expectedExceptions=DateTimeException.class)
  92     public void test_print_emptyCalendrical() throws Exception {
  93         getFormatter(NANO_OF_SECOND, 0, 9, true).formatTo(EMPTY_DTA, buf);
  94     }
  95 
  96     public void test_print_append() throws Exception {
  97         buf.append("EXISTING");
  98         getFormatter(NANO_OF_SECOND, 0, 9, true).formatTo(LocalTime.of(12, 30, 40, 3), buf);
  99         assertEquals(buf.toString(), "EXISTING.000000003");


 314     Object[][] provider_parseNothing() {
 315         return new Object[][] {
 316             {NANO_OF_SECOND, 3, 6, true, "", 0, 0},
 317             {NANO_OF_SECOND, 3, 6, true, "A", 0, 0},
 318             {NANO_OF_SECOND, 3, 6, true, ".", 0, 1},
 319             {NANO_OF_SECOND, 3, 6, true, ".5", 0, 1},
 320             {NANO_OF_SECOND, 3, 6, true, ".51", 0, 1},
 321             {NANO_OF_SECOND, 3, 6, true, ".A23456", 0, 1},
 322             {NANO_OF_SECOND, 3, 6, true, ".1A3456", 0, 1},
 323         };
 324     }
 325 
 326     @Test(dataProvider = "ParseNothing")
 327     public void test_parse_nothing(TemporalField field, int min, int max, boolean decimalPoint, String text, int pos, int expected) {
 328         ParsePosition ppos = new ParsePosition(pos);
 329         TemporalAccessor parsed = getFormatter(field, min, max, decimalPoint).parseUnresolved(text, ppos);
 330         assertEquals(ppos.getErrorIndex(), expected);
 331         assertEquals(parsed, null);
 332     }
 333 


















 334     //-----------------------------------------------------------------------
 335     public void test_toString() throws Exception {
 336         assertEquals(getFormatter(NANO_OF_SECOND, 3, 6, true).toString(), "Fraction(NanoOfSecond,3,6,DecimalPoint)");
 337     }
 338 
 339     public void test_toString_noDecimalPoint() throws Exception {
 340         assertEquals(getFormatter(NANO_OF_SECOND, 3, 6, false).toString(), "Fraction(NanoOfSecond,3,6)");
 341     }
 342 
 343 }
   1 /*
   2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  42  *    and/or other materials provided with the distribution.
  43  *
  44  *  * Neither the name of JSR-310 nor the names of its contributors
  45  *    may be used to endorse or promote products derived from this software
  46  *    without specific prior written permission.
  47  *
  48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59  */
  60 package test.java.time.format;
  61 
  62 import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
  63 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
  64 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
  65 import static org.testng.Assert.assertEquals;
  66 import static org.testng.Assert.fail;
  67 
  68 import java.text.ParsePosition;
  69 import java.time.DateTimeException;
  70 import java.time.LocalTime;
  71 import java.time.format.DateTimeFormatter;
  72 import java.time.temporal.TemporalAccessor;
  73 import java.time.temporal.TemporalField;
  74 
  75 import org.testng.annotations.DataProvider;
  76 import org.testng.annotations.Test;
  77 import test.java.time.temporal.MockFieldValue;
  78 
  79 /**
  80  * Test FractionPrinterParser.
  81  *
  82  * @bug 8230136
  83  */
  84 @Test
  85 public class TestFractionPrinterParser extends AbstractTestPrinterParser {
  86 
  87     private DateTimeFormatter getFormatter(TemporalField field, int minWidth, int maxWidth, boolean decimalPoint) {
  88         return builder.appendFraction(field, minWidth, maxWidth, decimalPoint).toFormatter(locale).withDecimalStyle(decimalStyle);
  89     }
  90 
  91     //-----------------------------------------------------------------------
  92     // print
  93     //-----------------------------------------------------------------------
  94     @Test(expectedExceptions=DateTimeException.class)
  95     public void test_print_emptyCalendrical() throws Exception {
  96         getFormatter(NANO_OF_SECOND, 0, 9, true).formatTo(EMPTY_DTA, buf);
  97     }
  98 
  99     public void test_print_append() throws Exception {
 100         buf.append("EXISTING");
 101         getFormatter(NANO_OF_SECOND, 0, 9, true).formatTo(LocalTime.of(12, 30, 40, 3), buf);
 102         assertEquals(buf.toString(), "EXISTING.000000003");


 317     Object[][] provider_parseNothing() {
 318         return new Object[][] {
 319             {NANO_OF_SECOND, 3, 6, true, "", 0, 0},
 320             {NANO_OF_SECOND, 3, 6, true, "A", 0, 0},
 321             {NANO_OF_SECOND, 3, 6, true, ".", 0, 1},
 322             {NANO_OF_SECOND, 3, 6, true, ".5", 0, 1},
 323             {NANO_OF_SECOND, 3, 6, true, ".51", 0, 1},
 324             {NANO_OF_SECOND, 3, 6, true, ".A23456", 0, 1},
 325             {NANO_OF_SECOND, 3, 6, true, ".1A3456", 0, 1},
 326         };
 327     }
 328 
 329     @Test(dataProvider = "ParseNothing")
 330     public void test_parse_nothing(TemporalField field, int min, int max, boolean decimalPoint, String text, int pos, int expected) {
 331         ParsePosition ppos = new ParsePosition(pos);
 332         TemporalAccessor parsed = getFormatter(field, min, max, decimalPoint).parseUnresolved(text, ppos);
 333         assertEquals(ppos.getErrorIndex(), expected);
 334         assertEquals(parsed, null);
 335     }
 336 
 337     @DataProvider(name="ParseMinWidth")
 338     Object[][] provider_parseMinWidth() {
 339         return new Object[][] {
 340             {MILLI_OF_SECOND, 3, 3, true, ".1x"},
 341             {MILLI_OF_SECOND, 3, 3, true, ".12x"},
 342             {MILLI_OF_SECOND, 3, 3, true, ".1234x"},
 343         };
 344     }
 345 
 346     @Test(dataProvider="ParseMinWidth", expectedExceptions=DateTimeException.class)
 347     public void test_parse_minWidth(TemporalField field, int min, int max, boolean decimalPoint, String text) throws Exception {
 348         builder
 349             .appendFraction(field, min, max, decimalPoint)
 350             .appendLiteral("x")
 351             .toFormatter(locale)
 352             .parse(text);
 353     }
 354 
 355     //-----------------------------------------------------------------------
 356     public void test_toString() throws Exception {
 357         assertEquals(getFormatter(NANO_OF_SECOND, 3, 6, true).toString(), "Fraction(NanoOfSecond,3,6,DecimalPoint)");
 358     }
 359 
 360     public void test_toString_noDecimalPoint() throws Exception {
 361         assertEquals(getFormatter(NANO_OF_SECOND, 3, 6, false).toString(), "Fraction(NanoOfSecond,3,6)");
 362     }
 363 
 364 }
< prev index next >