Skip to content

Commit

Permalink
Fix sonar issue of Remove this failure assertion and simply add the e…
Browse files Browse the repository at this point in the history
…xception type to the method signature (apache#25825)
  • Loading branch information
terrymanu authored May 21, 2023
1 parent 3ad52f6 commit 0c3082a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.time.Duration;
import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
Expand All @@ -35,7 +36,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand All @@ -62,19 +63,20 @@ void assertSubmitAndTaskFailed() {
ExecuteCallback callback = mock(ExecuteCallback.class);
ExecuteEngine executeEngine = ExecuteEngine.newCachedThreadInstance(ExecuteEngineTest.class.getSimpleName());
Future<?> future = executeEngine.submit(lifecycleExecutor, callback);
Throwable actualCause = null;
Optional<Throwable> actualCause = assertTimeout(Duration.ofSeconds(30L), () -> execute(future));
assertTrue(actualCause.isPresent());
assertThat(actualCause.get(), is(expectedException));
shutdownAndAwaitTerminal(executeEngine);
verify(callback).onFailure(expectedException);
}

private Optional<Throwable> execute(final Future<?> future) throws InterruptedException {
try {
// TODO assertTimeout
future.get();
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
fail();
return Optional.empty();
} catch (final ExecutionException ex) {
actualCause = ex.getCause();
return Optional.of(ex.getCause());
}
assertThat(actualCause, is(expectedException));
shutdownAndAwaitTerminal(executeEngine);
verify(callback).onFailure(expectedException);
}

@SneakyThrows({ReflectiveOperationException.class, InterruptedException.class})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@

import org.junit.jupiter.api.Test;

import java.security.InvalidKeyException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.X509Certificate;
import java.time.Instant;
import java.util.Date;
Expand All @@ -35,7 +30,6 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

class SSLUtilsTest {

Expand All @@ -49,20 +43,12 @@ void assertGenerateKeyPair() {
}

@Test
void assertGenerateSelfSignedX509Certificate() {
void assertGenerateSelfSignedX509Certificate() throws GeneralSecurityException {
KeyPair keyPair = SSLUtils.generateRSAKeyPair();
X509Certificate actual = SSLUtils.generateSelfSignedX509Certificate(keyPair);
try {
actual.checkValidity(new Date());
actual.checkValidity(Date.from(Instant.ofEpochMilli(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(365 * 99))));
} catch (final CertificateExpiredException | CertificateNotYetValidException ex) {
fail(ex);
}
try {
actual.verify(keyPair.getPublic());
} catch (final CertificateException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException | SignatureException ex) {
fail(ex);
}
actual.checkValidity(new Date());
actual.checkValidity(Date.from(Instant.ofEpochMilli(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(365 * 99))));
actual.verify(keyPair.getPublic());
assertThrows(SignatureException.class, () -> actual.verify(SSLUtils.generateRSAKeyPair().getPublic()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.sql.Connection;
import java.sql.SQLException;

import static org.junit.jupiter.api.Assertions.fail;

/**
* PostgreSQL set read only transaction integration test.
*/
Expand All @@ -55,8 +53,6 @@ private void assertSetReadOnly() throws SQLException {
assertQueryBalance(connection2);
executeWithLog(connection2, "update account set balance = 100 where id = 2;");
log.info("Using the driver of postgresql:42.4.1 expect to update successfully.");
} catch (final SQLException ex) {
fail("Update failed, should be successfully.");
}
}
}

0 comments on commit 0c3082a

Please sign in to comment.