001package horstmann.ch03_junit; 002import junit.framework.TestCase; 003 004/** 005 A class to test the Day class with JUnit. 006 */ 007public class DayTest extends TestCase 008{ 009 /** 010 This test tests the addDays method with positive 011 parameters. 012 */ 013 public void testAdd() 014 { 015 for (int i = 1; i <= MAX_DAYS; i = i * INCREMENT) 016 { 017 Day d1 = new Day(1970, 1, 1); 018 Day d2 = d1.addDays(i); 019 assert d2.daysFrom(d1) == i; 020 } 021 } 022 023 /** 024 This test tests the addDays method with negative 025 parameters. 026 */ 027 public void testAddNegative() 028 { 029 for (int i = 1; i <= MAX_DAYS; i = i * INCREMENT) 030 { 031 Day d1 = new Day(1970, 1, 1); 032 Day d2 = d1.addDays(-i); 033 assert d1.daysFrom(d2) == i; 034 } 035 } 036 037 /** 038 This test tests the daysFrom method. 039 */ 040 public void testDaysFrom() 041 { 042 Day d1 = new Day(1970, 1, 1); 043 Day d2 = new Day(2001, 1, 1); 044 int n = d1.daysFrom(d2); 045 Day d3 = d2.addDays(n); 046 assert d1.getYear() == d3.getYear(); 047 assert d1.getMonth() == d3.getMonth(); 048 assert d1.getDate() == d3.getDate(); 049 } 050 051 /** 052 This test tests arithmetic around the Gregorian 053 calendar change. 054 */ 055 public void testGregorianBoundary() 056 { 057 Day d1 = new Day(1580, 1, 1); 058 Day d2 = d1.addDays(MAX_DAYS); 059 Day d3 = d2.addDays(-MAX_DAYS); 060 assert d3.daysFrom(d1) == 0; 061 } 062 063 private static final int MAX_DAYS = 10000; 064 private static final int INCREMENT = 10; 065} 066