1616 */
1717package org .apache .dubbo .rpc .protocol .dubbo .telnet ;
1818
19+ import org .apache .dubbo .common .URL ;
1920import org .apache .dubbo .common .utils .NetUtils ;
2021import org .apache .dubbo .remoting .Channel ;
22+ import org .apache .dubbo .remoting .ChannelHandler ;
2123import org .apache .dubbo .remoting .RemotingException ;
2224import org .apache .dubbo .remoting .telnet .TelnetHandler ;
2325import org .apache .dubbo .rpc .model .ApplicationModel ;
3032import org .junit .jupiter .api .BeforeEach ;
3133import org .junit .jupiter .api .Test ;
3234
35+ import java .net .InetSocketAddress ;
36+ import java .util .Map ;
37+ import java .util .concurrent .ConcurrentHashMap ;
38+
3339import static org .junit .jupiter .api .Assertions .assertEquals ;
3440import static org .junit .jupiter .api .Assertions .assertTrue ;
35- import static org .junit .jupiter .api .Assertions .fail ;
3641import static org .mockito .BDDMockito .given ;
3742import static org .mockito .Mockito .mock ;
43+ import static org .mockito .Mockito .spy ;
3844
3945/**
40- * CountTelnetHandlerTest .java
46+ * InvokeTelnetHandlerTest .java
4147 */
4248public class InvokerTelnetHandlerTest {
4349
4450 private static TelnetHandler invoke = new InvokeTelnetHandler ();
51+ private static TelnetHandler select = new SelectTelnetHandler ();
4552 private Channel mockChannel ;
4653
4754 @ BeforeEach
@@ -56,136 +63,104 @@ public void after() {
5663
5764 @ SuppressWarnings ("unchecked" )
5865 @ Test
59- public void testInvokeDefaultSService () throws RemotingException {
66+ public void testInvokeDefaultService () throws RemotingException {
6067 mockChannel = mock (Channel .class );
61- given (mockChannel .getAttribute ("telnet.service" )).willReturn ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" );
68+ given (mockChannel .getAttribute ("telnet.service" )).willReturn (DemoService . class . getName () );
6269 given (mockChannel .getLocalAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:5555" ));
6370 given (mockChannel .getRemoteAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:20886" ));
6471
6572 ProviderModel providerModel = new ProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , "Dubbo" , "1.0.0" , new DemoServiceImpl (), DemoService .class );
66- ApplicationModel .initProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , providerModel );
73+ ApplicationModel .initProviderModel (DemoService . class . getName () , providerModel );
6774
68- String result = invoke .telnet (mockChannel , "DemoService. echo(\" ok\" )" );
69- assertTrue (result .contains ("Use default service org.apache.dubbo.rpc.protocol.dubbo.support.DemoService. \r \n \ " ok\" \r \n " ));
75+ String result = invoke .telnet (mockChannel , "echo(\" ok\" )" );
76+ assertTrue (result .contains ("result: \ " ok\" " ));
7077 }
7178
7279 @ SuppressWarnings ("unchecked" )
7380 @ Test
74- public void testInvokeByPassingNullValue () throws RemotingException {
81+ public void testInvokeWithSpecifyService () throws RemotingException {
7582 mockChannel = mock (Channel .class );
76- given (mockChannel .getAttribute ("telnet.service" )).willReturn ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" );
83+ given (mockChannel .getAttribute ("telnet.service" )).willReturn (null );
7784 given (mockChannel .getLocalAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:5555" ));
7885 given (mockChannel .getRemoteAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:20886" ));
7986
8087 ProviderModel providerModel = new ProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , "Dubbo" , "1.0.0" , new DemoServiceImpl (), DemoService .class );
8188 ApplicationModel .initProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , providerModel );
8289
83- // pass null value to parameter of primitive type
84- try {
85- invoke .telnet (mockChannel , "DemoService.add(null, 2)" );
86- fail ("It should cause a NullPointerException by the above code." );
87- } catch (NullPointerException ex ) {
88- String message = ex .getMessage ();
89- assertEquals ("The type of No.1 parameter is primitive(int), but the value passed is null." , message );
90- }
91-
92- try {
93- invoke .telnet (mockChannel , "DemoService.add(1, null)" );
94- fail ("It should cause a NullPointerException by the above code." );
95- } catch (NullPointerException ex ) {
96- String message = ex .getMessage ();
97- assertEquals ("The type of No.2 parameter is primitive(long), but the value passed is null." , message );
98- }
99-
100- // pass null value to parameter of object type
101- try {
102- invoke .telnet (mockChannel , "DemoService.sayHello(null)" );
103- } catch (NullPointerException ex ) {
104- fail ("It shouldn't cause a NullPointerException by the above code." );
105- }
90+ String result = invoke .telnet (mockChannel , "DemoService.echo(\" ok\" )" );
91+ assertTrue (result .contains ("result: \" ok\" " ));
10692 }
10793
94+ @ SuppressWarnings ("unchecked" )
10895 @ Test
109- public void testInvokeByPassingEnumValue () throws RemotingException {
96+ public void testInvokeByPassingNullValue () {
11097 mockChannel = mock (Channel .class );
111- given (mockChannel .getAttribute ("telnet.service" )).willReturn (null );
98+ given (mockChannel .getAttribute ("telnet.service" )).willReturn (DemoService . class . getName () );
11299 given (mockChannel .getLocalAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:5555" ));
113100 given (mockChannel .getRemoteAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:20886" ));
114101
115102 ProviderModel providerModel = new ProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , "Dubbo" , "1.0.0" , new DemoServiceImpl (), DemoService .class );
116103 ApplicationModel .initProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , providerModel );
117104
118- String result = invoke .telnet (mockChannel , "getType(\" High\" )" );
119- assertTrue (result .contains ("High" ));
105+ // pass null value to parameter of primitive type
106+ try {
107+ invoke .telnet (mockChannel , "sayHello(null)" );
108+ } catch (Exception ex ) {
109+ assertTrue (ex instanceof NullPointerException );
110+ }
120111 }
121112
122-
123- @ SuppressWarnings ("unchecked" )
124113 @ Test
125- public void testComplexParamWithoutSpecifyParamType () throws RemotingException {
114+ public void testInvokeByPassingEnumValue () throws RemotingException {
126115 mockChannel = mock (Channel .class );
127- given (mockChannel .getAttribute ("telnet.service" )).willReturn ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" );
116+ given (mockChannel .getAttribute ("telnet.service" )).willReturn (null );
128117 given (mockChannel .getLocalAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:5555" ));
129118 given (mockChannel .getRemoteAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:20886" ));
130119
131120 ProviderModel providerModel = new ProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , "Dubbo" , "1.0.0" , new DemoServiceImpl (), DemoService .class );
132121 ApplicationModel .initProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , providerModel );
133122
134- // pass json value to parameter of Person type
135-
136- String result = invoke .telnet (mockChannel , "DemoService.getPerson({\" name\" :\" zhangsan\" ,\" age\" :12})" );
137- assertTrue (result .contains ("No such method getPerson in service DemoService" ));
123+ String result = invoke .telnet (mockChannel , "getType(\" High\" )" );
124+ assertTrue (result .contains ("result: \" High\" " ));
138125 }
139126
127+
140128 @ SuppressWarnings ("unchecked" )
141129 @ Test
142- public void testComplexParamSpecifyParamType () throws RemotingException {
130+ public void testOverriddenMethodWithSpecifyParamType () throws RemotingException {
143131 mockChannel = mock (Channel .class );
144- given (mockChannel .getAttribute ("telnet.service" )).willReturn ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" );
132+ given (mockChannel .getAttribute ("telnet.service" )).willReturn (DemoService . class . getName () );
145133 given (mockChannel .getLocalAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:5555" ));
146134 given (mockChannel .getRemoteAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:20886" ));
147135
148136 ProviderModel providerModel = new ProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , "Dubbo" , "1.0.0" , new DemoServiceImpl (), DemoService .class );
149137 ApplicationModel .initProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , providerModel );
150138
151- // pass json value to parameter of Person type and specify it's type
152- // one parameter with type of Person
153- String result = invoke .telnet (mockChannel , "DemoService.getPerson({\" name\" :\" zhangsan\" ,\" age\" :12}) -p org.apache.dubbo.rpc.protocol.dubbo.support.Person" );
154- assertTrue (result .contains ("Use default service org.apache.dubbo.rpc.protocol.dubbo.support.DemoService.\r \n 1\r \n " ));
155-
156- // two parameter with type of Person
157- result = invoke .telnet (mockChannel , "DemoService.getPerson({\" name\" :\" zhangsan\" ,\" age\" :12},{\" name\" :\" lisi\" ,\" age\" :12}) " +
158- "-p org.apache.dubbo.rpc.protocol.dubbo.support.Person " +
159- "org.apache.dubbo.rpc.protocol.dubbo.support.Person" );
160- assertTrue (result .contains ("Use default service org.apache.dubbo.rpc.protocol.dubbo.support.DemoService.\r \n 2\r \n " ));
139+ String result = invoke .telnet (mockChannel , "getPerson({\" name\" :\" zhangsan\" ,\" age\" :12,\" class\" :\" org.apache.dubbo.rpc.protocol.dubbo.support.Person\" })" );
140+ assertTrue (result .contains ("result: 12" ));
161141 }
162142
163- @ SuppressWarnings ("unchecked" )
164143 @ Test
165- public void testComplexParamSpecifyWrongParamType () throws RemotingException {
166- mockChannel = mock (Channel .class );
167- given (mockChannel .getAttribute ("telnet.service" )).willReturn ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" );
144+ public void testInvokeOverriddenMethodBySelect () throws RemotingException {
145+ //create a real instance to keep the attribute values;
146+ mockChannel = spy (getChannelInstance ());
147+ given (mockChannel .getAttribute ("telnet.service" )).willReturn (DemoService .class .getName ());
168148 given (mockChannel .getLocalAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:5555" ));
169149 given (mockChannel .getRemoteAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:20886" ));
170150
171151 ProviderModel providerModel = new ProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , "Dubbo" , "1.0.0" , new DemoServiceImpl (), DemoService .class );
172152 ApplicationModel .initProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , providerModel );
173153
174- // pass json value to parameter of Person type
175- // wrong name of parameter class
176- String result = invoke .telnet (mockChannel , "DemoService.getPerson({\" name\" :\" zhangsan\" ,\" age\" :12}) -p wrongType" );
177- assertEquals ("Unknown parameter class for name wrongType" , result );
178-
179- // wrong number of parameter class
180- result = invoke .telnet (mockChannel , "DemoService.getPerson({\" name\" :\" zhangsan\" ,\" age\" :12},{\" name\" :\" lisi\" ,\" age\" :12}) " +
181- "-p org.apache.dubbo.rpc.protocol.dubbo.support.Person" );
182- assertEquals ("Parameter's number does not match the number of parameter class" , result );
154+ String param = "{\" name\" :\" Dubbo\" ,\" age\" :8}" ;
155+ String result = invoke .telnet (mockChannel , "getPerson(" + param + ")" );
156+ assertTrue (result .contains ("Please use the select command to select the method you want to invoke. eg: select 1" ));
157+ result = select .telnet (mockChannel , "1" );
158+ //result dependent on method order.
159+ assertTrue (result .contains ("result: 8" ) || result .contains ("result: \" Dubbo\" " ));
183160 }
184161
185-
186- @ SuppressWarnings ("unchecked" )
187162 @ Test
188- public void testInvokeAutoFindMethod () throws RemotingException {
163+ public void testInvokeMultiJsonParamMethod () throws RemotingException {
189164 mockChannel = mock (Channel .class );
190165 given (mockChannel .getAttribute ("telnet.service" )).willReturn (null );
191166 given (mockChannel .getLocalAddress ()).willReturn (NetUtils .toAddress ("127.0.0.1:5555" ));
@@ -194,8 +169,9 @@ public void testInvokeAutoFindMethod() throws RemotingException {
194169 ProviderModel providerModel = new ProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , "Dubbo" , "1.0.0" , new DemoServiceImpl (), DemoService .class );
195170 ApplicationModel .initProviderModel ("org.apache.dubbo.rpc.protocol.dubbo.support.DemoService" , providerModel );
196171
197- String result = invoke .telnet (mockChannel , "echo(\" ok\" )" );
198- assertTrue (result .contains ("ok" ));
172+ String param = "{\" name\" :\" Dubbo\" ,\" age\" :8},{\" name\" :\" Apache\" ,\" age\" :20}" ;
173+ String result = invoke .telnet (mockChannel , "getPerson(" + param + ")" );
174+ assertTrue (result .contains ("result: 28" ));
199175 }
200176
201177 @ Test
@@ -212,8 +188,93 @@ public void testMessageNull() throws RemotingException {
212188 public void testInvalidMessage () throws RemotingException {
213189 mockChannel = mock (Channel .class );
214190 given (mockChannel .getAttribute ("telnet.service" )).willReturn (null );
215-
216191 String result = invoke .telnet (mockChannel , "(" );
217192 assertEquals ("Invalid parameters, format: service.method(args)" , result );
218193 }
194+
195+ private Channel getChannelInstance () {
196+ return new Channel () {
197+ private final Map <String , Object > attributes = new ConcurrentHashMap <String , Object >();
198+
199+ @ Override
200+ public InetSocketAddress getRemoteAddress () {
201+ return null ;
202+ }
203+
204+ @ Override
205+ public boolean isConnected () {
206+ return false ;
207+ }
208+
209+ @ Override
210+ public boolean hasAttribute (String key ) {
211+ return attributes .containsKey (key );
212+ }
213+
214+ @ Override
215+ public Object getAttribute (String key ) {
216+ return attributes .get (key );
217+ }
218+
219+ @ Override
220+ public void setAttribute (String key , Object value ) {
221+ if (value == null ) { // The null value unallowed in the ConcurrentHashMap.
222+ attributes .remove (key );
223+ } else {
224+ attributes .put (key , value );
225+ }
226+ }
227+
228+ @ Override
229+ public void removeAttribute (String key ) {
230+ attributes .remove (key );
231+ }
232+
233+
234+ @ Override
235+ public URL getUrl () {
236+ return null ;
237+ }
238+
239+ @ Override
240+ public ChannelHandler getChannelHandler () {
241+ return null ;
242+ }
243+
244+ @ Override
245+ public InetSocketAddress getLocalAddress () {
246+ return null ;
247+ }
248+
249+ @ Override
250+ public void send (Object message ) throws RemotingException {
251+
252+ }
253+
254+ @ Override
255+ public void send (Object message , boolean sent ) throws RemotingException {
256+
257+ }
258+
259+ @ Override
260+ public void close () {
261+
262+ }
263+
264+ @ Override
265+ public void close (int timeout ) {
266+
267+ }
268+
269+ @ Override
270+ public void startClose () {
271+
272+ }
273+
274+ @ Override
275+ public boolean isClosed () {
276+ return false ;
277+ }
278+ };
279+ }
219280}
0 commit comments