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  */
  23 
  24 /*
  25  * This file is available under and governed by the GNU General Public
  26  * License version 2 only, as published by the Free Software Foundation.
  27  * However, the following notice accompanied the original version of this
  28  * file:
  29  *
  30  * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
  31  *
  32  * All rights reserved.
  33  *
  34  * Redistribution and use in source and binary forms, with or without
  35  * modification, are permitted provided that the following conditions are met:
  36  *
  37  *  * Redistributions of source code must retain the above copyright notice,
  38  *    this list of conditions and the following disclaimer.
  39  *
  40  *  * Redistributions in binary form must reproduce the above copyright notice,
  41  *    this list of conditions and the following disclaimer in the documentation
  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 java.time.format.*;
  63 
  64 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
  65 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
  66 import static org.testng.Assert.assertEquals;
  67 import static org.testng.Assert.fail;
  68 
  69 import java.text.ParsePosition;
  70 import java.time.DateTimeException;
  71 import java.time.LocalTime;
  72 import java.time.format.DateTimeBuilder;
  73 import java.time.temporal.TemporalField;
  74 
  75 import test.java.time.temporal.MockFieldValue;
  76 
  77 import org.testng.annotations.DataProvider;
  78 import org.testng.annotations.Test;
  79 
  80 /**
  81  * Test FractionPrinterParser.
  82  */
  83 @Test(groups={"implementation"})
  84 public class TestFractionPrinterParser extends AbstractTestPrinterParser {
  85 
  86     private DateTimeFormatter getFormatter(TemporalField field, int minWidth, int maxWidth, boolean decimalPoint) {
  87         return builder.appendFraction(field, minWidth, maxWidth, decimalPoint).toFormatter(locale).withSymbols(symbols);
  88     }
  89 
  90     //-----------------------------------------------------------------------
  91     // print
  92     //-----------------------------------------------------------------------
  93     @Test(expectedExceptions=DateTimeException.class)
  94     public void test_print_emptyCalendrical() throws Exception {
  95         getFormatter(NANO_OF_SECOND, 0, 9, true).printTo(EMPTY_DTA, buf);
  96     }
  97 
  98     public void test_print_append() throws Exception {
  99         buf.append("EXISTING");
 100         getFormatter(NANO_OF_SECOND, 0, 9, true).printTo(LocalTime.of(12, 30, 40, 3), buf);
 101         assertEquals(buf.toString(), "EXISTING.000000003");
 102     }
 103 
 104     //-----------------------------------------------------------------------
 105     @DataProvider(name="Nanos")
 106     Object[][] provider_nanos() {
 107         return new Object[][] {
 108             {0, 9, 0,           ""},
 109             {0, 9, 2,           ".000000002"},
 110             {0, 9, 20,          ".00000002"},
 111             {0, 9, 200,         ".0000002"},
 112             {0, 9, 2000,        ".000002"},
 113             {0, 9, 20000,       ".00002"},
 114             {0, 9, 200000,      ".0002"},
 115             {0, 9, 2000000,     ".002"},
 116             {0, 9, 20000000,    ".02"},
 117             {0, 9, 200000000,   ".2"},
 118             {0, 9, 1,           ".000000001"},
 119             {0, 9, 12,          ".000000012"},
 120             {0, 9, 123,         ".000000123"},
 121             {0, 9, 1234,        ".000001234"},
 122             {0, 9, 12345,       ".000012345"},
 123             {0, 9, 123456,      ".000123456"},
 124             {0, 9, 1234567,     ".001234567"},
 125             {0, 9, 12345678,    ".012345678"},
 126             {0, 9, 123456789,   ".123456789"},
 127 
 128             {1, 9, 0,           ".0"},
 129             {1, 9, 2,           ".000000002"},
 130             {1, 9, 20,          ".00000002"},
 131             {1, 9, 200,         ".0000002"},
 132             {1, 9, 2000,        ".000002"},
 133             {1, 9, 20000,       ".00002"},
 134             {1, 9, 200000,      ".0002"},
 135             {1, 9, 2000000,     ".002"},
 136             {1, 9, 20000000,    ".02"},
 137             {1, 9, 200000000,   ".2"},
 138 
 139             {2, 3, 0,           ".00"},
 140             {2, 3, 2,           ".000"},
 141             {2, 3, 20,          ".000"},
 142             {2, 3, 200,         ".000"},
 143             {2, 3, 2000,        ".000"},
 144             {2, 3, 20000,       ".000"},
 145             {2, 3, 200000,      ".000"},
 146             {2, 3, 2000000,     ".002"},
 147             {2, 3, 20000000,    ".02"},
 148             {2, 3, 200000000,   ".20"},
 149             {2, 3, 1,           ".000"},
 150             {2, 3, 12,          ".000"},
 151             {2, 3, 123,         ".000"},
 152             {2, 3, 1234,        ".000"},
 153             {2, 3, 12345,       ".000"},
 154             {2, 3, 123456,      ".000"},
 155             {2, 3, 1234567,     ".001"},
 156             {2, 3, 12345678,    ".012"},
 157             {2, 3, 123456789,   ".123"},
 158 
 159             {6, 6, 0,           ".000000"},
 160             {6, 6, 2,           ".000000"},
 161             {6, 6, 20,          ".000000"},
 162             {6, 6, 200,         ".000000"},
 163             {6, 6, 2000,        ".000002"},
 164             {6, 6, 20000,       ".000020"},
 165             {6, 6, 200000,      ".000200"},
 166             {6, 6, 2000000,     ".002000"},
 167             {6, 6, 20000000,    ".020000"},
 168             {6, 6, 200000000,   ".200000"},
 169             {6, 6, 1,           ".000000"},
 170             {6, 6, 12,          ".000000"},
 171             {6, 6, 123,         ".000000"},
 172             {6, 6, 1234,        ".000001"},
 173             {6, 6, 12345,       ".000012"},
 174             {6, 6, 123456,      ".000123"},
 175             {6, 6, 1234567,     ".001234"},
 176             {6, 6, 12345678,    ".012345"},
 177             {6, 6, 123456789,   ".123456"},
 178        };
 179     }
 180 
 181     @Test(dataProvider="Nanos")
 182     public void test_print_nanos(int minWidth, int maxWidth, int value, String result) throws Exception {
 183         getFormatter(NANO_OF_SECOND,  minWidth, maxWidth, true).printTo(new MockFieldValue(NANO_OF_SECOND, value), buf);
 184         if (result == null) {
 185             fail("Expected exception");
 186         }
 187         assertEquals(buf.toString(), result);
 188     }
 189 
 190     @Test(dataProvider="Nanos")
 191     public void test_print_nanos_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception {
 192         getFormatter(NANO_OF_SECOND,  minWidth, maxWidth, false).printTo(new MockFieldValue(NANO_OF_SECOND, value), buf);
 193         if (result == null) {
 194             fail("Expected exception");
 195         }
 196         assertEquals(buf.toString(), (result.startsWith(".") ? result.substring(1) : result));
 197     }
 198 
 199     //-----------------------------------------------------------------------
 200     @DataProvider(name="Seconds")
 201     Object[][] provider_seconds() {
 202         return new Object[][] {
 203             {0, 9, 0,  ""},
 204             {0, 9, 3,  ".05"},
 205             {0, 9, 6,  ".1"},
 206             {0, 9, 9,  ".15"},
 207             {0, 9, 12, ".2"},
 208             {0, 9, 15, ".25"},
 209             {0, 9, 30, ".5"},
 210             {0, 9, 45, ".75"},
 211 
 212             {2, 2, 0,  ".00"},
 213             {2, 2, 3,  ".05"},
 214             {2, 2, 6,  ".10"},
 215             {2, 2, 9,  ".15"},
 216             {2, 2, 12, ".20"},
 217             {2, 2, 15, ".25"},
 218             {2, 2, 30, ".50"},
 219             {2, 2, 45, ".75"},
 220         };
 221     }
 222 
 223     @Test(dataProvider="Seconds")
 224     public void test_print_seconds(int minWidth, int maxWidth, int value, String result) throws Exception {
 225         getFormatter(SECOND_OF_MINUTE, minWidth, maxWidth, true).printTo(new MockFieldValue(SECOND_OF_MINUTE, value), buf);
 226         if (result == null) {
 227             fail("Expected exception");
 228         }
 229         assertEquals(buf.toString(), result);
 230     }
 231 
 232     @Test(dataProvider="Seconds")
 233     public void test_print_seconds_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception {
 234         getFormatter(SECOND_OF_MINUTE, minWidth, maxWidth, false).printTo(new MockFieldValue(SECOND_OF_MINUTE, value), buf);
 235         if (result == null) {
 236             fail("Expected exception");
 237         }
 238         assertEquals(buf.toString(), (result.startsWith(".") ? result.substring(1) : result));
 239     }
 240 
 241     //-----------------------------------------------------------------------
 242     // parse
 243     //-----------------------------------------------------------------------
 244     @Test(dataProvider="Nanos")
 245     public void test_reverseParse(int minWidth, int maxWidth, int value, String result) throws Exception {
 246         ParsePosition pos = new ParsePosition(0);
 247         int expectedValue = fixParsedValue(maxWidth, value);
 248         DateTimeBuilder dtb = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).parseToBuilder(result, pos);
 249         assertEquals(pos.getIndex(), result.length());
 250         assertParsed(dtb, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue);
 251     }
 252 
 253     @Test(dataProvider="Nanos")
 254     public void test_reverseParse_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception {
 255         ParsePosition pos = new ParsePosition((result.startsWith(".") ? 1 : 0));
 256         DateTimeBuilder dtb = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, false).parseToBuilder(result, pos);
 257         assertEquals(pos.getIndex(), result.length());
 258         int expectedValue = fixParsedValue(maxWidth, value);
 259         assertParsed(dtb, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue);
 260     }
 261 
 262     @Test(dataProvider="Nanos")
 263     public void test_reverseParse_followedByNonDigit(int minWidth, int maxWidth, int value, String result) throws Exception {
 264         ParsePosition pos = new ParsePosition(0);
 265         int expectedValue = fixParsedValue(maxWidth, value);
 266         DateTimeBuilder dtb = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).parseToBuilder(result + " ", pos);
 267         assertEquals(pos.getIndex(), result.length());
 268         assertParsed(dtb, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue);
 269     }
 270 
 271 //    @Test(dataProvider="Nanos")
 272 //    public void test_reverseParse_followedByNonDigit_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception {
 273 //        FractionPrinterParser pp = new FractionPrinterParser(NANO_OF_SECOND, minWidth, maxWidth, false);
 274 //        int newPos = pp.parse(parseContext, result + " ", (result.startsWith(".") ? 1 : 0));
 275 //        assertEquals(newPos, result.length());
 276 //        int expectedValue = fixParsedValue(maxWidth, value);
 277 //        assertParsed(parseContext, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue);
 278 //    }
 279 
 280     @Test(dataProvider="Nanos")
 281     public void test_reverseParse_preceededByNonDigit(int minWidth, int maxWidth, int value, String result) throws Exception {
 282         ParsePosition pos = new ParsePosition(1);
 283         int expectedValue = fixParsedValue(maxWidth, value);
 284         DateTimeBuilder dtb = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).parseToBuilder(" " + result, pos);
 285         assertEquals(pos.getIndex(), result.length() + 1);
 286         assertParsed(dtb, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue);
 287     }
 288 
 289     private int fixParsedValue(int maxWidth, int value) {
 290         if (maxWidth < 9) {
 291             int power = (int) Math.pow(10, (9 - maxWidth));
 292             value = (value / power) * power;
 293         }
 294         return value;
 295     }
 296 
 297     @Test(dataProvider="Seconds")
 298     public void test_reverseParse_seconds(int minWidth, int maxWidth, int value, String result) throws Exception {
 299         ParsePosition pos = new ParsePosition(0);
 300         DateTimeBuilder dtb = getFormatter(SECOND_OF_MINUTE, minWidth, maxWidth, true).parseToBuilder(result, pos);
 301         assertEquals(pos.getIndex(), result.length());
 302         assertParsed(dtb, SECOND_OF_MINUTE, value == 0 && minWidth == 0 ? null : (long) value);
 303     }
 304 
 305     private void assertParsed(DateTimeBuilder dtb, TemporalField field, Long value) {
 306         if (value == null) {
 307             assertEquals(dtb.containsFieldValue(field), false);
 308         } else {
 309             assertEquals(dtb.getLong(field), (long)value);
 310         }
 311     }
 312 
 313     //-----------------------------------------------------------------------
 314     @DataProvider(name="ParseNothing")
 315     Object[][] provider_parseNothing() {
 316         return new Object[][] {
 317             {NANO_OF_SECOND, 3, 6, true, "", 0, 0},
 318             {NANO_OF_SECOND, 3, 6, true, "A", 0, 0},
 319             {NANO_OF_SECOND, 3, 6, true, ".", 0, 1},
 320             {NANO_OF_SECOND, 3, 6, true, ".5", 0, 1},
 321             {NANO_OF_SECOND, 3, 6, true, ".51", 0, 1},
 322             {NANO_OF_SECOND, 3, 6, true, ".A23456", 0, 1},
 323             {NANO_OF_SECOND, 3, 6, true, ".1A3456", 0, 1},
 324         };
 325     }
 326 
 327     @Test(dataProvider = "ParseNothing")
 328     public void test_parse_nothing(TemporalField field, int min, int max, boolean decimalPoint, String text, int pos, int expected) {
 329         ParsePosition ppos = new ParsePosition(pos);
 330         DateTimeBuilder dtb = getFormatter(field, min, max, decimalPoint).parseToBuilder(text, ppos);
 331         assertEquals(ppos.getErrorIndex(), expected);
 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 }