Skip to content

Commit 59da412

Browse files
authored
fix issue-8516 on 2.6.x (#8623)
1 parent 2e39f6d commit 59da412

File tree

6 files changed

+211
-3
lines changed

6 files changed

+211
-3
lines changed

dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/support/MockInvoker.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,15 @@ public static Throwable getThrowable(String throwstr) {
147147
}
148148

149149
@SuppressWarnings("unchecked")
150-
private Invoker<T> getInvoker(String mockService) {
150+
private Invoker<T> getInvoker(String mock) {
151+
Class<T> serviceType = (Class<T>) ReflectUtils.forName(url.getServiceInterface());
152+
String mockService = ConfigUtils.isDefault(mock) ? serviceType.getName() + "Mock" : mock;
151153
Invoker<T> invoker = (Invoker<T>) mocks.get(mockService);
152154
if (invoker != null) {
153155
return invoker;
154156
}
155157

156-
Class<T> serviceType = (Class<T>) ReflectUtils.forName(url.getServiceInterface());
157-
T mockObject = (T) getMockObject(mockService, serviceType);
158+
T mockObject = (T) getMockObject(mock, serviceType);
158159
invoker = proxyFactory.getInvoker(mockObject, serviceType, url);
159160
if (mocks.size() < 10000) {
160161
mocks.put(mockService, invoker);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.rpc.support;
18+
19+
public interface DemoServiceA {
20+
String methodA();
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.rpc.support;
18+
19+
/**
20+
* default mock service for DemoServiceA
21+
*/
22+
public class DemoServiceAMock implements DemoServiceA{
23+
public static final String MOCK_VALUE = "mockA";
24+
@Override
25+
public String methodA() {
26+
return MOCK_VALUE;
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.rpc.support;
18+
19+
public interface DemoServiceB {
20+
String methodB();
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.rpc.support;
18+
19+
/**
20+
* default mock service for DemoServiceA
21+
*/
22+
public class DemoServiceBMock implements DemoServiceB {
23+
public static final String MOCK_VALUE = "mockB";
24+
25+
@Override
26+
public String methodB() {
27+
return MOCK_VALUE;
28+
}
29+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.rpc.support;
18+
19+
import com.alibaba.dubbo.common.URL;
20+
import com.alibaba.dubbo.rpc.RpcInvocation;
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
import java.lang.reflect.Type;
25+
import java.util.ArrayList;
26+
import java.util.HashMap;
27+
28+
import static com.alibaba.dubbo.common.Constants.MOCK_KEY;
29+
30+
31+
public class MockInvokerTest {
32+
33+
@Test
34+
public void testParseMockValue() throws Exception {
35+
Assert.assertNull(MockInvoker.parseMockValue("null"));
36+
Assert.assertNull(MockInvoker.parseMockValue("empty"));
37+
38+
Assert.assertTrue((Boolean) MockInvoker.parseMockValue("true"));
39+
Assert.assertFalse((Boolean) MockInvoker.parseMockValue("false"));
40+
41+
Assert.assertEquals(123, MockInvoker.parseMockValue("123"));
42+
Assert.assertEquals("foo", MockInvoker.parseMockValue("foo"));
43+
Assert.assertEquals("foo", MockInvoker.parseMockValue("\"foo\""));
44+
Assert.assertEquals("foo", MockInvoker.parseMockValue("\'foo\'"));
45+
46+
Assert.assertEquals(
47+
new HashMap<Object, Object>(), MockInvoker.parseMockValue("{}"));
48+
Assert.assertEquals(
49+
new ArrayList<Object>(), MockInvoker.parseMockValue("[]"));
50+
Assert.assertEquals("foo",
51+
MockInvoker.parseMockValue("foo", new Type[]{String.class}));
52+
}
53+
54+
@Test
55+
public void testInvoke() {
56+
URL url = URL.valueOf("remote://1.2.3.4/" + String.class.getName());
57+
url = url.addParameter(MOCK_KEY, "return ");
58+
MockInvoker mockInvoker = new MockInvoker(url);
59+
60+
RpcInvocation invocation = new RpcInvocation();
61+
invocation.setMethodName("getSomething");
62+
Assert.assertEquals(new HashMap<Object, Object>(),
63+
mockInvoker.invoke(invocation).getAttachments());
64+
}
65+
66+
@Test
67+
public void testGetDefaultObject() {
68+
// test methodA in DemoServiceAMock
69+
final Class<DemoServiceA> demoServiceAClass = DemoServiceA.class;
70+
URL url = URL.valueOf("remote://1.2.3.4/" + demoServiceAClass.getName());
71+
url = url.addParameter(MOCK_KEY, "force:true");
72+
MockInvoker mockInvoker = new MockInvoker(url);
73+
74+
RpcInvocation invocation = new RpcInvocation();
75+
invocation.setMethodName("methodA");
76+
Assert.assertEquals(new HashMap<Object, Object>(),
77+
mockInvoker.invoke(invocation).getAttachments());
78+
79+
// test methodB in DemoServiceBMock
80+
final Class<DemoServiceB> demoServiceBClass = DemoServiceB.class;
81+
url = URL.valueOf("remote://1.2.3.4/" + demoServiceBClass.getName());
82+
url = url.addParameter(MOCK_KEY, "force:true");
83+
mockInvoker = new MockInvoker(url);
84+
invocation = new RpcInvocation();
85+
invocation.setMethodName("methodB");
86+
Assert.assertEquals(new HashMap<Object, Object>(),
87+
mockInvoker.invoke(invocation).getAttachments());
88+
}
89+
90+
@Test
91+
public void testNormalizeMock() {
92+
Assert.assertNull(MockInvoker.normalizeMock(null));
93+
94+
Assert.assertEquals("", MockInvoker.normalizeMock(""));
95+
Assert.assertEquals("", MockInvoker.normalizeMock("fail:"));
96+
Assert.assertEquals("", MockInvoker.normalizeMock("force:"));
97+
Assert.assertEquals("throw", MockInvoker.normalizeMock("throw"));
98+
Assert.assertEquals("default", MockInvoker.normalizeMock("fail"));
99+
Assert.assertEquals("default", MockInvoker.normalizeMock("force"));
100+
Assert.assertEquals("default", MockInvoker.normalizeMock("true"));
101+
Assert.assertEquals("default",
102+
MockInvoker.normalizeMock("default"));
103+
Assert.assertEquals("return null",
104+
MockInvoker.normalizeMock("return"));
105+
Assert.assertEquals("return null",
106+
MockInvoker.normalizeMock("return null"));
107+
}
108+
}

0 commit comments

Comments
 (0)