From b3b9477cacd270dea4a835b20d5f0fe9595b35ef Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 1 Nov 2017 13:36:26 -0400 Subject: [PATCH] Use 'application' if spring.application.name is not set. Also log a warning. fixes gh-152 --- .../ZookeeperPropertySourceLocator.java | 5 +++ ...tySourceLocatorNoApplicationNameTests.java | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorNoApplicationNameTests.java diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java index 10a660b9..5805d423 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java @@ -81,6 +81,11 @@ public class ZookeeperPropertySourceLocator implements PropertySourceLocator { if (environment instanceof ConfigurableEnvironment) { ConfigurableEnvironment env = (ConfigurableEnvironment) environment; String appName = env.getProperty("spring.application.name"); + if (appName == null) { + // use default "application" (which config client does) + appName = "application"; + log.warn("spring.application.name is not set. Using default of 'application'"); + } List profiles = Arrays.asList(env.getActiveProfiles()); String root = this.properties.getRoot(); diff --git a/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorNoApplicationNameTests.java b/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorNoApplicationNameTests.java new file mode 100644 index 00000000..715d9a30 --- /dev/null +++ b/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorNoApplicationNameTests.java @@ -0,0 +1,42 @@ +/* + * Copyright 2013-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.zookeeper.config; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.api.GetChildrenBuilder; +import org.junit.Test; +import org.mockito.Mockito; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.mock.env.MockEnvironment; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * @author Spencer Gibb + */ +public class ZookeeperPropertySourceLocatorNoApplicationNameTests { + + @Test + public void defaultSpringApplicationNameWorks() { + CuratorFramework curator = mock(CuratorFramework.class); + when(curator.getChildren()).thenReturn(mock(GetChildrenBuilder.class)); + ZookeeperPropertySourceLocator locator = new ZookeeperPropertySourceLocator(curator, new ZookeeperConfigProperties()); + locator.locate(new MockEnvironment()); + } +}