diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/metadata/reviser/index/EncryptIndexReviser.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/metadata/reviser/index/EncryptIndexReviser.java index 0799bdf36168b..51c25890f981c 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/metadata/reviser/index/EncryptIndexReviser.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/metadata/reviser/index/EncryptIndexReviser.java @@ -50,8 +50,7 @@ public Optional revise(final String tableName, final IndexMetaDat columns.add(each); } } - IndexMetaData result = new IndexMetaData(originalMetaData.getName()); - result.getColumns().addAll(columns); + IndexMetaData result = new IndexMetaData(originalMetaData.getName(), columns); result.setUnique(originalMetaData.isUnique()); return Optional.of(result); } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/reviser/index/ShardingIndexReviser.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/reviser/index/ShardingIndexReviser.java index c8b5e2d8ee464..62d5c2c38bf71 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/reviser/index/ShardingIndexReviser.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/reviser/index/ShardingIndexReviser.java @@ -39,8 +39,8 @@ public Optional revise(final String tableName, final IndexMetaDat if (shardingTable.getActualDataNodes().isEmpty()) { return Optional.empty(); } - IndexMetaData result = new IndexMetaData(IndexMetaDataUtils.getLogicIndexName(originalMetaData.getName(), shardingTable.getActualDataNodes().iterator().next().getTableName())); - result.getColumns().addAll(originalMetaData.getColumns()); + IndexMetaData result = new IndexMetaData( + IndexMetaDataUtils.getLogicIndexName(originalMetaData.getName(), shardingTable.getActualDataNodes().iterator().next().getTableName()), originalMetaData.getColumns()); result.setUnique(originalMetaData.isUnique()); return Optional.of(result); } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java index 4c79f9c4d5332..6f17024d8dfea 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java @@ -20,11 +20,11 @@ import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.reviser.MetaDataReviseEntry; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; import java.util.Arrays; import java.util.Collection; @@ -32,7 +32,6 @@ import java.util.Optional; import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.equalToObject; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -43,6 +42,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +@ExtendWith(MockitoExtension.class) class IndexReviseEngineTest { @Mock @@ -51,19 +51,14 @@ class IndexReviseEngineTest { @InjectMocks private IndexReviseEngine indexReviseEngine; - @BeforeEach - void setUp() { - MockitoAnnotations.openMocks(this); - } - @Test void assertReviseIsPresentIsFalse() { when(metaDataReviseEntry.getIndexReviser(any(), anyString())).thenReturn(Optional.empty()); - Collection indexMetaDataCollection = Collections.singletonList(new IndexMetaData("index")); - Collection actual = indexReviseEngine.revise("tableName", indexMetaDataCollection); + Collection indexMetaDataList = Collections.singletonList(new IndexMetaData("index")); + Collection actual = indexReviseEngine.revise("tableName", indexMetaDataList); assertNotNull(actual); assertThat(actual.size(), is(1)); - assertThat(actual, equalToObject(indexMetaDataCollection)); + assertThat(actual, is(indexMetaDataList)); } @Test diff --git a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/type/IndexMetaDataLoader.java b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/type/IndexMetaDataLoader.java index 1f38e1d6892c7..306c8e1e4296e 100644 --- a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/type/IndexMetaDataLoader.java +++ b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/loader/type/IndexMetaDataLoader.java @@ -25,7 +25,9 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; +import java.util.LinkedList; import java.util.Map; /** @@ -57,8 +59,7 @@ public static Collection load(final Connection connection, final continue; } if (!result.containsKey(indexName)) { - IndexMetaData indexMetaData = new IndexMetaData(indexName); - indexMetaData.getColumns().add(resultSet.getString("COLUMN_NAME")); + IndexMetaData indexMetaData = new IndexMetaData(indexName, new LinkedList<>(Collections.singleton(resultSet.getString("COLUMN_NAME")))); indexMetaData.setUnique(!resultSet.getBoolean("NON_UNIQUE")); result.put(indexName, indexMetaData); } else { diff --git a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/model/IndexMetaData.java b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/model/IndexMetaData.java index 729f63aa6b373..d50313e80c5c4 100644 --- a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/model/IndexMetaData.java +++ b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/metadata/data/model/IndexMetaData.java @@ -31,14 +31,18 @@ */ @RequiredArgsConstructor @Getter +@Setter @EqualsAndHashCode @ToString public final class IndexMetaData { private final String name; - private final Collection columns = new LinkedList<>(); + private final Collection columns; - @Setter private boolean unique; + + public IndexMetaData(final String name) { + this(name, new LinkedList<>()); + } } diff --git a/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoader.java b/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoader.java index 0b3499d007a95..dc8a5571de4fa 100644 --- a/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoader.java +++ b/infra/database/type/mysql/src/main/java/org/apache/shardingsphere/infra/database/mysql/metadata/data/loader/MySQLMetaDataLoader.java @@ -184,8 +184,7 @@ private Map> loadIndexMetaData(final DataSourc if (indexMap.containsKey(indexName)) { indexMap.get(indexName).getColumns().add(resultSet.getString("COLUMN_NAME")); } else { - IndexMetaData indexMetaData = new IndexMetaData(indexName); - indexMetaData.getColumns().add(resultSet.getString("COLUMN_NAME")); + IndexMetaData indexMetaData = new IndexMetaData(indexName, new LinkedList<>(Collections.singleton(resultSet.getString("COLUMN_NAME")))); indexMetaData.setUnique("0".equals(resultSet.getString("NON_UNIQUE"))); indexMap.put(indexName, indexMetaData); } diff --git a/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java b/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java index d8258aa12efc0..afe23facaf1f2 100644 --- a/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java +++ b/infra/database/type/oracle/src/main/java/org/apache/shardingsphere/infra/database/oracle/metadata/data/loader/OracleMetaDataLoader.java @@ -199,9 +199,8 @@ private Map> loadIndexMetaData(final Connectio if (!result.containsKey(tableName)) { result.put(tableName, new LinkedList<>()); } - IndexMetaData indexMetaData = new IndexMetaData(indexName); + IndexMetaData indexMetaData = new IndexMetaData(indexName, loadIndexColumnNames(connection, tableName, indexName)); indexMetaData.setUnique(isUnique); - indexMetaData.getColumns().addAll(loadIndexColumnNames(connection, tableName, indexName)); result.get(tableName).add(indexMetaData); } } diff --git a/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoader.java b/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoader.java index 83e8d3f0084bf..327a44522cf20 100644 --- a/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoader.java +++ b/infra/database/type/sqlserver/src/main/java/org/apache/shardingsphere/infra/database/sqlserver/metadata/data/loader/SQLServerMetaDataLoader.java @@ -140,8 +140,7 @@ private Map> loadIndexMetaData(final DataSourc if (indexMap.containsKey(indexName)) { indexMap.get(indexName).getColumns().add(resultSet.getString("COLUMN_NAME")); } else { - IndexMetaData indexMetaData = new IndexMetaData(indexName); - indexMetaData.getColumns().add(resultSet.getString("COLUMN_NAME")); + IndexMetaData indexMetaData = new IndexMetaData(indexName, new LinkedList<>(Collections.singleton(resultSet.getString("COLUMN_NAME")))); indexMetaData.setUnique("1".equals(resultSet.getString("IS_UNIQUE"))); indexMap.put(indexName, indexMetaData); }