View Javadoc
1   /*
2    * Copyright (C) 2006-2007, Shawn O. Pearce <spearce@spearce.org> 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.lib;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.time.Instant;
17  import java.time.ZoneId;
18  import java.util.Date;
19  import java.util.TimeZone;
20  
21  import org.junit.Test;
22  
23  public class PersonIdentTest {
24  
25  	@Test
26  	public void test001_NewIdent() {
27  		final PersonIdent p = new PersonIdent("A U Thor", "author@example.com",
28  				new Date(1142878501000L), TimeZone.getTimeZone("EST"));
29  		assertEquals("A U Thor", p.getName());
30  		assertEquals("author@example.com", p.getEmailAddress());
31  		assertEquals(1142878501000L, p.getWhen().getTime());
32  		assertEquals("A U Thor <author@example.com> 1142878501 -0500",
33  				p.toExternalString());
34  	}
35  
36  	@Test
37  	public void test002_NewIdent() {
38  		final PersonIdent p = new PersonIdent("A U Thor", "author@example.com",
39  				new Date(1142878501000L), TimeZone.getTimeZone("GMT+0230"));
40  		assertEquals("A U Thor", p.getName());
41  		assertEquals("author@example.com", p.getEmailAddress());
42  		assertEquals(1142878501000L, p.getWhen().getTime());
43  		assertEquals("A U Thor <author@example.com> 1142878501 +0230",
44  				p.toExternalString());
45  	}
46  
47  	@Test
48  	public void testNewIdentInstant() {
49  		PersonIdent p = new PersonIdent("A U Thor", "author@example.com",
50  				Instant.ofEpochMilli(1142878501000L),
51  				ZoneId.of("America/New_York"));
52  		assertEquals("A U Thor", p.getName());
53  		assertEquals("author@example.com", p.getEmailAddress());
54  		assertEquals(Instant.ofEpochMilli(1142878501000L),
55  				p.getWhenAsInstant());
56  		assertEquals("A U Thor <author@example.com> 1142878501 -0500",
57  				p.toExternalString());
58  		assertEquals(ZoneId.of("GMT-05:00"), p.getZoneId());
59  	}
60  
61  	@Test
62  	public void testNewIdentInstant2() {
63  		final PersonIdent p = new PersonIdent("A U Thor", "author@example.com",
64  				Instant.ofEpochMilli(1142878501000L),
65  				ZoneId.of("Asia/Kolkata"));
66  		assertEquals("A U Thor", p.getName());
67  		assertEquals("author@example.com", p.getEmailAddress());
68  		assertEquals(Instant.ofEpochMilli(1142878501000L),
69  				p.getWhenAsInstant());
70  		assertEquals("A U Thor <author@example.com> 1142878501 +0530",
71  				p.toExternalString());
72  		assertEquals(ZoneId.of("GMT+05:30"), p.getZoneId());
73  	}
74  
75  	@SuppressWarnings("unused")
76  	@Test(expected = IllegalArgumentException.class)
77  	public void nullForNameShouldThrowIllegalArgumentException() {
78  		new PersonIdent(null, "author@example.com");
79  	}
80  
81  	@SuppressWarnings("unused")
82  	@Test(expected = IllegalArgumentException.class)
83  	public void nullForEmailShouldThrowIllegalArgumentException() {
84  		new PersonIdent("A U Thor", null);
85  	}
86  
87  	@Test
88  	public void testToExternalStringTrimsNameAndEmail() throws Exception {
89  		PersonIdent personIdent = new PersonIdent(" \u0010A U Thor  ",
90  				"  author@example.com \u0009");
91  
92  		assertEquals(" \u0010A U Thor  ", personIdent.getName());
93  		assertEquals("  author@example.com \u0009", personIdent.getEmailAddress());
94  
95  		String externalString = personIdent.toExternalString();
96  		assertTrue(externalString.startsWith("A U Thor <author@example.com>"));
97  	}
98  
99  	@Test
100 	public void testToExternalStringTrimsAllWhitespace() {
101 		String ws = "  \u0001 \n ";
102 		PersonIdent personIdent = new PersonIdent(ws, ws);
103 		assertEquals(ws, personIdent.getName());
104 		assertEquals(ws, personIdent.getEmailAddress());
105 
106 		String externalString = personIdent.toExternalString();
107 		assertTrue(externalString.startsWith(" <>"));
108 	}
109 
110 	@Test
111 	public void testToExternalStringTrimsOtherBadCharacters() {
112 		String name = " Foo\r\n<Bar> ";
113 		String email = " Baz>\n\u1234<Quux ";
114 		PersonIdent personIdent = new PersonIdent(name, email);
115 		assertEquals(name, personIdent.getName());
116 		assertEquals(email, personIdent.getEmailAddress());
117 
118 		String externalString = personIdent.toExternalString();
119 		assertTrue(externalString.startsWith("Foo\rBar <Baz\u1234Quux>"));
120 	}
121 
122 	@Test
123 	public void testEmptyNameAndEmail() {
124 		PersonIdent personIdent = new PersonIdent("", "");
125 		assertEquals("", personIdent.getName());
126 		assertEquals("", personIdent.getEmailAddress());
127 
128 		String externalString = personIdent.toExternalString();
129 		assertTrue(externalString.startsWith(" <>"));
130 	}
131 
132 	@Test
133 	public void testAppendSanitized() {
134 		StringBuilder r = new StringBuilder();
135 		PersonIdent.appendSanitized(r, " Baz>\n\u1234<Quux ");
136 		assertEquals("Baz\u1234Quux", r.toString());
137 	}
138 }
139