| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.types; |
| 34 | | | |
| 35 | | | import junit.framework.TestCase; |
| 36 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 37 | | | |
| 38 | | | |
| 39 | | | |
| 40 | | | @author |
| 41 | | | |
| 42 | 100 | | public class YearMonthTest |
| 43 | | | extends TestCase |
| 44 | | | { |
| 45 | | | |
| 46 | | | |
| 47 | | | public final void testFromStringPositive () |
| 48 | | | { |
| 49 | 100 | | testGood("2005-02Z", "2005-02Z"); |
| 50 | 100 | | testGood("2005-03UTC", "2005-03Z"); |
| 51 | 100 | | testGood("2005-04GMT", "2005-04Z"); |
| 52 | 100 | | testGood("-2005-02", "-2005-02Z"); |
| 53 | 100 | | testGood("2005-12", "2005-12Z"); |
| 54 | 100 | | testGood("12005-01", "12005-01Z"); |
| 55 | 100 | | testGood("0005-12", "0005-12Z"); |
| 56 | 100 | | testGood("-0005-12", "-0005-12Z"); |
| 57 | 100 | | } |
| 58 | | | |
| 59 | | | |
| 60 | | | public final void testFromStringNegative () |
| 61 | | | { |
| 62 | 100 | | testBad(null); |
| 63 | 100 | | testBad(""); |
| 64 | 100 | | testBad("2005"); |
| 65 | 100 | | testBad("5-02"); |
| 66 | 100 | | testBad("2005-2"); |
| 67 | 100 | | testBad("2005-2Z"); |
| 68 | 100 | | testBad("2005--2"); |
| 69 | 100 | | testBad("2005-02PST"); |
| 70 | 100 | | testBad("2005--02"); |
| 71 | 100 | | testBad("2005-+2"); |
| 72 | 100 | | testBad("0000-01"); |
| 73 | 100 | | } |
| 74 | | | |
| 75 | | | |
| 76 | | | |
| 77 | | | @throws |
| 78 | | | |
| 79 | | | public final void testToPeriod () |
| 80 | | | throws Exception |
| 81 | | | { |
| 82 | 100 | | final YearMonth ym = YearMonth.fromString("1999-04"); |
| 83 | 100 | | final Period p = ym.toPeriod(); |
| 84 | 100 | | assertEquals("Start date of period unexpected", |
| 85 | | | Date.fromString("1999-04-01T00:00:00Z"), p.getStartTime()); |
| 86 | 100 | | assertEquals("End date of period unexpected", |
| 87 | | | Date.fromString("1999-04-30T23:59:59.999Z"), p.getEndTime()); |
| 88 | | | |
| 89 | 100 | | final YearMonth ym2 = YearMonth.fromString("2000-02"); |
| 90 | 100 | | final Period p2 = ym2.toPeriod(); |
| 91 | 100 | | assertEquals("Start date of period unexpected (leep year)", |
| 92 | | | Date.fromString("2000-02-01T00:00:00Z"), p2.getStartTime()); |
| 93 | 100 | | assertEquals("End date of period unexpected (leep year)", |
| 94 | | | Date.fromString("2000-02-29T23:59:59.999Z"), p2.getEndTime()); |
| 95 | 100 | | } |
| 96 | | | |
| 97 | | | private void testGood (String str, String ref) |
| 98 | | | { |
| 99 | 100 | | assertEquals("String representation unexpected.", |
| 100 | | | ref, YearMonth.fromString(str).toString()); |
| 101 | 100 | | } |
| 102 | | | |
| 103 | | | private void testBad (String str) |
| 104 | | | { |
| 105 | | | try |
| 106 | | | { |
| 107 | 0 | | YearMonth.fromString(str); |
| 108 | 0 | | fail("YearMonth shold not be valid: '" + str + "'."); |
| 109 | | | } |
| 110 | 100 | | catch (ArgumentMalformedException ex) |
| 111 | | | { |
| 112 | | | |
| 113 | 0 | | } |
| 114 | 100 | | } |
| 115 | | | |
| 116 | | | } |