|
| 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 org.apache.dubbo.spring.boot.context.event; |
| 18 | + |
| 19 | +import org.apache.dubbo.common.utils.SystemPropertyConfigUtils; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.springframework.boot.SpringApplication; |
| 26 | +import org.springframework.boot.WebApplicationType; |
| 27 | +import org.springframework.boot.builder.SpringApplicationBuilder; |
| 28 | +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; |
| 29 | +import org.springframework.context.ApplicationListener; |
| 30 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 31 | +import org.springframework.core.env.MapPropertySource; |
| 32 | +import org.springframework.core.env.MutablePropertySources; |
| 33 | + |
| 34 | +import static org.apache.dubbo.common.constants.CommonConstants.DubboProperty.DUBBO_NETWORK_IGNORED_INTERFACE; |
| 35 | +import static org.apache.dubbo.common.constants.CommonConstants.DubboProperty.DUBBO_PREFERRED_NETWORK_INTERFACE; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 37 | + |
| 38 | +/** |
| 39 | + * @since 3.3 |
| 40 | + */ |
| 41 | +public class DubboNetInterfaceConfigApplicationListenerTest { |
| 42 | + |
| 43 | + private static final String USE_NETWORK_INTERFACE_NAME = "eth0"; |
| 44 | + |
| 45 | + private static final String IGNORED_NETWORK_INTERFACE_NAME = "eth1"; |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testOnApplicationEvent() { |
| 49 | + |
| 50 | + SpringApplicationBuilder builder = |
| 51 | + new SpringApplicationBuilder(DubboNetInterfaceConfigApplicationListenerTest.class); |
| 52 | + builder.listeners(new NetworkInterfaceApplicationListener()); |
| 53 | + builder.web(WebApplicationType.NONE); |
| 54 | + SpringApplication application = builder.build(); |
| 55 | + application.run(); |
| 56 | + |
| 57 | + String preferredNetworkInterface = |
| 58 | + SystemPropertyConfigUtils.getSystemProperty(DUBBO_PREFERRED_NETWORK_INTERFACE); |
| 59 | + String ignoredNetworkInterface = SystemPropertyConfigUtils.getSystemProperty(DUBBO_NETWORK_IGNORED_INTERFACE); |
| 60 | + assertEquals(USE_NETWORK_INTERFACE_NAME, preferredNetworkInterface); |
| 61 | + assertEquals(IGNORED_NETWORK_INTERFACE_NAME, ignoredNetworkInterface); |
| 62 | + } |
| 63 | + |
| 64 | + static class NetworkInterfaceApplicationListener |
| 65 | + implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) { |
| 69 | + ConfigurableEnvironment environment = applicationEnvironmentPreparedEvent.getEnvironment(); |
| 70 | + MutablePropertySources propertySources = environment.getPropertySources(); |
| 71 | + |
| 72 | + Map<String, Object> map = new HashMap<>(); |
| 73 | + map.put(DUBBO_PREFERRED_NETWORK_INTERFACE, USE_NETWORK_INTERFACE_NAME); |
| 74 | + map.put(DUBBO_NETWORK_IGNORED_INTERFACE, IGNORED_NETWORK_INTERFACE_NAME); |
| 75 | + propertySources.addLast(new MapPropertySource("networkInterfaceConfig", map)); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments