Skip to content

Commit

Permalink
Add more test cases on ZookeeperExceptionHandler (#32811)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Sep 8, 2024
1 parent d68017f commit 6322c10
Showing 1 changed file with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,63 @@
package org.apache.shardingsphere.mode.repository.cluster.zookeeper.handler;

import org.apache.shardingsphere.mode.repository.cluster.exception.ClusterRepositoryPersistException;
import org.apache.zookeeper.KeeperException.ConnectionLossException;
import org.apache.zookeeper.KeeperException.NoNodeException;
import org.apache.zookeeper.KeeperException.NodeExistsException;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ZookeeperExceptionHandlerTest {

@Test
void assertHandleException() {
ZookeeperExceptionHandler.handleException(null);
ZookeeperExceptionHandler.handleException(new NoNodeException(""));
ZookeeperExceptionHandler.handleException(new Exception(new NoNodeException("")));
try {
ZookeeperExceptionHandler.handleException(new Exception(""));
fail("must be failed handle new Exception().");
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
assertThat(ex, instanceOf(ClusterRepositoryPersistException.class));
}
void assertHandleNullException() {
assertDoesNotThrow(() -> ZookeeperExceptionHandler.handleException(null));
}

@Test
void assertHandleConnectionLossException() {
assertDoesNotThrow(() -> ZookeeperExceptionHandler.handleException(new ConnectionLossException()));
}

@Test
void assertHandleConnectionLossExceptionWithCause() {
assertDoesNotThrow(() -> ZookeeperExceptionHandler.handleException(new Exception(new ConnectionLossException())));
}

@Test
void assertHandleNoNodeException() {
assertDoesNotThrow(() -> ZookeeperExceptionHandler.handleException(new NoNodeException()));
}

@Test
void assertHandleNoNodeExceptionWithCause() {
assertDoesNotThrow(() -> ZookeeperExceptionHandler.handleException(new Exception(new NoNodeException())));
}

@Test
void assertHandleNodeExistsException() {
assertDoesNotThrow(() -> ZookeeperExceptionHandler.handleException(new NodeExistsException()));
}

@Test
void assertHandleNodeExistsExceptionWithCause() {
assertDoesNotThrow(() -> ZookeeperExceptionHandler.handleException(new Exception(new NodeExistsException())));
}

@Test
void assertHandleInterruptedException() {
assertDoesNotThrow(() -> ZookeeperExceptionHandler.handleException(new InterruptedException()));
}

@Test
void assertHandleUnIgnoredException() {
assertThrows(ClusterRepositoryPersistException.class, () -> ZookeeperExceptionHandler.handleException(new Exception("")));
}

@Test
void assertHandleUnIgnoredExceptionWithCause() {
assertThrows(ClusterRepositoryPersistException.class, () -> ZookeeperExceptionHandler.handleException(new Exception(new RuntimeException(""))));
}
}

0 comments on commit 6322c10

Please sign in to comment.