From e018ba0e660204ddc31a37aad35f9358b42a4cc2 Mon Sep 17 00:00:00 2001 From: sluk3r Date: Thu, 10 Oct 2019 14:42:23 +0800 Subject: [PATCH] improved coverage for package table (#3201) * test case added for TableMetas and TableMetaData * 1. remove ok, e.g. instead of assertGetOk, use assertGet 2. instead of Lists.newArrayList(), use Collections.singleton * 1. remove ok, e.g. instead of assertGetOk, use assertGet 2. instead of Lists.newArrayList(), use Collections.singletonList --- .../metadata/table/TableMetaDataTest.java | 34 ++++++++ .../core/metadata/table/TableMetasTest.java | 86 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/metadata/table/TableMetaDataTest.java create mode 100644 sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/metadata/table/TableMetasTest.java diff --git a/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/metadata/table/TableMetaDataTest.java b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/metadata/table/TableMetaDataTest.java new file mode 100644 index 0000000000..ef66f2d2f3 --- /dev/null +++ b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/metadata/table/TableMetaDataTest.java @@ -0,0 +1,34 @@ +/* + * 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.core.metadata.table; + +import org.apache.shardingsphere.core.metadata.column.ColumnMetaData; +import org.junit.Test; + +import java.util.Collections; + +import static org.junit.Assert.assertTrue; + +public final class TableMetaDataTest { + + @Test + public void assertContainsIndex() { + TableMetaData tableMetaData = new TableMetaData(Collections.singletonList(new ColumnMetaData("name", "dataType", true)), Collections.singleton("indexName")); + assertTrue(tableMetaData.containsIndex("indexName")); + } +} diff --git a/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/metadata/table/TableMetasTest.java b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/metadata/table/TableMetasTest.java new file mode 100644 index 0000000000..6dcf4ed3ff --- /dev/null +++ b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/metadata/table/TableMetasTest.java @@ -0,0 +1,86 @@ +/* + * 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.core.metadata.table; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Sets; +import org.apache.shardingsphere.core.metadata.column.ColumnMetaData; +import org.junit.Test; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +public final class TableMetasTest { + + @Test + public void assertGet() { + TableMetaData tableMetaData = mock(TableMetaData.class); + TableMetas tableMetas = new TableMetas(ImmutableMap.of("tableMetaData", tableMetaData)); + assertThat(tableMetas.get("tableMetaData"), is(tableMetaData)); + } + + @Test + public void assertPut() { + TableMetas tableMetas = new TableMetas(Collections.emptyMap()); + TableMetaData tableMetaData = mock(TableMetaData.class); + tableMetas.put("tableMetaData", tableMetaData); + assertThat(tableMetas.get("tableMetaData"), is(tableMetaData)); + } + + @Test + public void assertRemove() { + TableMetas tableMetas = new TableMetas(ImmutableMap.of("tableMetaData", mock(TableMetaData.class))); + tableMetas.remove("tableMetaData"); + assertNull(tableMetas.get("tableMetaData")); + } + + @Test + public void assertContainsTable() { + assertTrue(new TableMetas(ImmutableMap.of("tableMetaData", mock(TableMetaData.class))).containsTable("tableMetaData")); + } + + @Test + public void assertContainsColumn() { + TableMetaData tableMetaData = new TableMetaData(Collections.singletonList(new ColumnMetaData("name", "dataType", false)), Collections.emptyList()); + assertTrue(new TableMetas(ImmutableMap.of("tableMetaData", tableMetaData)).containsColumn("tableMetaData", "name")); + } + + @Test + public void assertGetAllColumnNamesWhenContainsKey() { + TableMetaData tableMetaData = new TableMetaData(Collections.singletonList(new ColumnMetaData("name", "dataType", false)), Collections.emptyList()); + assertThat(new TableMetas(ImmutableMap.of("tableMetaData", tableMetaData)).getAllColumnNames("tableMetaData"), is((List) Collections.singletonList("name"))); + } + + @Test + public void assertGetAllColumnNamesWhenNotContainsKey() { + TableMetaData tableMetaData = new TableMetaData(Collections.singletonList(new ColumnMetaData("name", "dataType", false)), Collections.emptyList()); + assertThat(new TableMetas(ImmutableMap.of("tableMetaData", tableMetaData)).getAllColumnNames("other_tableMetaData"), is(Collections.emptyList())); + } + + @Test + public void assertGetAllTableNames() { + assertThat(new TableMetas(ImmutableMap.of("tableMetaData", mock(TableMetaData.class))).getAllTableNames(), is((Collection) Sets.newHashSet("tableMetaData"))); + } +} -- GitLab