Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize MethodConfigTest #13234

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,31 @@ void testSticky() {
}

@Test
void testConvertMethodConfig2AsyncInfo() throws Exception{
org.apache.dubbo.config.MethodConfig methodConfig = new org.apache.dubbo.config.MethodConfig();
methodConfig.setOninvokeMethod("setName");
void testConvertMethodConfig2AsyncInfo() throws Exception {
MethodConfig methodConfig = new MethodConfig();
String methodName = "setName";
methodConfig.setOninvokeMethod(methodName);
methodConfig.setOnthrowMethod(methodName);
methodConfig.setOnreturnMethod(methodName);
methodConfig.setOninvoke(new Person());
methodConfig.setOnthrow(new Person());
methodConfig.setOnreturn(new Person());

AsyncMethodInfo methodInfo = methodConfig.convertMethodConfig2AsyncInfo();

assertEquals(methodInfo.getOninvokeMethod(), Person.class.getMethod("setName", String.class));
assertEquals(methodInfo.getOninvokeMethod(), Person.class.getMethod(methodName, String.class));
assertEquals(methodInfo.getOnthrowMethod(), Person.class.getMethod(methodName, String.class));
assertEquals(methodInfo.getOnreturnMethod(), Person.class.getMethod(methodName, String.class));
}

//@Test
public void testOnreturn() {
void testOnreturn() {
MethodConfig method = new MethodConfig();
method.setOnreturn("on-return-object");
assertThat(method.getOnreturn(), equalTo((Object) "on-return-object"));
assertThat(method.getOnreturn(), equalTo("on-return-object"));
Map<String, String> attributes = new HashMap<>();
MethodConfig.appendAttributes(attributes, method);
assertThat(attributes, hasEntry((Object) ON_RETURN_INSTANCE_ATTRIBUTE_KEY, (Object) "on-return-object"));
assertThat(attributes, hasEntry(ON_RETURN_INSTANCE_ATTRIBUTE_KEY, "on-return-object"));
Map<String, String> parameters = new HashMap<String, String>();
MethodConfig.appendParameters(parameters, method);
assertThat(parameters.size(), is(0));
Expand All @@ -137,20 +144,20 @@ void testOnreturnMethod() {
assertThat(method.getOnreturnMethod(), equalTo("on-return-method"));
Map<String, String> attributes = new HashMap<>();
MethodConfig.appendAttributes(attributes, method);
assertThat(attributes, hasEntry((Object) ON_RETURN_METHOD_ATTRIBUTE_KEY, (Object) "on-return-method"));
assertThat(attributes, hasEntry(ON_RETURN_METHOD_ATTRIBUTE_KEY, "on-return-method"));
Map<String, String> parameters = new HashMap<String, String>();
MethodConfig.appendParameters(parameters, method);
assertThat(parameters.size(), is(0));
}

//@Test
public void testOnthrow() {
void testOnthrow() {
MethodConfig method = new MethodConfig();
method.setOnthrow("on-throw-object");
assertThat(method.getOnthrow(), equalTo((Object) "on-throw-object"));
assertThat(method.getOnthrow(), equalTo("on-throw-object"));
Map<String, String> attributes = new HashMap<>();
MethodConfig.appendAttributes(attributes, method);
assertThat(attributes, hasEntry((Object) ON_THROW_INSTANCE_ATTRIBUTE_KEY, (Object) "on-throw-object"));
assertThat(attributes, hasEntry(ON_THROW_INSTANCE_ATTRIBUTE_KEY, "on-throw-object"));
Map<String, String> parameters = new HashMap<String, String>();
MethodConfig.appendParameters(parameters, method);
assertThat(parameters.size(), is(0));
Expand All @@ -163,20 +170,20 @@ void testOnthrowMethod() {
assertThat(method.getOnthrowMethod(), equalTo("on-throw-method"));
Map<String, String> attributes = new HashMap<>();
MethodConfig.appendAttributes(attributes, method);
assertThat(attributes, hasEntry((Object) ON_THROW_METHOD_ATTRIBUTE_KEY, (Object) "on-throw-method"));
assertThat(attributes, hasEntry(ON_THROW_METHOD_ATTRIBUTE_KEY, "on-throw-method"));
Map<String, String> parameters = new HashMap<String, String>();
MethodConfig.appendParameters(parameters, method);
assertThat(parameters.size(), is(0));
}

//@Test
public void testOninvoke() {
void testOninvoke() {
MethodConfig method = new MethodConfig();
method.setOninvoke("on-invoke-object");
assertThat(method.getOninvoke(), equalTo((Object) "on-invoke-object"));
assertThat(method.getOninvoke(), equalTo("on-invoke-object"));
Map<String, String> attributes = new HashMap<>();
MethodConfig.appendAttributes(attributes, method);
assertThat(attributes, hasEntry((Object) ON_INVOKE_INSTANCE_ATTRIBUTE_KEY, (Object) "on-invoke-object"));
assertThat(attributes, hasEntry(ON_INVOKE_INSTANCE_ATTRIBUTE_KEY, "on-invoke-object"));
Map<String, String> parameters = new HashMap<String, String>();
MethodConfig.appendParameters(parameters, method);
assertThat(parameters.size(), is(0));
Expand All @@ -189,7 +196,7 @@ void testOninvokeMethod() {
assertThat(method.getOninvokeMethod(), equalTo("on-invoke-method"));
Map<String, String> attributes = new HashMap<>();
MethodConfig.appendAttributes(attributes, method);
assertThat(attributes, hasEntry((Object) ON_INVOKE_METHOD_ATTRIBUTE_KEY, (Object) "on-invoke-method"));
assertThat(attributes, hasEntry(ON_INVOKE_METHOD_ATTRIBUTE_KEY, "on-invoke-method"));
Map<String, String> parameters = new HashMap<String, String>();
MethodConfig.appendParameters(parameters, method);
assertThat(parameters.size(), is(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.api.DemoService;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.common.Person;
import org.apache.dubbo.config.provider.impl.DemoServiceImpl;
import org.apache.dubbo.rpc.model.AsyncMethodInfo;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -50,6 +52,7 @@
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;

class MethodConfigTest {
private static final String METHOD_NAME = "sayHello";
Expand Down Expand Up @@ -119,7 +122,7 @@ void testStaticConstructor() throws NoSuchFieldException {
}

@Test
void testName() throws Exception {
void testName() {
MethodConfig method = new MethodConfig();
method.setName("hello");
assertThat(method.getName(), equalTo("hello"));
Expand All @@ -129,42 +132,42 @@ void testName() throws Exception {
}

@Test
void testStat() throws Exception {
void testStat() {
MethodConfig method = new MethodConfig();
method.setStat(10);
assertThat(method.getStat(), equalTo(10));
}

@Test
void testRetry() throws Exception {
void testRetry() {
MethodConfig method = new MethodConfig();
method.setRetry(true);
assertThat(method.isRetry(), is(true));
}

@Test
void testReliable() throws Exception {
void testReliable() {
MethodConfig method = new MethodConfig();
method.setReliable(true);
assertThat(method.isReliable(), is(true));
}

@Test
void testExecutes() throws Exception {
void testExecutes() {
MethodConfig method = new MethodConfig();
method.setExecutes(10);
assertThat(method.getExecutes(), equalTo(10));
}

@Test
void testDeprecated() throws Exception {
void testDeprecated() {
MethodConfig method = new MethodConfig();
method.setDeprecated(true);
assertThat(method.getDeprecated(), is(true));
}

@Test
void testArguments() throws Exception {
void testArguments() {
MethodConfig method = new MethodConfig();
ArgumentConfig argument = new ArgumentConfig();
method.setArguments(Collections.singletonList(argument));
Expand All @@ -173,14 +176,32 @@ void testArguments() throws Exception {
}

@Test
void testSticky() throws Exception {
void testSticky() {
MethodConfig method = new MethodConfig();
method.setSticky(true);
assertThat(method.getSticky(), is(true));
}

@Test
void testConvertMethodConfig2AsyncInfo() throws Exception {
MethodConfig methodConfig = new MethodConfig();
String methodName = "setName";
methodConfig.setOninvokeMethod(methodName);
methodConfig.setOnthrowMethod(methodName);
methodConfig.setOnreturnMethod(methodName);
methodConfig.setOninvoke(new Person());
methodConfig.setOnthrow(new Person());
methodConfig.setOnreturn(new Person());

AsyncMethodInfo methodInfo = methodConfig.convertMethodConfig2AsyncInfo();

assertEquals(methodInfo.getOninvokeMethod(), Person.class.getMethod(methodName, String.class));
assertEquals(methodInfo.getOnthrowMethod(), Person.class.getMethod(methodName, String.class));
assertEquals(methodInfo.getOnreturnMethod(), Person.class.getMethod(methodName, String.class));
}

//@Test
public void testOnReturn() throws Exception {
void testOnReturn() {
MethodConfig method = new MethodConfig();
method.setOnreturn("on-return-object");
assertThat(method.getOnreturn(), equalTo("on-return-object"));
Expand All @@ -193,72 +214,72 @@ public void testOnReturn() throws Exception {
}

@Test
void testOnReturnMethod() throws Exception {
void testOnReturnMethod() {
MethodConfig method = new MethodConfig();
method.setOnreturnMethod("on-return-method");
assertThat(method.getOnreturnMethod(), equalTo("on-return-method"));
Map<String, String> attributes = new HashMap<>();
MethodConfig.appendAttributes(attributes, method);
assertThat(attributes, hasEntry((Object) ON_RETURN_METHOD_ATTRIBUTE_KEY, (Object) "on-return-method"));
assertThat(attributes, hasEntry(ON_RETURN_METHOD_ATTRIBUTE_KEY, "on-return-method"));
Map<String, String> parameters = new HashMap<String, String>();
MethodConfig.appendParameters(parameters, method);
assertThat(parameters.size(), is(0));
}

//@Test
public void testOnThrow() throws Exception {
void testOnThrow() {
MethodConfig method = new MethodConfig();
method.setOnthrow("on-throw-object");
assertThat(method.getOnthrow(), equalTo((Object) "on-throw-object"));
assertThat(method.getOnthrow(), equalTo("on-throw-object"));
Map<String, String> attributes = new HashMap<>();
MethodConfig.appendAttributes(attributes, method);
assertThat(attributes, hasEntry((Object) ON_THROW_INSTANCE_ATTRIBUTE_KEY, (Object) "on-throw-object"));
assertThat(attributes, hasEntry(ON_THROW_INSTANCE_ATTRIBUTE_KEY, "on-throw-object"));
Map<String, String> parameters = new HashMap<String, String>();
MethodConfig.appendParameters(parameters, method);
assertThat(parameters.size(), is(0));
}

@Test
void testOnThrowMethod() throws Exception {
void testOnThrowMethod() {
MethodConfig method = new MethodConfig();
method.setOnthrowMethod("on-throw-method");
assertThat(method.getOnthrowMethod(), equalTo("on-throw-method"));
Map<String, String> attributes = new HashMap<>();
MethodConfig.appendAttributes(attributes, method);
assertThat(attributes, hasEntry((Object) ON_THROW_METHOD_ATTRIBUTE_KEY, (Object) "on-throw-method"));
assertThat(attributes, hasEntry(ON_THROW_METHOD_ATTRIBUTE_KEY, "on-throw-method"));
Map<String, String> parameters = new HashMap<String, String>();
MethodConfig.appendParameters(parameters, method);
assertThat(parameters.size(), is(0));
}

//@Test
public void testOnInvoke() throws Exception {
void testOnInvoke() {
MethodConfig method = new MethodConfig();
method.setOninvoke("on-invoke-object");
assertThat(method.getOninvoke(), equalTo((Object) "on-invoke-object"));
assertThat(method.getOninvoke(), equalTo("on-invoke-object"));
Map<String, String> attributes = new HashMap<>();
MethodConfig.appendAttributes(attributes, method);
assertThat(attributes, hasEntry((Object) ON_INVOKE_INSTANCE_ATTRIBUTE_KEY, (Object) "on-invoke-object"));
assertThat(attributes, hasEntry(ON_INVOKE_INSTANCE_ATTRIBUTE_KEY, "on-invoke-object"));
Map<String, String> parameters = new HashMap<String, String>();
MethodConfig.appendParameters(parameters, method);
assertThat(parameters.size(), is(0));
}

@Test
void testOnInvokeMethod() throws Exception {
void testOnInvokeMethod() {
MethodConfig method = new MethodConfig();
method.setOninvokeMethod("on-invoke-method");
assertThat(method.getOninvokeMethod(), equalTo("on-invoke-method"));
Map<String, String> attributes = new HashMap<>();
MethodConfig.appendAttributes(attributes, method);
assertThat(attributes, hasEntry((Object) ON_INVOKE_METHOD_ATTRIBUTE_KEY, (Object) "on-invoke-method"));
assertThat(attributes, hasEntry(ON_INVOKE_METHOD_ATTRIBUTE_KEY, "on-invoke-method"));
Map<String, String> parameters = new HashMap<String, String>();
MethodConfig.appendParameters(parameters, method);
assertThat(parameters.size(), is(0));
}

@Test
void testReturn() throws Exception {
void testReturn() {
MethodConfig method = new MethodConfig();
method.setReturn(true);
assertThat(method.isReturn(), is(true));
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.dubbo.config.common;

import java.io.Serializable;

/**
* Person.java
*/
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}
Loading