test/java/time/tck/java/time/TCKYearMonth.java

Print this page

        

@@ -55,11 +55,11 @@
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
-package tck.java.time.temporal;
+package tck.java.time;
 
 import static java.time.temporal.ChronoField.EPOCH_MONTH;
 import static java.time.temporal.ChronoField.ERA;
 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
 import static java.time.temporal.ChronoField.YEAR;

@@ -69,39 +69,40 @@
 import static org.testng.Assert.fail;
 
 import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
 import java.time.Clock;
 import java.time.DateTimeException;
 import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
 import java.time.Month;
+import java.time.Year;
+import java.time.YearMonth;
 import java.time.ZoneId;
 import java.time.ZoneOffset;
+import java.time.chrono.IsoChronology;
 import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeFormatters;
 import java.time.format.DateTimeParseException;
 import java.time.temporal.ChronoField;
+import java.time.temporal.ChronoUnit;
 import java.time.temporal.JulianFields;
+import java.time.temporal.Queries;
 import java.time.temporal.TemporalAccessor;
 import java.time.temporal.TemporalField;
-import java.time.temporal.Year;
-import java.time.temporal.YearMonth;
+import java.time.temporal.TemporalQuery;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
-import tck.java.time.AbstractDateTimeTest;
 
 /**
  * Test YearMonth.
  */
 @Test

@@ -151,11 +152,11 @@
 
     @Test
     public void test_serialization_format() throws Exception {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         try (DataOutputStream dos = new DataOutputStream(baos) ) {
-            dos.writeByte(5);
+            dos.writeByte(12);       // java.time.temporal.Ser.YEAR_MONTH_TYPE
             dos.writeInt(2012);
             dos.writeByte(9);
         }
         byte[] bytes = baos.toByteArray();
         assertSerializedBySer(YearMonth.of(2012, 9), bytes);

@@ -376,18 +377,18 @@
     //-----------------------------------------------------------------------
     // parse(DateTimeFormatter)
     //-----------------------------------------------------------------------
     @Test(groups={"tck"})
     public void factory_parse_formatter() {
-        DateTimeFormatter f = DateTimeFormatters.pattern("y M");
+        DateTimeFormatter f = DateTimeFormatter.ofPattern("y M");
         YearMonth test = YearMonth.parse("2010 12", f);
         assertEquals(test, YearMonth.of(2010, 12));
     }
 
     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
     public void factory_parse_formatter_nullText() {
-        DateTimeFormatter f = DateTimeFormatters.pattern("y M");
+        DateTimeFormatter f = DateTimeFormatter.ofPattern("y M");
         YearMonth.parse((String) null, f);
     }
 
     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
     public void factory_parse_formatter_nullFormatter() {

@@ -413,10 +414,41 @@
         assertEquals(TEST_2008_06.getLong(ChronoField.ERA), 1);
         assertEquals(TEST_2008_06.getLong(ChronoField.EPOCH_MONTH), (2008 - 1970) * 12 + 6 - 1);
     }
 
     //-----------------------------------------------------------------------
+    // query(TemporalQuery)
+    //-----------------------------------------------------------------------
+    @DataProvider(name="query")
+    Object[][] data_query() {
+        return new Object[][] {
+                {TEST_2008_06, Queries.chronology(), IsoChronology.INSTANCE},
+                {TEST_2008_06, Queries.zoneId(), null},
+                {TEST_2008_06, Queries.precision(), ChronoUnit.MONTHS},
+                {TEST_2008_06, Queries.zone(), null},
+                {TEST_2008_06, Queries.offset(), null},
+                {TEST_2008_06, Queries.localDate(), null},
+                {TEST_2008_06, Queries.localTime(), null},
+        };
+    }
+
+    @Test(dataProvider="query")
+    public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
+        assertEquals(temporal.query(query), expected);
+    }
+
+    @Test(dataProvider="query")
+    public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
+        assertEquals(query.queryFrom(temporal), expected);
+    }
+
+    @Test(expectedExceptions=NullPointerException.class)
+    public void test_query_null() {
+        TEST_2008_06.query(null);
+    }
+
+    //-----------------------------------------------------------------------
     // get*()
     //-----------------------------------------------------------------------
     @DataProvider(name="sampleDates")
     Object[][] provider_sampleDates() {
         return new Object[][] {

@@ -425,10 +457,18 @@
             {-1, 3},
             {0, 12},
         };
     }
 
