Skip to content
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

[grid] TomlConfig: migrate TOML library to tomlj/tomlj #14470

Merged
merged 23 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ maven.install(
"io.opentelemetry:opentelemetry-sdk-testing:1.43.0",
"io.opentelemetry:opentelemetry-sdk-trace:1.43.0",
"io.opentelemetry.semconv:opentelemetry-semconv:1.25.0-alpha",
"io.ous:jtoml:2.0.0",
"it.ozimov:embedded-redis:0.7.3",
"net.bytebuddy:byte-buddy:1.15.10",
"org.htmlunit:htmlunit-core-js:4.5.0",
Expand All @@ -221,6 +220,7 @@ maven.install(
"org.redisson:redisson:3.37.0",
"org.slf4j:slf4j-api:2.0.16",
"org.slf4j:slf4j-jdk14:2.0.16",
"org.tomlj:tomlj:1.1.1",
"org.zeromq:jeromq:0.6.0",
],
excluded_artifacts = [
Expand Down
52 changes: 37 additions & 15 deletions java/maven_install.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
"__INPUT_ARTIFACTS_HASH": 707988571,
"__RESOLVED_ARTIFACTS_HASH": -1165730169,
"__INPUT_ARTIFACTS_HASH": -1685742929,
"__RESOLVED_ARTIFACTS_HASH": -1218025727,
"conflict_resolution": {
"com.google.code.gson:gson:2.8.9": "com.google.code.gson:gson:2.11.0",
"com.google.errorprone:error_prone_annotations:2.3.2": "com.google.errorprone:error_prone_annotations:2.28.0",
Expand Down Expand Up @@ -444,13 +444,6 @@
},
"version": "1.43.0"
},
"io.ous:jtoml": {
"shasums": {
"jar": "3cabdae2244c999addebb8c31ae452fbdc874b4f26a163539954b8eeb5d6acc6",
"sources": "f479f2acdf7a362dc86a5c9310ddaec7b34a87f0a8a6f46dde41c1069b2a2138"
},
"version": "2.0.0"
},
"io.projectreactor:reactor-core": {
"shasums": {
"jar": "44f055fbd033b6c976c53fb2e04b59027e79fb2312c37d2eaa54c77ea1ea80fe",
Expand Down Expand Up @@ -514,6 +507,13 @@
},
"version": "12.4"
},
"org.antlr:antlr4-runtime": {
"shasums": {
"jar": "e06c6553c1ccc14d36052ec4b0fc6f13b808cf957b5b1dc3f61bf401996ada59",
"sources": "6fa0efb711a152842ecda1d41ceab94fb2049f833e00e360e161ac0e7a3819fe"
},
"version": "4.11.1"
},
"org.apache.bcel:bcel": {
"shasums": {
"jar": "a119a4420350dea669acfd84120ecc7e5742dcabcc82b0b9f9755dc692335aa2",
Expand Down Expand Up @@ -801,6 +801,13 @@
},
"version": "1.7.21"
},
"org.tomlj:tomlj": {
"shasums": {
"jar": "383b7c66966c42ee4913ec977a7b6573631866bddc5318ec4a6215b688dd0d6c",
"sources": "533276104d58ebc92ce049c59a129717ce7daf0e9340969256fdb61f4f06f717"
},
"version": "1.1.1"
},
"org.xmlresolver:xmlresolver": {
"shasums": {
"data": "173904bdbd783ba0fac92c5bcc05da5d09f0ce7eed24346666ea0a239461f9b4",
Expand Down Expand Up @@ -1159,6 +1166,10 @@
"org.slf4j:slf4j-simple": [
"org.slf4j:slf4j-api"
],
"org.tomlj:tomlj": [
"org.antlr:antlr4-runtime",
"org.checkerframework:checker-qual"
],
"org.xmlresolver:xmlresolver": [
"org.apache.httpcomponents.client5:httpclient5",
"org.apache.httpcomponents.core5:httpcore5"
Expand Down Expand Up @@ -1786,10 +1797,6 @@
"io.opentelemetry.sdk.trace.internal.data",
"io.opentelemetry.sdk.trace.samplers"
],
"io.ous:jtoml": [
"io.ous.jtoml",
"io.ous.jtoml.impl"
],
"io.projectreactor:reactor-core": [
"reactor.adapter",
"reactor.core",
Expand Down Expand Up @@ -2014,6 +2021,15 @@
"net.sf.saxon.xpath",
"net.sf.saxon.z"
],
"org.antlr:antlr4-runtime": [
"org.antlr.v4.runtime",
"org.antlr.v4.runtime.atn",
"org.antlr.v4.runtime.dfa",
"org.antlr.v4.runtime.misc",
"org.antlr.v4.runtime.tree",
"org.antlr.v4.runtime.tree.pattern",
"org.antlr.v4.runtime.tree.xpath"
],
"org.apache.bcel:bcel": [
"org.apache.bcel",
"org.apache.bcel.classfile",
Expand Down Expand Up @@ -2898,6 +2914,10 @@
"org.slf4j:slf4j-simple": [
"org.slf4j.impl"
],
"org.tomlj:tomlj": [
"org.tomlj",
"org.tomlj.internal"
],
"org.xmlresolver:xmlresolver": [
"org.xmlresolver",
"org.xmlresolver.cache",
Expand Down Expand Up @@ -3095,8 +3115,6 @@
"io.opentelemetry:opentelemetry-sdk-trace",
"io.opentelemetry:opentelemetry-sdk-trace:jar:sources",
"io.opentelemetry:opentelemetry-sdk:jar:sources",
"io.ous:jtoml",
"io.ous:jtoml:jar:sources",
"io.projectreactor:reactor-core",
"io.projectreactor:reactor-core:jar:sources",
"io.reactivex.rxjava3:rxjava",
Expand All @@ -3115,6 +3133,8 @@
"net.bytebuddy:byte-buddy:jar:sources",
"net.sf.saxon:Saxon-HE",
"net.sf.saxon:Saxon-HE:jar:sources",
"org.antlr:antlr4-runtime",
"org.antlr:antlr4-runtime:jar:sources",
"org.apache.bcel:bcel",
"org.apache.bcel:bcel:jar:sources",
"org.apache.commons:commons-exec",
Expand Down Expand Up @@ -3197,6 +3217,8 @@
"org.slf4j:slf4j-jdk14:jar:sources",
"org.slf4j:slf4j-simple",
"org.slf4j:slf4j-simple:jar:sources",
"org.tomlj:tomlj",
"org.tomlj:tomlj:jar:sources",
"org.xmlresolver:xmlresolver",
"org.xmlresolver:xmlresolver:jar:data",
"org.xmlresolver:xmlresolver:jar:sources",
Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/grid/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ java_library(
"//java/src/org/openqa/selenium/json",
artifact("com.beust:jcommander"),
artifact("com.google.guava:guava"),
artifact("io.ous:jtoml"),
artifact("org.tomlj:tomlj"),
],
)
42 changes: 29 additions & 13 deletions java/src/org/openqa/selenium/grid/config/TomlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;
import io.ous.jtoml.JToml;
import io.ous.jtoml.ParseException;
import io.ous.jtoml.Toml;
import io.ous.jtoml.TomlTable;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
Expand All @@ -31,18 +27,31 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.openqa.selenium.internal.Require;
import org.tomlj.Toml;
import org.tomlj.TomlArray;
import org.tomlj.TomlParseError;
import org.tomlj.TomlParseResult;
import org.tomlj.TomlTable;

public class TomlConfig implements Config {

private final Toml toml;
private final TomlParseResult toml;

public TomlConfig(Reader reader) {
try {
toml = JToml.parse(reader);
toml = Toml.parse(reader);

if (toml.hasErrors()) {
String error =
toml.errors().stream().map(TomlParseError::toString).collect(Collectors.joining("\n"));

throw new ConfigException(error);
}
} catch (IOException e) {
throw new ConfigException("Unable to read TOML.", e);
} catch (ParseException e) {
} catch (TomlParseError e) {
throw new ConfigException(
e.getCause()
+ "\n Validate the config using https://www.toml-lint.com/. "
Expand All @@ -65,7 +74,7 @@ public Optional<List<String>> getAll(String section, String option) {
Require.nonNull("Section to read", section);
Require.nonNull("Option to read", option);

if (!toml.containsKey(section)) {
if (!toml.contains(section)) {
return Optional.empty();
}

Expand All @@ -74,21 +83,28 @@ public Optional<List<String>> getAll(String section, String option) {
throw new ConfigException(String.format("Section %s is not a section! %s", section, raw));
}

TomlTable table = toml.getTomlTable(section);
TomlTable table = toml.getTable(section);
Object value = null;
if (table != null) {
value = table.get(option);
}

Object value = table.getOrDefault(option, null);
if (value == null) {
return Optional.empty();
}

if (value instanceof TomlArray) {
value = ((TomlArray) value).toList();
}

if (value instanceof Collection) {
Collection<?> collection = (Collection<?>) value;
// Case when an array of tables is used as config
// https://toml.io/en/v1.0.0-rc.3#array-of-tables
if (collection.stream().anyMatch(item -> item instanceof TomlTable)) {
if (collection.stream().anyMatch(TomlTable.class::isInstance)) {
return Optional.of(
collection.stream()
.map(item -> (TomlTable) item)
.map(TomlTable.class::cast)
.map(TomlTable::toMap)
.map(this::toEntryList)
.flatMap(Collection::stream)
Expand All @@ -106,7 +122,7 @@ public Optional<List<String>> getAll(String section, String option) {
return Optional.of(toEntryList(((TomlTable) value).toMap()));
}

return Optional.of(ImmutableList.of(String.valueOf(value)));
return Optional.of(List.of(String.valueOf(value)));
}

@Override
Expand Down
1 change: 0 additions & 1 deletion java/test/org/openqa/selenium/grid/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ java_test_suite(
"//java/src/org/openqa/selenium/json",
artifact("com.beust:jcommander"),
artifact("com.google.guava:guava"),
artifact("io.ous:jtoml"),
artifact("org.junit.jupiter:junit-jupiter-api"),
artifact("org.assertj:assertj-core"),
] + JUNIT5_DEPS,
Expand Down
15 changes: 11 additions & 4 deletions java/test/org/openqa/selenium/grid/config/TomlConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.grid.config;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.StringReader;
import java.util.Arrays;
Expand All @@ -30,18 +31,24 @@ class TomlConfigTest {

@Test
void shouldUseATableAsASection() {
String raw = "[cheeses]\nselected=brie";
String raw = "[cheeses]\nselected=\"brie\"";
pujagani marked this conversation as resolved.
Show resolved Hide resolved
Config config = new TomlConfig(new StringReader(raw));

assertThat(config.get("cheeses", "selected")).isEqualTo(Optional.of("brie"));
}

@Test
void shouldCheckForErrorsAndThrow() {
String raw = "[cheeses]\nselected=brie";
assertThatThrownBy(() -> new TomlConfig(new StringReader(raw)))
.isInstanceOf(ConfigException.class);
}

@Test
void shouldContainConfigFromArrayOfTables() {
String[] rawConfig =
new String[] {
"[cheeses]",
"default = manchego",
"default = \"manchego\"",
"[[cheeses.type]]",
"name = \"soft cheese\"",
"default = \"brie\"",
Expand Down Expand Up @@ -104,7 +111,7 @@ void ensureCanReadListOfLists() {
String[] rawConfig =
new String[] {
"[cheeses]",
"default = manchego",
"default = \"manchego\"",
"[[cheeses.type]]",
"name = \"soft cheese\"",
"default = \"brie\"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void ensureBasicFunctionality() {
"[node]\n"
+ "selenium-manager = false\n"
+ "driver-implementation = "
+ browser.displayName())));
+ String.format("\"%s\"", browser.displayName()))));

Server<?> server =
new NettyServer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void setup() {
"[node]\n"
+ "selenium-manager = false\n"
+ "driver-implementation = "
+ browser.displayName())));
+ String.format("\"%s\"", browser.displayName()))));

driver = new RemoteWebDriver(deployment.getServer().getUrl(), browser.getCapabilities());
driver = new Augmenter().augment(driver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void setupServers() {
+ "selenium-manager = true\n"
+ "enable-managed-downloads = true\n"
+ "driver-implementation = "
+ browser.displayName())));
+ String.format("\"%s\"", browser.displayName()))));
tearDowns.add(deployment);

server = deployment.getServer();
Expand Down
2 changes: 1 addition & 1 deletion java/test/org/openqa/selenium/grid/router/StressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void setupServers() {
new StringReader(
"[node]\n"
+ "driver-implementation = "
+ browser.displayName()
+ String.format("\"%s\"", browser.displayName())
+ "\n"
+ "session-timeout = 11"
+ "\n"
Expand Down
Loading