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