This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send all RoboZonky-specific properties to a file (#102)
- Loading branch information
Showing
13 changed files
with
215 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
robozonky-api/src/main/java/com/github/triceo/robozonky/internal/api/Settings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright 2017 Lukáš Petrovický | ||
* | ||
* 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 com.github.triceo.robozonky.internal.api; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.nio.file.Files; | ||
import java.util.Properties; | ||
import java.util.function.Function; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* These are RoboZonky settings read from a property file at system startup. The location of this file will be looked | ||
* up in a property {@link #FILE_LOCATION_PROPERTY}. Defaults for all settings looked up through this class come from | ||
* {@link System#getProperties()}. | ||
*/ | ||
public enum Settings { | ||
|
||
INSTANCE; // cheap thread-safe singleton | ||
|
||
public static final String FILE_LOCATION_PROPERTY = "robozonky.properties.file"; | ||
private final Logger LOGGER = LoggerFactory.getLogger(Settings.class); | ||
|
||
private Properties getProperties() { | ||
final String filename = System.getProperty(Settings.FILE_LOCATION_PROPERTY); | ||
if (filename == null) { | ||
return new Properties(); | ||
} | ||
final File f = new File(filename); | ||
if (!f.exists()) { | ||
throw new IllegalStateException("Properties file does not exist: " + f.getAbsolutePath()); | ||
} | ||
try (final Reader r = Files.newBufferedReader(f.toPath(), Defaults.CHARSET)) { | ||
final Properties p = new Properties(); | ||
p.load(r); | ||
LOGGER.debug("Loaded from '{}'.", f.getAbsolutePath()); | ||
return p; | ||
} catch (final IOException ex) { | ||
throw new IllegalStateException("Cannot read properties.", ex); | ||
} | ||
} | ||
|
||
private final Properties properties = this.getProperties(); | ||
|
||
public <T> T get(final String key, final Function<String, T> adapter) { | ||
final String val = properties.containsKey(key) ? properties.getProperty(key) : System.getProperty(key); | ||
return adapter.apply(val); | ||
} | ||
|
||
public String get(final String key, final String defaultValue) { | ||
return get(key, value -> value == null ? defaultValue : value); | ||
} | ||
|
||
public int get(final String key, final int defaultValue) { | ||
return get(key, value -> { | ||
try { | ||
return Integer.parseInt(value); | ||
} catch (final NumberFormatException ex) { | ||
return defaultValue; | ||
} | ||
}); | ||
} | ||
|
||
public boolean get(final String key) { | ||
return get(key, Boolean::parseBoolean); | ||
} | ||
|
||
/** | ||
* When set to true, this is essentially a controlled memory leak. Generally only useful for testing. | ||
* @return | ||
*/ | ||
public boolean isDebugEventStorageEnabled() { | ||
return get("robozonky.debug.enable_event_storage"); | ||
} | ||
|
||
public int getTokenRefreshBeforeExpirationInSeconds() { | ||
return get("robozonky.default.token_refresh_seconds", 60); | ||
} | ||
|
||
public int getRemoteResourceRefreshIntervalInMinutes() { | ||
return get("robozonky.default.resource_refresh_minutes", 5); | ||
} | ||
|
||
public int getCaptchaDelayInSeconds() { | ||
return get("robozonky.default.captcha_protection_seconds", 120); | ||
} | ||
|
||
public int getDefaultDryRunBalance() { | ||
return get("robozonky.default.dry_run_balance", -1); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
robozonky-api/src/test/java/com/github/triceo/robozonky/internal/api/SettingsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2017 Lukáš Petrovický | ||
* | ||
* 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 com.github.triceo.robozonky.internal.api; | ||
|
||
import java.util.UUID; | ||
|
||
import org.assertj.core.api.SoftAssertions; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.contrib.java.lang.system.RestoreSystemProperties; | ||
|
||
/** | ||
* Created by lpetrovi on 14.4.17. | ||
*/ | ||
public class SettingsTest { | ||
|
||
@Rule | ||
public final RestoreSystemProperties propertiesRestorer = new RestoreSystemProperties(); | ||
|
||
@Test | ||
public void properties() { | ||
SoftAssertions.assertSoftly(softly -> { | ||
softly.assertThat(Settings.INSTANCE.get("user.dir", "")).isNotEqualTo(""); | ||
softly.assertThat(Settings.INSTANCE.get(UUID.randomUUID().toString(), "")) | ||
.isEqualTo(""); | ||
softly.assertThat(Settings.INSTANCE.isDebugEventStorageEnabled()).isFalse(); | ||
softly.assertThat(Settings.INSTANCE.getTokenRefreshBeforeExpirationInSeconds()).isEqualTo(60); | ||
softly.assertThat(Settings.INSTANCE.getRemoteResourceRefreshIntervalInMinutes()).isEqualTo(5); | ||
softly.assertThat(Settings.INSTANCE.getCaptchaDelayInSeconds()).isEqualTo(120); | ||
softly.assertThat(Settings.INSTANCE.getDefaultDryRunBalance()).isEqualTo(-1); | ||
}); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.