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 .spring .schema ;
18+
19+ import com .alibaba .dubbo .config .ApplicationConfig ;
20+ import com .alibaba .dubbo .config .ModuleConfig ;
21+ import com .alibaba .dubbo .config .MonitorConfig ;
22+ import com .alibaba .dubbo .config .ProtocolConfig ;
23+ import com .alibaba .dubbo .config .ProviderConfig ;
24+ import com .alibaba .dubbo .config .spring .ConfigTest ;
25+ import com .alibaba .dubbo .config .spring .ServiceBean ;
26+ import com .alibaba .dubbo .config .spring .api .DemoService ;
27+ import com .alibaba .dubbo .config .spring .impl .DemoServiceImpl ;
28+ import org .junit .Test ;
29+ import org .springframework .beans .factory .BeanCreationException ;
30+ import org .springframework .context .support .ClassPathXmlApplicationContext ;
31+
32+ import java .util .Map ;
33+
34+ import static org .hamcrest .CoreMatchers .is ;
35+ import static org .hamcrest .CoreMatchers .not ;
36+ import static org .hamcrest .CoreMatchers .nullValue ;
37+ import static org .junit .Assert .assertThat ;
38+
39+ public class DubboNamespaceHandlerTest {
40+ @ Test
41+ public void testProviderXml () {
42+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/demo-provider.xml" );
43+ ctx .start ();
44+
45+ ProtocolConfig protocolConfig = ctx .getBean (ProtocolConfig .class );
46+ assertThat (protocolConfig , not (nullValue ()));
47+ assertThat (protocolConfig .getName (), is ("dubbo" ));
48+ assertThat (protocolConfig .getPort (), is (20813 ));
49+
50+ ApplicationConfig applicationConfig = ctx .getBean (ApplicationConfig .class );
51+ assertThat (applicationConfig , not (nullValue ()));
52+ assertThat (applicationConfig .getName (), is ("demo-provider" ));
53+
54+ DemoService service = ctx .getBean (DemoService .class );
55+ assertThat (service , not (nullValue ()));
56+ }
57+
58+ @ Test
59+ public void testMultiProtocol () {
60+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/multi-protocol.xml" );
61+ ctx .start ();
62+
63+ Map <String , ProtocolConfig > protocolConfigMap = ctx .getBeansOfType (ProtocolConfig .class );
64+ assertThat (protocolConfigMap .size (), is (2 ));
65+
66+ ProtocolConfig rmiProtocolConfig = protocolConfigMap .get ("rmi" );
67+ assertThat (rmiProtocolConfig .getPort (), is (10991 ));
68+
69+ ProtocolConfig dubboProtocolConfig = protocolConfigMap .get ("dubbo" );
70+ assertThat (dubboProtocolConfig .getPort (), is (20881 ));
71+ }
72+
73+ @ Test
74+ public void testDefaultProtocol () {
75+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/override-protocol.xml" );
76+ ctx .start ();
77+
78+ ProtocolConfig protocolConfig = ctx .getBean (ProtocolConfig .class );
79+ assertThat (protocolConfig .getName (), is ("dubbo" ));
80+ }
81+
82+ @ Test
83+ public void testCustomParameter () {
84+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/customize-parameter.xml" );
85+ ctx .start ();
86+
87+ ProtocolConfig protocolConfig = ctx .getBean (ProtocolConfig .class );
88+ assertThat (protocolConfig .getParameters ().size (), is (1 ));
89+ assertThat (protocolConfig .getParameters ().get ("protocol-paramA" ), is ("protocol-paramA" ));
90+
91+ ServiceBean serviceBean = ctx .getBean (ServiceBean .class );
92+ assertThat (serviceBean .getParameters ().size (), is (1 ));
93+ assertThat (serviceBean .getParameters ().get ("service-paramA" ), is ("service-paramA" ));
94+ }
95+
96+
97+ @ Test
98+ public void testDelayFixedTime () {
99+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/delay-fixed-time.xml" );
100+ ctx .start ();
101+
102+ assertThat (ctx .getBean (ServiceBean .class ).getDelay (), is (300 ));
103+ }
104+
105+ @ Test
106+ public void testTimeoutConfig () {
107+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/provider-nested-service.xml" );
108+ ctx .start ();
109+
110+ Map <String , ProviderConfig > providerConfigMap = ctx .getBeansOfType (ProviderConfig .class );
111+
112+ assertThat (providerConfigMap .get ("com.alibaba.dubbo.config.ProviderConfig" ).getTimeout (), is (2000 ));
113+ }
114+
115+ @ Test
116+ public void testMonitor () {
117+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/provider-with-monitor.xml" );
118+ ctx .start ();
119+
120+ assertThat (ctx .getBean (MonitorConfig .class ), not (nullValue ()));
121+ }
122+
123+ @ Test (expected = BeanCreationException .class )
124+ public void testMultiMonitor () {
125+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/multi-monitor.xml" );
126+ ctx .start ();
127+ }
128+
129+ @ Test (expected = BeanCreationException .class )
130+ public void testMultiProviderConfig () {
131+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/provider-multi.xml" );
132+ ctx .start ();
133+ }
134+
135+ @ Test
136+ public void testModuleInfo () {
137+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/provider-with-module.xml" );
138+ ctx .start ();
139+
140+ ModuleConfig moduleConfig = ctx .getBean (ModuleConfig .class );
141+ assertThat (moduleConfig .getName (), is ("test-module" ));
142+ }
143+
144+ @ Test (expected = BeanCreationException .class )
145+ public void testNotificationWithWrongBean () {
146+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/consumer-notification.xml" );
147+ ctx .start ();
148+ }
149+
150+ @ Test
151+ public void testProperty () {
152+ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext (ConfigTest .class .getPackage ().getName ().replace ('.' , '/' ) + "/service-class.xml" );
153+ ctx .start ();
154+
155+ ServiceBean serviceBean = ctx .getBean (ServiceBean .class );
156+
157+ String prefix = ((DemoServiceImpl ) serviceBean .getRef ()).getPrefix ();
158+ assertThat (prefix , is ("welcome:" ));
159+ }
160+ }
0 commit comments