Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

T0001_PersonIdentTest.java 3.5KB

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