1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-26 05:24:49 +02:00
pgpainless/pgpainless-core/src/test/java/org/pgpainless/key/UserIdTest.java

210 lines
7.8 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>, 2021 Flowcrypt a.s.
//
// SPDX-License-Identifier: Apache-2.0
2020-11-13 15:59:28 +01:00
package org.pgpainless.key;
2020-11-13 16:31:59 +01:00
import org.junit.jupiter.api.Test;
2020-11-13 15:59:28 +01:00
import org.pgpainless.key.util.UserId;
2021-02-21 14:11:09 +01:00
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
2020-11-13 15:59:28 +01:00
public class UserIdTest {
2020-11-13 16:31:59 +01:00
@Test
2020-11-13 15:59:28 +01:00
public void throwForNullName() {
2021-02-21 14:11:09 +01:00
assertThrows(IllegalArgumentException.class, () -> UserId.newBuilder().withName(null));
2020-11-13 15:59:28 +01:00
}
2020-11-13 16:31:59 +01:00
@Test
2020-11-13 15:59:28 +01:00
public void throwForNullComment() {
2021-02-21 14:11:09 +01:00
assertThrows(IllegalArgumentException.class, () -> UserId.newBuilder().withComment(null));
2020-11-13 15:59:28 +01:00
}
2020-11-13 16:31:59 +01:00
@Test
2020-11-13 15:59:28 +01:00
public void throwForNullEmail() {
2021-02-21 14:11:09 +01:00
assertThrows(IllegalArgumentException.class, () -> UserId.newBuilder().withEmail(null));
2020-11-13 15:59:28 +01:00
}
@Test
public void testFormatOnlyName() {
assertEquals(
"Juliet Capulet",
2021-02-21 14:11:09 +01:00
UserId.newBuilder().withName("Juliet Capulet")
2020-11-13 15:59:28 +01:00
.build().toString());
}
@Test
public void testFormatNameAndComment() {
assertEquals(
"Juliet Capulet (from the play)",
2021-02-21 14:11:09 +01:00
UserId.newBuilder().withName("Juliet Capulet")
2020-11-13 15:59:28 +01:00
.withComment("from the play")
2021-02-21 14:11:09 +01:00
.noEmail().build().toString());
2020-11-13 15:59:28 +01:00
}
@Test
public void testFormatNameCommentAndMail() {
assertEquals("Juliet Capulet (from the play) <juliet@capulet.lit>",
2021-02-21 14:11:09 +01:00
UserId.newBuilder().withName("Juliet Capulet")
2020-11-13 15:59:28 +01:00
.withComment("from the play")
.withEmail("juliet@capulet.lit")
2021-02-21 14:11:09 +01:00
.build()
2020-11-13 15:59:28 +01:00
.toString());
}
@Test
public void testFormatNameAndEmail() {
assertEquals("Juliet Capulet <juliet@capulet.lit>",
2021-02-21 14:11:09 +01:00
UserId.newBuilder().withName("Juliet Capulet")
2020-11-13 15:59:28 +01:00
.noComment()
.withEmail("juliet@capulet.lit")
2021-02-21 14:11:09 +01:00
.build()
2020-11-13 15:59:28 +01:00
.toString());
}
2021-01-21 14:52:11 +01:00
@Test
public void throwIfOnlyEmailEmailNull() {
assertThrows(IllegalArgumentException.class, () -> UserId.onlyEmail(null));
}
@Test
public void testNameAndEmail() {
UserId userId = UserId.nameAndEmail("Maurice Moss", "moss.m@reynholm.co.uk");
assertEquals("Maurice Moss <moss.m@reynholm.co.uk>", userId.toString());
}
2021-02-21 14:11:09 +01:00
@Test
void testBuilderWithName() {
final UserId userId = UserId.newBuilder().withName("John Smith").build();
assertEquals("John Smith", userId.getName());
assertNull(userId.getComment());
assertNull(userId.getEmail());
}
@Test
void testBuilderWithComment() {
final UserId userId = UserId.newBuilder().withComment("Sales Dept.").build();
assertNull(userId.getName());
assertEquals("Sales Dept.", userId.getComment());
assertNull(userId.getEmail());
}
@Test
void testBuilderWithEmail() {
final UserId userId = UserId.newBuilder().withEmail("john.smith@example.com").build();
assertNull(userId.getName());
assertNull(userId.getComment());
assertEquals("john.smith@example.com", userId.getEmail());
}
@Test
void testBuilderWithAll() {
final UserId userId = UserId.newBuilder().withEmail("john.smith@example.com")
.withName("John Smith")
.withEmail("john.smith@example.com")
.withComment("Sales Dept.").build();
assertEquals("John Smith", userId.getName());
assertEquals("Sales Dept.", userId.getComment());
assertEquals("john.smith@example.com", userId.getEmail());
}
@Test
void testBuilderNoName() {
final UserId.Builder builder = UserId.newBuilder()
.withEmail("john.smith@example.com")
.withName("John Smith")
.withComment("Sales Dept.").build().toBuilder();
final UserId userId = builder.noName().build();
assertNull(userId.getName());
assertEquals("Sales Dept.", userId.getComment());
assertEquals("john.smith@example.com", userId.getEmail());
}
@Test
void testBuilderNoComment() {
final UserId.Builder builder = UserId.newBuilder()
.withEmail("john.smith@example.com")
.withName("John Smith")
.withComment("Sales Dept.").build().toBuilder();
final UserId userId = builder.noComment().build();
assertEquals("John Smith", userId.getName());
assertNull(userId.getComment());
assertEquals("john.smith@example.com", userId.getEmail());
}
@Test
void testBuilderNoEmail() {
final UserId.Builder builder = UserId.newBuilder()
.withEmail("john.smith@example.com")
.withName("John Smith")
.withComment("Sales Dept.").build().toBuilder();
final UserId userId = builder.noEmail().build();
assertEquals("John Smith", userId.getName());
assertEquals("Sales Dept.", userId.getComment());
assertNull(userId.getEmail());
}
@Test
void testEmailOnlyFormatting() {
final UserId userId = UserId.onlyEmail("john.smith@example.com");
assertEquals("john.smith@example.com", userId.toString());
}
@Test
void testEmptyNameAndValidEmailFormatting() {
final UserId userId = UserId.nameAndEmail("", "john.smith@example.com");
assertEquals("john.smith@example.com", userId.toString());
assertEquals("john.smith@example.com", userId.asString(false));
assertEquals("john.smith@example.com", userId.asString(true));
}
@Test
void testEmptyNameAndEmptyCommentAndValidEmailFormatting() {
final UserId userId = UserId.newBuilder()
.withComment("")
.withName("")
.withEmail("john.smith@example.com")
.build();
assertEquals(" () <john.smith@example.com>", userId.toString());
assertEquals(" () <john.smith@example.com>", userId.asString(false));
assertEquals("john.smith@example.com", userId.asString(true));
}
@Test
void testEqualsWithDifferentCaseEmails() {
final String name = "John Smith";
final String comment = "Sales Dept.";
final String email = "john.smith@example.com";
final String upperEmail = email.toUpperCase();
final UserId userId1 = UserId.newBuilder().withComment(comment).withName(name).withEmail(email).build();
final UserId userId2 = UserId.newBuilder().withComment(comment).withName(name).withEmail(upperEmail).build();
assertEquals(userId1, userId2);
}
@Test
void testNotEqualWithDifferentNames() {
final String name1 = "John Smith";
final String name2 = "Don Duck";
final String comment = "Sales Dept.";
final String email = "john.smith@example.com";
final UserId userId1 = UserId.newBuilder().withComment(comment).withName(name1).withEmail(email).build();
final UserId userId2 = UserId.newBuilder().withComment(comment).withName(name2).withEmail(email).build();
assertNotEquals(userId1, userId2);
}
@Test
void testNotEqualWithDifferentComments() {
final String name = "John Smith";
final String comment1 = "Sales Dept.";
final String comment2 = "Legal Dept.";
final String email = "john.smith@example.com";
final UserId userId1 = UserId.newBuilder().withComment(comment1).withName(name).withEmail(email).build();
final UserId userId2 = UserId.newBuilder().withComment(comment2).withName(name).withEmail(email).build();
assertNotEquals(userId1, userId2);
}
2020-11-13 15:59:28 +01:00
}