Allows multiple repos in FileMonitorConfiguration. (#1247)
Fixes gh-1239
This commit is contained in:
committed by
Spencer Gibb
parent
3b3083b5e7
commit
03c26d528d
@@ -31,6 +31,7 @@ import java.nio.file.WatchService;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -57,6 +58,7 @@ import org.springframework.util.PatternMatchUtils;
|
||||
* (i.e. a git repository with a "file:" URI) or to a native repository.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Gilles Robert
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@@ -66,13 +68,13 @@ public class FileMonitorConfiguration implements SmartLifecycle, ResourceLoaderA
|
||||
private static final Log log = LogFactory.getLog(FileMonitorConfiguration.class);
|
||||
|
||||
@Autowired
|
||||
PropertyPathEndpoint endpoint;
|
||||
private PropertyPathEndpoint endpoint;
|
||||
|
||||
@Autowired(required = false)
|
||||
AbstractScmEnvironmentRepository scmRepository;
|
||||
private List<AbstractScmEnvironmentRepository> scmRepositories;
|
||||
|
||||
@Autowired(required = false)
|
||||
NativeEnvironmentRepository nativeEnvironmentRepository;
|
||||
private NativeEnvironmentRepository nativeEnvironmentRepository;
|
||||
|
||||
private boolean running;
|
||||
|
||||
@@ -185,17 +187,21 @@ public class FileMonitorConfiguration implements SmartLifecycle, ResourceLoaderA
|
||||
}
|
||||
|
||||
private Set<Path> getFileRepo() {
|
||||
if (this.scmRepository != null) {
|
||||
if (this.scmRepositories != null) {
|
||||
String repositoryUri = null;
|
||||
Set<Path> paths = new LinkedHashSet<>();
|
||||
try {
|
||||
|
||||
Resource resource = this.resourceLoader
|
||||
.getResource(this.scmRepository.getUri());
|
||||
if (resource instanceof FileSystemResource) {
|
||||
return Collections.singleton(Paths.get(resource.getURI()));
|
||||
for (AbstractScmEnvironmentRepository repository : scmRepositories) {
|
||||
repositoryUri = repository.getUri();
|
||||
Resource resource = this.resourceLoader.getResource(repositoryUri);
|
||||
if (resource instanceof FileSystemResource) {
|
||||
paths.add(Paths.get(resource.getURI()));
|
||||
}
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
catch (IOException e) {
|
||||
log.error("Cannot resolve URI for path: " + this.scmRepository.getUri());
|
||||
log.error("Cannot resolve URI for path: " + repositoryUri);
|
||||
}
|
||||
}
|
||||
if (this.nativeEnvironmentRepository != null) {
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright 2015-2019 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.config.monitor;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cloud.config.server.environment.AbstractScmEnvironmentRepository;
|
||||
import org.springframework.cloud.config.server.environment.JGitEnvironmentProperties;
|
||||
import org.springframework.cloud.config.server.environment.JGitEnvironmentRepository;
|
||||
import org.springframework.cloud.config.server.environment.NativeEnvironmentProperties;
|
||||
import org.springframework.cloud.config.server.environment.NativeEnvironmentRepository;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.io.FileSystemResourceLoader;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Gilles Robert
|
||||
*
|
||||
*/
|
||||
public class FileMonitorConfigurationTest {
|
||||
|
||||
private static final String SAMPLE_PATH = "resources/pathsamples";
|
||||
|
||||
private FileMonitorConfiguration fileMonitorConfiguration = new FileMonitorConfiguration();
|
||||
|
||||
private List<AbstractScmEnvironmentRepository> repositories = new ArrayList<>();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
fileMonitorConfiguration.setResourceLoader(new FileSystemResourceLoader());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
fileMonitorConfiguration.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStart_whenRepositoriesAreNull() {
|
||||
// given
|
||||
|
||||
// when
|
||||
fileMonitorConfiguration.start();
|
||||
|
||||
// then
|
||||
Set<Path> directory = getDirectory();
|
||||
assertThat(directory).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStart_withNativeEnvironmentRepository() {
|
||||
// given
|
||||
NativeEnvironmentRepository repository = createNativeEnvironmentRepository();
|
||||
ReflectionTestUtils.setField(fileMonitorConfiguration,
|
||||
"nativeEnvironmentRepository", repository);
|
||||
|
||||
// when
|
||||
fileMonitorConfiguration.start();
|
||||
|
||||
// then
|
||||
assertOnDirectory(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStart_withOneScmRepository() {
|
||||
// given
|
||||
AbstractScmEnvironmentRepository repository = createScmEnvironmentRepository(
|
||||
SAMPLE_PATH);
|
||||
addScmRepository(repository);
|
||||
|
||||
// when
|
||||
fileMonitorConfiguration.start();
|
||||
|
||||
// then
|
||||
assertOnDirectory(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStart_withTwoScmRepositories() {
|
||||
// given
|
||||
AbstractScmEnvironmentRepository repository = createScmEnvironmentRepository(
|
||||
SAMPLE_PATH);
|
||||
AbstractScmEnvironmentRepository secondRepository = createScmEnvironmentRepository(
|
||||
"anotherPath");
|
||||
addScmRepository(repository);
|
||||
addScmRepository(secondRepository);
|
||||
|
||||
// when
|
||||
fileMonitorConfiguration.start();
|
||||
|
||||
// then
|
||||
assertOnDirectory(2);
|
||||
}
|
||||
|
||||
private void addScmRepository(AbstractScmEnvironmentRepository... repository) {
|
||||
repositories.addAll(Arrays.asList(repository));
|
||||
ReflectionTestUtils.setField(fileMonitorConfiguration, "scmRepositories",
|
||||
repositories);
|
||||
}
|
||||
|
||||
private NativeEnvironmentRepository createNativeEnvironmentRepository() {
|
||||
ConfigurableEnvironment environment = createConfigurableEnvironment();
|
||||
NativeEnvironmentProperties properties = new NativeEnvironmentProperties();
|
||||
properties.setSearchLocations(new String[] { "classpath:pathsamples" });
|
||||
return new NativeEnvironmentRepository(environment, properties);
|
||||
}
|
||||
|
||||
private AbstractScmEnvironmentRepository createScmEnvironmentRepository(String uri) {
|
||||
ConfigurableEnvironment environment = createConfigurableEnvironment();
|
||||
JGitEnvironmentProperties properties = new JGitEnvironmentProperties();
|
||||
properties.setUri(uri);
|
||||
return new JGitEnvironmentRepository(environment, properties);
|
||||
}
|
||||
|
||||
private void assertOnDirectory(int expectedDirectorySize) {
|
||||
Set<Path> directory = getDirectory();
|
||||
assertThat(directory).isNotNull();
|
||||
assertThat(directory).hasSize(expectedDirectorySize);
|
||||
}
|
||||
|
||||
private ConfigurableEnvironment createConfigurableEnvironment() {
|
||||
return new MockEnvironment();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Set<Path> getDirectory() {
|
||||
return (Set<Path>) ReflectionTestUtils.getField(fileMonitorConfiguration,
|
||||
"directory");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user