22
33import static org .junit .Assert .*;
44
5+ import java .time .format .DateTimeFormatter ;
56import java .util .Calendar ;
67import java .util .Date ;
78import java .util .GregorianCalendar ;
@@ -51,7 +52,6 @@ public void test_Getting_Calendar_information() {
5152 assertTrue (31 == calendar .getMaximum (calendar .DAY_OF_MONTH ));
5253 assertTrue (1 == calendar .getMinimum (calendar .DAY_OF_MONTH ));
5354 assertTrue (52 == calendar .getWeeksInWeekYear ());
54-
5555 }
5656
5757 @ Test
@@ -60,16 +60,29 @@ public void test_Compare_Date_FirstDate_Greater_SecondDate() {
6060 GregorianCalendar firstDate = new GregorianCalendar (2018 , 6 , 28 );
6161 GregorianCalendar secondDate = new GregorianCalendar (2018 , 5 , 28 );
6262 assertTrue (1 == calendarDemo .compareDates (firstDate , secondDate ));
63-
6463 }
64+
65+ @ Test
66+ public void test_CompareFirstDateGreaterSecondDate () {
67+ GregorianCalendar firstDate = new GregorianCalendar (2018 , 6 , 28 );
68+ GregorianCalendar secondDate = new GregorianCalendar (2018 , 5 , 28 );
69+ assertTrue (1 == firstDate .compareTo (secondDate ));
70+ }
71+
6572
6673 @ Test
6774 public void test_Compare_Date_FirstDate_Smaller_SecondDate () {
6875 GregorianCalendarExample calendarDemo = new GregorianCalendarExample ();
6976 GregorianCalendar firstDate = new GregorianCalendar (2018 , 5 , 28 );
7077 GregorianCalendar secondDate = new GregorianCalendar (2018 , 6 , 28 );
7178 assertTrue (-1 == calendarDemo .compareDates (firstDate , secondDate ));
72-
79+ }
80+
81+ @ Test
82+ public void test_CompareFirstDateSmallerSecondDate () {
83+ GregorianCalendar firstDate = new GregorianCalendar (2018 , 5 , 28 );
84+ GregorianCalendar secondDate = new GregorianCalendar (2018 , 6 , 28 );
85+ assertTrue (-1 == firstDate .compareTo (secondDate ));
7386 }
7487
7588 @ Test
@@ -78,7 +91,13 @@ public void test_Compare_Date_Both_Dates_Equal() {
7891 GregorianCalendar firstDate = new GregorianCalendar (2018 , 6 , 28 );
7992 GregorianCalendar secondDate = new GregorianCalendar (2018 , 6 , 28 );
8093 assertTrue (0 == calendarDemo .compareDates (firstDate , secondDate ));
81-
94+ }
95+
96+ @ Test
97+ public void test_CompareDatesEqual () {
98+ GregorianCalendar firstDate = new GregorianCalendar (2018 , 6 , 28 );
99+ GregorianCalendar secondDate = new GregorianCalendar (2018 , 6 , 28 );
100+ assertTrue (0 == firstDate .compareTo (secondDate ));
82101 }
83102
84103 @ Test
@@ -88,6 +107,13 @@ public void test_date_format() {
88107 assertEquals ("28 Jul 2018" , calendarDemo .formatDate (calendar ));
89108 }
90109
110+ @ Test
111+ public void test_dateFormatdMMMuuuu () {
112+ String expectedDate = new GregorianCalendar (2018 , 6 , 28 ).toZonedDateTime ()
113+ .format (DateTimeFormatter .ofPattern ("d MMM uuuu" ));
114+ assertEquals ("28 Jul 2018" , expectedDate );
115+ }
116+
91117 @ Test
92118 public void test_addDays () {
93119 GregorianCalendarExample calendarDemo = new GregorianCalendarExample ();
@@ -96,7 +122,22 @@ public void test_addDays() {
96122 calendarExpected .add (Calendar .DATE , 1 );
97123 Date expectedDate = calendarExpected .getTime ();
98124 assertEquals (expectedDate , calendarDemo .addDays (calendarActual , 1 ));
125+ }
99126
127+ @ Test
128+ public void test_addOneDay () {
129+ final int finalDay29 = 29 ;
130+ GregorianCalendar calendarExpected = new GregorianCalendar (2018 , 6 , 28 );
131+ calendarExpected .add (Calendar .DATE , 1 );
132+ assertEquals (calendarExpected .get (Calendar .DATE ), finalDay29 );
133+ }
134+
135+ @ Test
136+ public void test_subtractOneDay () {
137+ final int finalDay27 = 27 ;
138+ GregorianCalendar calendarExpected = new GregorianCalendar (2018 , 6 , 28 );
139+ calendarExpected .add (Calendar .DATE , -1 );
140+ assertEquals (calendarExpected .get (Calendar .DATE ), finalDay27 );
100141 }
101142
102143 @ Test
@@ -107,7 +148,6 @@ public void test_subDays() {
107148 calendarExpected .add (Calendar .DATE , -1 );
108149 Date expectedDate = calendarExpected .getTime ();
109150 assertEquals (expectedDate , calendarDemo .subtractDays (calendarActual , 1 ));
110-
111151 }
112152
113153 @ Test
@@ -120,6 +160,24 @@ public void test_rollAdd() {
120160 assertEquals (expectedDate , calendarDemo .rollAdd (calendarActual , 8 ));
121161 }
122162
163+ @ Test
164+ public void test_rollAddEightMonths () {
165+ final int rolledUpMonthMarch = 2 , orginalYear2018 = 2018 ;
166+ GregorianCalendar calendarExpected = new GregorianCalendar (2018 , 6 , 28 );
167+ calendarExpected .roll (Calendar .MONTH , 8 );
168+ assertEquals (calendarExpected .get (Calendar .MONTH ), rolledUpMonthMarch );
169+ assertEquals (calendarExpected .get (Calendar .YEAR ), orginalYear2018 );
170+ }
171+
172+ @ Test
173+ public void test_rollSubstractEightMonths () {
174+ final int rolledDownMonthNovember = 10 , orginalYear2018 = 2018 ;
175+ GregorianCalendar calendarExpected = new GregorianCalendar (2018 , 6 , 28 );
176+ calendarExpected .roll (Calendar .MONTH , -8 );
177+ assertEquals (calendarExpected .get (Calendar .MONTH ), rolledDownMonthNovember );
178+ assertEquals (calendarExpected .get (Calendar .YEAR ), orginalYear2018 );
179+ }
180+
123181 @ Test
124182 public void test_rollSubtract () {
125183 GregorianCalendarExample calendarDemo = new GregorianCalendarExample ();
@@ -138,7 +196,16 @@ public void test_setMonth() {
138196 calendarExpected .set (Calendar .MONTH , 3 );
139197 Date expectedDate = calendarExpected .getTime ();
140198 assertEquals (expectedDate , calendarDemo .setMonth (calendarActual , 3 ));
199+ }
141200
201+ @ Test
202+ public void test_setMonthApril () {
203+ final int setMonthApril = 3 , orginalYear2018 = 2018 , originalDate28 = 28 ;
204+ GregorianCalendar calendarExpected = new GregorianCalendar (2018 , 6 , 28 );
205+ calendarExpected .set (Calendar .MONTH , 3 );
206+ assertEquals (calendarExpected .get (Calendar .MONTH ), setMonthApril );
207+ assertEquals (calendarExpected .get (Calendar .YEAR ), orginalYear2018 );
208+ assertEquals (calendarExpected .get (Calendar .DATE ), originalDate28 );
142209 }
143210
144211 @ Test
@@ -149,7 +216,6 @@ public void test_toXMLGregorianCalendar() throws DatatypeConfigurationException
149216 GregorianCalendar calendarExpected = new GregorianCalendar (2018 , 6 , 28 );
150217 XMLGregorianCalendar expectedXMLGregorianCalendar = datatypeFactory .newXMLGregorianCalendar (calendarExpected );
151218 assertEquals (expectedXMLGregorianCalendar , calendarDemo .toXMLGregorianCalendar (calendarActual ));
152-
153219 }
154220
155221 @ Test
@@ -165,4 +231,13 @@ public void test_isLeapYear_False() {
165231 assertEquals (false , calendarDemo .isLeapYearExample (2018 ));
166232
167233 }
234+
235+ @ Test
236+ public void test_toDate () throws DatatypeConfigurationException {
237+ GregorianCalendar calendarActual = new GregorianCalendar (2018 , 6 , 28 );
238+ DatatypeFactory datatypeFactory = DatatypeFactory .newInstance ();
239+ XMLGregorianCalendar expectedXMLGregorianCalendar = datatypeFactory .newXMLGregorianCalendar (calendarActual );
240+ expectedXMLGregorianCalendar .toGregorianCalendar ().getTime ();
241+ assertEquals (calendarActual .getTime (), expectedXMLGregorianCalendar .toGregorianCalendar ().getTime () );
242+ }
168243}
0 commit comments