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.DAY_OF_MONTH;
  65 import static java.time.temporal.ChronoField.HOUR_OF_DAY;
  66 import static org.testng.Assert.assertEquals;
  67 import static org.testng.Assert.fail;
  68 
  69 import java.time.DateTimeException;
  70 import java.time.LocalDate;
  71 import test.java.time.temporal.MockFieldValue;
  72 
  73 import org.testng.annotations.DataProvider;
  74 import org.testng.annotations.Test;
  75 
  76 /**
  77  * Test SimpleNumberPrinterParser.
  78  */
  79 @Test
  80 public class TestNumberPrinter extends AbstractTestPrinterParser {
  81 
  82     //-----------------------------------------------------------------------
  83     @Test(expectedExceptions=DateTimeException.class)
  84     public void test_print_emptyCalendrical() throws Exception {
  85         getFormatter(DAY_OF_MONTH, 1, 2, SignStyle.NEVER).printTo(EMPTY_DTA, buf);
  86     }
  87 
  88     public void test_print_append() throws Exception {
  89         buf.append("EXISTING");
  90         getFormatter(DAY_OF_MONTH, 1, 2, SignStyle.NEVER).printTo(LocalDate.of(2012, 1, 3), buf);
  91         assertEquals(buf.toString(), "EXISTING3");
  92     }
  93 
  94     //-----------------------------------------------------------------------
  95     @DataProvider(name="Pad")
  96     Object[][] provider_pad() {
  97         return new Object[][] {
  98             {1, 1, -10, null},
  99             {1, 1, -9, "9"},
 100             {1, 1, -1, "1"},
 101             {1, 1, 0, "0"},
 102             {1, 1, 3, "3"},
 103             {1, 1, 9, "9"},
 104             {1, 1, 10, null},
 105 
 106             {1, 2, -100, null},
 107             {1, 2, -99, "99"},
 108             {1, 2, -10, "10"},
 109             {1, 2, -9, "9"},
 110             {1, 2, -1, "1"},
 111             {1, 2, 0, "0"},
 112             {1, 2, 3, "3"},
 113             {1, 2, 9, "9"},
 114             {1, 2, 10, "10"},
 115             {1, 2, 99, "99"},
 116             {1, 2, 100, null},
 117 
 118             {2, 2, -100, null},
 119             {2, 2, -99, "99"},
 120             {2, 2, -10, "10"},
 121             {2, 2, -9, "09"},
 122             {2, 2, -1, "01"},
 123             {2, 2, 0, "00"},
 124             {2, 2, 3, "03"},
 125             {2, 2, 9, "09"},
 126             {2, 2, 10, "10"},
 127             {2, 2, 99, "99"},
 128             {2, 2, 100, null},
 129 
 130             {1, 3, -1000, null},
 131             {1, 3, -999, "999"},
 132             {1, 3, -100, "100"},
 133             {1, 3, -99, "99"},
 134             {1, 3, -10, "10"},
 135             {1, 3, -9, "9"},
 136             {1, 3, -1, "1"},
 137             {1, 3, 0, "0"},
 138             {1, 3, 3, "3"},
 139             {1, 3, 9, "9"},
 140             {1, 3, 10, "10"},
 141             {1, 3, 99, "99"},
 142             {1, 3, 100, "100"},
 143             {1, 3, 999, "999"},
 144             {1, 3, 1000, null},
 145 
 146             {2, 3, -1000, null},
 147             {2, 3, -999, "999"},
 148             {2, 3, -100, "100"},
 149             {2, 3, -99, "99"},
 150             {2, 3, -10, "10"},
 151             {2, 3, -9, "09"},
 152             {2, 3, -1, "01"},
 153             {2, 3, 0, "00"},
 154             {2, 3, 3, "03"},
 155             {2, 3, 9, "09"},
 156             {2, 3, 10, "10"},
 157             {2, 3, 99, "99"},
 158             {2, 3, 100, "100"},
 159             {2, 3, 999, "999"},
 160             {2, 3, 1000, null},
 161 
 162             {3, 3, -1000, null},
 163             {3, 3, -999, "999"},
 164             {3, 3, -100, "100"},
 165             {3, 3, -99, "099"},
 166             {3, 3, -10, "010"},
 167             {3, 3, -9, "009"},
 168             {3, 3, -1, "001"},
 169             {3, 3, 0, "000"},
 170             {3, 3, 3, "003"},
 171             {3, 3, 9, "009"},
 172             {3, 3, 10, "010"},
 173             {3, 3, 99, "099"},
 174             {3, 3, 100, "100"},
 175             {3, 3, 999, "999"},
 176             {3, 3, 1000, null},
 177 
 178             {1, 10, Integer.MAX_VALUE - 1, "2147483646"},
 179             {1, 10, Integer.MAX_VALUE, "2147483647"},
 180             {1, 10, Integer.MIN_VALUE + 1, "2147483647"},
 181             {1, 10, Integer.MIN_VALUE, "2147483648"},
 182        };
 183     }
 184 
 185     @Test(dataProvider="Pad")
 186     public void test_pad_NOT_NEGATIVE(int minPad, int maxPad, long value, String result) throws Exception {
 187         try {
 188             getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.NOT_NEGATIVE).printTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
 189             if (result == null || value < 0) {
 190                 fail("Expected exception");
 191             }
 192             assertEquals(buf.toString(), result);
 193         } catch (DateTimePrintException ex) {
 194             if (result == null || value < 0) {
 195                 assertEquals(ex.getMessage().contains(DAY_OF_MONTH.getName()), true);
 196             } else {
 197                 throw ex;
 198             }
 199         }
 200     }
 201 
 202     @Test(dataProvider="Pad")
 203     public void test_pad_NEVER(int minPad, int maxPad, long value, String result) throws Exception {
 204         try {
 205             getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.NEVER).printTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
 206             if (result == null) {
 207                 fail("Expected exception");
 208             }
 209             assertEquals(buf.toString(), result);
 210         } catch (DateTimePrintException ex) {
 211             if (result != null) {
 212                 throw ex;
 213             }
 214             assertEquals(ex.getMessage().contains(DAY_OF_MONTH.getName()), true);
 215         }
 216     }
 217 
 218     @Test(dataProvider="Pad")
 219     public void test_pad_NORMAL(int minPad, int maxPad, long value, String result) throws Exception {
 220         try {
 221             getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.NORMAL).printTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
 222             if (result == null) {
 223                 fail("Expected exception");
 224             }
 225             assertEquals(buf.toString(), (value < 0 ? "-" + result : result));
 226         } catch (DateTimePrintException ex) {
 227             if (result != null) {
 228                 throw ex;
 229             }
 230             assertEquals(ex.getMessage().contains(DAY_OF_MONTH.getName()), true);
 231         }
 232     }
 233 
 234     @Test(dataProvider="Pad")
 235     public void test_pad_ALWAYS(int minPad, int maxPad, long value, String result) throws Exception {
 236         try {
 237             getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.ALWAYS).printTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
 238             if (result == null) {
 239                 fail("Expected exception");
 240             }
 241             assertEquals(buf.toString(), (value < 0 ? "-" + result : "+" + result));
 242         } catch (DateTimePrintException ex) {
 243             if (result != null) {
 244                 throw ex;
 245             }
 246             assertEquals(ex.getMessage().contains(DAY_OF_MONTH.getName()), true);
 247         }
 248     }
 249 
 250     @Test(dataProvider="Pad")
 251     public void test_pad_EXCEEDS_PAD(int minPad, int maxPad, long value, String result) throws Exception {
 252         try {
 253             getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.EXCEEDS_PAD).printTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
 254             if (result == null) {
 255                 fail("Expected exception");
 256                 return;  // unreachable
 257             }
 258             if (result.length() > minPad || value < 0) {
 259                 result = (value < 0 ? "-" + result : "+" + result);
 260             }
 261             assertEquals(buf.toString(), result);
 262         } catch (DateTimePrintException ex) {
 263             if (result != null) {
 264                 throw ex;
 265             }
 266             assertEquals(ex.getMessage().contains(DAY_OF_MONTH.getName()), true);
 267         }
 268     }
 269 
 270     //-----------------------------------------------------------------------
 271     public void test_toString1() throws Exception {
 272         assertEquals(getFormatter(HOUR_OF_DAY, 1, 19, SignStyle.NORMAL).toString(), "Value(HourOfDay)");
 273     }
 274 
 275     public void test_toString2() throws Exception {
 276         assertEquals(getFormatter(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE).toString(), "Value(HourOfDay,2)");
 277     }
 278 
 279     public void test_toString3() throws Exception {
 280         assertEquals(getFormatter(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE).toString(), "Value(HourOfDay,1,2,NOT_NEGATIVE)");
 281     }
 282 
 283 }