Создание, анализ ирефакторинг
Скачать 3.16 Mb.
|
416 Приложение Б . org.jfree.date.SerialDate листинг Б .2 (продолжение) 253 * Количество високосных годов с 1900 до 1999 включительно равно 24. 254 */ 255 public void testLeapYearCount1999() { 256 assertEquals(SerialDate.leapYearCount(1999), 24); 257 } 258 259 /** 260 * Количество високосных годов с 1900 до 2000 включительно равно 25. 261 */ 262 public void testLeapYearCount2000() { 263 assertEquals(SerialDate.leapYearCount(2000), 25); 264 } 265 266 /** 267 * Сериализовать экземпляр, восстановить и проверить на равенство. 268 */ 269 public void testSerialization() { 270 271 SerialDate d1 = SerialDate.createInstance(15, 4, 2000); 272 SerialDate d2 = null; 273 274 try { 275 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 276 ObjectOutput out = new ObjectOutputStream(buffer); 277 out.writeObject(d1); 278 out.close(); 279 280 ObjectInput in = new ObjectInputStream( new ByteArrayInputStream(buffer.toByteArray())); 281 d2 = (SerialDate) in.readObject(); 282 in.close(); 283 } 284 catch (Exception e) { 285 System.out.println(e.toString()); 286 } 287 assertEquals(d1, d2); 288 289 } 290 291 /** 292 * Тест для ошибки по отчету 1096282 (исправлено). 293 */ 294 public void test1096282() { 295 SerialDate d = SerialDate.createInstance(29, 2, 2004); 296 d = SerialDate.addYears(1, d); 297 SerialDate expected = SerialDate.createInstance(28, 2, 2005); 298 assertTrue(d.isOn(expected)); 299 } 300 301 /** 416 org.jfree.date.SerialDate 417 302 * Различные тесты для метода addMonths(). 303 */ 304 public void testAddMonths() { 305 SerialDate d1 = SerialDate.createInstance(31, 5, 2004); 306 307 SerialDate d2 = SerialDate.addMonths(1, d1); 308 assertEquals(30, d2.getDayOfMonth()); 309 assertEquals(6, d2.getMonth()); 310 assertEquals(2004, d2.getYYYY()); 311 312 SerialDate d3 = SerialDate.addMonths(2, d1); 313 assertEquals(31, d3.getDayOfMonth()); 314 assertEquals(7, d3.getMonth()); 315 assertEquals(2004, d3.getYYYY()); 316 317 SerialDate d4 = SerialDate.addMonths(1, SerialDate.addMonths(1, d1)); 318 assertEquals(30, d4.getDayOfMonth()); 319 assertEquals(7, d4.getMonth()); 320 assertEquals(2004, d4.getYYYY()); 321 } 322 } листинг Б .3 . MonthConstants.java 1 /* ======================================================================== 2 * JCommon : библиотека классов общего назначения для платформы Java(tm) 3 * ======================================================================== 4 * 5 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. 6 * 7 * Информация о проекте: http://www.jfree.org/jcommon/index.html 8 * 9 * Библиотека распространяется бесплатно; вы можете свободно распространять 10 * и/или изменять ее на условиях лицензии Lesser General Public License 11 * в формулировке Free Software Foundation; либо версии 2.1 лицензии, либо 12 * (на ваше усмотрение) любой последующей версии. 13 * 14 * Библиотека распространяется в надежде, что она будет полезна, но 15 * БЕЗ КАКИХ-ЛИБО ГАРАНТИЙ, даже без подразумеваемой гарантии ПРИГОДНОСТИ 16 * для КОНКРЕТНОЙ ЦЕЛИ. За подробностями обращайтесь к GNU Lesser General 17 * Public License. 18 * 19 * Вы должны получить копию лицензии GNU Lesser General Public License 20 * с этой библиотекой; если этого не произошло, обратитесь в Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22 * USA. 23 * 24 * [Java является зарегистрированной торговой маркой Sun Microsystems, Inc. 25 * в Соединенных Штатах и других странах]. 26 * 27 * ------------------- 28 * MonthConstants.java продолжение 417 418 Приложение Б . org.jfree.date.SerialDate листинг Б .3 (продолжение) 29 * ------------------- 30 * (C) Copyright 2002, 2003, by Object Refinery Limited. 31 * 32 * Автор: Дэвид Гилберт (для Object Refinery Limited); 33 * Участники: -; 34 * 35 * $Id: MonthConstants.java,v 1.4 2005/11/16 15:58:40 taqua Exp $ 36 * 37 * Изменения 38 * ------- 39 * 29.05.2002 : Версия 1 (код перемещен из класса SerialDate) (DG); 40 * 41 */ 42 43 package org.jfree.date; 44 45 /** 46 * Константы месяцев. Обратите внимание: константы НЕ ЭКИВАЛЕНТНЫ определяемым 47 * в java.util.Calendar (где JANUARY=0, а DECEMBER=11). 48 * 49 * Используются классами SerialDate и RegularTimePeriod. 50 * 51 * @author Дэвид Гилберт 52 */ 53 public interface MonthConstants { 54 55 /** Константа для января. */ 56 public static final int JANUARY = 1; 57 58 /** Константа для февраля. */ 59 public static final int FEBRUARY = 2; 60 61 /** Константа для мая. */ 62 public static final int MARCH = 3; 63 64 /** Константа для апреля. */ 65 public static final int APRIL = 4; 66 67 /** Константа для мая. */ 68 public static final int MAY = 5; 69 70 /** Константа для июня. */ 71 public static final int JUNE = 6; 72 73 /** Константа для июля. */ 74 public static final int JULY = 7; 75 76 /** Константа для августа. */ 77 public static final int AUGUST = 8; 78 418 org.jfree.date.SerialDate 419 79 /** Константа для сентября. */ 80 public static final int SEPTEMBER = 9; 81 82 /** Константа для октября. */ 83 public static final int OCTOBER = 10; 84 85 /** Константа для ноября. */ 86 public static final int NOVEMBER = 11; 87 88 /** Константа для декабря. */ 89 public static final int DECEMBER = 12; 90 91 } листинг Б .4 . BobsSerialDateTest.java 1 package org.jfree.date.junit; 2 3 import junit.framework.TestCase; 4 import org.jfree.date.*; 5 import static org.jfree.date.SerialDate.*; 6 7 import java.util.*; 8 9 public class BobsSerialDateTest extends TestCase { 10 11 public void testIsValidWeekdayCode() throws Exception { 12 for (int day = 1; day <= 7; day++) 13 assertTrue(isValidWeekdayCode(day)); 14 assertFalse(isValidWeekdayCode(0)); 15 assertFalse(isValidWeekdayCode(8)); 16 } 17 18 public void testStringToWeekdayCode() throws Exception { 19 20 assertEquals(-1, stringToWeekdayCode("Hello")); 21 assertEquals(MONDAY, stringToWeekdayCode("Monday")); 22 assertEquals(MONDAY, stringToWeekdayCode("Mon")); 23 //todo assertEquals(MONDAY,stringToWeekdayCode("monday")); 24 // assertEquals(MONDAY,stringToWeekdayCode("MONDAY")); 25 // assertEquals(MONDAY, stringToWeekdayCode("mon")); 26 27 assertEquals(TUESDAY, stringToWeekdayCode("Tuesday")); 28 assertEquals(TUESDAY, stringToWeekdayCode("Tue")); 29 // assertEquals(TUESDAY,stringToWeekdayCode("tuesday")); 30 // assertEquals(TUESDAY,stringToWeekdayCode("TUESDAY")); 31 // assertEquals(TUESDAY, stringToWeekdayCode("tue")); 32 // assertEquals(TUESDAY, stringToWeekdayCode("tues")); 33 34 assertEquals(WEDNESDAY, stringToWeekdayCode("Wednesday")); 35 assertEquals(WEDNESDAY, stringToWeekdayCode("Wed")); 36 // assertEquals(WEDNESDAY,stringToWeekdayCode("wednesday")); продолжение 419 420 Приложение Б . org.jfree.date.SerialDate листинг Б .4 (продолжение) 37 // assertEquals(WEDNESDAY,stringToWeekdayCode("WEDNESDAY")); 38 // assertEquals(WEDNESDAY, stringToWeekdayCode("wed")); 39 40 assertEquals(THURSDAY, stringToWeekdayCode("Thursday")); 41 assertEquals(THURSDAY, stringToWeekdayCode("Thu")); 42 // assertEquals(THURSDAY,stringToWeekdayCode("thursday")); 43 // assertEquals(THURSDAY,stringToWeekdayCode("THURSDAY")); 44 // assertEquals(THURSDAY, stringToWeekdayCode("thu")); 45 // assertEquals(THURSDAY, stringToWeekdayCode("thurs")); 46 47 assertEquals(FRIDAY, stringToWeekdayCode("Friday")); 48 assertEquals(FRIDAY, stringToWeekdayCode("Fri")); 49 // assertEquals(FRIDAY,stringToWeekdayCode("friday")); 50 // assertEquals(FRIDAY,stringToWeekdayCode("FRIDAY")); 51 // assertEquals(FRIDAY, stringToWeekdayCode("fri")); 52 53 assertEquals(SATURDAY, stringToWeekdayCode("Saturday")); 54 assertEquals(SATURDAY, stringToWeekdayCode("Sat")); 55 // assertEquals(SATURDAY,stringToWeekdayCode("saturday")); 56 // assertEquals(SATURDAY,stringToWeekdayCode("SATURDAY")); 57 // assertEquals(SATURDAY, stringToWeekdayCode("sat")); 58 59 assertEquals(SUNDAY, stringToWeekdayCode("Sunday")); 60 assertEquals(SUNDAY, stringToWeekdayCode("Sun")); 61 // assertEquals(SUNDAY,stringToWeekdayCode("sunday")); 62 // assertEquals(SUNDAY,stringToWeekdayCode("SUNDAY")); 63 // assertEquals(SUNDAY, stringToWeekdayCode("sun")); 64 } 65 66 public void testWeekdayCodeToString() throws Exception { 67 assertEquals("Sunday", weekdayCodeToString(SUNDAY)); 68 assertEquals("Monday", weekdayCodeToString(MONDAY)); 69 assertEquals("Tuesday", weekdayCodeToString(TUESDAY)); 70 assertEquals("Wednesday", weekdayCodeToString(WEDNESDAY)); 71 assertEquals("Thursday", weekdayCodeToString(THURSDAY)); 72 assertEquals("Friday", weekdayCodeToString(FRIDAY)); 73 assertEquals("Saturday", weekdayCodeToString(SATURDAY)); 74 } 75 76 public void testIsValidMonthCode() throws Exception { 77 for (int i = 1; i <= 12; i++) 78 assertTrue(isValidMonthCode(i)); 79 assertFalse(isValidMonthCode(0)); 80 assertFalse(isValidMonthCode(13)); 81 } 82 83 public void testMonthToQuarter() throws Exception { 84 assertEquals(1, monthCodeToQuarter(JANUARY)); 85 assertEquals(1, monthCodeToQuarter(FEBRUARY)); 86 assertEquals(1, monthCodeToQuarter(MARCH)); 420 org.jfree.date.SerialDate 421 87 assertEquals(2, monthCodeToQuarter(APRIL)); 88 assertEquals(2, monthCodeToQuarter(MAY)); 89 assertEquals(2, monthCodeToQuarter(JUNE)); 90 assertEquals(3, monthCodeToQuarter(JULY)); 91 assertEquals(3, monthCodeToQuarter(AUGUST)); 92 assertEquals(3, monthCodeToQuarter(SEPTEMBER)); 93 assertEquals(4, monthCodeToQuarter(OCTOBER)); 94 assertEquals(4, monthCodeToQuarter(NOVEMBER)); 95 assertEquals(4, monthCodeToQuarter(DECEMBER)); 96 97 try { 98 monthCodeToQuarter(-1); 99 fail("Invalid Month Code should throw exception"); 100 } catch (IllegalArgumentException e) { 101 } 102 } 103 104 public void testMonthCodeToString() throws Exception { 105 assertEquals("January", monthCodeToString(JANUARY)); 106 assertEquals("February", monthCodeToString(FEBRUARY)); 107 assertEquals("March", monthCodeToString(MARCH)); 108 assertEquals("April", monthCodeToString(APRIL)); 109 assertEquals("May", monthCodeToString(MAY)); 110 assertEquals("June", monthCodeToString(JUNE)); 111 assertEquals("July", monthCodeToString(JULY)); 112 assertEquals("August", monthCodeToString(AUGUST)); 113 assertEquals("September", monthCodeToString(SEPTEMBER)); 114 assertEquals("October", monthCodeToString(OCTOBER)); 115 assertEquals("November", monthCodeToString(NOVEMBER)); 116 assertEquals("December", monthCodeToString(DECEMBER)); 117 118 assertEquals("Jan", monthCodeToString(JANUARY, true)); 119 assertEquals("Feb", monthCodeToString(FEBRUARY, true)); 120 assertEquals("Mar", monthCodeToString(MARCH, true)); 121 assertEquals("Apr", monthCodeToString(APRIL, true)); 122 assertEquals("May", monthCodeToString(MAY, true)); 123 assertEquals("Jun", monthCodeToString(JUNE, true)); 124 assertEquals("Jul", monthCodeToString(JULY, true)); 125 assertEquals("Aug", monthCodeToString(AUGUST, true)); 126 assertEquals("Sep", monthCodeToString(SEPTEMBER, true)); 127 assertEquals("Oct", monthCodeToString(OCTOBER, true)); 128 assertEquals("Nov", monthCodeToString(NOVEMBER, true)); 129 assertEquals("Dec", monthCodeToString(DECEMBER, true)); 130 131 try { 132 monthCodeToString(-1); 133 fail("Invalid month code should throw exception"); 134 } catch (IllegalArgumentException e) { 135 } 136 137 } продолжение 421 422 Приложение Б . org.jfree.date.SerialDate листинг Б .4 (продолжение) 138 139 public void testStringToMonthCode() throws Exception { 140 assertEquals(JANUARY,stringToMonthCode("1")); 141 assertEquals(FEBRUARY,stringToMonthCode("2")); 142 assertEquals(MARCH,stringToMonthCode("3")); 143 assertEquals(APRIL,stringToMonthCode("4")); 144 assertEquals(MAY,stringToMonthCode("5")); 145 assertEquals(JUNE,stringToMonthCode("6")); 146 assertEquals(JULY,stringToMonthCode("7")); 147 assertEquals(AUGUST,stringToMonthCode("8")); 148 assertEquals(SEPTEMBER,stringToMonthCode("9")); 149 assertEquals(OCTOBER,stringToMonthCode("10")); 150 assertEquals(NOVEMBER, stringToMonthCode("11")); 151 assertEquals(DECEMBER,stringToMonthCode("12")); 152 153 //todo assertEquals(-1, stringToMonthCode("0")); 154 // assertEquals(-1, stringToMonthCode("13")); 155 156 assertEquals(-1,stringToMonthCode("Hello")); 157 158 for (int m = 1; m <= 12; m++) { 159 assertEquals(m, stringToMonthCode(monthCodeToString(m, false))); 160 assertEquals(m, stringToMonthCode(monthCodeToString(m, true))); 161 } 162 163 // assertEquals(1,stringToMonthCode("jan")); 164 // assertEquals(2,stringToMonthCode("feb")); 165 // assertEquals(3,stringToMonthCode("mar")); 166 // assertEquals(4,stringToMonthCode("apr")); 167 // assertEquals(5,stringToMonthCode("may")); 168 // assertEquals(6,stringToMonthCode("jun")); 169 // assertEquals(7,stringToMonthCode("jul")); 170 // assertEquals(8,stringToMonthCode("aug")); 171 // assertEquals(9,stringToMonthCode("sep")); 172 // assertEquals(10,stringToMonthCode("oct")); 173 // assertEquals(11,stringToMonthCode("nov")); 174 // assertEquals(12,stringToMonthCode("dec")); 175 176 // assertEquals(1,stringToMonthCode("JAN")); 177 // assertEquals(2,stringToMonthCode("FEB")); 178 // assertEquals(3,stringToMonthCode("MAR")); 179 // assertEquals(4,stringToMonthCode("APR")); 180 // assertEquals(5,stringToMonthCode("MAY")); 181 // assertEquals(6,stringToMonthCode("JUN")); 182 // assertEquals(7,stringToMonthCode("JUL")); 183 // assertEquals(8,stringToMonthCode("AUG")); 184 // assertEquals(9,stringToMonthCode("SEP")); 185 // assertEquals(10,stringToMonthCode("OCT")); 186 // assertEquals(11,stringToMonthCode("NOV")); 187 // assertEquals(12,stringToMonthCode("DEC")); 422 org.jfree.date.SerialDate 423 188 189 // assertEquals(1,stringToMonthCode("january")); 190 // assertEquals(2,stringToMonthCode("february")); 191 // assertEquals(3,stringToMonthCode("march")); 192 // assertEquals(4,stringToMonthCode("april")); 193 // assertEquals(5,stringToMonthCode("may")); 194 // assertEquals(6,stringToMonthCode("june")); 195 // assertEquals(7,stringToMonthCode("july")); 196 // assertEquals(8,stringToMonthCode("august")); 197 // assertEquals(9,stringToMonthCode("september")); 198 // assertEquals(10,stringToMonthCode("october")); 199 // assertEquals(11,stringToMonthCode("november")); 200 // assertEquals(12,stringToMonthCode("december")); 201 202 // assertEquals(1,stringToMonthCode("JANUARY")); 203 // assertEquals(2,stringToMonthCode("FEBRUARY")); 204 // assertEquals(3,stringToMonthCode("MAR")); 205 // assertEquals(4,stringToMonthCode("APRIL")); 206 // assertEquals(5,stringToMonthCode("MAY")); 207 // assertEquals(6,stringToMonthCode("JUNE")); 208 // assertEquals(7,stringToMonthCode("JULY")); 209 // assertEquals(8,stringToMonthCode("AUGUST")); 210 // assertEquals(9,stringToMonthCode("SEPTEMBER")); 211 // assertEquals(10,stringToMonthCode("OCTOBER")); 212 // assertEquals(11,stringToMonthCode("NOVEMBER")); 213 // assertEquals(12,stringToMonthCode("DECEMBER")); 214 } 215 216 public void testIsValidWeekInMonthCode() throws Exception { 217 for (int w = 0; w <= 4; w++) { 218 assertTrue(isValidWeekInMonthCode(w)); 219 } 220 assertFalse(isValidWeekInMonthCode(5)); 221 } 222 223 public void testIsLeapYear() throws Exception { 224 assertFalse(isLeapYear(1900)); 225 assertFalse(isLeapYear(1901)); 226 assertFalse(isLeapYear(1902)); 227 assertFalse(isLeapYear(1903)); 228 assertTrue(isLeapYear(1904)); 229 assertTrue(isLeapYear(1908)); 230 assertFalse(isLeapYear(1955)); 231 assertTrue(isLeapYear(1964)); 232 assertTrue(isLeapYear(1980)); 233 assertTrue(isLeapYear(2000)); 234 assertFalse(isLeapYear(2001)); 235 assertFalse(isLeapYear(2100)); 236 } 237 238 public void testLeapYearCount() throws Exception { продолжение 423 424 Приложение Б . org.jfree.date.SerialDate листинг Б .4 (продолжение) 239 assertEquals(0, leapYearCount(1900)); 240 assertEquals(0, leapYearCount(1901)); 241 assertEquals(0, leapYearCount(1902)); 242 assertEquals(0, leapYearCount(1903)); 243 assertEquals(1, leapYearCount(1904)); 244 assertEquals(1, leapYearCount(1905)); 245 assertEquals(1, leapYearCount(1906)); 246 assertEquals(1, leapYearCount(1907)); 247 assertEquals(2, leapYearCount(1908)); 248 assertEquals(24, leapYearCount(1999)); 249 assertEquals(25, leapYearCount(2001)); 250 assertEquals(49, leapYearCount(2101)); 251 assertEquals(73, leapYearCount(2201)); 252 assertEquals(97, leapYearCount(2301)); 253 assertEquals(122, leapYearCount(2401)); 254 } 255 256 public void testLastDayOfMonth() throws Exception { 257 assertEquals(31, lastDayOfMonth(JANUARY, 1901)); 258 assertEquals(28, lastDayOfMonth(FEBRUARY, 1901)); 259 assertEquals(31, lastDayOfMonth(MARCH, 1901)); 260 assertEquals(30, lastDayOfMonth(APRIL, 1901)); 261 assertEquals(31, lastDayOfMonth(MAY, 1901)); 262 assertEquals(30, lastDayOfMonth(JUNE, 1901)); 263 assertEquals(31, lastDayOfMonth(JULY, 1901)); 264 assertEquals(31, lastDayOfMonth(AUGUST, 1901)); 265 assertEquals(30, lastDayOfMonth(SEPTEMBER, 1901)); 266 assertEquals(31, lastDayOfMonth(OCTOBER, 1901)); 267 assertEquals(30, lastDayOfMonth(NOVEMBER, 1901)); 268 assertEquals(31, lastDayOfMonth(DECEMBER, 1901)); 269 assertEquals(29, lastDayOfMonth(FEBRUARY, 1904)); 270 } 271 272 public void testAddDays() throws Exception { 273 SerialDate newYears = d(1, JANUARY, 1900); 274 assertEquals(d(2, JANUARY, 1900), addDays(1, newYears)); 275 assertEquals(d(1, FEBRUARY, 1900), addDays(31, newYears)); 276 assertEquals(d(1, JANUARY, 1901), addDays(365, newYears)); 277 assertEquals(d(31, DECEMBER, 1904), addDays(5 * 365, newYears)); 278 } 279 280 private static SpreadsheetDate d(int day, int month, int year) {return new SpreadsheetDate(day, month, year);} 281 282 public void testAddMonths() throws Exception { 283 assertEquals(d(1, FEBRUARY, 1900), addMonths(1, d(1, JANUARY, 1900))); 284 assertEquals(d(28, FEBRUARY, 1900), addMonths(1, d(31, JANUARY, 1900))); 285 assertEquals(d(28, FEBRUARY, 1900), addMonths(1, d(30, JANUARY, 1900))); 286 assertEquals(d(28, FEBRUARY, 1900), addMonths(1, d(29, JANUARY, 1900))); 287 assertEquals(d(28, FEBRUARY, 1900), addMonths(1, d(28, JANUARY, 1900))); 288 assertEquals(d(27, FEBRUARY, 1900), addMonths(1, d(27, JANUARY, 1900))); 289 290 assertEquals(d(30, JUNE, 1900), addMonths(5, d(31, JANUARY, 1900))); 424 |