|
15 | 15 | */
|
16 | 16 | package com.weibo.api.motan.serialize.protobuf;
|
17 | 17 |
|
18 |
| -import com.weibo.api.motan.exception.MotanBizException; |
19 |
| -import com.weibo.api.motan.exception.MotanServiceException; |
20 |
| -import com.weibo.api.motan.serialize.ProtobufSerialization; |
21 |
| -import org.junit.After; |
22 |
| -import org.junit.Assert; |
23 |
| -import org.junit.Before; |
24 |
| -import org.junit.Test; |
25 |
| - |
26 | 18 | import com.weibo.api.motan.config.ProtocolConfig;
|
27 | 19 | import com.weibo.api.motan.config.RefererConfig;
|
28 | 20 | import com.weibo.api.motan.config.RegistryConfig;
|
29 | 21 | import com.weibo.api.motan.config.ServiceConfig;
|
| 22 | +import com.weibo.api.motan.exception.MotanBizException; |
| 23 | +import com.weibo.api.motan.exception.MotanServiceException; |
30 | 24 | import com.weibo.api.motan.serialize.protobuf.gen.UserProto.Address;
|
31 | 25 | import com.weibo.api.motan.serialize.protobuf.gen.UserProto.User;
|
32 |
| - |
33 |
| -import java.io.IOException; |
| 26 | +import org.junit.After; |
| 27 | +import org.junit.Assert; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Test; |
34 | 30 |
|
35 | 31 | public class TestProtoBuf {
|
36 |
| - private ServiceConfig<HelloService> serviceConfig; |
37 |
| - private RefererConfig<HelloService> refererConfig; |
38 |
| - private HelloService service; |
39 |
| - |
40 |
| - @Before |
41 |
| - public void setUp() throws InterruptedException { |
42 |
| - ProtocolConfig protocolConfig = new ProtocolConfig(); |
43 |
| - protocolConfig.setId("testMotan"); |
44 |
| - protocolConfig.setName("motan"); |
45 |
| - protocolConfig.setSerialization("protobuf"); |
46 |
| - protocolConfig.setCodec("motan"); |
47 |
| - |
48 |
| - RegistryConfig registryConfig = new RegistryConfig(); |
49 |
| - registryConfig.setAddress("127.0.0.1"); |
50 |
| - registryConfig.setPort(8002); |
51 |
| - |
52 |
| - serviceConfig = new ServiceConfig<>(); |
53 |
| - serviceConfig.setRef(new HelloServiceImpl()); |
54 |
| - serviceConfig.setInterface(HelloService.class); |
55 |
| - serviceConfig.setProtocol(protocolConfig); |
56 |
| - serviceConfig.setExport("testMotan:18002"); |
57 |
| - serviceConfig.setRegistry(registryConfig); |
58 |
| - serviceConfig.setShareChannel(true); |
59 |
| - serviceConfig.export(); |
60 |
| - |
61 |
| - refererConfig = new RefererConfig<>(); |
62 |
| - refererConfig.setDirectUrl("127.0.0.1:18002"); |
63 |
| - refererConfig.setProtocol(protocolConfig); |
64 |
| - refererConfig.setInterface(HelloService.class); |
65 |
| - |
66 |
| - service = refererConfig.getRef(); |
67 |
| - Thread.sleep(20L); |
68 |
| - } |
69 |
| - |
70 |
| - @Test |
71 |
| - public void testPrimitiveType() { |
72 |
| - Assert.assertEquals("-1", service.sumAsString(Integer.MAX_VALUE, Integer.MIN_VALUE)); |
73 |
| - Assert.assertEquals("-1", service.sumAsString(-2, 1)); |
74 |
| - Assert.assertEquals((Long) 100L, service.boxIfNotZero(100)); |
75 |
| - Assert.assertNull(service.boxIfNotZero(0)); |
76 |
| - } |
77 |
| - |
78 |
| - @Test |
79 |
| - public void testException() { |
80 |
| - try { |
81 |
| - service.testException(); |
82 |
| - Assert.fail("should throw MotanServiceException"); |
83 |
| - } catch (MotanServiceException mse){ |
84 |
| - Assert.assertTrue(mse.getMessage().contains(MotanBizException.class.getName())); |
85 |
| - Assert.assertTrue(mse.getMessage().contains("provider call process error")); |
86 |
| - } |
87 |
| - } |
88 |
| - |
89 |
| - @Test |
90 |
| - public void testNull() { |
91 |
| - Assert.assertTrue(service.isNull(null)); |
92 |
| - |
93 |
| - User user = User.newBuilder().setId(120).setName("zhou").build(); |
94 |
| - |
95 |
| - Assert.assertFalse(service.isNull(user)); |
96 |
| - } |
97 |
| - |
98 |
| - @Test |
99 |
| - public void testProtobuf() { |
100 |
| - Address address = service.queryByUid(1); |
101 |
| - Assert.assertEquals(1, address.getId()); |
102 |
| - |
103 |
| - User user = User.newBuilder().setId(120).setName("zhou").setGender(false).addAddress(address).build(); |
104 |
| - |
105 |
| - Assert.assertTrue(service.isUserAddress(user, address)); |
106 |
| - |
107 |
| - User newOne = service.copy(user); |
108 |
| - Assert.assertEquals(user.getId(), newOne.getId()); |
109 |
| - Assert.assertEquals(user.getName(), newOne.getName()); |
110 |
| - Assert.assertEquals(user.getGender(), newOne.getGender()); |
111 |
| - Assert.assertEquals(user.getAddress(0).getId(), newOne.getAddress(0).getId()); |
112 |
| - } |
113 |
| - |
114 |
| - @After |
115 |
| - public void tearDown() { |
116 |
| - refererConfig.destroy(); |
117 |
| - serviceConfig.unexport(); |
118 |
| - } |
| 32 | + private ServiceConfig<HelloService> serviceConfig; |
| 33 | + private RefererConfig<HelloService> refererConfig; |
| 34 | + private HelloService service; |
| 35 | + |
| 36 | + @Before |
| 37 | + public void setUp() throws InterruptedException { |
| 38 | + ProtocolConfig protocolConfig = new ProtocolConfig(); |
| 39 | + protocolConfig.setId("testMotan"); |
| 40 | + protocolConfig.setName("motan"); |
| 41 | + protocolConfig.setSerialization("protobuf"); |
| 42 | + protocolConfig.setCodec("motan"); |
| 43 | + |
| 44 | + RegistryConfig registryConfig = new RegistryConfig(); |
| 45 | + registryConfig.setAddress("127.0.0.1"); |
| 46 | + registryConfig.setPort(8002); |
| 47 | + |
| 48 | + serviceConfig = new ServiceConfig<>(); |
| 49 | + serviceConfig.setRef(new HelloServiceImpl()); |
| 50 | + serviceConfig.setInterface(HelloService.class); |
| 51 | + serviceConfig.setProtocol(protocolConfig); |
| 52 | + serviceConfig.setExport("testMotan:18002"); |
| 53 | + serviceConfig.setRegistry(registryConfig); |
| 54 | + serviceConfig.setShareChannel(true); |
| 55 | + serviceConfig.export(); |
| 56 | + |
| 57 | + refererConfig = new RefererConfig<>(); |
| 58 | + refererConfig.setDirectUrl("127.0.0.1:18002"); |
| 59 | + refererConfig.setProtocol(protocolConfig); |
| 60 | + refererConfig.setInterface(HelloService.class); |
| 61 | + // 设置超时时间为1秒 |
| 62 | + refererConfig.setRequestTimeout(1000); |
| 63 | + |
| 64 | + service = refererConfig.getRef(); |
| 65 | + Thread.sleep(20L); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testPrimitiveType() { |
| 70 | + Assert.assertEquals("-1", service.sumAsString(Integer.MAX_VALUE, Integer.MIN_VALUE)); |
| 71 | + Assert.assertEquals("-1", service.sumAsString(-2, 1)); |
| 72 | + Assert.assertEquals((Long) 100L, service.boxIfNotZero(100)); |
| 73 | + Assert.assertNull(service.boxIfNotZero(0)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testException() { |
| 78 | + try { |
| 79 | + service.testException(); |
| 80 | + Assert.fail("should throw MotanServiceException"); |
| 81 | + } catch (MotanServiceException mse) { |
| 82 | + Assert.assertTrue(mse.getMessage().contains(MotanBizException.class.getName())); |
| 83 | + Assert.assertTrue(mse.getMessage().contains("provider call process error")); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testNull() { |
| 89 | + Assert.assertTrue(service.isNull(null)); |
| 90 | + |
| 91 | + User user = User.newBuilder().setId(120).setName("zhou").build(); |
| 92 | + |
| 93 | + Assert.assertFalse(service.isNull(user)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testProtobuf() { |
| 98 | + Address address = service.queryByUid(1); |
| 99 | + Assert.assertEquals(1, address.getId()); |
| 100 | + |
| 101 | + User user = User.newBuilder().setId(120).setName("zhou").setGender(false).addAddress(address).build(); |
| 102 | + |
| 103 | + Assert.assertTrue(service.isUserAddress(user, address)); |
| 104 | + |
| 105 | + User newOne = service.copy(user); |
| 106 | + Assert.assertEquals(user.getId(), newOne.getId()); |
| 107 | + Assert.assertEquals(user.getName(), newOne.getName()); |
| 108 | + Assert.assertEquals(user.getGender(), newOne.getGender()); |
| 109 | + Assert.assertEquals(user.getAddress(0).getId(), newOne.getAddress(0).getId()); |
| 110 | + } |
| 111 | + |
| 112 | + @After |
| 113 | + public void tearDown() { |
| 114 | + refererConfig.destroy(); |
| 115 | + serviceConfig.unexport(); |
| 116 | + } |
119 | 117 | }
|
0 commit comments