diff options
author | Matteo Mara <matteo.mara@sonarsource.com> | 2024-03-20 15:14:10 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-03-22 20:02:32 +0000 |
commit | 7f3f44f830868197b19cbd8a6e151cb7885d0213 (patch) | |
tree | 3780541b2c5540a086f3d11fa9cdd08e689457cd /sonar-plugin-api-impl | |
parent | 7d20b99c3cdb1a7c044b5ab51ddede8819e1305d (diff) | |
download | sonarqube-7f3f44f830868197b19cbd8a6e151cb7885d0213.tar.gz sonarqube-7f3f44f830868197b19cbd8a6e151cb7885d0213.zip |
NO JIRA fix code smells after moving to lang3 library
Diffstat (limited to 'sonar-plugin-api-impl')
3 files changed, 68 insertions, 5 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/NewRule.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/NewRule.java index f30a0b58371..660b31fa69b 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/NewRule.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/NewRule.java @@ -66,7 +66,7 @@ public class NewRule { } public NewRule setStatus(@Nullable RuleStatus s) { - this.status = (RuleStatus) ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus()); + this.status = ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus()); return this; } diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java index cc62c256c08..adb0d1a0171 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/impl/ws/ValidatingRequest.java @@ -22,6 +22,7 @@ package org.sonar.api.impl.ws; import java.io.InputStream; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.Set; import javax.annotation.CheckForNull; import javax.annotation.Nullable; @@ -33,7 +34,6 @@ import static java.lang.String.format; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static java.util.Objects.requireNonNull; -import static org.apache.commons.lang3.StringUtils.defaultString; import static org.sonar.api.utils.Preconditions.checkArgument; /** @@ -68,7 +68,7 @@ public abstract class ValidatingRequest extends Request { public String param(String key) { WebService.Param definition = action.param(key); String rawValue = readParam(key, definition); - String rawValueOrDefault = defaultString(rawValue, definition.defaultValue()); + String rawValueOrDefault = Objects.toString(rawValue, definition.defaultValue()); String value = rawValueOrDefault == null ? null : trim(rawValueOrDefault); validateRequiredValue(key, definition, rawValue); if (value == null) { @@ -124,7 +124,7 @@ public abstract class ValidatingRequest extends Request { @Override public List<String> paramAsStrings(String key) { WebService.Param definition = action.param(key); - String value = defaultString(readParam(key, definition), definition.defaultValue()); + String value = Objects.toString(readParam(key, definition), definition.defaultValue()); if (value == null) { return null; } @@ -152,7 +152,7 @@ public abstract class ValidatingRequest extends Request { private String readParam(String key, @Nullable WebService.Param definition) { checkArgument(definition != null, "BUG - parameter '%s' is undefined for action '%s'", key, action.key()); String deprecatedKey = definition.deprecatedKey(); - String param = deprecatedKey != null ? defaultString(readParam(deprecatedKey), readParam(key)) : readParam(key); + String param = deprecatedKey != null ? Objects.toString(readParam(deprecatedKey), readParam(key)) : readParam(key); if (param != null && param.contains("\0")) { throw new IllegalArgumentException("Request parameters are not allowed to contain NUL character"); } diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/ws/ValidatingRequestTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/ws/ValidatingRequestTest.java new file mode 100644 index 00000000000..1c017da4d5d --- /dev/null +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/ws/ValidatingRequestTest.java @@ -0,0 +1,63 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.impl.ws; + +import org.junit.Test; +import org.mockito.Mockito; +import org.sonar.api.server.ws.WebService; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ValidatingRequestTest { + + ValidatingRequest underTest = Mockito.mock(ValidatingRequest.class, Mockito.CALLS_REAL_METHODS); + @Test + public void paramReturnsTheDefaultValue_whenDefaultIsSetAndValueIsNot() { + underTest.setAction(mock(WebService.Action.class)); + WebService.Param param = mockParamWithDefaultValue("default"); + + when(underTest.action().param("param")).thenReturn(param); + + assertThat(underTest.param("param")).isEqualTo("default"); + } + + @Test + public void paramAsStringsReturnsTheDefaultValues_whenDefaultIsSetAndValueIsNot() { + underTest.setAction(mock(WebService.Action.class)); + WebService.Param param = mockParamWithDefaultValue("default,values"); + + when(underTest.action().param("param")).thenReturn(param); + + assertThat(underTest.paramAsStrings("param")).containsExactly("default", "values"); + } + + private static WebService.Param mockParamWithDefaultValue(String defaultValue) { + WebService.Param param = mock(WebService.Param.class); + when(param.defaultValue()).thenReturn(defaultValue); + when(param.possibleValues()).thenReturn(null); + when(param.maximumLength()).thenReturn(10); + when(param.maximumLength()).thenReturn(10); + when(param.maximumValue()).thenReturn(null); + when(param.maxValuesAllowed()).thenReturn(2); + return param; + } +} |