Skip to content

Commit

Permalink
Add test cases for YamlAdvisorsConfigurationSwapper (apache#22983)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Dec 20, 2022
1 parent 497b46f commit 867e76a
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ public final class YamlPointcutParameterConfiguration {

private int index;

private String type;
private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import org.apache.shardingsphere.agent.config.advisor.AdvisorConfiguration;
import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorConfiguration;
import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorsConfiguration;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedList;

Expand All @@ -33,16 +31,6 @@ public final class YamlAdvisorsConfigurationSwapper {

private final YamlAdvisorConfigurationSwapper advisorConfigurationSwapper = new YamlAdvisorConfigurationSwapper();

/**
* Unmarshal advisors configuration.
*
* @param inputStream input stream
* @return unmarshalled advisors configuration
*/
public YamlAdvisorsConfiguration unmarshal(final InputStream inputStream) {
return new Yaml().loadAs(inputStream, YamlAdvisorsConfiguration.class);
}

/**
* Swap from YAML advisors configuration to advisor configurations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ public Optional<ElementMatcher<? super MethodDescription>> swapToObject(final Ya
return Optional.of(appendParameters(yamlPointcutConfig, ElementMatchers.isConstructor()));
}
if ("method".equals(yamlPointcutConfig.getType())) {
return Optional.of(appendParameters(yamlPointcutConfig, ElementMatchers.namedOneOf(yamlPointcutConfig.getName())));
return Optional.of(appendParameters(yamlPointcutConfig, ElementMatchers.named(yamlPointcutConfig.getName())));
}
return Optional.empty();
}

private ElementMatcher<? super MethodDescription> appendParameters(final YamlPointcutConfiguration yamlPointcutConfig, final Junction<? super MethodDescription> pointcut) {
if (yamlPointcutConfig.getParams().isEmpty()) {
return pointcut.and(ElementMatchers.takesNoArguments());
}
Junction<? super MethodDescription> result = pointcut;
for (YamlPointcutParameterConfiguration each : yamlPointcutConfig.getParams()) {
pointcut.and(ElementMatchers.takesArgument(each.getIndex(), ElementMatchers.named(each.getType())));
result = result.and(ElementMatchers.takesArgument(each.getIndex(), ElementMatchers.named(each.getName())));
}
return pointcut;
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.agent.core.plugin.yaml.fixture;

import org.apache.shardingsphere.agent.advice.MethodInvocationResult;
import org.apache.shardingsphere.agent.advice.TargetAdviceObject;
import org.apache.shardingsphere.agent.advice.type.ConstructorAdvice;
import org.apache.shardingsphere.agent.advice.type.InstanceMethodAdvice;
import org.apache.shardingsphere.agent.advice.type.StaticMethodAdvice;

import java.lang.reflect.Method;

public final class YamlAdviceFixture implements ConstructorAdvice, InstanceMethodAdvice, StaticMethodAdvice {

@Override
public void onConstructor(final TargetAdviceObject target, final Object[] args) {
}

@Override
public void beforeMethod(final TargetAdviceObject target, final Method method, final Object[] args, final MethodInvocationResult invocationResult) {
}

@Override
public void beforeMethod(final Class<?> clazz, final Method method, final Object[] args, final MethodInvocationResult result) {
}

@Override
public void afterMethod(final TargetAdviceObject target, final Method method, final Object[] args, final MethodInvocationResult invocationResult) {
}

@Override
public void afterMethod(final Class<?> clazz, final Method method, final Object[] args, final MethodInvocationResult result) {
}

@Override
public void onThrowing(final TargetAdviceObject target, final Method method, final Object[] args, final Throwable throwable) {
}

@Override
public void onThrowing(final Class<?> clazz, final Method method, final Object[] args, final Throwable throwable) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.agent.core.plugin.yaml.fixture;

import lombok.NoArgsConstructor;

@NoArgsConstructor
public final class YamlTargetObjectFixture {

public YamlTargetObjectFixture(final String value) {
}

/**
* Call instance method.
*/
public void call() {
}

/**
* Call instance method.
*
* @param value value
*/
public void call(final String value) {
}

/**
* Call instance method.
*
* @param value1 value1
* @param value2 value2
*/
public void call(final String value1, final String value2) {
}

/**
* Call static method.
*/
public static void staticCall() {
}

/**
* Call static method.
*
* @param value value
*/
public static void staticCall(final String value) {
}

/**
* Call static method.
*
* @param value1 value1
* @param value2 value2
*/
public static void staticCall(final String value1, final String value2) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

package org.apache.shardingsphere.agent.core.plugin.yaml.swapper;

import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorConfiguration;
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.shardingsphere.agent.config.advisor.AdvisorConfiguration;
import org.apache.shardingsphere.agent.config.advisor.MethodAdvisorConfiguration;
import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlAdvisorsConfiguration;
import org.apache.shardingsphere.agent.core.plugin.yaml.entity.YamlPointcutConfiguration;
import org.apache.shardingsphere.agent.core.plugin.yaml.fixture.YamlAdviceFixture;
import org.apache.shardingsphere.agent.core.plugin.yaml.fixture.YamlTargetObjectFixture;
import org.junit.Test;
import org.yaml.snakeyaml.Yaml;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
Expand All @@ -35,51 +36,30 @@
public final class YamlAdvisorsConfigurationSwapperTest {

@Test
public void assertUnmarshal() {
YamlAdvisorsConfiguration actual = new YamlAdvisorsConfigurationSwapper().unmarshal(getClass().getResourceAsStream("/advisors.yaml"));
assertThat(actual.getAdvisors().size(), is(5));
List<YamlAdvisorConfiguration> actualYamlAdvisorConfigs = new ArrayList<>(actual.getAdvisors());
assertYamlAdvisorConfiguration(actualYamlAdvisorConfigs.get(0), createExpectedYamlAdvisorConfiguration("org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask",
"org.apache.shardingsphere.agent.metrics.core.advice.CommandExecutorTaskAdvice",
Arrays.asList(createExpectedYamlPointcutConfiguration("run", "method"), createExpectedYamlPointcutConfiguration("processException", "method"))));
assertYamlAdvisorConfiguration(actualYamlAdvisorConfigs.get(1), createExpectedYamlAdvisorConfiguration("org.apache.shardingsphere.proxy.frontend.netty.FrontendChannelInboundHandler",
"org.apache.shardingsphere.agent.metrics.core.advice.ChannelHandlerAdvice",
Arrays.asList(createExpectedYamlPointcutConfiguration("channelActive", "method"),
createExpectedYamlPointcutConfiguration("channelRead", "method"), createExpectedYamlPointcutConfiguration("channelInactive", "method"))));
assertYamlAdvisorConfiguration(actualYamlAdvisorConfigs.get(2), createExpectedYamlAdvisorConfiguration("org.apache.shardingsphere.infra.route.engine.SQLRouteEngine",
"org.apache.shardingsphere.agent.metrics.core.advice.SQLRouteEngineAdvice", Collections.singleton(createExpectedYamlPointcutConfiguration("route", "method"))));
assertYamlAdvisorConfiguration(actualYamlAdvisorConfigs.get(3), createExpectedYamlAdvisorConfiguration(
"org.apache.shardingsphere.proxy.backend.communication.jdbc.transaction.BackendTransactionManager",
"org.apache.shardingsphere.agent.metrics.core.advice.TransactionAdvice",
Arrays.asList(createExpectedYamlPointcutConfiguration("commit", "method"), createExpectedYamlPointcutConfiguration("rollback", "method"))));
assertYamlAdvisorConfiguration(actualYamlAdvisorConfigs.get(4), createExpectedYamlAdvisorConfiguration("org.apache.shardingsphere.infra.config.datasource.JDBCParameterDecoratorHelper",
"org.apache.shardingsphere.agent.metrics.core.advice.DataSourceAdvice", Collections.singleton(createExpectedYamlPointcutConfiguration("decorate", "method"))));
public void assertSwapToObject() {
Collection<AdvisorConfiguration> actual = new YamlAdvisorsConfigurationSwapper().swapToObject(
new Yaml().loadAs(getClass().getResourceAsStream("/conf/advisors.yaml"), YamlAdvisorsConfiguration.class), "YAML_FIXTURE");
assertThat(actual.size(), is(1));
assertAdvisorConfiguration(actual.iterator().next());
}

private void assertYamlAdvisorConfiguration(final YamlAdvisorConfiguration actual, final YamlAdvisorConfiguration expected) {
assertThat(actual.getTarget(), is(expected.getTarget()));
assertThat(actual.getAdvice(), is(expected.getAdvice()));
assertThat(actual.getPointcuts().isEmpty(), is(expected.getPointcuts().isEmpty()));
Iterator<YamlPointcutConfiguration> expectedYamlPointcutConfigs = expected.getPointcuts().iterator();
for (YamlPointcutConfiguration each : actual.getPointcuts()) {
YamlPointcutConfiguration expectedYamlPointcutConfig = expectedYamlPointcutConfigs.next();
assertThat(each.getName(), is(expectedYamlPointcutConfig.getName()));
assertThat(each.getType(), is(expectedYamlPointcutConfig.getType()));
private void assertAdvisorConfiguration(final AdvisorConfiguration actual) {
assertThat(actual.getTargetClassName(), is(YamlTargetObjectFixture.class.getName()));
assertThat(actual.getAdvisors().size(), is(8));
for (MethodAdvisorConfiguration each : actual.getAdvisors()) {
assertThat(each.getAdviceClassName(), is(YamlAdviceFixture.class.getName()));
}
}

private YamlAdvisorConfiguration createExpectedYamlAdvisorConfiguration(final String target, final String advice, final Collection<YamlPointcutConfiguration> yamlPointcutConfigs) {
YamlAdvisorConfiguration result = new YamlAdvisorConfiguration();
result.setTarget(target);
result.setAdvice(advice);
result.setPointcuts(yamlPointcutConfigs);
return result;
}

private YamlPointcutConfiguration createExpectedYamlPointcutConfiguration(final String name, final String type) {
YamlPointcutConfiguration result = new YamlPointcutConfiguration();
result.setName(name);
result.setType(type);
return result;
List<MethodAdvisorConfiguration> actualAdvisorConfig = new ArrayList<>(actual.getAdvisors());
assertThat(actualAdvisorConfig.get(0).getPointcut(), is(ElementMatchers.isConstructor().and(ElementMatchers.takesNoArguments())));
assertThat(actualAdvisorConfig.get(1).getPointcut(), is(ElementMatchers.isConstructor().and(ElementMatchers.takesArgument(0, ElementMatchers.named("value")))));
assertThat(actualAdvisorConfig.get(2).getPointcut(), is(ElementMatchers.named("call").and(ElementMatchers.takesNoArguments())));
assertThat(actualAdvisorConfig.get(3).getPointcut(), is(ElementMatchers.named("call").and(ElementMatchers.takesArgument(0, ElementMatchers.named("value")))));
assertThat(actualAdvisorConfig.get(4).getPointcut(), is(ElementMatchers.named("call")
.and(ElementMatchers.takesArgument(0, ElementMatchers.named("value1"))).and(ElementMatchers.takesArgument(1, ElementMatchers.named("value2")))));
assertThat(actualAdvisorConfig.get(5).getPointcut(), is(ElementMatchers.named("staticCall").and(ElementMatchers.takesNoArguments())));
assertThat(actualAdvisorConfig.get(6).getPointcut(), is(ElementMatchers.named("staticCall").and(ElementMatchers.takesArgument(0, ElementMatchers.named("value")))));
assertThat(actualAdvisorConfig.get(7).getPointcut(), is(ElementMatchers.named("staticCall")
.and(ElementMatchers.takesArgument(0, ElementMatchers.named("value1"))).and(ElementMatchers.takesArgument(1, ElementMatchers.named("value2")))));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ private static AdvisorConfiguration createAdvisorConfiguration() {
AdvisorConfiguration result = new AdvisorConfiguration("org.apache.shardingsphere.agent.core.transformer.fixture.targeted.TargetObjectFixture");
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.isConstructor().and(ElementMatchers.takesArguments(1)), FooAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.isConstructor().and(ElementMatchers.takesArguments(1)), BarAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("callInstanceMethod"), FooAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("callInstanceMethod"), BarAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("callInstanceMethodWhenExceptionThrown"), FooAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("callInstanceMethodWhenExceptionThrown"), BarAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("callStaticMethod"), FooAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("callStaticMethod"), BarAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("callStaticMethodWhenExceptionThrown"), FooAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("callStaticMethodWhenExceptionThrown"), BarAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("call"), FooAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("call"), BarAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("callWhenExceptionThrown"), FooAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("callWhenExceptionThrown"), BarAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("staticCall"), FooAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("staticCall"), BarAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("staticCallWhenExceptionThrown"), FooAdvice.class.getName()));
result.getAdvisors().add(new MethodAdvisorConfiguration(ElementMatchers.named("staticCallWhenExceptionThrown"), BarAdvice.class.getName()));
return result;
}

Expand All @@ -94,15 +94,15 @@ public void assertAdviceConstructor() {
@Test
public void assertAdviceInstanceMethod() {
List<String> queue = new LinkedList<>();
new TargetObjectFixture(new LinkedList<>()).callInstanceMethod(queue);
new TargetObjectFixture(new LinkedList<>()).call(queue);
assertThat(queue, is(Arrays.asList("foo before instance method", "bar before instance method", "on instance method", "foo after instance method", "bar after instance method")));
}

@Test
public void assertAdviceInstanceMethodWhenExceptionThrown() {
List<String> queue = new LinkedList<>();
try {
new TargetObjectFixture(new LinkedList<>()).callInstanceMethodWhenExceptionThrown(queue);
new TargetObjectFixture(new LinkedList<>()).callWhenExceptionThrown(queue);
} catch (final UnsupportedOperationException ignored) {
}
assertThat(queue, is(Arrays.asList("foo before instance method", "bar before instance method",
Expand All @@ -112,15 +112,15 @@ public void assertAdviceInstanceMethodWhenExceptionThrown() {
@Test
public void assertAdviceStaticMethod() {
List<String> queue = new LinkedList<>();
TargetObjectFixture.callStaticMethod(queue);
TargetObjectFixture.staticCall(queue);
assertThat(queue, is(Arrays.asList("foo before static method", "bar before static method", "on static method", "foo after static method", "bar after static method")));
}

@Test
public void assertAdviceStaticMethodWhenExceptionThrown() {
List<String> queue = new LinkedList<>();
try {
TargetObjectFixture.callStaticMethodWhenExceptionThrown(queue);
TargetObjectFixture.staticCallWhenExceptionThrown(queue);
} catch (final UnsupportedOperationException ignored) {
}
assertThat(queue, is(Arrays.asList("foo before static method", "bar before static method",
Expand Down
Loading

0 comments on commit 867e76a

Please sign in to comment.