/** * Checks for the presence of a supplied or system property named {@code spring.cloud.propertiesFile}. If the property * is present, load its contents into {@link #fileProperties}. If there's a problem, log but continue. */ private void readFileProperties() { fileProperties = new Properties(); logger.fine("looking for a properties file"); // will search system properties and the classpath File propertiesFile = new PropertiesFileResolver(env).findCloudPropertiesFile(); if (propertiesFile == null) { logger.info("not loading service definitions from a properties file"); return; } if (!fileExists(propertiesFile)) { logger.info("properties file " + propertiesFile + " does not exist; probably running in a real cloud"); return; } logger.info("loading service definitions from properties file " + propertiesFile); try { InputStream fis = openFile(propertiesFile); fileProperties.load(fis); fis.close(); } catch (IOException e) { logger.log(Level.SEVERE, "exception while loading properties from file " + propertiesFile, e); return; } logger.info("properties loaded successfully"); }
File findCloudPropertiesFile() { File file = findCloudPropertiesFileFromSystem(); if (file != null) { logger.info("using configuration file from system properties"); return file; } file = findCloudPropertiesFileFromClasspath(); if (file != null) logger.info("using configuration file derived from " + classpathPropertiesFilename); else logger.info("did not find any Spring Cloud configuration file"); return file; }
@Test public void testClasspathFileWithoutKey() { resolver = new PropertiesFileResolver(env, "localconfig.testuris.properties"); assertNull(resolver.findCloudPropertiesFileFromClasspath()); }
@Test public void testSecurityExceptionHandling() { env = mock(PassthroughEnvironmentAccessor.class); resolver = new PropertiesFileResolver(env); when(env.getSystemProperty(LocalConfigConnector.PROPERTIES_FILE_PROPERTY)).thenThrow(new SecurityException()); assertNull(resolver.findCloudPropertiesFileFromSystem()); verify(env).getSystemProperty(LocalConfigConnector.PROPERTIES_FILE_PROPERTY); }
@Test public void testNowhere() { assertNull(resolver.findCloudPropertiesFile()); }
@Test public void testMissingSystemProperty() { assertNull(resolver.findCloudPropertiesFileFromSystem()); }
@Before public void setDefaults() { env = new PassthroughEnvironmentAccessor(); resolver = new PropertiesFileResolver(env); }
File configFile = new File(new StrSubstitutor(systemPropertiesLookup(env)).replace(template)); logger.info("derived configuration file name: " + configFile);
@Test public void testNoClasspathFile() { resolver = new PropertiesFileResolver(env, "bazquux.properties"); assertNull(resolver.findCloudPropertiesFileFromClasspath()); }
@Test public void testFromSystem() { env.setSystemProperty(LocalConfigConnector.PROPERTIES_FILE_PROPERTY, PROPERTIES_FILE_NAME); assertEquals(PROPERTIES_FILE_NAME, resolver.findCloudPropertiesFile().getPath()); }
@Test public void testSystemProperty() { env.setSystemProperty(LocalConfigConnector.PROPERTIES_FILE_PROPERTY, PROPERTIES_FILE_NAME); assertEquals(PROPERTIES_FILE_NAME, resolver.findCloudPropertiesFileFromSystem().getPath()); }
File configFile = new File(new StrSubstitutor(systemPropertiesLookup(env)).replace(template)); logger.info("derived configuration file name: " + configFile);
/** * Checks for the presence of a supplied or system property named {@code spring.cloud.propertiesFile}. If the property * is present, load its contents into {@link #fileProperties}. If there's a problem, log but continue. */ private void readFileProperties() { fileProperties = new Properties(); logger.fine("looking for a properties file"); // will search system properties and the classpath File propertiesFile = new PropertiesFileResolver(env).findCloudPropertiesFile(); if (propertiesFile == null) { logger.info("not loading service definitions from a properties file"); return; } if (!fileExists(propertiesFile)) { logger.info("properties file " + propertiesFile + " does not exist; probably running in a real cloud"); return; } logger.info("loading service definitions from properties file " + propertiesFile); try { InputStream fis = openFile(propertiesFile); fileProperties.load(fis); fis.close(); } catch (IOException e) { logger.log(Level.SEVERE, "exception while loading properties from file " + propertiesFile, e); return; } logger.info("properties loaded successfully"); }
@Test public void testLiteral() { resolver = new PropertiesFileResolver(env, "spring-cloud-literal.properties"); assertEquals(PROPERTIES_FILE_NAME, resolver.findCloudPropertiesFileFromClasspath().getPath()); }
File findCloudPropertiesFile() { File file = findCloudPropertiesFileFromSystem(); if (file != null) { logger.info("using configuration file from system properties"); return file; } file = findCloudPropertiesFileFromClasspath(); if (file != null) logger.info("using configuration file derived from " + classpathPropertiesFilename); else logger.info("did not find any Spring Cloud configuration file"); return file; }
@Test public void testPrecedence() { env.setSystemProperty(LocalConfigConnector.PROPERTIES_FILE_PROPERTY, PROPERTIES_FILE_NAME); resolver = new PropertiesFileResolver(env, "spring-cloud-literal.properties"); assertEquals(PROPERTIES_FILE_NAME, resolver.findCloudPropertiesFile().getPath()); } }
@Test public void testTemplate() { resolver = new PropertiesFileResolver(env, "spring-cloud-template.properties"); env.setSystemProperty("user.home", "/foo"); assertEquals(PROPERTIES_FILE_NAME, resolver.findCloudPropertiesFileFromClasspath().getPath()); }
@Test public void testFromClasspath() { resolver = new PropertiesFileResolver(env, "spring-cloud-template.properties"); env.setSystemProperty("user.home", "/foo"); assertEquals(PROPERTIES_FILE_NAME, resolver.findCloudPropertiesFile().getPath()); }