View Javadoc
1   /*
2    * Copyright (C) 2012, Christian Halstrick and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.util;
11  
12  import static org.junit.Assert.fail;
13  
14  import java.text.ParseException;
15  import java.util.Calendar;
16  import java.util.GregorianCalendar;
17  
18  import org.eclipse.jgit.junit.MockSystemReader;
19  import org.junit.After;
20  import org.junit.Before;
21  import org.junit.experimental.theories.DataPoints;
22  import org.junit.experimental.theories.Theories;
23  import org.junit.experimental.theories.Theory;
24  import org.junit.runner.RunWith;
25  
26  /**
27   * Tests which assert that unparseable Strings lead to ParseExceptions
28   */
29  @RunWith(Theories.class)
30  public class GitDateParserBadlyFormattedTest {
31  	private String dateStr;
32  
33  	@Before
34  	public void setUp() {
35  		MockSystemReader mockSystemReader = new MockSystemReader();
36  		SystemReader.setInstance(mockSystemReader);
37  	}
38  
39  	@After
40  	public void tearDown() {
41  		SystemReader.setInstance(null);
42  	}
43  
44  	public GitDateParserBadlyFormattedTest(String dateStr) {
45  		this.dateStr = dateStr;
46  	}
47  
48  	@DataPoints
49  	public static String[] getDataPoints() {
50  		return new String[] { "", "1970", "3000.3000.3000", "3 yesterday ago",
51  				"now yesterday ago", "yesterdays", "3.day. 2.week.ago",
52  				"day ago", "Gra Feb 21 15:35:00 2007 +0100",
53  				"Sun Feb 21 15:35:00 2007 +0100",
54  				"Wed Feb 21 15:35:00 Grand +0100" };
55  	}
56  
57  	@Theory
58  	public void badlyFormattedWithExplicitRef() {
59  		Calendar ref = new GregorianCalendar(SystemReader.getInstance()
60  				.getTimeZone(), SystemReader.getInstance().getLocale());
61  		try {
62  			GitDateParser.parse(dateStr, ref, SystemReader.getInstance()
63  					.getLocale());
64  			fail("The expected ParseException while parsing '" + dateStr
65  					+ "' did not occur.");
66  		} catch (ParseException e) {
67  			// expected
68  		}
69  	}
70  
71  	@Theory
72  	public void badlyFormattedWithoutRef() {
73  		try {
74  			GitDateParser.parse(dateStr, null, SystemReader.getInstance()
75  					.getLocale());
76  			fail("The expected ParseException while parsing '" + dateStr
77  					+ "' did not occur.");
78  		} catch (ParseException e) {
79  			// expected
80  		}
81  	}
82  }