未验证 提交 de66bc44 编写于 作者: Z zhyee 提交者: GitHub

Improve unit test coverage of shadow-core (#5622)

* add ShadowConfigurationLoggerTest

* add ShadowRuleTest

* add dependencies;
add ShadowRuleConfigurationYamlSwapperTest and YamlRootRuleConfigurationsForYamlShadowRuleConfigurationTest
上级 e1125c55
......@@ -34,5 +34,13 @@
<artifactId>shadow-core-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
</dependencies>
</project>
/*
* 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.shadow.log;
import lombok.SneakyThrows;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.underlying.common.log.ConfigurationLogger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
@RunWith(MockitoJUnitRunner.class)
public final class ShadowConfigurationLoggerTest {
@Mock
private Logger log;
@Before
@SneakyThrows
public void setLog() {
setFinalStaticField(ConfigurationLogger.class.getDeclaredField("log"), log);
}
@SneakyThrows
private void setFinalStaticField(final Field field, final Object newValue) {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
@Test
public void assertLogShadowuleConfiguration() {
String yaml = "rules:\n"
+ "- !SHADOW\n"
+ " column: shadow\n"
+ " shadowMappings:\n"
+ " ds: shadow_ds\n";
assertLogInfo(yaml);
ConfigurationLogger.log(Collections.singletonList(getShadowRuleConfiguration()));
}
private ShadowRuleConfiguration getShadowRuleConfiguration() {
return new ShadowRuleConfiguration("shadow", Collections.singletonMap("ds", "shadow_ds"));
}
private void assertLogInfo(final String logContent) {
doAnswer(invocationOnMock -> {
assertThat(invocationOnMock.getArgument(1).toString(), is("Rule configurations: "));
assertThat(invocationOnMock.getArgument(2).toString(), is(logContent));
return null;
}).when(log).info(anyString(), anyString(), anyString());
}
}
/*
* 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.shadow.rule;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.junit.Test;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class ShadowRuleTest {
@Test(expected = IllegalArgumentException.class)
public void assertNewWithEmptyDataSourceRule() {
new ShadowRule(new ShadowRuleConfiguration("", Collections.emptyMap()));
}
@Test
public void assertFindDataSourceRule() {
assertShadowRule(createShadowRule());
}
private ShadowRule createShadowRule() {
ShadowRuleConfiguration configuration = new ShadowRuleConfiguration("shadow", Collections.singletonMap("ds", "shadow_ds"));
return new ShadowRule(configuration);
}
private void assertShadowRule(final ShadowRule rule) {
assertThat(rule.getColumn(), is("shadow"));
assertThat(rule.getShadowMappings().size(), is(1));
assertThat(rule.getShadowMappings().get("ds"), is("shadow_ds"));
}
}
/*
* 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.shadow.yaml.swapper;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.yaml.config.YamlShadowRuleConfiguration;
import org.junit.Test;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class ShadowRuleConfigurationYamlSwapperTest {
@Test
public void assertSwapToYaml() {
ShadowRuleConfiguration shadowRuleConfiguration = new ShadowRuleConfiguration("shadow", Collections.singletonMap("ds", "shadow_ds"));
YamlShadowRuleConfiguration actual = new ShadowRuleConfigurationYamlSwapper().swap(shadowRuleConfiguration);
assertThat(actual.getColumn(), is("shadow"));
assertThat(actual.getShadowMappings().size(), is(1));
assertThat(actual.getShadowMappings().get("ds"), is("shadow_ds"));
}
}
/*
* 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.shadow.yaml.swapper;
import org.apache.shardingsphere.shadow.yaml.config.YamlShadowRuleConfiguration;
import org.apache.shardingsphere.underlying.common.yaml.config.YamlRootRuleConfigurations;
import org.apache.shardingsphere.underlying.common.yaml.engine.YamlEngine;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
public final class YamlRootRuleConfigurationsForYamlShadowRuleConfigurationTest {
@Test
public void assertUnmarshalWithYamlFile() throws IOException {
URL url = getClass().getClassLoader().getResource("yaml/shadow-rule.yaml");
assertNotNull(url);
YamlRootRuleConfigurations rootRuleConfigurations = YamlEngine.unmarshal(new File(url.getFile()), YamlRootRuleConfigurations.class);
assertThat(rootRuleConfigurations.getRules().size(), is(1));
assertShadowRule((YamlShadowRuleConfiguration) rootRuleConfigurations.getRules().iterator().next());
}
@Test
public void assertUnmarshalWithYamlBytes() throws IOException {
URL url = getClass().getClassLoader().getResource("yaml/shadow-rule.yaml");
assertNotNull(url);
StringBuilder yamlContent = new StringBuilder();
try (
FileReader fileReader = new FileReader(url.getFile());
BufferedReader reader = new BufferedReader(fileReader)) {
String line;
while (null != (line = reader.readLine())) {
yamlContent.append(line).append("\n");
}
}
YamlRootRuleConfigurations rootRuleConfigurations = YamlEngine.unmarshal(yamlContent.toString().getBytes(), YamlRootRuleConfigurations.class);
assertThat(rootRuleConfigurations.getRules().size(), is(1));
assertShadowRule((YamlShadowRuleConfiguration) rootRuleConfigurations.getRules().iterator().next());
}
private void assertShadowRule(final YamlShadowRuleConfiguration actual) {
assertThat(actual.getColumn(), is("shadow"));
assertThat(actual.getShadowMappings().size(), is(1));
assertThat(actual.getShadowMappings().get("ds"), is("shadow_ds"));
}
}
#
# 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.
#
dataSources:
ds: !!com.zaxxer.hikari.HikariDataSource
driverClassName: org.h2.Driver
jdbcUrl: jdbc:h2:mem:master_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
shadow_ds: !!com.zaxxer.hikari.HikariDataSource
driverClassName: org.h2.Driver
jdbcUrl: jdbc:h2:mem:slave_ds_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
rules:
- !SHADOW
column: shadow
shadowMappings:
ds: shadow_ds
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册