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) 2011-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 tck.java.time; 61 62 import static org.testng.Assert.assertEquals; 63 import static org.testng.Assert.fail; 64 65 import java.time.DateTimeException; 66 import java.time.temporal.TemporalAccessor; 67 import java.time.temporal.TemporalField; 68 import java.time.temporal.TemporalQuery; 69 import java.util.List; 70 71 import org.testng.annotations.Test; 72 import test.java.time.temporal.MockFieldNoValue; 73 74 /** 75 * Base test class for {@code Temporal}. 76 */ 77 public abstract class AbstractDateTimeTest extends AbstractTCKTest { 78 79 /** 80 * Sample {@code Temporal} objects. 81 * @return the objects, not null 82 */ samples()83 protected abstract List<TemporalAccessor> samples(); 84 85 /** 86 * List of valid supported fields. 87 * @return the fields, not null 88 */ validFields()89 protected abstract List<TemporalField> validFields(); 90 91 /** 92 * List of invalid unsupported fields. 93 * @return the fields, not null 94 */ invalidFields()95 protected abstract List<TemporalField> invalidFields(); 96 97 //----------------------------------------------------------------------- 98 // isSupported(TemporalField) 99 //----------------------------------------------------------------------- 100 @Test() basicTest_isSupported_TemporalField_supported()101 public void basicTest_isSupported_TemporalField_supported() { 102 for (TemporalAccessor sample : samples()) { 103 for (TemporalField field : validFields()) { 104 assertEquals(sample.isSupported(field), true, "Failed on " + sample + " " + field); 105 } 106 } 107 } 108 109 @Test() basicTest_isSupported_TemporalField_unsupported()110 public void basicTest_isSupported_TemporalField_unsupported() { 111 for (TemporalAccessor sample : samples()) { 112 for (TemporalField field : invalidFields()) { 113 assertEquals(sample.isSupported(field), false, "Failed on " + sample + " " + field); 114 } 115 } 116 } 117 118 @Test() basicTest_isSupported_TemporalField_null()119 public void basicTest_isSupported_TemporalField_null() { 120 for (TemporalAccessor sample : samples()) { 121 assertEquals(sample.isSupported(null), false, "Failed on " + sample); 122 } 123 } 124 125 //----------------------------------------------------------------------- 126 // range(TemporalField) 127 //----------------------------------------------------------------------- 128 @Test() basicTest_range_TemporalField_supported()129 public void basicTest_range_TemporalField_supported() { 130 for (TemporalAccessor sample : samples()) { 131 for (TemporalField field : validFields()) { 132 sample.range(field); // no exception 133 } 134 } 135 } 136 137 @Test() basicTest_range_TemporalField_unsupported()138 public void basicTest_range_TemporalField_unsupported() { 139 for (TemporalAccessor sample : samples()) { 140 for (TemporalField field : invalidFields()) { 141 try { 142 sample.range(field); 143 fail("Failed on " + sample + " " + field); 144 } catch (DateTimeException ex) { 145 // expected 146 } 147 } 148 } 149 } 150 151 @Test() basicTest_range_TemporalField_null()152 public void basicTest_range_TemporalField_null() { 153 for (TemporalAccessor sample : samples()) { 154 try { 155 sample.range(null); 156 fail("Failed on " + sample); 157 } catch (NullPointerException ex) { 158 // expected 159 } 160 } 161 } 162 163 //----------------------------------------------------------------------- 164 // get(TemporalField) 165 //----------------------------------------------------------------------- 166 @Test() basicTest_get_TemporalField_supported()167 public void basicTest_get_TemporalField_supported() { 168 for (TemporalAccessor sample : samples()) { 169 for (TemporalField field : validFields()) { 170 if (sample.range(field).isIntValue()) { 171 sample.get(field); // no exception 172 } else { 173 try { 174 sample.get(field); 175 fail("Failed on " + sample + " " + field); 176 } catch (DateTimeException ex) { 177 // expected 178 } 179 } 180 } 181 } 182 } 183 184 @Test() basicTest_get_TemporalField_unsupported()185 public void basicTest_get_TemporalField_unsupported() { 186 for (TemporalAccessor sample : samples()) { 187 for (TemporalField field : invalidFields()) { 188 try { 189 sample.get(field); 190 fail("Failed on " + sample + " " + field); 191 } catch (DateTimeException ex) { 192 // expected 193 } 194 } 195 } 196 } 197 198 @Test(expectedExceptions=DateTimeException.class) test_get_TemporalField_invalidField()199 public void test_get_TemporalField_invalidField() { 200 for (TemporalAccessor sample : samples()) { 201 sample.get(MockFieldNoValue.INSTANCE); 202 } 203 } 204 205 @Test() basicTest_get_TemporalField_null()206 public void basicTest_get_TemporalField_null() { 207 for (TemporalAccessor sample : samples()) { 208 try { 209 sample.get(null); 210 fail("Failed on " + sample); 211 } catch (NullPointerException ex) { 212 // expected 213 } 214 } 215 } 216 217 //----------------------------------------------------------------------- 218 // getLong(TemporalField) 219 //----------------------------------------------------------------------- 220 @Test() basicTest_getLong_TemporalField_supported()221 public void basicTest_getLong_TemporalField_supported() { 222 for (TemporalAccessor sample : samples()) { 223 for (TemporalField field : validFields()) { 224 sample.getLong(field); // no exception 225 } 226 } 227 } 228 229 @Test() basicTest_getLong_TemporalField_unsupported()230 public void basicTest_getLong_TemporalField_unsupported() { 231 for (TemporalAccessor sample : samples()) { 232 for (TemporalField field : invalidFields()) { 233 try { 234 sample.getLong(field); 235 fail("Failed on " + sample + " " + field); 236 } catch (DateTimeException ex) { 237 // expected 238 } 239 } 240 } 241 } 242 243 @Test(expectedExceptions=DateTimeException.class) test_getLong_TemporalField_invalidField()244 public void test_getLong_TemporalField_invalidField() { 245 for (TemporalAccessor sample : samples()) { 246 sample.getLong(MockFieldNoValue.INSTANCE); 247 } 248 } 249 250 @Test() basicTest_getLong_TemporalField_null()251 public void basicTest_getLong_TemporalField_null() { 252 for (TemporalAccessor sample : samples()) { 253 try { 254 sample.getLong(null); 255 fail("Failed on " + sample); 256 } catch (NullPointerException ex) { 257 // expected 258 } 259 } 260 } 261 262 //----------------------------------------------------------------------- 263 @Test basicTest_query()264 public void basicTest_query() { 265 for (TemporalAccessor sample : samples()) { 266 assertEquals(sample.query(new TemporalQuery<String>() { 267 @Override 268 public String queryFrom(TemporalAccessor temporal) { 269 return "foo"; 270 } 271 }), "foo"); 272 } 273 } 274 275 } 276