Skip to content

Commit

Permalink
Add test cases on PipelineJdbcUtils (#33280)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 17, 2024
1 parent 59cc0a3 commit 15604a3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ public final class PipelineJdbcUtils {
* @return true or false
*/
public static boolean isIntegerColumn(final int columnType) {
return Types.INTEGER == columnType || Types.BIGINT == columnType || Types.SMALLINT == columnType || Types.TINYINT == columnType;
switch (columnType) {
case Types.INTEGER:
case Types.BIGINT:
case Types.SMALLINT:
case Types.TINYINT:
return true;
default:
return false;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.data.pipeline.core.util;

import org.junit.jupiter.api.Test;

import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class PipelineJdbcUtilsTest {

@Test
void assertIsIntegerColumn() {
assertTrue(PipelineJdbcUtils.isIntegerColumn(Types.INTEGER));
assertTrue(PipelineJdbcUtils.isIntegerColumn(Types.BIGINT));
assertTrue(PipelineJdbcUtils.isIntegerColumn(Types.SMALLINT));
assertTrue(PipelineJdbcUtils.isIntegerColumn(Types.TINYINT));
assertFalse(PipelineJdbcUtils.isIntegerColumn(Types.VARCHAR));
}

@Test
void assertIsStringColumn() {
assertTrue(PipelineJdbcUtils.isStringColumn(Types.CHAR));
assertTrue(PipelineJdbcUtils.isStringColumn(Types.VARCHAR));
assertTrue(PipelineJdbcUtils.isStringColumn(Types.LONGVARCHAR));
assertTrue(PipelineJdbcUtils.isStringColumn(Types.NCHAR));
assertTrue(PipelineJdbcUtils.isStringColumn(Types.NVARCHAR));
assertTrue(PipelineJdbcUtils.isStringColumn(Types.LONGNVARCHAR));
assertFalse(PipelineJdbcUtils.isStringColumn(Types.INTEGER));
}

@Test
void assertIsBinaryColumn() {
assertTrue(PipelineJdbcUtils.isBinaryColumn(Types.BINARY));
assertTrue(PipelineJdbcUtils.isBinaryColumn(Types.VARBINARY));
assertTrue(PipelineJdbcUtils.isBinaryColumn(Types.LONGVARBINARY));
assertFalse(PipelineJdbcUtils.isBinaryColumn(Types.VARCHAR));
}

@Test
void assertCancelStatementWhenIsClosed() throws SQLException {
Statement statement = mock(Statement.class);
when(statement.isClosed()).thenReturn(true);
PipelineJdbcUtils.cancelStatement(statement);
verify(statement, times(0)).cancel();
}

@Test
void assertCancelStatementWhenIsNotClosed() throws SQLException {
Statement statement = mock(Statement.class);
PipelineJdbcUtils.cancelStatement(statement);
verify(statement).cancel();
}

@Test
void assertCancelStatementWhenSQLExceptionThrown() throws SQLException {
Statement statement = mock(Statement.class);
when(statement.isClosed()).thenThrow(SQLException.class);
PipelineJdbcUtils.cancelStatement(statement);
verify(statement, times(0)).cancel();
}
}

0 comments on commit 15604a3

Please sign in to comment.