-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UncheckedGeneralSecurityException (#983)
Closes #981
- Loading branch information
1 parent
0dec1b3
commit 8256d47
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/org/kiwiproject/security/UncheckedGeneralSecurityException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/org/kiwiproject/security/UncheckedGeneralSecurityExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |