test/java/time/test/java/time/format/TestReducedParser.java

Print this page




  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.DAY_OF_YEAR;
  65 import static java.time.temporal.ChronoField.YEAR;
  66 import static org.testng.Assert.assertEquals;
  67 import static org.testng.Assert.assertTrue;
  68 
  69 import java.text.ParsePosition;
  70 import java.time.format.DateTimeBuilder;

  71 import java.time.temporal.TemporalField;
  72 
  73 import org.testng.annotations.DataProvider;
  74 import org.testng.annotations.Test;
  75 
  76 /**
  77  * Test ReducedPrinterParser.
  78  */
  79 @Test(groups={"implementation"})
  80 public class TestReducedParser extends AbstractTestPrinterParser {
  81 
  82     private DateTimeFormatter getFormatter0(TemporalField field, int width, int baseValue) {
  83         return builder.appendValueReduced(field, width, baseValue).toFormatter(locale).withSymbols(symbols);
  84     }
  85 
  86     //-----------------------------------------------------------------------
  87     @DataProvider(name="error")
  88     Object[][] data_error() {
  89         return new Object[][] {
  90             {YEAR, 2, 2010, "12", -1, IndexOutOfBoundsException.class},
  91             {YEAR, 2, 2010, "12", 3, IndexOutOfBoundsException.class},
  92         };
  93     }
  94 
  95     @Test(dataProvider="error")
  96     public void test_parse_error(TemporalField field, int width, int baseValue, String text, int pos, Class<?> expected) {
  97         try {
  98             getFormatter0(field, width, baseValue).parseToBuilder(text, new ParsePosition(pos));
  99         } catch (RuntimeException ex) {
 100             assertTrue(expected.isInstance(ex));
 101         }
 102     }
 103 
 104     //-----------------------------------------------------------------------
 105     public void test_parse_fieldRangeIgnored() throws Exception {
 106         ParsePosition pos = new ParsePosition(0);
 107         DateTimeBuilder dtb = getFormatter0(DAY_OF_YEAR, 3, 10).parseToBuilder("456", pos);
 108         assertEquals(pos.getIndex(), 3);
 109         assertParsed(dtb, DAY_OF_YEAR, 456L);  // parsed dayOfYear=456
 110     }
 111 
 112     //-----------------------------------------------------------------------
 113     @DataProvider(name="Parse")
 114     Object[][] provider_parse() {
 115         return new Object[][] {
 116              // negative zero
 117             {YEAR, 1, 2010, "-0", 0, 0, null},
 118 
 119             // general
 120             {YEAR, 2, 2010, "Xxx12Xxx", 3, 5, 2012},
 121             {YEAR, 2, 2010, "12345", 0, 2, 2012},
 122             {YEAR, 2, 2010, "12-45", 0, 2, 2012},
 123 
 124             // insufficient digits
 125             {YEAR, 2, 2010, "0", 0, 0, null},
 126             {YEAR, 2, 2010, "1", 0, 0, null},
 127             {YEAR, 2, 2010, "1", 1, 1, null},
 128             {YEAR, 2, 2010, "1-2", 0, 0, null},
 129             {YEAR, 2, 2010, "9", 0, 0, null},


 148 
 149             // parse OK 2
 150             {YEAR, 2, 2010, "00", 0, 2, 2100},
 151             {YEAR, 2, 2010, "09", 0, 2, 2109},
 152             {YEAR, 2, 2010, "10", 0, 2, 2010},
 153             {YEAR, 2, 2010, "99", 0, 2, 2099},
 154             {YEAR, 2, 2010, "100", 0, 2, 2010},
 155 
 156             // parse OK 2
 157             {YEAR, 2, -2005, "05", 0, 2, -2005},
 158             {YEAR, 2, -2005, "00", 0, 2, -2000},
 159             {YEAR, 2, -2005, "99", 0, 2, -1999},
 160             {YEAR, 2, -2005, "06", 0, 2, -1906},
 161             {YEAR, 2, -2005, "100", 0, 2, -1910},
 162        };
 163     }
 164 
 165     @Test(dataProvider="Parse")
 166     public void test_parse(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
 167         ParsePosition ppos = new ParsePosition(pos);
 168         DateTimeBuilder dtb = getFormatter0(field, width, baseValue).parseToBuilder(input, ppos);
 169         if (ppos.getErrorIndex() != -1) {
 170             assertEquals(ppos.getErrorIndex(), parseLen);
 171         } else {
 172             assertEquals(ppos.getIndex(), parseLen);
 173             assertParsed(dtb, YEAR, parseVal != null ? (long) parseVal : null);
 174         }
 175     }
 176 
 177     @Test(dataProvider="Parse")
 178     public void test_parseLenient(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
 179         setStrict(false);
 180         ParsePosition ppos = new ParsePosition(pos);
 181         DateTimeBuilder dtb = getFormatter0(field, width, baseValue).parseToBuilder(input, ppos);
 182         if (ppos.getErrorIndex() != -1) {
 183             assertEquals(ppos.getErrorIndex(), parseLen);
 184         } else {
 185             assertEquals(ppos.getIndex(), parseLen);
 186             assertParsed(dtb, YEAR, parseVal != null ? (long) parseVal : null);
 187         }
 188     }
 189 
 190     private void assertParsed(DateTimeBuilder dtb, TemporalField field, Long value) {
 191         if (value == null) {
 192             assertEquals(dtb, null);
 193         } else {
 194             assertEquals(dtb.getLong(field), (long)value);

 195         }
 196     }
 197 
 198 }


  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.DAY_OF_YEAR;
  63 import static java.time.temporal.ChronoField.YEAR;
  64 import static org.testng.Assert.assertEquals;
  65 import static org.testng.Assert.assertTrue;
  66 
  67 import java.text.ParsePosition;
  68 import java.time.format.DateTimeFormatter;
  69 import java.time.temporal.TemporalAccessor;
  70 import java.time.temporal.TemporalField;
  71 
  72 import org.testng.annotations.DataProvider;
  73 import org.testng.annotations.Test;
  74 
  75 /**
  76  * Test ReducedPrinterParser.
  77  */
  78 @Test(groups={"implementation"})
  79 public class TestReducedParser extends AbstractTestPrinterParser {
  80 
  81     private DateTimeFormatter getFormatter0(TemporalField field, int width, int baseValue) {
  82         return builder.appendValueReduced(field, width, baseValue).toFormatter(locale).withSymbols(symbols);
  83     }
  84 
  85     //-----------------------------------------------------------------------
  86     @DataProvider(name="error")
  87     Object[][] data_error() {
  88         return new Object[][] {
  89             {YEAR, 2, 2010, "12", -1, IndexOutOfBoundsException.class},
  90             {YEAR, 2, 2010, "12", 3, IndexOutOfBoundsException.class},
  91         };
  92     }
  93 
  94     @Test(dataProvider="error")
  95     public void test_parse_error(TemporalField field, int width, int baseValue, String text, int pos, Class<?> expected) {
  96         try {
  97             getFormatter0(field, width, baseValue).parseUnresolved(text, new ParsePosition(pos));
  98         } catch (RuntimeException ex) {
  99             assertTrue(expected.isInstance(ex));
 100         }
 101     }
 102 
 103     //-----------------------------------------------------------------------
 104     public void test_parse_fieldRangeIgnored() throws Exception {
 105         ParsePosition pos = new ParsePosition(0);
 106         TemporalAccessor parsed = getFormatter0(DAY_OF_YEAR, 3, 10).parseUnresolved("456", pos);
 107         assertEquals(pos.getIndex(), 3);
 108         assertParsed(parsed, DAY_OF_YEAR, 456L);  // parsed dayOfYear=456
 109     }
 110 
 111     //-----------------------------------------------------------------------
 112     @DataProvider(name="Parse")
 113     Object[][] provider_parse() {
 114         return new Object[][] {
 115              // negative zero
 116             {YEAR, 1, 2010, "-0", 0, 0, null},
 117 
 118             // general
 119             {YEAR, 2, 2010, "Xxx12Xxx", 3, 5, 2012},
 120             {YEAR, 2, 2010, "12345", 0, 2, 2012},
 121             {YEAR, 2, 2010, "12-45", 0, 2, 2012},
 122 
 123             // insufficient digits
 124             {YEAR, 2, 2010, "0", 0, 0, null},
 125             {YEAR, 2, 2010, "1", 0, 0, null},
 126             {YEAR, 2, 2010, "1", 1, 1, null},
 127             {YEAR, 2, 2010, "1-2", 0, 0, null},
 128             {YEAR, 2, 2010, "9", 0, 0, null},


 147 
 148             // parse OK 2
 149             {YEAR, 2, 2010, "00", 0, 2, 2100},
 150             {YEAR, 2, 2010, "09", 0, 2, 2109},
 151             {YEAR, 2, 2010, "10", 0, 2, 2010},
 152             {YEAR, 2, 2010, "99", 0, 2, 2099},
 153             {YEAR, 2, 2010, "100", 0, 2, 2010},
 154 
 155             // parse OK 2
 156             {YEAR, 2, -2005, "05", 0, 2, -2005},
 157             {YEAR, 2, -2005, "00", 0, 2, -2000},
 158             {YEAR, 2, -2005, "99", 0, 2, -1999},
 159             {YEAR, 2, -2005, "06", 0, 2, -1906},
 160             {YEAR, 2, -2005, "100", 0, 2, -1910},
 161        };
 162     }
 163 
 164     @Test(dataProvider="Parse")
 165     public void test_parse(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
 166         ParsePosition ppos = new ParsePosition(pos);
 167         TemporalAccessor parsed = getFormatter0(field, width, baseValue).parseUnresolved(input, ppos);
 168         if (ppos.getErrorIndex() != -1) {
 169             assertEquals(ppos.getErrorIndex(), parseLen);
 170         } else {
 171             assertEquals(ppos.getIndex(), parseLen);
 172             assertParsed(parsed, YEAR, parseVal != null ? (long) parseVal : null);
 173         }
 174     }
 175 
 176     @Test(dataProvider="Parse")
 177     public void test_parseLenient(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
 178         setStrict(false);
 179         ParsePosition ppos = new ParsePosition(pos);
 180         TemporalAccessor parsed = getFormatter0(field, width, baseValue).parseUnresolved(input, ppos);
 181         if (ppos.getErrorIndex() != -1) {
 182             assertEquals(ppos.getErrorIndex(), parseLen);
 183         } else {
 184             assertEquals(ppos.getIndex(), parseLen);
 185             assertParsed(parsed, YEAR, parseVal != null ? (long) parseVal : null);
 186         }
 187     }
 188 
 189     private void assertParsed(TemporalAccessor parsed, TemporalField field, Long value) {
 190         if (value == null) {
 191             assertEquals(parsed, null);
 192         } else {
 193             assertEquals(parsed.isSupported(field), true);
 194             assertEquals(parsed.getLong(field), (long) value);
 195         }
 196     }
 197 
 198 }