Skip to content

Commit

Permalink
Add UncheckedGeneralSecurityException (#983)
Browse files Browse the repository at this point in the history
Closes #981
  • Loading branch information
sleberknight authored May 22, 2023
1 parent 0dec1b3 commit 8256d47
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.kiwiproject.security;

import static java.util.Objects.requireNonNull;

import java.security.GeneralSecurityException;

/**
* Wraps a {@link GeneralSecurityException} with an unchecked exception.
*/
public class UncheckedGeneralSecurityException extends RuntimeException {

/**
* Constructs a new instance.
*
* @param cause the {@link GeneralSecurityException}
*/
public UncheckedGeneralSecurityException(GeneralSecurityException cause) {
super(requireNonNull(cause));
}

/**
* Constructs a new instance.
*
* @param message the detail message, may be null
* @param cause the {@link GeneralSecurityException}
*/
public UncheckedGeneralSecurityException(String message, GeneralSecurityException cause) {
super(message, requireNonNull(cause));
}

/**
* {@inheritDoc}
*/
@Override
public synchronized GeneralSecurityException getCause() {
return (GeneralSecurityException) super.getCause();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.kiwiproject.security;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;

@DisplayName("UncheckedGeneralSecurityException")
class UncheckedGeneralSecurityExceptionTest {

@Test
void shouldConstructWithMessage() {
var cause = newGeneralSecurityException();
var exception = new UncheckedGeneralSecurityException("nope", cause);

assertThat(exception.getMessage()).isEqualTo("nope");
assertThat(exception.getCause()).isSameAs(cause);
}

@Test
void shouldConstructWithoutMessage() {
var cause = newGeneralSecurityException();
var exception = new UncheckedGeneralSecurityException(cause);

assertThat(exception.getMessage()).contains(cause.getMessage());
assertThat(exception.getCause()).isSameAs(cause);
}

private static GeneralSecurityException newGeneralSecurityException() {
return new NoSuchAlgorithmException("bad algorithm: FooBar123");
}
}

0 comments on commit 8256d47

Please sign in to comment.