View Javadoc
1   /*
2    * Copyright (C) 2010, Marc Strapetz <marc.strapetz@syntevo.com> 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  
11  package org.eclipse.jgit.util;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  import java.util.Date;
16  import java.util.TimeZone;
17  
18  import org.eclipse.jgit.lib.PersonIdent;
19  import org.junit.Test;
20  
21  public class RawParseUtils_ParsePersonIdentTest {
22  
23  	@Test
24  	public void testParsePersonIdent_legalCases() {
25  		final Date when = new Date(1234567890000l);
26  		final TimeZone tz = TimeZone.getTimeZone("GMT-7");
27  
28  		assertPersonIdent("Me <me@example.com> 1234567890 -0700",
29  				new PersonIdent("Me", "me@example.com", when, tz));
30  
31  		assertPersonIdent(" Me <me@example.com> 1234567890 -0700",
32  				new PersonIdent(" Me", "me@example.com", when, tz));
33  
34  		assertPersonIdent("A U Thor <author@example.com> 1234567890 -0700",
35  				new PersonIdent("A U Thor", "author@example.com", when, tz));
36  
37  		assertPersonIdent("A U Thor<author@example.com> 1234567890 -0700",
38  				new PersonIdent("A U Thor", "author@example.com", when, tz));
39  
40  		assertPersonIdent("A U Thor<author@example.com>1234567890 -0700",
41  				new PersonIdent("A U Thor", "author@example.com", when, tz));
42  
43  		assertPersonIdent(
44  				" A U Thor   < author@example.com > 1234567890 -0700",
45  				new PersonIdent(" A U Thor  ", " author@example.com ", when, tz));
46  
47  		assertPersonIdent("A U Thor<author@example.com>1234567890 -0700",
48  				new PersonIdent("A U Thor", "author@example.com", when, tz));
49  	}
50  
51  	@Test
52  	public void testParsePersonIdent_fuzzyCases() {
53  		final Date when = new Date(1234567890000l);
54  		final TimeZone tz = TimeZone.getTimeZone("GMT-7");
55  
56  		assertPersonIdent(
57  				"A U Thor <author@example.com>,  C O. Miter <comiter@example.com> 1234567890 -0700",
58  				new PersonIdent("A U Thor", "author@example.com", when, tz));
59  
60  		assertPersonIdent(
61  				"A U Thor <author@example.com> and others 1234567890 -0700",
62  				new PersonIdent("A U Thor", "author@example.com", when, tz));
63  	}
64  
65  	@Test
66  	public void testParsePersonIdent_incompleteCases() {
67  		final Date when = new Date(1234567890000l);
68  		final TimeZone tz = TimeZone.getTimeZone("GMT-7");
69  
70  		assertPersonIdent("Me <> 1234567890 -0700", new PersonIdent("Me", "",
71  				when, tz));
72  
73  		assertPersonIdent(" <me@example.com> 1234567890 -0700",
74  				new PersonIdent("", "me@example.com", when, tz));
75  
76  		assertPersonIdent(" <> 1234567890 -0700", new PersonIdent("", "", when,
77  				tz));
78  
79  		assertPersonIdent("<>", new PersonIdent("", "", 0, 0));
80  
81  		assertPersonIdent(" <>", new PersonIdent("", "", 0, 0));
82  
83  		assertPersonIdent("<me@example.com>", new PersonIdent("",
84  				"me@example.com", 0, 0));
85  
86  		assertPersonIdent(" <me@example.com>", new PersonIdent("",
87  				"me@example.com", 0, 0));
88  
89  		assertPersonIdent("Me <>", new PersonIdent("Me", "", 0, 0));
90  
91  		assertPersonIdent("Me <me@example.com>", new PersonIdent("Me",
92  				"me@example.com", 0, 0));
93  
94  		assertPersonIdent("Me <me@example.com> 1234567890", new PersonIdent(
95  				"Me", "me@example.com", 0, 0));
96  
97  		assertPersonIdent("Me <me@example.com> 1234567890 ", new PersonIdent(
98  				"Me", "me@example.com", 0, 0));
99  	}
100 
101 	@Test
102 	public void testParsePersonIdent_malformedCases() {
103 		assertPersonIdent("Me me@example.com> 1234567890 -0700", null);
104 		assertPersonIdent("Me <me@example.com 1234567890 -0700", null);
105 	}
106 
107 	private static void assertPersonIdent(String line, PersonIdent expected) {
108 		PersonIdent actual = RawParseUtils.parsePersonIdent(line);
109 		assertEquals(expected, actual);
110 	}
111 }