+    @Test(dataProvider="sampleDates")
+    public void test_get(int y, int m) {
+        YearMonth a = YearMonth.of(y, m);
+        assertEquals(a.getYear(), y);
+        assertEquals(a.getMonth(), Month.of(m));
+        assertEquals(a.getMonthValue(), m);
+    }
+
     //-----------------------------------------------------------------------
     // with(Year)
     //-----------------------------------------------------------------------
     @Test(groups={"tck"})
     public void test_with_Year() {

@@ -868,20 +908,70 @@
     }
 
     //-----------------------------------------------------------------------
     // atDay(int)
     //-----------------------------------------------------------------------
-    @Test(groups={"tck"})
-    public void test_atDay_int() {
-        YearMonth test = YearMonth.of(2008, 6);
-        assertEquals(test.atDay(30), LocalDate.of(2008, 6, 30));
+    @DataProvider(name="atDay")
+    Object[][] data_atDay() {
+        return new Object[][] {
+                {YearMonth.of(2008, 6), 8, LocalDate.of(2008, 6, 8)},
+
+                {YearMonth.of(2008, 1), 31, LocalDate.of(2008, 1, 31)},
+                {YearMonth.of(2008, 2), 29, LocalDate.of(2008, 2, 29)},
+                {YearMonth.of(2008, 3), 31, LocalDate.of(2008, 3, 31)},
+                {YearMonth.of(2008, 4), 30, LocalDate.of(2008, 4, 30)},
+
+                {YearMonth.of(2009, 1), 32, null},
+                {YearMonth.of(2009, 1), 0, null},
+                {YearMonth.of(2009, 2), 29, null},
+                {YearMonth.of(2009, 2), 30, null},
+                {YearMonth.of(2009, 2), 31, null},
+                {YearMonth.of(2009, 4), 31, null},
+        };
     }
 
-    @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
-    public void test_atDay_int_invalidDay() {
-        YearMonth test = YearMonth.of(2008, 6);
-        test.atDay(31);
+    @Test(dataProvider="atDay")
+    public void test_atDay(YearMonth test, int day, LocalDate expected) {
+        if (expected != null) {
+            assertEquals(test.atDay(day), expected);
+        } else {
+            try {
+                test.atDay(day);
+                fail();
+            } catch (DateTimeException ex) {
+                // expected
+            }
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    // atEndOfMonth()
+    //-----------------------------------------------------------------------
+    @DataProvider(name="atEndOfMonth")
+    Object[][] data_atEndOfMonth() {
+        return new Object[][] {
+                {YearMonth.of(2008, 1), LocalDate.of(2008, 1, 31)},
+                {YearMonth.of(2008, 2), LocalDate.of(2008, 2, 29)},
+                {YearMonth.of(2008, 3), LocalDate.of(2008, 3, 31)},
+                {YearMonth.of(2008, 4), LocalDate.of(2008, 4, 30)},
+                {YearMonth.of(2008, 5), LocalDate.of(2008, 5, 31)},
+                {YearMonth.of(2008, 6), LocalDate.of(2008, 6, 30)},
+                {YearMonth.of(2008, 12), LocalDate.of(2008, 12, 31)},
+
+                {YearMonth.of(2009, 1), LocalDate.of(2009, 1, 31)},
+                {YearMonth.of(2009, 2), LocalDate.of(2009, 2, 28)},
+                {YearMonth.of(2009, 3), LocalDate.of(2009, 3, 31)},
+                {YearMonth.of(2009, 4), LocalDate.of(2009, 4, 30)},
+                {YearMonth.of(2009, 5), LocalDate.of(2009, 5, 31)},
+                {YearMonth.of(2009, 6), LocalDate.of(2009, 6, 30)},
+                {YearMonth.of(2009, 12), LocalDate.of(2009, 12, 31)},
+        };
+    }
+
+    @Test(dataProvider="atEndOfMonth")
+    public void test_atEndOfMonth(YearMonth test, LocalDate expected) {
+        assertEquals(test.atEndOfMonth(), expected);
     }
 
     //-----------------------------------------------------------------------
     // compareTo()
     //-----------------------------------------------------------------------

@@ -1031,11 +1121,11 @@
     //-----------------------------------------------------------------------
     // toString(DateTimeFormatter)
     //-----------------------------------------------------------------------
     @Test(groups={"tck"})
     public void test_toString_formatter() {
-        DateTimeFormatter f = DateTimeFormatters.pattern("y M");
+        DateTimeFormatter f = DateTimeFormatter.ofPattern("y M");
         String t = YearMonth.of(2010, 12).toString(f);
         assertEquals(t, "2010 12");
     }
 
     @Test(expectedExceptions=NullPointerException.class, groups={"tck"})