1
2
3
4
5
6
7
8
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.util.Date;
17 import java.util.TimeZone;
18
19 import org.junit.Test;
20
21 public class T0001_PersonIdentTest {
22
23 @Test
24 public void test001_NewIdent() {
25 final PersonIdent p = new PersonIdent("A U Thor", "author@example.com",
26 new Date(1142878501000L), TimeZone.getTimeZone("EST"));
27 assertEquals("A U Thor", p.getName());
28 assertEquals("author@example.com", p.getEmailAddress());
29 assertEquals(1142878501000L, p.getWhen().getTime());
30 assertEquals("A U Thor <author@example.com> 1142878501 -0500",
31 p.toExternalString());
32 }
33
34 @Test
35 public void test002_NewIdent() {
36 final PersonIdent p = new PersonIdent("A U Thor", "author@example.com",
37 new Date(1142878501000L), TimeZone.getTimeZone("GMT+0230"));
38 assertEquals("A U Thor", p.getName());
39 assertEquals("author@example.com", p.getEmailAddress());
40 assertEquals(1142878501000L, p.getWhen().getTime());
41 assertEquals("A U Thor <author@example.com> 1142878501 +0230",
42 p.toExternalString());
43 }
44
45 @SuppressWarnings("unused")
46 @Test(expected = IllegalArgumentException.class)
47 public void nullForNameShouldThrowIllegalArgumentException() {
48 new PersonIdent(null, "author@example.com");
49 }
50
51 @SuppressWarnings("unused")
52 @Test(expected = IllegalArgumentException.class)
53 public void nullForEmailShouldThrowIllegalArgumentException() {
54 new PersonIdent("A U Thor", null);
55 }
56
57 @Test
58 public void testToExternalStringTrimsNameAndEmail() throws Exception {
59 PersonIdent personIdent = new PersonIdent(" \u0010A U Thor ",
60 " author@example.com \u0009");
61
62 assertEquals(" \u0010A U Thor ", personIdent.getName());
63 assertEquals(" author@example.com \u0009", personIdent.getEmailAddress());
64
65 String externalString = personIdent.toExternalString();
66 assertTrue(externalString.startsWith("A U Thor <author@example.com>"));
67 }
68
69 @Test
70 public void testToExternalStringTrimsAllWhitespace() {
71 String ws = " \u0001 \n ";
72 PersonIdent personIdent = new PersonIdent(ws, ws);
73 assertEquals(ws, personIdent.getName());
74 assertEquals(ws, personIdent.getEmailAddress());
75
76 String externalString = personIdent.toExternalString();
77 assertTrue(externalString.startsWith(" <>"));
78 }
79
80 @Test
81 public void testToExternalStringTrimsOtherBadCharacters() {
82 String name = " Foo\r\n<Bar> ";
83 String email = " Baz>\n\u1234<Quux ";
84 PersonIdent personIdent = new PersonIdent(name, email);
85 assertEquals(name, personIdent.getName());
86 assertEquals(email, personIdent.getEmailAddress());
87
88 String externalString = personIdent.toExternalString();
89 assertTrue(externalString.startsWith("Foo\rBar <Baz\u1234Quux>"));
90 }
91
92 @Test
93 public void testEmptyNameAndEmail() {
94 PersonIdent personIdent = new PersonIdent("", "");
95 assertEquals("", personIdent.getName());
96 assertEquals("", personIdent.getEmailAddress());
97
98 String externalString = personIdent.toExternalString();
99 assertTrue(externalString.startsWith(" <>"));
100 }
101
102 @Test
103 public void testAppendSanitized() {
104 StringBuilder r = new StringBuilder();
105 PersonIdent.appendSanitized(r, " Baz>\n\u1234<Quux ");
106 assertEquals("Baz\u1234Quux", r.toString());
107 }
108 }
109