1 /* 2 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * * Neither the name of JSR-310 nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.threeten.bp; 33 34 import static org.testng.Assert.assertEquals; 35 import static org.testng.Assert.fail; 36 37 import java.util.List; 38 39 import org.testng.annotations.Test; 40 import org.threeten.bp.temporal.TemporalAccessor; 41 import org.threeten.bp.temporal.TemporalField; 42 import org.threeten.bp.temporal.TemporalQuery; 43 44 /** 45 * Base test class for {@code DateTime}. 46 */ 47 public abstract class AbstractDateTimeTest extends AbstractTest { 48 49 /** 50 * Sample {@code DateTime} objects. 51 * @return the objects, not null 52 */ samples()53 protected abstract List<TemporalAccessor> samples(); 54 55 /** 56 * List of valid supported fields. 57 * @return the fields, not null 58 */ validFields()59 protected abstract List<TemporalField> validFields(); 60 61 /** 62 * List of invalid unsupported fields. 63 * @return the fields, not null 64 */ invalidFields()65 protected abstract List<TemporalField> invalidFields(); 66 67 //----------------------------------------------------------------------- 68 // isSupported(DateTimeField) 69 //----------------------------------------------------------------------- 70 @Test basicTest_isSupported_DateTimeField_supported()71 public void basicTest_isSupported_DateTimeField_supported() { 72 for (TemporalAccessor sample : samples()) { 73 for (TemporalField field : validFields()) { 74 assertEquals(sample.isSupported(field), true, "Failed on " + sample + " " + field); 75 } 76 } 77 } 78 79 @Test basicTest_isSupported_DateTimeField_unsupported()80 public void basicTest_isSupported_DateTimeField_unsupported() { 81 for (TemporalAccessor sample : samples()) { 82 for (TemporalField field : invalidFields()) { 83 assertEquals(sample.isSupported(field), false, "Failed on " + sample + " " + field); 84 } 85 } 86 } 87 88 @Test basicTest_isSupported_DateTimeField_null()89 public void basicTest_isSupported_DateTimeField_null() { 90 for (TemporalAccessor sample : samples()) { 91 assertEquals(sample.isSupported(null), false, "Failed on " + sample); 92 } 93 } 94 95 //----------------------------------------------------------------------- 96 // range(DateTimeField) 97 //----------------------------------------------------------------------- 98 // TODO needs implementations of week fields 99 // @Test 100 // public void basicTest_range_DateTimeField_supported() { 101 // for (DateTimeAccessor sample : samples()) { 102 // for (DateTimeField field : validFields()) { 103 // sample.range(field); // no exception 104 // } 105 // } 106 // } 107 108 @Test basicTest_range_DateTimeField_unsupported()109 public void basicTest_range_DateTimeField_unsupported() { 110 for (TemporalAccessor sample : samples()) { 111 for (TemporalField field : invalidFields()) { 112 try { 113 sample.range(field); 114 fail("Failed on " + sample + " " + field); 115 } catch (DateTimeException ex) { 116 // expected 117 } 118 } 119 } 120 } 121 122 @Test basicTest_range_DateTimeField_null()123 public void basicTest_range_DateTimeField_null() { 124 for (TemporalAccessor sample : samples()) { 125 try { 126 sample.range(null); 127 fail("Failed on " + sample); 128 } catch (NullPointerException ex) { 129 // expected 130 } 131 } 132 } 133 134 //----------------------------------------------------------------------- 135 // get(DateTimeField) 136 //----------------------------------------------------------------------- 137 // TODO needs implementations of week fields 138 // @Test 139 // public void basicTest_get_DateTimeField_supported() { 140 // for (DateTimeAccessor sample : samples()) { 141 // for (DateTimeField field : validFields()) { 142 // if (sample.range(field).isIntValue()) { 143 // sample.get(field); // no exception 144 // } else { 145 // try { 146 // sample.get(field); 147 // fail("Failed on " + sample + " " + field); 148 // } catch (DateTimeException ex) { 149 // // expected 150 // } 151 // } 152 // } 153 // } 154 // } 155 156 @Test basicTest_get_DateTimeField_unsupported()157 public void basicTest_get_DateTimeField_unsupported() { 158 for (TemporalAccessor sample : samples()) { 159 for (TemporalField field : invalidFields()) { 160 try { 161 sample.get(field); 162 fail("Failed on " + sample + " " + field); 163 } catch (DateTimeException ex) { 164 // expected 165 } 166 } 167 } 168 } 169 170 @Test basicTest_get_DateTimeField_null()171 public void basicTest_get_DateTimeField_null() { 172 for (TemporalAccessor sample : samples()) { 173 try { 174 sample.get(null); 175 fail("Failed on " + sample); 176 } catch (NullPointerException ex) { 177 // expected 178 } 179 } 180 } 181 182 //----------------------------------------------------------------------- 183 // getLong(DateTimeField) 184 //----------------------------------------------------------------------- 185 // TODO needs implementations of week fields 186 // @Test 187 // public void basicTest_getLong_DateTimeField_supported() { 188 // for (DateTimeAccessor sample : samples()) { 189 // for (DateTimeField field : validFields()) { 190 // sample.getLong(field); // no exception 191 // } 192 // } 193 // } 194 195 @Test basicTest_getLong_DateTimeField_unsupported()196 public void basicTest_getLong_DateTimeField_unsupported() { 197 for (TemporalAccessor sample : samples()) { 198 for (TemporalField field : invalidFields()) { 199 try { 200 sample.getLong(field); 201 fail("Failed on " + sample + " " + field); 202 } catch (DateTimeException ex) { 203 // expected 204 } 205 } 206 } 207 } 208 209 @Test basicTest_getLong_DateTimeField_null()210 public void basicTest_getLong_DateTimeField_null() { 211 for (TemporalAccessor sample : samples()) { 212 try { 213 sample.getLong(null); 214 fail("Failed on " + sample); 215 } catch (NullPointerException ex) { 216 // expected 217 } 218 } 219 } 220 221 //----------------------------------------------------------------------- 222 @Test basicTest_query()223 public void basicTest_query() { 224 for (TemporalAccessor sample : samples()) { 225 assertEquals(sample.query(new TemporalQuery<String>() { 226 @Override 227 public String queryFrom(TemporalAccessor dateTime) { 228 return "foo"; 229 } 230 }), "foo"); 231 } 232 } 233 234 } 235