mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2024-12-22 04:57:56 +01:00
Profile: Use Optional for description
This commit is contained in:
parent
aa88904711
commit
c479cc8ef3
3 changed files with 171 additions and 7 deletions
|
@ -38,8 +38,10 @@ public class ListProfilesExternal implements ListProfiles {
|
|||
private static List<Profile> toProfiles(String output) {
|
||||
List<Profile> profiles = new ArrayList<>();
|
||||
for (String line : output.split("\n")) {
|
||||
String[] split = line.split(": ");
|
||||
profiles.add(new Profile(split[0], split[1]));
|
||||
if (line.trim().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
profiles.add(Profile.parse(line));
|
||||
}
|
||||
return profiles;
|
||||
}
|
||||
|
|
|
@ -4,8 +4,12 @@
|
|||
|
||||
package sop;
|
||||
|
||||
import sop.util.Optional;
|
||||
import sop.util.UTF8Util;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Tuple class bundling a profile name and description.
|
||||
*
|
||||
|
@ -15,7 +19,7 @@ import sop.util.UTF8Util;
|
|||
public class Profile {
|
||||
|
||||
private final String name;
|
||||
private final String description;
|
||||
private final Optional<String> description;
|
||||
|
||||
/**
|
||||
* Create a new {@link Profile} object.
|
||||
|
@ -24,15 +28,62 @@ public class Profile {
|
|||
* @param name profile name
|
||||
* @param description profile description
|
||||
*/
|
||||
public Profile(String name, String description) {
|
||||
public Profile(@Nonnull String name, @Nullable String description) {
|
||||
if (name.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("Name cannot be empty.");
|
||||
}
|
||||
if (name.contains(":")) {
|
||||
throw new IllegalArgumentException("Name cannot contain ':'.");
|
||||
}
|
||||
if (name.contains(" ") || name.contains("\n") || name.contains("\t") || name.contains("\r")) {
|
||||
throw new IllegalArgumentException("Name cannot contain whitespace characters.");
|
||||
}
|
||||
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
|
||||
if (description == null) {
|
||||
this.description = Optional.ofEmpty();
|
||||
} else {
|
||||
String trimmedDescription = description.trim();
|
||||
if (trimmedDescription.isEmpty()) {
|
||||
this.description = Optional.ofEmpty();
|
||||
} else {
|
||||
this.description = Optional.of(trimmedDescription);
|
||||
}
|
||||
}
|
||||
|
||||
if (exceeds1000CharLineLimit(this)) {
|
||||
throw new IllegalArgumentException("The line representation of a profile MUST NOT exceed 1000 bytes.");
|
||||
}
|
||||
}
|
||||
|
||||
public Profile(String name) {
|
||||
this(name, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a {@link Profile} from its string representation.
|
||||
*
|
||||
* @param string string representation
|
||||
* @return profile
|
||||
*/
|
||||
public static Profile parse(String string) {
|
||||
if (string.contains(": ")) {
|
||||
// description after colon, e.g. "default: Use implementers recommendations."
|
||||
String name = string.substring(0, string.indexOf(": "));
|
||||
String description = string.substring(string.indexOf(": ") + 2);
|
||||
return new Profile(name, description.trim());
|
||||
}
|
||||
|
||||
if (string.endsWith(":")) {
|
||||
// empty description, e.g. "default:"
|
||||
return new Profile(string.substring(0, string.length() - 1));
|
||||
}
|
||||
|
||||
// no description
|
||||
return new Profile(string.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name (also known as identifier) of the profile.
|
||||
* A profile name is a UTF-8 string that has no whitespace in it.
|
||||
|
@ -48,6 +99,7 @@ public class Profile {
|
|||
*
|
||||
* @return name
|
||||
*/
|
||||
@Nonnull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -57,17 +109,26 @@ public class Profile {
|
|||
*
|
||||
* @return description
|
||||
*/
|
||||
public String getDescription() {
|
||||
@Nonnull
|
||||
public Optional<String> getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public boolean hasDescription() {
|
||||
return description.isPresent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the profile into a String for displaying.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return getName() + ": " + getDescription();
|
||||
if (getDescription().isEmpty()) {
|
||||
return getName();
|
||||
}
|
||||
return getName() + ": " + getDescription().get();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
101
sop-java/src/test/java/sop/ProfileTest.java
Normal file
101
sop-java/src/test/java/sop/ProfileTest.java
Normal file
|
@ -0,0 +1,101 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sop.Profile;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ProfileTest {
|
||||
|
||||
@Test
|
||||
public void toStringFull() {
|
||||
Profile profile = new Profile("default", "Use the implementers recommendations.");
|
||||
assertEquals("default: Use the implementers recommendations.", profile.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringNameOnly() {
|
||||
Profile profile = new Profile("default");
|
||||
assertEquals("default", profile.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseFull() {
|
||||
String string = "default: Use the implementers recommendations.";
|
||||
Profile profile = Profile.parse(string);
|
||||
assertEquals("default", profile.getName());
|
||||
assertTrue(profile.hasDescription());
|
||||
assertEquals("Use the implementers recommendations.", profile.getDescription().get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseNameOnly() {
|
||||
String string = "rfc4880";
|
||||
Profile profile = Profile.parse(string);
|
||||
assertEquals("rfc4880", profile.getName());
|
||||
assertFalse(profile.hasDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseEmptyDescription() {
|
||||
String string = "rfc4880: ";
|
||||
Profile profile = Profile.parse(string);
|
||||
assertEquals("rfc4880", profile.getName());
|
||||
assertFalse(profile.hasDescription());
|
||||
|
||||
string = "rfc4880:";
|
||||
profile = Profile.parse(string);
|
||||
assertEquals("rfc4880", profile.getName());
|
||||
assertFalse(profile.hasDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTooLongProfile() {
|
||||
// 1200 chars
|
||||
String string = "longDescription: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
|
||||
assertThrows(IllegalArgumentException.class, () -> Profile.parse(string));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructTooLongProfile() {
|
||||
// name + description = 1200 chars
|
||||
String name = "longDescription";
|
||||
String description = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile(name, description));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nameCannotBeEmpty() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile(""));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile(""), "Description Text.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nameCannotContainColons() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default:"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default:", "DescriptionText"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("rfc:4880"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("rfc:4880", "OpenPGP Message Format"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nameCannotContainWhitespace() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default profile"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default profile", "With description."));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default\nprofile"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default\nprofile", "With description"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default\tprofile"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default\tprofile", "With description"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default\r\nprofile"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default\r\nprofile", "With description"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default\rprofile"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Profile("default\rprofile", "With description"));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue