Skip to content

Commit 6a1fe5b

Browse files
authored
unit test for dubbo-config-api (apache#1733)
* finish unit test for AbstractMethodConfig * finish unit test for AbstractReferenceConfig * move to right package * finish unit test for AbstractReferenceConfig * finish unit test for AbstractMethodConfig * finish unit test for AbstractReferenceConfig * move to right package * finish unit test for AbstractReferenceConfig * unit test for AbstractServiceConfig, also fix logic issue in setListener/getListener
1 parent 836b1b1 commit 6a1fe5b

File tree

8 files changed

+544
-2
lines changed

8 files changed

+544
-2
lines changed

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractServiceConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ public String getFilter() {
207207
@Override
208208
@Parameter(key = Constants.EXPORTER_LISTENER_KEY, append = true)
209209
public String getListener() {
210-
return super.getListener();
210+
return listener;
211211
}
212212

213213
@Override
214214
public void setListener(String listener) {
215215
checkMultiExtension(ExporterListener.class, "listener", listener);
216-
super.setListener(listener);
216+
this.listener = listener;
217217
}
218218

219219
public Boolean isRegister() {
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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.config;
18+
19+
import org.junit.Test;
20+
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
import static org.hamcrest.Matchers.equalTo;
25+
import static org.hamcrest.Matchers.is;
26+
import static org.hamcrest.Matchers.isEmptyOrNullString;
27+
import static org.hamcrest.Matchers.sameInstance;
28+
import static org.junit.Assert.assertThat;
29+
30+
public class AbstractMethodConfigTest {
31+
@Test
32+
public void testTimeout() throws Exception {
33+
MethodConfig methodConfig = new MethodConfig();
34+
methodConfig.setTimeout(10);
35+
assertThat(methodConfig.getTimeout(), equalTo(10));
36+
}
37+
38+
@Test
39+
public void testRetries() throws Exception {
40+
MethodConfig methodConfig = new MethodConfig();
41+
methodConfig.setRetries(3);
42+
assertThat(methodConfig.getRetries(), equalTo(3));
43+
}
44+
45+
@Test
46+
public void testLoadbalance() throws Exception {
47+
MethodConfig methodConfig = new MethodConfig();
48+
methodConfig.setLoadbalance("mockloadbalance");
49+
assertThat(methodConfig.getLoadbalance(), equalTo("mockloadbalance"));
50+
}
51+
52+
@Test
53+
public void testAsync() throws Exception {
54+
MethodConfig methodConfig = new MethodConfig();
55+
methodConfig.setAsync(true);
56+
assertThat(methodConfig.isAsync(), is(true));
57+
}
58+
59+
@Test
60+
public void testActives() throws Exception {
61+
MethodConfig methodConfig = new MethodConfig();
62+
methodConfig.setActives(10);
63+
assertThat(methodConfig.getActives(), equalTo(10));
64+
}
65+
66+
@Test
67+
public void testSent() throws Exception {
68+
MethodConfig methodConfig = new MethodConfig();
69+
methodConfig.setSent(true);
70+
assertThat(methodConfig.getSent(), is(true));
71+
}
72+
73+
@Test
74+
public void testMock() throws Exception {
75+
MethodConfig methodConfig = new MethodConfig();
76+
methodConfig.setMock((Boolean) null);
77+
assertThat(methodConfig.getMock(), isEmptyOrNullString());
78+
methodConfig.setMock(true);
79+
assertThat(methodConfig.getMock(), equalTo("true"));
80+
methodConfig.setMock("return null");
81+
assertThat(methodConfig.getMock(), equalTo("return null"));
82+
methodConfig.setMock("mock");
83+
assertThat(methodConfig.getMock(), equalTo("mock"));
84+
}
85+
86+
@Test
87+
public void testMerger() throws Exception {
88+
MethodConfig methodConfig = new MethodConfig();
89+
methodConfig.setMerger("merger");
90+
assertThat(methodConfig.getMerger(), equalTo("merger"));
91+
}
92+
93+
@Test
94+
public void testCache() throws Exception {
95+
MethodConfig methodConfig = new MethodConfig();
96+
methodConfig.setCache("cache");
97+
assertThat(methodConfig.getCache(), equalTo("cache"));
98+
}
99+
100+
@Test
101+
public void testValidation() throws Exception {
102+
MethodConfig methodConfig = new MethodConfig();
103+
methodConfig.setValidation("validation");
104+
assertThat(methodConfig.getValidation(), equalTo("validation"));
105+
}
106+
107+
@Test
108+
public void testParameters() throws Exception {
109+
MethodConfig methodConfig = new MethodConfig();
110+
Map<String, String> parameters = new HashMap<String, String>();
111+
parameters.put("key", "value");
112+
methodConfig.setParameters(parameters);
113+
assertThat(methodConfig.getParameters(), sameInstance(parameters));
114+
}
115+
116+
private static class MethodConfig extends AbstractMethodConfig {
117+
118+
}
119+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
18+
package com.alibaba.dubbo.config;
19+
20+
import com.alibaba.dubbo.common.Constants;
21+
import org.junit.Test;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
import static org.hamcrest.Matchers.equalTo;
27+
import static org.hamcrest.Matchers.hasKey;
28+
import static org.hamcrest.Matchers.hasValue;
29+
import static org.hamcrest.Matchers.is;
30+
import static org.junit.Assert.assertThat;
31+
32+
public class AbstractReferenceConfigTest {
33+
34+
@Test
35+
public void testCheck() throws Exception {
36+
ReferenceConfig referenceConfig = new ReferenceConfig();
37+
referenceConfig.setCheck(true);
38+
assertThat(referenceConfig.isCheck(), is(true));
39+
}
40+
41+
@Test
42+
public void testInit() throws Exception {
43+
ReferenceConfig referenceConfig = new ReferenceConfig();
44+
referenceConfig.setInit(true);
45+
assertThat(referenceConfig.isInit(), is(true));
46+
}
47+
48+
@Test
49+
public void testGeneric() throws Exception {
50+
ReferenceConfig referenceConfig = new ReferenceConfig();
51+
referenceConfig.setGeneric(true);
52+
assertThat(referenceConfig.isGeneric(), is(true));
53+
Map<String, String> parameters = new HashMap<String, String>();
54+
AbstractInterfaceConfig.appendParameters(parameters, referenceConfig);
55+
// FIXME: not sure why AbstractReferenceConfig has both isGeneric and getGeneric
56+
assertThat(parameters, hasKey("generic"));
57+
}
58+
59+
@Test
60+
public void testInjvm() throws Exception {
61+
ReferenceConfig referenceConfig = new ReferenceConfig();
62+
referenceConfig.setInit(true);
63+
assertThat(referenceConfig.isInit(), is(true));
64+
}
65+
66+
@Test
67+
public void testFilter() throws Exception {
68+
ReferenceConfig referenceConfig = new ReferenceConfig();
69+
referenceConfig.setFilter("mockfilter");
70+
assertThat(referenceConfig.getFilter(), equalTo("mockfilter"));
71+
Map<String, String> parameters = new HashMap<String, String>();
72+
parameters.put(Constants.REFERENCE_FILTER_KEY, "prefilter");
73+
AbstractInterfaceConfig.appendParameters(parameters, referenceConfig);
74+
assertThat(parameters, hasValue("prefilter,mockfilter"));
75+
}
76+
77+
@Test
78+
public void testListener() throws Exception {
79+
ReferenceConfig referenceConfig = new ReferenceConfig();
80+
referenceConfig.setListener("mockinvokerlistener");
81+
assertThat(referenceConfig.getListener(), equalTo("mockinvokerlistener"));
82+
Map<String, String> parameters = new HashMap<String, String>();
83+
parameters.put(Constants.INVOKER_LISTENER_KEY, "prelistener");
84+
AbstractInterfaceConfig.appendParameters(parameters, referenceConfig);
85+
assertThat(parameters, hasValue("prelistener,mockinvokerlistener"));
86+
}
87+
88+
@Test
89+
public void testLazy() throws Exception {
90+
ReferenceConfig referenceConfig = new ReferenceConfig();
91+
referenceConfig.setLazy(true);
92+
assertThat(referenceConfig.getLazy(), is(true));
93+
}
94+
95+
@Test
96+
public void testOnconnect() throws Exception {
97+
ReferenceConfig referenceConfig = new ReferenceConfig();
98+
referenceConfig.setOnconnect("onConnect");
99+
assertThat(referenceConfig.getOnconnect(), equalTo("onConnect"));
100+
assertThat(referenceConfig.getStubevent(), is(true));
101+
}
102+
103+
@Test
104+
public void testOndisconnect() throws Exception {
105+
ReferenceConfig referenceConfig = new ReferenceConfig();
106+
referenceConfig.setOndisconnect("onDisconnect");
107+
assertThat(referenceConfig.getOndisconnect(), equalTo("onDisconnect"));
108+
assertThat(referenceConfig.getStubevent(), is(true));
109+
}
110+
111+
@Test
112+
public void testStubevent() throws Exception {
113+
ReferenceConfig referenceConfig = new ReferenceConfig();
114+
referenceConfig.setOnconnect("onConnect");
115+
Map<String, String> parameters = new HashMap<String, String>();
116+
AbstractInterfaceConfig.appendParameters(parameters, referenceConfig);
117+
assertThat(parameters, hasKey(Constants.STUB_EVENT_KEY));
118+
}
119+
120+
@Test
121+
public void testReconnect() throws Exception {
122+
ReferenceConfig referenceConfig = new ReferenceConfig();
123+
referenceConfig.setReconnect("reconnect");
124+
Map<String, String> parameters = new HashMap<String, String>();
125+
AbstractInterfaceConfig.appendParameters(parameters, referenceConfig);
126+
assertThat(referenceConfig.getReconnect(), equalTo("reconnect"));
127+
assertThat(parameters, hasKey(Constants.RECONNECT_KEY));
128+
}
129+
130+
@Test
131+
public void testSticky() throws Exception {
132+
ReferenceConfig referenceConfig = new ReferenceConfig();
133+
referenceConfig.setSticky(true);
134+
Map<String, String> parameters = new HashMap<String, String>();
135+
AbstractInterfaceConfig.appendParameters(parameters, referenceConfig);
136+
assertThat(referenceConfig.getSticky(), is(true));
137+
assertThat(parameters, hasKey(Constants.CLUSTER_STICKY_KEY));
138+
}
139+
140+
@Test
141+
public void testVersion() throws Exception {
142+
ReferenceConfig referenceConfig = new ReferenceConfig();
143+
referenceConfig.setVersion("version");
144+
assertThat(referenceConfig.getVersion(), equalTo("version"));
145+
}
146+
147+
@Test
148+
public void testGroup() throws Exception {
149+
ReferenceConfig referenceConfig = new ReferenceConfig();
150+
referenceConfig.setGroup("group");
151+
assertThat(referenceConfig.getGroup(), equalTo("group"));
152+
}
153+
154+
private static class ReferenceConfig extends AbstractReferenceConfig {
155+
156+
}
157+
}

0 commit comments

Comments
 (0)