diff options
author | Matteo Mara <matteo.mara@sonarsource.com> | 2024-10-07 18:13:33 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-10-14 20:03:03 +0000 |
commit | 1ad3667d30dd82213ae47bea2c267188f003b2c7 (patch) | |
tree | 422f1f66b48114de0e79de9f7bd53aceff3f7c91 /server/sonar-main | |
parent | ce2e686a3e269f937ffb51635928cd1a597b2d07 (diff) | |
download | sonarqube-1ad3667d30dd82213ae47bea2c267188f003b2c7.tar.gz sonarqube-1ad3667d30dd82213ae47bea2c267188f003b2c7.zip |
NO-JIRA Stop using deprecated methods from RandomStringUtils
Diffstat (limited to 'server/sonar-main')
7 files changed, 61 insertions, 63 deletions
diff --git a/server/sonar-main/src/test/java/org/sonar/application/AbstractStopRequestWatcherTest.java b/server/sonar-main/src/test/java/org/sonar/application/AbstractStopRequestWatcherTest.java index a81e4cc1d97..7934e228046 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/AbstractStopRequestWatcherTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/AbstractStopRequestWatcherTest.java @@ -37,7 +37,7 @@ public class AbstractStopRequestWatcherTest { @Rule public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60)); - private String threadName = RandomStringUtils.randomAlphabetic(12); + private String threadName = RandomStringUtils.secure().nextAlphabetic(12); private TestBooleanSupplier booleanSupplier = new TestBooleanSupplier(); private TestAction stopAction = new TestAction(); diff --git a/server/sonar-main/src/test/java/org/sonar/application/SchedulerImplTest.java b/server/sonar-main/src/test/java/org/sonar/application/SchedulerImplTest.java index 14f7975d271..e083d5d2b8e 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/SchedulerImplTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/SchedulerImplTest.java @@ -53,7 +53,7 @@ import org.sonar.process.cluster.hz.HazelcastMember; import static com.google.common.collect.ImmutableMap.of; import static java.util.Collections.synchronizedList; -import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric; +import static org.apache.commons.lang3.RandomStringUtils.secure; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; @@ -410,8 +410,8 @@ public class SchedulerImplTest { } private ImmutableMap.Builder<String, String> addRequiredNodeProperties(ImmutableMap.Builder<String, String> builder) { - builder.put(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(4)); - builder.put(CLUSTER_NODE_HOST.getKey(), randomAlphanumeric(4)); + builder.put(CLUSTER_NODE_NAME.getKey(), secure().nextAlphanumeric(4)); + builder.put(CLUSTER_NODE_HOST.getKey(), secure().nextAlphanumeric(4)); builder.put(CLUSTER_NODE_HZ_PORT.getKey(), String.valueOf(1 + new Random().nextInt(999))); return builder; } diff --git a/server/sonar-main/src/test/java/org/sonar/application/cluster/health/SearchNodeHealthProviderTest.java b/server/sonar-main/src/test/java/org/sonar/application/cluster/health/SearchNodeHealthProviderTest.java index 9e1f27a1f48..2f93d9cd9f3 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/cluster/health/SearchNodeHealthProviderTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/cluster/health/SearchNodeHealthProviderTest.java @@ -30,8 +30,7 @@ import org.sonar.process.Props; import org.sonar.process.cluster.health.NodeHealth; import static java.lang.String.valueOf; -import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; -import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric; +import static org.apache.commons.lang3.RandomStringUtils.secure; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; import static org.mockito.Mockito.mock; @@ -59,7 +58,7 @@ public class SearchNodeHealthProviderTest { @Test public void constructor_throws_NPE_if_NetworkUtils_getHostname_returns_null_and_property_is_not_set() { Properties properties = new Properties(); - properties.put(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); + properties.put(CLUSTER_NODE_NAME.getKey(), secure().nextAlphanumeric(3)); Props props = new Props(properties); assertThatThrownBy(() -> new SearchNodeHealthProvider(props, clusterAppState, networkUtils, clock)) @@ -69,8 +68,8 @@ public class SearchNodeHealthProviderTest { @Test public void constructor_throws_IAE_if_property_node_port_is_not_set() { Properties properties = new Properties(); - properties.put(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); - when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(34)); + properties.put(CLUSTER_NODE_NAME.getKey(), secure().nextAlphanumeric(3)); + when(networkUtils.getHostname()).thenReturn(secure().nextAlphanumeric(34)); Props props = new Props(properties); assertThatThrownBy(() -> new SearchNodeHealthProvider(props, clusterAppState, networkUtils, clock)) @@ -80,11 +79,11 @@ public class SearchNodeHealthProviderTest { @Test public void constructor_throws_FormatException_if_property_node_port_is_not_an_integer() { - String port = randomAlphabetic(3); + String port = secure().nextAlphabetic(3); Properties properties = new Properties(); - properties.put(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); + properties.put(CLUSTER_NODE_NAME.getKey(), secure().nextAlphanumeric(3)); properties.put(CLUSTER_NODE_HZ_PORT.getKey(), port); - when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(34)); + when(networkUtils.getHostname()).thenReturn(secure().nextAlphanumeric(34)); Props props = new Props(properties); assertThatThrownBy(() -> new SearchNodeHealthProvider(props, clusterAppState, networkUtils, clock)) @@ -94,12 +93,12 @@ public class SearchNodeHealthProviderTest { @Test public void get_returns_name_and_port_from_properties_at_constructor_time() { - String name = randomAlphanumeric(3); + String name = secure().nextAlphanumeric(3); int port = 1 + random.nextInt(4); Properties properties = new Properties(); properties.setProperty(CLUSTER_NODE_NAME.getKey(), name); properties.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), valueOf(port)); - when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(34)); + when(networkUtils.getHostname()).thenReturn(secure().nextAlphanumeric(34)); when(clock.now()).thenReturn(1L + random.nextInt(87)); SearchNodeHealthProvider underTest = new SearchNodeHealthProvider(new Props(properties), clusterAppState, networkUtils, clock); @@ -109,7 +108,7 @@ public class SearchNodeHealthProviderTest { assertThat(nodeHealth.getDetails().getPort()).isEqualTo(port); // change values in properties - properties.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(6)); + properties.setProperty(CLUSTER_NODE_NAME.getKey(), secure().nextAlphanumeric(6)); properties.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), valueOf(1 + random.nextInt(99))); NodeHealth newNodeHealth = underTest.get(); @@ -120,9 +119,9 @@ public class SearchNodeHealthProviderTest { @Test public void get_returns_host_from_property_if_set_at_constructor_time() { - String host = randomAlphanumeric(55); + String host = secure().nextAlphanumeric(55); Properties properties = new Properties(); - properties.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); + properties.setProperty(CLUSTER_NODE_NAME.getKey(), secure().nextAlphanumeric(3)); properties.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), valueOf(1 + random.nextInt(4))); properties.setProperty(CLUSTER_NODE_HOST.getKey(), host); when(clock.now()).thenReturn(1L + random.nextInt(87)); @@ -133,7 +132,7 @@ public class SearchNodeHealthProviderTest { assertThat(nodeHealth.getDetails().getHost()).isEqualTo(host); // change now - properties.setProperty(CLUSTER_NODE_HOST.getKey(), randomAlphanumeric(96)); + properties.setProperty(CLUSTER_NODE_HOST.getKey(), secure().nextAlphanumeric(96)); NodeHealth newNodeHealth = underTest.get(); @@ -151,9 +150,9 @@ public class SearchNodeHealthProviderTest { } private void getReturnsHostFromNetworkUtils(@Nullable String hostPropertyValue) { - String host = randomAlphanumeric(34); + String host = secure().nextAlphanumeric(34); Properties properties = new Properties(); - properties.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); + properties.setProperty(CLUSTER_NODE_NAME.getKey(), secure().nextAlphanumeric(3)); properties.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), valueOf(1 + random.nextInt(4))); if (hostPropertyValue != null) { properties.setProperty(CLUSTER_NODE_HOST.getKey(), hostPropertyValue); @@ -167,7 +166,7 @@ public class SearchNodeHealthProviderTest { assertThat(nodeHealth.getDetails().getHost()).isEqualTo(host); // change now - when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(96)); + when(networkUtils.getHostname()).thenReturn(secure().nextAlphanumeric(96)); NodeHealth newNodeHealth = underTest.get(); @@ -218,11 +217,11 @@ public class SearchNodeHealthProviderTest { } private long setRequiredPropertiesAndMocks(Properties properties) { - properties.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); + properties.setProperty(CLUSTER_NODE_NAME.getKey(), secure().nextAlphanumeric(3)); properties.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), valueOf(1 + random.nextInt(4))); long now = 1L + random.nextInt(87); when(clock.now()).thenReturn(now); - when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(34)); + when(networkUtils.getHostname()).thenReturn(secure().nextAlphanumeric(34)); return now; } } diff --git a/server/sonar-main/src/test/java/org/sonar/application/command/AbstractCommandTest.java b/server/sonar-main/src/test/java/org/sonar/application/command/AbstractCommandTest.java index 4ba830d0fa2..0b0fbae30e4 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/command/AbstractCommandTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/command/AbstractCommandTest.java @@ -33,7 +33,7 @@ import org.mockito.Mockito; import org.sonar.process.ProcessId; import org.sonar.process.System2; -import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric; +import static org.apache.commons.lang3.RandomStringUtils.secure; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.when; @@ -72,7 +72,7 @@ public class AbstractCommandTest { }; - assertThatThrownBy(() -> underTest.setEnvVariable(null, randomAlphanumeric(30))) + assertThatThrownBy(() -> underTest.setEnvVariable(null, secure().nextAlphanumeric(30))) .isInstanceOf(NullPointerException.class) .hasMessage("key can't be null"); } @@ -84,7 +84,7 @@ public class AbstractCommandTest { }; - assertThatThrownBy(() -> underTest.setEnvVariable(randomAlphanumeric(30), null)) + assertThatThrownBy(() -> underTest.setEnvVariable(secure().nextAlphanumeric(30), null)) .isInstanceOf(NullPointerException.class) .hasMessage("value can't be null"); } @@ -107,8 +107,8 @@ public class AbstractCommandTest { File workDir = temp.newFolder(); System2 system2 = Mockito.mock(System2.class); Map<String, String> env = new HashMap<>(); - String key1 = randomAlphanumeric(3); - env.put(key1, randomAlphanumeric(9)); + String key1 = secure().nextAlphanumeric(3); + env.put(key1, secure().nextAlphanumeric(9)); when(system2.getenv()).thenReturn(env); AbstractCommand underTest = new AbstractCommand(ProcessId.ELASTICSEARCH, workDir, system2) { diff --git a/server/sonar-main/src/test/java/org/sonar/application/command/EsJvmOptionsTest.java b/server/sonar-main/src/test/java/org/sonar/application/command/EsJvmOptionsTest.java index b26ff72651d..7180b5dd653 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/command/EsJvmOptionsTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/command/EsJvmOptionsTest.java @@ -31,7 +31,7 @@ import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.sonar.process.Props; -import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric; +import static org.apache.commons.lang3.RandomStringUtils.secure; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -100,7 +100,7 @@ public class EsJvmOptionsTest { @Test public void constructor_forces_boostrap_checks_if_jdbc_url_property_is_not_h2() throws IOException { - properties.put("sonar.jdbc.url", randomAlphanumeric(53)); + properties.put("sonar.jdbc.url", secure().nextAlphanumeric(53)); File tmpDir = temporaryFolder.newFolder(); EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir); @@ -147,7 +147,7 @@ public class EsJvmOptionsTest { @Test public void boostrap_checks_can_be_set_true_if_jdbc_other_than_h2() throws IOException { - properties.put("sonar.jdbc.url", randomAlphanumeric(53)); + properties.put("sonar.jdbc.url", secure().nextAlphanumeric(53)); properties.put("sonar.es.bootstrap.checks.disable", "true"); File tmpDir = temporaryFolder.newFolder(); @@ -160,7 +160,7 @@ public class EsJvmOptionsTest { @Test public void boostrap_checks_can_be_set_false_if_jdbc_other_than_h2() throws IOException { - properties.put("sonar.jdbc.url", randomAlphanumeric(53)); + properties.put("sonar.jdbc.url", secure().nextAlphanumeric(53)); properties.put("sonar.es.bootstrap.checks.disable", "false"); File tmpDir = temporaryFolder.newFolder(); diff --git a/server/sonar-main/src/test/java/org/sonar/application/command/JvmOptionsTest.java b/server/sonar-main/src/test/java/org/sonar/application/command/JvmOptionsTest.java index ae4a4ad6d7e..550cb2303a7 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/command/JvmOptionsTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/command/JvmOptionsTest.java @@ -41,8 +41,7 @@ import org.sonar.process.MessageException; import org.sonar.process.Props; import static java.lang.String.valueOf; -import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; -import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric; +import static org.apache.commons.lang3.RandomStringUtils.secure; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.fail; @@ -51,9 +50,9 @@ import static org.assertj.core.api.Assertions.fail; public class JvmOptionsTest { private final Random random = new Random(); - private final String randomPropertyName = randomAlphanumeric(3); - private final String randomPrefix = "-" + randomAlphabetic(5).toLowerCase(Locale.ENGLISH); - private final String randomValue = randomAlphanumeric(4).toLowerCase(Locale.ENGLISH); + private final String randomPropertyName = secure().nextAlphanumeric(3); + private final String randomPrefix = "-" + secure().nextAlphabetic(5).toLowerCase(Locale.ENGLISH); + private final String randomValue = secure().nextAlphanumeric(4).toLowerCase(Locale.ENGLISH); private final Properties properties = new Properties(); private final JvmOptions underTest = new JvmOptions(); @@ -98,7 +97,7 @@ public class JvmOptionsTest { @Test public void constructor_throws_IAE_if_any_option_prefix_does_not_start_with_dash() { - String invalidPrefix = randomAlphanumeric(3); + String invalidPrefix = secure().nextAlphanumeric(3); Map<String, String> mandatoryJvmOptions = shuffleThenToMap( Stream.of( IntStream.range(0, random.nextInt(10)).mapToObj(i -> new Option("-B", valueOf(i))), @@ -146,7 +145,7 @@ public class JvmOptionsTest { @Test public void add_throws_IAE_if_argument_does_not_start_with_dash() { - expectJvmOptionNotEmptyAndStartByDashIAE(() -> underTest.add(randomAlphanumeric(3))); + expectJvmOptionNotEmptyAndStartByDashIAE(() -> underTest.add(secure().nextAlphanumeric(3))); } @@ -162,10 +161,10 @@ public class JvmOptionsTest { public void add_throws_MessageException_if_option_starts_with_prefix_of_mandatory_option_but_has_different_value() { String[] optionOverrides = { randomPrefix, - randomPrefix + randomAlphanumeric(1), - randomPrefix + randomAlphanumeric(2), - randomPrefix + randomAlphanumeric(3), - randomPrefix + randomAlphanumeric(4), + randomPrefix + secure().nextAlphanumeric(1), + randomPrefix + secure().nextAlphanumeric(2), + randomPrefix + secure().nextAlphanumeric(3), + randomPrefix + secure().nextAlphanumeric(4), randomPrefix + randomValue.substring(1), randomPrefix + randomValue.substring(2), randomPrefix + randomValue.substring(3) @@ -187,10 +186,10 @@ public class JvmOptionsTest { public void add_checks_against_mandatory_options_is_case_sensitive() { String[] optionOverrides = { randomPrefix, - randomPrefix + randomAlphanumeric(1), - randomPrefix + randomAlphanumeric(2), - randomPrefix + randomAlphanumeric(3), - randomPrefix + randomAlphanumeric(4), + randomPrefix + secure().nextAlphanumeric(1), + randomPrefix + secure().nextAlphanumeric(2), + randomPrefix + secure().nextAlphanumeric(3), + randomPrefix + secure().nextAlphanumeric(4), randomPrefix + randomValue.substring(1), randomPrefix + randomValue.substring(2), randomPrefix + randomValue.substring(3) @@ -267,10 +266,10 @@ public class JvmOptionsTest { randomPrefix + randomValue.substring(1), randomPrefix + randomValue.substring(2), randomPrefix + randomValue.substring(3), - randomPrefix + randomValue.substring(3) + randomAlphanumeric(1), - randomPrefix + randomValue.substring(3) + randomAlphanumeric(2), - randomPrefix + randomValue.substring(3) + randomAlphanumeric(3), - randomPrefix + randomValue + randomAlphanumeric(1) + randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(1), + randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(2), + randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(3), + randomPrefix + randomValue + secure().nextAlphanumeric(1) }; JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue)); @@ -296,10 +295,10 @@ public class JvmOptionsTest { randomPrefix + randomValue.substring(1), randomPrefix + randomValue.substring(2), randomPrefix + randomValue.substring(3), - randomPrefix + randomValue.substring(3) + randomAlphanumeric(1), - randomPrefix + randomValue.substring(3) + randomAlphanumeric(2), - randomPrefix + randomValue.substring(3) + randomAlphanumeric(3), - randomPrefix + randomValue + randomAlphanumeric(1) + randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(1), + randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(2), + randomPrefix + randomValue.substring(3) + secure().nextAlphanumeric(3), + randomPrefix + randomValue + secure().nextAlphanumeric(1) }; JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue)); @@ -313,7 +312,7 @@ public class JvmOptionsTest { @Test public void addFromMandatoryProperty_reports_all_overriding_options_in_single_exception() { String overriding1 = randomPrefix; - String overriding2 = randomPrefix + randomValue + randomAlphanumeric(1); + String overriding2 = randomPrefix + randomValue + secure().nextAlphanumeric(1); properties.setProperty(randomPropertyName, "-foo " + overriding1 + " -bar " + overriding2); JvmOptions underTest = new JvmOptions(ImmutableMap.of(randomPrefix, randomValue)); diff --git a/server/sonar-main/src/test/java/org/sonar/application/es/EsSettingsTest.java b/server/sonar-main/src/test/java/org/sonar/application/es/EsSettingsTest.java index b5e264b8ddb..fbbe56b27cb 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/es/EsSettingsTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/es/EsSettingsTest.java @@ -43,7 +43,7 @@ import org.sonar.process.Props; import org.sonar.process.System2; import static java.util.Optional.ofNullable; -import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric; +import static org.apache.commons.lang3.RandomStringUtils.secure; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; @@ -111,7 +111,7 @@ public class EsSettingsTest { public void constructor_logs_warning_if_env_variable_ES_JVM_OPTIONS_is_set_and_non_empty() { this.listAppender = ListAppender.attachMemoryAppenderToLoggerOf(EsSettings.class); Props props = minimalProps(); - when(system.getenv("ES_JVM_OPTIONS")).thenReturn(randomAlphanumeric(2)); + when(system.getenv("ES_JVM_OPTIONS")).thenReturn(secure().nextAlphanumeric(2)); new EsSettings(props, new EsInstallation(props), system); assertThat(listAppender.getLogs()) @@ -122,11 +122,11 @@ public class EsSettingsTest { private Props minimalProps() { Props props = new Props(new Properties()); - props.set(PATH_HOME.getKey(), randomAlphanumeric(12)); - props.set(PATH_DATA.getKey(), randomAlphanumeric(12)); - props.set(PATH_TEMP.getKey(), randomAlphanumeric(12)); - props.set(PATH_LOGS.getKey(), randomAlphanumeric(12)); - props.set(CLUSTER_NAME.getKey(), randomAlphanumeric(12)); + props.set(PATH_HOME.getKey(), secure().nextAlphanumeric(12)); + props.set(PATH_DATA.getKey(), secure().nextAlphanumeric(12)); + props.set(PATH_TEMP.getKey(), secure().nextAlphanumeric(12)); + props.set(PATH_LOGS.getKey(), secure().nextAlphanumeric(12)); + props.set(CLUSTER_NAME.getKey(), secure().nextAlphanumeric(12)); return props; } |