Skip to content

Commit

Permalink
Fixes nativeTest unit that failed to execute
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Nov 12, 2023
1 parent 8ec8f8d commit 7427de4
Show file tree
Hide file tree
Showing 19 changed files with 299 additions and 421 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
*/
public final class ReadwriteSplittingRuleConfigurationChecker implements RuleConfigurationChecker<ReadwriteSplittingRuleConfiguration> {

private final String inlineExpressionTypePrefix = "<LITERAL>";

@Override
public void check(final String databaseName, final ReadwriteSplittingRuleConfiguration config, final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> builtRules) {
Collection<ReadwriteSplittingDataSourceRuleConfiguration> configs = config.getDataSources();
Expand Down Expand Up @@ -79,7 +81,10 @@ private void checkDataSources(final String databaseName, final Map<String, DataS

private void checkWriteDataSourceNames(final String databaseName, final Map<String, DataSource> dataSourceMap, final Collection<String> addedWriteDataSourceNames,
final ReadwriteSplittingDataSourceRuleConfiguration config, final Collection<ShardingSphereRule> rules) {
for (String each : InlineExpressionParserFactory.newInstance(config.getWriteDataSourceName()).splitAndEvaluate()) {
String resultInlineExpression = null == System.getProperty("org.graalvm.nativeimage.imagecode") || !"runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"))
? config.getWriteDataSourceName()
: inlineExpressionTypePrefix + config.getWriteDataSourceName();
for (String each : InlineExpressionParserFactory.newInstance(resultInlineExpression).splitAndEvaluate()) {
ShardingSpherePreconditions.checkState(dataSourceMap.containsKey(each) || containsInOtherRules(each, rules),
() -> new DataSourceNameExistedException(String.format("Write data source name `%s` not in database `%s`.", each, databaseName)));
ShardingSpherePreconditions.checkState(addedWriteDataSourceNames.add(each),
Expand All @@ -97,7 +102,10 @@ private boolean containsInOtherRules(final String datasourceName, final Collecti
}

private void checkReadeDataSourceNames(final String databaseName, final Map<String, DataSource> dataSourceMap, final Collection<String> addedReadDataSourceNames, final String readDataSourceName) {
for (String each : InlineExpressionParserFactory.newInstance(readDataSourceName).splitAndEvaluate()) {
String resultInlineExpression = null == System.getProperty("org.graalvm.nativeimage.imagecode") || !"runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"))
? readDataSourceName
: inlineExpressionTypePrefix + readDataSourceName;
for (String each : InlineExpressionParserFactory.newInstance(resultInlineExpression).splitAndEvaluate()) {
ShardingSpherePreconditions.checkState(dataSourceMap.containsKey(each),
() -> new DataSourceNameExistedException(String.format("Read data source name `%s` not in database `%s`.", each, databaseName)));
ShardingSpherePreconditions.checkState(addedReadDataSourceNames.add(each),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
*/
public final class ReadwriteSplittingRule implements DatabaseRule, DataSourceContainedRule, StaticDataSourceContainedRule, ExportableRule, StorageConnectorReusableRule {

private final String inlineExpressionTypePrefix = "<LITERAL>";

private final String databaseName;

@Getter
Expand Down Expand Up @@ -103,10 +105,22 @@ private Map<String, ReadwriteSplittingDataSourceRule> createDataSourceRules(fina

private Map<String, ReadwriteSplittingDataSourceRule> createStaticDataSourceRules(final ReadwriteSplittingDataSourceRuleConfiguration config,
final ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm) {
List<String> inlineReadwriteDataSourceNames = InlineExpressionParserFactory.newInstance(config.getName()).splitAndEvaluate();
List<String> inlineWriteDatasourceNames = InlineExpressionParserFactory.newInstance(config.getWriteDataSourceName()).splitAndEvaluate();
String resultConfigNameInlineExpression = null == System.getProperty("org.graalvm.nativeimage.imagecode") || !"runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"))
? config.getName()
: inlineExpressionTypePrefix + config.getName();
List<String> inlineReadwriteDataSourceNames = InlineExpressionParserFactory.newInstance(resultConfigNameInlineExpression).splitAndEvaluate();
String resultConfigWriteDataSourceNameInlineExpression =
null == System.getProperty("org.graalvm.nativeimage.imagecode") || !"runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"))
? config.getWriteDataSourceName()
: inlineExpressionTypePrefix + config.getWriteDataSourceName();
List<String> inlineWriteDatasourceNames = InlineExpressionParserFactory.newInstance(resultConfigWriteDataSourceNameInlineExpression).splitAndEvaluate();
List<List<String>> inlineReadDatasourceNames = config.getReadDataSourceNames().stream()
.map(each -> InlineExpressionParserFactory.newInstance(each).splitAndEvaluate()).collect(Collectors.toList());
.map(each -> {
String resultInlineExpression = null == System.getProperty("org.graalvm.nativeimage.imagecode") || !"runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"))
? each
: inlineExpressionTypePrefix + each;
return InlineExpressionParserFactory.newInstance(resultInlineExpression).splitAndEvaluate();
}).collect(Collectors.toList());
ShardingSpherePreconditions.checkState(inlineWriteDatasourceNames.size() == inlineReadwriteDataSourceNames.size(),
() -> new InvalidInlineExpressionDataSourceNameException("Inline expression write data source names size error."));
inlineReadDatasourceNames.forEach(each -> ShardingSpherePreconditions.checkState(each.size() == inlineReadwriteDataSourceNames.size(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ public final class EncryptTest {

@Test
void testEncryptInLocalTransactions() throws SQLException, IOException {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/encrypt.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
try {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/encrypt.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
} finally {
this.cleanEnvironment();
}
}

private void initEnvironment() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ public final class MaskTest {

@Test
void testMaskInLocalTransactions() throws SQLException, IOException {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/mask.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
try {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/mask.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
} finally {
this.cleanEnvironment();
}
}

private void initEnvironment() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.apache.shardingsphere.infra.nativetest.jdbc.features.repository.OrderRepository;
import org.h2.jdbc.JdbcSQLSyntaxErrorException;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.IOException;
Expand All @@ -40,8 +38,6 @@

public final class ReadWriteSplittingTest {

private static final Logger LOGGER = LoggerFactory.getLogger(ReadWriteSplittingTest.class);

private OrderRepository orderRepository;

private OrderItemRepository orderItemRepository;
Expand All @@ -50,13 +46,16 @@ public final class ReadWriteSplittingTest {

@Test
void testReadWriteSplittingInLocalTransactions() throws SQLException, IOException {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/readwrite-splitting.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
try {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/readwrite-splitting.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
} finally {
this.cleanEnvironment();
}
}

private void initEnvironment() throws SQLException {
Expand All @@ -69,18 +68,15 @@ private void initEnvironment() throws SQLException {
}

private void processSuccess() throws SQLException {
LOGGER.info("-------------- Process Success Begin ---------------");
List<Long> orderIds = insertData();
// This is intentional because the read operation is in the slave database and the corresponding table does not exist.
assertThrows(JdbcSQLSyntaxErrorException.class, this::printData);
deleteData(orderIds);
// This is intentional because the read operation is in the slave database and the corresponding table does not exist.
assertThrows(JdbcSQLSyntaxErrorException.class, this::printData);
LOGGER.info("-------------- Process Success Finish --------------");
}

private List<Long> insertData() throws SQLException {
LOGGER.info("---------------------------- Insert Data ----------------------------");
List<Long> result = new ArrayList<>(10);
for (int i = 1; i <= 10; i++) {
Order order = new Order();
Expand All @@ -97,9 +93,7 @@ private List<Long> insertData() throws SQLException {
orderItem.setStatus("INSERT_TEST");
orderItemRepository.insert(orderItem);

Address address = new Address();
address.setAddressId((long) i);
address.setAddressName("address_test_" + i);
Address address = new Address((long) i, "address_test_" + i);
addressRepository.insert(address);

result.add(order.getOrderId());
Expand All @@ -108,7 +102,6 @@ private List<Long> insertData() throws SQLException {
}

private void deleteData(final List<Long> orderIds) throws SQLException {
LOGGER.info("---------------------------- Delete Data ----------------------------");
long count = 1;
for (Long each : orderIds) {
orderRepository.delete(each);
Expand All @@ -118,18 +111,9 @@ private void deleteData(final List<Long> orderIds) throws SQLException {
}

private void printData() throws SQLException {
LOGGER.info("---------------------------- Print Order Data -----------------------");
for (Object each : orderRepository.selectAll()) {
LOGGER.info(each.toString());
}
LOGGER.info("---------------------------- Print OrderItem Data -------------------");
for (Object each : orderItemRepository.selectAll()) {
LOGGER.info(each.toString());
}
LOGGER.info("---------------------------- Print Address Data -------------------");
for (Object each : addressRepository.selectAll()) {
LOGGER.info(each.toString());
}
orderRepository.selectAll();
orderItemRepository.selectAll();
addressRepository.selectAll();
}

private void cleanEnvironment() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ public final class ShadowTest {

@Test
void testShadowInLocalTransactions() throws SQLException, IOException {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/shadow.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
try {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/shadow.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
} finally {
this.cleanEnvironment();
}
}

private void initEnvironment() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ public final class ShardingTest {

@Test
void testShardingInLocalTransactions() throws SQLException, IOException {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/sharding.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
this.cleanEnvironment();
try {
DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(FileTestUtils.readFromFileURLString("yaml/sharding.yaml"));
orderRepository = new OrderRepository(dataSource);
orderItemRepository = new OrderItemRepository(dataSource);
addressRepository = new AddressRepository(dataSource);
this.initEnvironment();
this.processSuccess();
} finally {
this.cleanEnvironment();
}
}

private void initEnvironment() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.infra.nativetest.jdbc.features.algorithm;

import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;

@SuppressWarnings("unused")
public final class ClassBasedInlineShardingAlgorithmFixture implements StandardShardingAlgorithm<Integer> {

@Override
public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Integer> shardingValue) {
String resultDatabaseName = "ds_" + shardingValue.getValue() % 2;
for (String each : availableTargetNames) {
if (each.equals(resultDatabaseName)) {
return each;
}
}
return null;
}

@Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Integer> shardingValue) {
throw new RuntimeException("This algorithm class does not support range queries.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.nativetest.jdbc.features;
package org.apache.shardingsphere.infra.nativetest.jdbc.features.algorithm;

import lombok.Getter;
import org.apache.shardingsphere.encrypt.api.context.EncryptContext;
import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm;

import java.util.Properties;

@SuppressWarnings("LombokGetterMayBeUsed")
public final class TestQueryAssistedShardingEncryptAlgorithm implements AssistedEncryptAlgorithm {

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"resources":{
"includes":[{
"pattern":"\\Qsql/H2.xml\\E"
}, {
"pattern":"\\Qsql/MySQL.xml\\E"
}]},
"bundles":[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# limitations under the License.
#

org.apache.shardingsphere.infra.nativetest.jdbc.features.TestQueryAssistedShardingEncryptAlgorithm
org.apache.shardingsphere.infra.nativetest.jdbc.features.algorithm.TestQueryAssistedShardingEncryptAlgorithm
34 changes: 0 additions & 34 deletions infra/nativetest/src/test/resources/logback.xml

This file was deleted.

Loading

0 comments on commit 7427de4

Please sign in to comment.