Skip to content

Commit

Permalink
Add unit tests for org.apache.dubbo.rpc.support.MockInvoker
Browse files Browse the repository at this point in the history
These tests were written using Diffblue Cover.
  • Loading branch information
EricHetti committed Jun 24, 2019
1 parent 041a6ad commit 9e8f09e
Showing 1 changed file with 122 additions and 0 deletions.
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"));
}
}

0 comments on commit 9e8f09e

Please sign in to comment.