123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- /*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
- package io.opentelemetry.javaagent.tooling.config
- import org.junit.Rule
- import org.junit.contrib.java.lang.system.EnvironmentVariables
- import spock.lang.Shared
- import spock.lang.Specification
- import spock.lang.TempDir
- import spock.util.environment.RestoreSystemProperties
- @RestoreSystemProperties
- class ConfigurationFileTest extends Specification {
- @Rule
- public final EnvironmentVariables environmentVariables = new EnvironmentVariables()
- @TempDir
- @Shared
- public File tmpDir
- def "should use env property"() {
- given:
- def path = createFile("config", "property1=val-env")
- environmentVariables.set(ConfigInitializer.CONFIGURATION_FILE_ENV_VAR, path)
- when:
- def properties = ConfigInitializer.loadConfigurationFile()
- then:
- properties.get("property1") == "val-env"
- }
- def "should use system property"() {
- given:
- def path = createFile("config", "property1=val-sys")
- System.setProperty(ConfigInitializer.CONFIGURATION_FILE_PROPERTY, path)
- when:
- def properties = ConfigInitializer.loadConfigurationFile()
- then:
- properties.get("property1") == "val-sys"
- }
- def "system property should take precedence over env property"() {
- given:
- def pathEnv = createFile("configEnv", "property1=val-env")
- def pathSys = createFile("configSys", "property1=val-sys")
- environmentVariables.set(ConfigInitializer.CONFIGURATION_FILE_ENV_VAR, pathEnv)
- System.setProperty(ConfigInitializer.CONFIGURATION_FILE_PROPERTY, pathSys)
- when:
- def properties = ConfigInitializer.loadConfigurationFile()
- then:
- properties.get("property1") == "val-sys"
- }
- def "should return empty properties if file does not exist"() {
- given:
- environmentVariables.set(ConfigInitializer.CONFIGURATION_FILE_ENV_VAR, "somePath")
- when:
- def properties = ConfigInitializer.loadConfigurationFile()
- then:
- !properties.propertyNames().hasMoreElements()
- }
- def "should return empty properties if property is not set"() {
- when:
- def properties = ConfigInitializer.loadConfigurationFile()
- then:
- !properties.propertyNames().hasMoreElements()
- }
- def createFile(String name, String contents) {
- def file = new File(tmpDir, name)
- file.write(contents)
- return file.getAbsolutePath()
- }
- }
|