|
| 1 | +package com.baeldung.regexp.datepattern; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import com.baeldung.regexp.datepattern.DateMatcher; |
| 7 | +import com.baeldung.regexp.datepattern.GregorianDateMatcher; |
| 8 | + |
| 9 | +public class GregorianDateMatcherUnitTest { |
| 10 | + |
| 11 | + private DateMatcher matcher = new GregorianDateMatcher(); |
| 12 | + |
| 13 | + @Test |
| 14 | + public void whenDateHasValidFormat_thenPatternMatches() { |
| 15 | + Assert.assertTrue(matcher.match("2017-12-31")); |
| 16 | + Assert.assertTrue(matcher.match("2018-01-01")); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + public void whenDateHasInvalidFormat_thenNoMatch() { |
| 21 | + Assert.assertFalse(matcher.match("2018-01")); |
| 22 | + Assert.assertFalse(matcher.match("2018-01-01-01")); |
| 23 | + Assert.assertFalse(matcher.match("2018-01-XX")); |
| 24 | + Assert.assertFalse(matcher.match(" 2018-01-01")); |
| 25 | + Assert.assertFalse(matcher.match("2018-01-01 ")); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void whenDateIsInRestrictedRange_thenPatternMatches() { |
| 30 | + Assert.assertTrue(matcher.match("1900-01-01")); |
| 31 | + Assert.assertTrue(matcher.match("2205-05-25")); |
| 32 | + Assert.assertTrue(matcher.match("2999-12-31")); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void whenDateIsOutOfRestrictedRange_thenNoMatch() { |
| 37 | + Assert.assertFalse(matcher.match("1899-12-31")); |
| 38 | + Assert.assertFalse(matcher.match("3000-01-01")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void whenMonthIsJanMarMayJulAugOctDec_thenMonthHas31Days() { |
| 43 | + Assert.assertTrue(matcher.match("2018-01-31")); |
| 44 | + Assert.assertTrue(matcher.match("2018-03-31")); |
| 45 | + Assert.assertTrue(matcher.match("2018-05-31")); |
| 46 | + Assert.assertTrue(matcher.match("2018-07-31")); |
| 47 | + Assert.assertTrue(matcher.match("2018-08-31")); |
| 48 | + Assert.assertTrue(matcher.match("2018-10-31")); |
| 49 | + Assert.assertTrue(matcher.match("2018-12-31")); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void whenMonthIsFebAprJunSepNov_thenMonthHasNo31Days() { |
| 54 | + Assert.assertFalse(matcher.match("2018-02-31")); |
| 55 | + Assert.assertFalse(matcher.match("2018-04-31")); |
| 56 | + Assert.assertFalse(matcher.match("2018-06-31")); |
| 57 | + Assert.assertFalse(matcher.match("2018-09-31")); |
| 58 | + Assert.assertFalse(matcher.match("2018-11-31")); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void whenMonthIsFeb_thenMonthHasNo30Days() { |
| 63 | + Assert.assertFalse(matcher.match("2000-02-30")); |
| 64 | + Assert.assertFalse(matcher.match("2400-02-30")); |
| 65 | + Assert.assertFalse(matcher.match("2420-02-30")); |
| 66 | + Assert.assertFalse(matcher.match("2421-02-30")); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void whenYearIsLeap_thenFebHas29Days() { |
| 71 | + Assert.assertTrue(matcher.match("2000-02-29")); |
| 72 | + Assert.assertTrue(matcher.match("2400-02-29")); |
| 73 | + Assert.assertTrue(matcher.match("2800-02-29")); |
| 74 | + Assert.assertTrue(matcher.match("2004-02-29")); |
| 75 | + Assert.assertTrue(matcher.match("2008-02-29")); |
| 76 | + Assert.assertTrue(matcher.match("2012-02-29")); |
| 77 | + Assert.assertTrue(matcher.match("2016-02-29")); |
| 78 | + Assert.assertTrue(matcher.match("2020-02-29")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void whenYearIsNotLeap_thenFebHasNo29Days() { |
| 83 | + Assert.assertFalse(matcher.match("2017-02-29")); |
| 84 | + Assert.assertFalse(matcher.match("2018-02-29")); |
| 85 | + Assert.assertFalse(matcher.match("2019-02-29")); |
| 86 | + Assert.assertFalse(matcher.match("2100-02-29")); |
| 87 | + Assert.assertFalse(matcher.match("2200-02-29")); |
| 88 | + Assert.assertFalse(matcher.match("2300-02-29")); |
| 89 | + } |
| 90 | +} |
0 commit comments