-
-
Notifications
You must be signed in to change notification settings - Fork 760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve the error message on session creation #994
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.appium.java_client.internal; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UncheckedIOException; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class Config { | ||
private static Config mainInstance = null; | ||
private static final String MAIN_CONFIG = "main.properties"; | ||
private static final Map<String, Properties> cache = new ConcurrentHashMap<>(); | ||
private final String configName; | ||
|
||
/** | ||
* Retrieve a facade for the main config. | ||
* | ||
* @return interaction helper for 'main.properties' config | ||
*/ | ||
public static synchronized Config main() { | ||
if (mainInstance == null) { | ||
mainInstance = new Config(MAIN_CONFIG); | ||
} | ||
return mainInstance; | ||
} | ||
|
||
private Config(String configName) { | ||
this.configName = configName; | ||
} | ||
|
||
/** | ||
* Retrieve a value from properties file. | ||
* | ||
* @param key the name of the corresponding key which value to retrieve | ||
* @param valueType the expected type of the value to be retrieved | ||
* @return the actual value | ||
* @throws IllegalArgumentException if the given key does not exist | ||
* @throws ClassCastException if the retrieved value cannot be cast to `valueType` type | ||
*/ | ||
public <T> T getValue(String key, Class<T> valueType) { | ||
return getOptionalValue(key, valueType) | ||
.orElseThrow(() -> new IllegalArgumentException( | ||
String.format("There is no '%s' key in '%s' config", key, configName) | ||
)); | ||
} | ||
|
||
/** | ||
* Retrieve a value from properties file. | ||
* | ||
* @param key the name of the corresponding key which value to retrieve | ||
* @param valueType the expected type of the value to be retrieved | ||
* @return the actual value or {@link Optional#empty()} if the key is not present | ||
* @throws UncheckedIOException if the given properties file does not exist/not accessible | ||
* @throws ClassCastException if the retrieved value cannot be cast to `valueType` type | ||
*/ | ||
public <T> Optional<T> getOptionalValue(String key, Class<T> valueType) { | ||
final Properties cachedProps = cache.computeIfAbsent(configName, (k) -> { | ||
try (InputStream configFileStream = getClass().getClassLoader().getResourceAsStream(configName)) { | ||
final Properties p = new Properties(); | ||
p.load(configFileStream); | ||
return p; | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(String.format("Configuration file '%s' cannot be loaded", | ||
configName), e); | ||
} | ||
}); | ||
return cachedProps.containsKey(key) ? Optional.of(valueType.cast(cachedProps.get(key))) : Optional.empty(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
selenium.version=@selenium.version@ |
34 changes: 34 additions & 0 deletions
34
src/test/java/io/appium/java_client/internal/ConfigTest.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,34 @@ | ||
package io.appium.java_client.internal; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
public class ConfigTest { | ||
private static final String EXISTING_KEY = "selenium.version"; | ||
private static final String MISSING_KEY = "bla"; | ||
|
||
@Test | ||
public void verifyGettingExistingValue() { | ||
assertThat(Config.main().getValue(EXISTING_KEY, String.class).length(), greaterThan(0)); | ||
assertTrue(Config.main().getOptionalValue(EXISTING_KEY, String.class).isPresent()); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void verifyGettingNonExistingValue() { | ||
assertThat(Config.main().getValue(MISSING_KEY, String.class).length(), greaterThan(0)); | ||
} | ||
|
||
@Test(expected = ClassCastException.class) | ||
public void verifyGettingExistingValueWithWrongClass() { | ||
assertThat(Config.main().getValue(EXISTING_KEY, Integer.class), greaterThan(0)); | ||
} | ||
|
||
@Test | ||
public void verifyGettingNonExistingOptionalValue() { | ||
assertFalse(Config.main().getOptionalValue(MISSING_KEY, String.class).isPresent()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed checkstyle