test/java/time/test/java/time/temporal/TestDateTimeBuilderCombinations.java

Print this page




  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.temporal;
  61 
  62 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
  63 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
  64 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
  65 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
  66 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
  67 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
  68 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
  69 import static java.time.temporal.ChronoField.EPOCH_DAY;
  70 import static java.time.temporal.ChronoField.EPOCH_MONTH;
  71 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  72 import static java.time.temporal.ChronoField.YEAR;
  73 import static org.testng.Assert.assertEquals;

  74 

  75 import java.time.LocalDate;
  76 import java.time.format.DateTimeBuilder;


  77 import java.time.temporal.TemporalField;
  78 
  79 import org.testng.annotations.DataProvider;
  80 import org.testng.annotations.Test;
  81 
  82 /**
  83  * Test.
  84  */
  85 public class TestDateTimeBuilderCombinations {
  86 
  87     @DataProvider(name = "combine")
  88     Object[][] data_combine() {
  89         return new Object[][] {
  90             {YEAR, 2012, MONTH_OF_YEAR, 6, DAY_OF_MONTH, 3, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  91             {EPOCH_MONTH, (2012 - 1970) * 12 + 6 - 1, DAY_OF_MONTH, 3, null, null, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  92             {YEAR, 2012, ALIGNED_WEEK_OF_YEAR, 6, DAY_OF_WEEK, 3, null, null, LocalDate.class, LocalDate.of(2012, 2, 8)},
  93             {YEAR, 2012, DAY_OF_YEAR, 155, null, null, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  94 //            {ERA, 1, YEAR_OF_ERA, 2012, DAY_OF_YEAR, 155, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  95             {YEAR, 2012, MONTH_OF_YEAR, 6, null, null, null, null, LocalDate.class, null},
  96             {EPOCH_DAY, 12, null, null, null, null, null, null, LocalDate.class, LocalDate.of(1970, 1, 13)},
  97         };
  98     }
  99 
 100     @Test(dataProvider = "combine")
 101     public void test_derive(TemporalField field1, Number value1, TemporalField field2, Number value2,
 102             TemporalField field3, Number value3, TemporalField field4, Number value4, Class<?> query, Object expectedVal) {
 103         DateTimeBuilder builder = new DateTimeBuilder(field1, value1.longValue());





























 104         if (field2 != null) {
 105             builder.addFieldValue(field2, value2.longValue());

 106         }
 107         if (field3 != null) {
 108             builder.addFieldValue(field3, value3.longValue());

 109         }
 110         if (field4 != null) {
 111             builder.addFieldValue(field4, value4.longValue());
















 112         }
 113         builder.resolve();
 114         assertEquals(builder.extract((Class<?>) query), expectedVal);
 115     }
 116 
 117     //-----------------------------------------------------------------------
 118     @DataProvider(name = "normalized")
 119     Object[][] data_normalized() {
 120         return new Object[][] {
 121             {YEAR, 2127, null, null, null, null, YEAR, 2127},
 122             {MONTH_OF_YEAR, 12, null, null, null, null, MONTH_OF_YEAR, 12},
 123             {DAY_OF_YEAR, 127, null, null, null, null, DAY_OF_YEAR, 127},
 124             {DAY_OF_MONTH, 23, null, null, null, null, DAY_OF_MONTH, 23},
 125             {DAY_OF_WEEK, 127, null, null, null, null, DAY_OF_WEEK, 127L},
 126             {ALIGNED_WEEK_OF_YEAR, 23, null, null, null, null, ALIGNED_WEEK_OF_YEAR, 23},
 127             {ALIGNED_DAY_OF_WEEK_IN_YEAR, 4, null, null, null, null, ALIGNED_DAY_OF_WEEK_IN_YEAR, 4L},
 128             {ALIGNED_WEEK_OF_MONTH, 4, null, null, null, null, ALIGNED_WEEK_OF_MONTH, 4},
 129             {ALIGNED_DAY_OF_WEEK_IN_MONTH, 3, null, null, null, null, ALIGNED_DAY_OF_WEEK_IN_MONTH, 3},
 130             {EPOCH_MONTH, 15, null, null, null, null, EPOCH_MONTH, null},
 131             {EPOCH_MONTH, 15, null, null, null, null, YEAR, 1971},
 132             {EPOCH_MONTH, 15, null, null, null, null, MONTH_OF_YEAR, 4},
 133         };
 134     }
 135 
 136     @Test(dataProvider = "normalized")
 137     public void test_normalized(TemporalField field1, Number value1, TemporalField field2, Number value2,
 138             TemporalField field3, Number value3, TemporalField query, Number expectedVal) {
 139         DateTimeBuilder builder = new DateTimeBuilder(field1, value1.longValue());
 140         if (field2 != null) {
 141             builder.addFieldValue(field2, value2.longValue());






 142         }
 143         if (field3 != null) {
 144             builder.addFieldValue(field3, value3.longValue());
 145         }
 146         builder.resolve();



 147         if (expectedVal != null) {
 148             assertEquals(builder.getLong(query), expectedVal.longValue());
 149         } else {
 150             assertEquals(builder.containsFieldValue(query), false);
 151         }
 152     }
 153 
 154     //-----------------------------------------------------------------------
 155     // TODO: maybe reinstate
 156 //    public void test_split() {
 157 //        DateTimeBuilder builder = new DateTimeBuilder();
 158 //        builder.addCalendrical(LocalDateTime.of(2012, 6, 30, 12, 30));
 159 //        builder.addCalendrical(ZoneOffset.ofHours(2));
 160 //        builder.resolve();
 161 //        assertEquals(builder.build(LocalDate.class), LocalDate.of(2012, 6, 30));
 162 //        assertEquals(builder.build(LocalTime.class), LocalTime.of(12, 30));
 163 //        assertEquals(builder.build(ZoneOffset.class), ZoneOffset.ofHours(2));
 164 //
 165 //        assertEquals(builder.build(LocalDateTime.class), LocalDateTime.of(2012, 6, 30, 12, 30));
 166 //        assertEquals(builder.build(OffsetDate.class), OffsetDate.of(LocalDate.of(2012, 6, 30), ZoneOffset.ofHours(2)));
 167 //        assertEquals(builder.build(OffsetTime.class), OffsetTime.of(LocalTime.of(12, 30), ZoneOffset.ofHours(2)));
 168 ////        assertEquals(builder.build(OffsetDateTime.class), OffsetDateTime.of(2012, 6, 30, 12, 30, ZoneOffset.ofHours(2)));
 169 //    }
 170 
 171 }


  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.temporal;
  61 
  62 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH;
  63 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR;
  64 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH;
  65 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR;
  66 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
  67 import static java.time.temporal.ChronoField.DAY_OF_WEEK;
  68 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
  69 import static java.time.temporal.ChronoField.EPOCH_DAY;
  70 import static java.time.temporal.ChronoField.EPOCH_MONTH;
  71 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
  72 import static java.time.temporal.ChronoField.YEAR;
  73 import static org.testng.Assert.assertEquals;
  74 import static org.testng.Assert.fail;
  75 
  76 import java.time.DateTimeException;
  77 import java.time.LocalDate;
  78 import java.time.format.DateTimeFormatter;
  79 import java.time.format.DateTimeFormatterBuilder;
  80 import java.time.temporal.TemporalAccessor;
  81 import java.time.temporal.TemporalField;
  82 
  83 import org.testng.annotations.DataProvider;
  84 import org.testng.annotations.Test;
  85 
  86 /**
  87  * Test.
  88  */
  89 public class TestDateTimeBuilderCombinations {
  90 
  91     @DataProvider(name = "combine")
  92     Object[][] data_combine() {
  93         return new Object[][] {
  94             {YEAR, 2012, MONTH_OF_YEAR, 6, DAY_OF_MONTH, 3, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  95             {EPOCH_MONTH, (2012 - 1970) * 12 + 6 - 1, DAY_OF_MONTH, 3, null, null, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  96             {YEAR, 2012, ALIGNED_WEEK_OF_YEAR, 6, DAY_OF_WEEK, 3, null, null, LocalDate.class, LocalDate.of(2012, 2, 8)},
  97             {YEAR, 2012, DAY_OF_YEAR, 155, null, null, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  98 //            {ERA, 1, YEAR_OF_ERA, 2012, DAY_OF_YEAR, 155, null, null, LocalDate.class, LocalDate.of(2012, 6, 3)},
  99             {YEAR, 2012, MONTH_OF_YEAR, 6, null, null, null, null, LocalDate.class, null},
 100             {EPOCH_DAY, 12, null, null, null, null, null, null, LocalDate.class, LocalDate.of(1970, 1, 13)},
 101         };
 102     }
 103 
 104     @Test(dataProvider = "combine")
 105     public void test_derive(final TemporalField field1, final Number value1,
 106                             final TemporalField field2, final Number value2,
 107                             final TemporalField field3, final Number value3,
 108                             final TemporalField field4, final Number value4,
 109                             Class<?> query, Object expectedVal) {
 110         // mock for testing that does not fully comply with TemporalAccessor contract
 111         TemporalAccessor test = new TemporalAccessor() {
 112             @Override
 113             public boolean isSupported(TemporalField field) {
 114                 return field == field1 || field == field2 || field == field3 || field == field4;
 115             }
 116             @Override
 117             public long getLong(TemporalField field) {
 118                 if (field == field1) {
 119                     return value1.longValue();
 120                 }
 121                 if (field == field2) {
 122                     return value2.longValue();
 123                 }
 124                 if (field == field3) {
 125                     return value3.longValue();
 126                 }
 127                 if (field == field4) {
 128                     return value4.longValue();
 129                 }
 130                 throw new DateTimeException("Unsupported");
 131             }
 132         };
 133         String str = "";
 134         DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
 135         dtfb.appendValue(field1).appendLiteral('-');
 136         str += value1 + "-";
 137         if (field2 != null) {
 138             dtfb.appendValue(field2).appendLiteral('-');
 139             str += value2 + "-";
 140         }
 141         if (field3 != null) {
 142             dtfb.appendValue(field3).appendLiteral('-');
 143             str += value3 + "-";
 144         }
 145         if (field4 != null) {
 146             dtfb.appendValue(field4).appendLiteral('-');
 147             str += value4 + "-";
 148         }
 149         TemporalAccessor parsed = dtfb.toFormatter().parse(str);
 150         if (query == LocalDate.class) {
 151             if (expectedVal != null) {
 152                 assertEquals(parsed.query(LocalDate::from), expectedVal);
 153             } else {
 154                 try {
 155                     parsed.query(LocalDate::from);
 156                     fail();
 157                 } catch (DateTimeException ex) {
 158                     // expected
 159                 }
 160             }
 161         } else {
 162             throw new IllegalArgumentException();
 163         }


 164     }
 165 
 166     //-----------------------------------------------------------------------
 167     @DataProvider(name = "normalized")
 168     Object[][] data_normalized() {
 169         return new Object[][] {
 170             {YEAR, 2127, YEAR, 2127},
 171             {MONTH_OF_YEAR, 12, MONTH_OF_YEAR, 12},
 172             {DAY_OF_YEAR, 127, DAY_OF_YEAR, 127},
 173             {DAY_OF_MONTH, 23, DAY_OF_MONTH, 23},
 174             {DAY_OF_WEEK, 127, DAY_OF_WEEK, 127L},
 175             {ALIGNED_WEEK_OF_YEAR, 23, ALIGNED_WEEK_OF_YEAR, 23},
 176             {ALIGNED_DAY_OF_WEEK_IN_YEAR, 4, ALIGNED_DAY_OF_WEEK_IN_YEAR, 4L},
 177             {ALIGNED_WEEK_OF_MONTH, 4, ALIGNED_WEEK_OF_MONTH, 4},
 178             {ALIGNED_DAY_OF_WEEK_IN_MONTH, 3, ALIGNED_DAY_OF_WEEK_IN_MONTH, 3},
 179             {EPOCH_MONTH, 15, EPOCH_MONTH, null},
 180             {EPOCH_MONTH, 15, YEAR, 1971},
 181             {EPOCH_MONTH, 15, MONTH_OF_YEAR, 4},
 182         };
 183     }
 184 
 185     @Test(dataProvider = "normalized")
 186     public void test_normalized(final TemporalField field1, final Number value1, TemporalField expectedField, Number expectedVal) {
 187         // mock for testing that does not fully comply with TemporalAccessor contract
 188         TemporalAccessor test = new TemporalAccessor() {
 189             @Override
 190             public boolean isSupported(TemporalField field) {
 191                 return field == field1;
 192             }
 193             @Override
 194             public long getLong(TemporalField field) {
 195                 if (field == field1) {
 196                     return value1.longValue();
 197                 }
 198                 throw new DateTimeException("Unsupported");

 199             }
 200         };
 201         DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field1).toFormatter();
 202         String str = value1.toString();
 203         TemporalAccessor temporal = f.parse(str);
 204         if (expectedVal != null) {
 205             assertEquals(temporal.getLong(expectedField), expectedVal.longValue());
 206         } else {
 207             assertEquals(temporal.isSupported(expectedField), false);
 208         }
 209     }
 210 

















 211 }