-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for org.apache.dubbo.rpc.support.MockInvoker
These tests were written using Diffblue Cover.
- Loading branch information
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/support/MockInvokerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package org.apache.dubbo.rpc.support; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
import org.apache.dubbo.common.URL; | ||
import org.apache.dubbo.rpc.*; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.apache.dubbo.rpc.Constants.MOCK_KEY; | ||
|
||
public class MockInvokerTest { | ||
|
||
@Test | ||
public void testParseMockValue() throws Exception { | ||
Assertions.assertNull(MockInvoker.parseMockValue("null")); | ||
Assertions.assertNull(MockInvoker.parseMockValue("empty")); | ||
|
||
Assertions.assertTrue((Boolean) MockInvoker.parseMockValue("true")); | ||
Assertions.assertFalse((Boolean) MockInvoker.parseMockValue("false")); | ||
|
||
Assertions.assertEquals(123, MockInvoker.parseMockValue("123")); | ||
Assertions.assertEquals("foo", MockInvoker.parseMockValue("foo")); | ||
Assertions.assertEquals("foo", MockInvoker.parseMockValue("\"foo\"")); | ||
Assertions.assertEquals("foo", MockInvoker.parseMockValue("\'foo\'")); | ||
|
||
Assertions.assertEquals( | ||
new HashMap<>(), MockInvoker.parseMockValue("{}")); | ||
Assertions.assertEquals( | ||
new ArrayList<>(), MockInvoker.parseMockValue("[]")); | ||
Assertions.assertEquals("foo", | ||
MockInvoker.parseMockValue("foo", new Type[]{String.class})); | ||
} | ||
|
||
@Test | ||
public void testInvoke() { | ||
URL url = URL.valueOf("remote://1.2.3.4/" + String.class.getName()); | ||
url = url.addParameter(MOCK_KEY, "return "); | ||
MockInvoker mockInvoker = new MockInvoker(url, String.class); | ||
|
||
RpcInvocation invocation = new RpcInvocation(); | ||
invocation.setMethodName("getSomething"); | ||
Assertions.assertEquals(new HashMap<>(), | ||
mockInvoker.invoke(invocation).getAttachments()); | ||
} | ||
|
||
@Test | ||
public void testInvokeThrowsRpcException1() { | ||
URL url = URL.valueOf("remote://1.2.3.4/" + String.class.getName()); | ||
MockInvoker mockInvoker = new MockInvoker(url, null); | ||
|
||
Assertions.assertThrows(RpcException.class, | ||
() -> mockInvoker.invoke(new RpcInvocation())); | ||
} | ||
|
||
@Test | ||
public void testInvokeThrowsRpcException2() { | ||
URL url = URL.valueOf("remote://1.2.3.4/" + String.class.getName()); | ||
url = url.addParameter(MOCK_KEY, "fail"); | ||
MockInvoker mockInvoker = new MockInvoker(url, String.class); | ||
|
||
RpcInvocation invocation = new RpcInvocation(); | ||
invocation.setMethodName("getSomething"); | ||
Assertions.assertThrows(RpcException.class, | ||
() -> mockInvoker.invoke(invocation)); | ||
} | ||
|
||
@Test | ||
public void testInvokeThrowsRpcException3() { | ||
URL url = URL.valueOf("remote://1.2.3.4/" + String.class.getName()); | ||
url = url.addParameter(MOCK_KEY, "throw"); | ||
MockInvoker mockInvoker = new MockInvoker(url, String.class); | ||
|
||
RpcInvocation invocation = new RpcInvocation(); | ||
invocation.setMethodName("getSomething"); | ||
Assertions.assertThrows(RpcException.class, | ||
() -> mockInvoker.invoke(invocation)); | ||
} | ||
|
||
@Test | ||
public void testGetThrowable() { | ||
Assertions.assertThrows(RpcException.class, | ||
() -> MockInvoker.getThrowable("Exception.class")); | ||
} | ||
|
||
@Test | ||
public void testGetMockObject() { | ||
Assertions.assertEquals("", | ||
MockInvoker.getMockObject("java.lang.String", String.class)); | ||
|
||
Assertions.assertThrows(IllegalStateException.class, () -> MockInvoker | ||
.getMockObject("true", String.class)); | ||
Assertions.assertThrows(IllegalStateException.class, () -> MockInvoker | ||
.getMockObject("default", String.class)); | ||
Assertions.assertThrows(IllegalStateException.class, () -> MockInvoker | ||
.getMockObject("java.lang.String", Integer.class)); | ||
Assertions.assertThrows(IllegalStateException.class, () -> MockInvoker | ||
.getMockObject("java.io.Serializable", Serializable.class)); | ||
} | ||
|
||
@Test | ||
public void testNormalizeMock() { | ||
Assertions.assertNull(MockInvoker.normalizeMock(null)); | ||
|
||
Assertions.assertEquals("", MockInvoker.normalizeMock("")); | ||
Assertions.assertEquals("", MockInvoker.normalizeMock("fail:")); | ||
Assertions.assertEquals("", MockInvoker.normalizeMock("force:")); | ||
Assertions.assertEquals("throw", MockInvoker.normalizeMock("throw")); | ||
Assertions.assertEquals("default", MockInvoker.normalizeMock("fail")); | ||
Assertions.assertEquals("default", MockInvoker.normalizeMock("force")); | ||
Assertions.assertEquals("default", MockInvoker.normalizeMock("true")); | ||
Assertions.assertEquals("default", | ||
MockInvoker.normalizeMock("default")); | ||
Assertions.assertEquals("return null", | ||
MockInvoker.normalizeMock("return")); | ||
Assertions.assertEquals("return null", | ||
MockInvoker.normalizeMock("return null")); | ||
} | ||
} |