|
| 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