Skip to content

Commit

Permalink
Refactor OracleXAConnectionWrapperTest (#33767)
Browse files Browse the repository at this point in the history
* Refactor OracleXAConnectionWrapperTest

* Refactor OracleXAConnectionWrapperTest
  • Loading branch information
terrymanu authored Nov 22, 2024
1 parent dff17cf commit fa2c403
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,47 @@

package org.apache.shardingsphere.transaction.xa.jta.connection.dialect;

import oracle.jdbc.internal.OracleConnection;
import org.junit.jupiter.api.BeforeEach;
import com.zaxxer.hikari.HikariDataSource;
import oracle.jdbc.xa.client.OracleXAConnection;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.transaction.xa.fixture.DataSourceUtils;
import org.apache.shardingsphere.transaction.xa.jta.connection.XAConnectionWrapper;
import org.apache.shardingsphere.transaction.xa.jta.datasource.properties.XADataSourceDefinition;
import org.apache.shardingsphere.transaction.xa.jta.datasource.swapper.DataSourceSwapper;
import org.junit.jupiter.api.Test;
import org.postgresql.core.BaseConnection;

import javax.sql.DataSource;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class OracleXAConnectionWrapperTest {

private OracleXAConnectionWrapper xaConnectionWrapper;

@BeforeEach
void setUp() {
xaConnectionWrapper = new OracleXAConnectionWrapper();
xaConnectionWrapper.init(new Properties());
}
private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "Oracle");

@Test
void assertWrap() throws SQLException {
XADataSource xaDataSource = mock(XADataSource.class);
Connection connection = mockConnection();
XAConnection actual = xaConnectionWrapper.wrap(xaDataSource, connection);
assertThat(actual, instanceOf(XAConnection.class));
XAConnection actual = DatabaseTypedSPILoader.getService(XAConnectionWrapper.class, databaseType).wrap(createXADataSource(), mockConnection());
assertThat(actual, instanceOf(OracleXAConnection.class));
}

@Test
void assertGetDatabaseType() {
assertThat(xaConnectionWrapper.getDatabaseType(), is("Oracle"));
private XADataSource createXADataSource() {
DataSource dataSource = DataSourceUtils.build(HikariDataSource.class, databaseType, "foo_ds");
return new DataSourceSwapper(DatabaseTypedSPILoader.getService(XADataSourceDefinition.class, databaseType)).swap(dataSource);
}

private Connection mockConnection() throws SQLException {
Connection mockConnection = mock(Connection.class);
when(mockConnection.unwrap(OracleConnection.class))
.thenReturn(mock(OracleConnection.class, RETURNS_DEEP_STUBS));
return mockConnection;
Connection result = mock(Connection.class);
when(result.unwrap(BaseConnection.class)).thenReturn(mock(BaseConnection.class));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -930,14 +930,14 @@ private VariableAssignSegment getVariableAssignSegment(final OptionValueContext
if (null != ctx.optionValueNoOptionType()) {
return getVariableAssignSegment(ctx.optionValueNoOptionType());
}
VariableSegment variable = new VariableSegment(ctx.internalVariableName().start.getStartIndex(), ctx.internalVariableName().stop.getStopIndex(), ctx.internalVariableName().getText());
variable.setScope(ctx.optionType().getText());
VariableSegment variable = new VariableSegment(
ctx.internalVariableName().start.getStartIndex(), ctx.internalVariableName().stop.getStopIndex(), ctx.internalVariableName().getText(), ctx.optionType().getText());
return new VariableAssignSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), variable, ctx.setExprOrDefault().getText());
}

private VariableAssignSegment getVariableAssignSegment(final OptionValueListContext ctx) {
VariableSegment variable = new VariableSegment(ctx.internalVariableName().start.getStartIndex(), ctx.internalVariableName().stop.getStopIndex(), ctx.internalVariableName().getText());
variable.setScope(ctx.optionType().getText());
VariableSegment variable = new VariableSegment(
ctx.internalVariableName().start.getStartIndex(), ctx.internalVariableName().stop.getStopIndex(), ctx.internalVariableName().getText(), ctx.optionType().getText());
return new VariableAssignSegment(ctx.start.getStartIndex(), ctx.setExprOrDefault().stop.getStopIndex(), variable, ctx.setExprOrDefault().getText());
}

Expand Down

0 comments on commit fa2c403

Please sign in to comment.