Skip to content

Commit

Permalink
chore: remove some code smells
Browse files Browse the repository at this point in the history
* unused imports
* unnecessary assignments or initializations
* usages of `Arrays#asList` with single arguments
* inner classes that weren't static
  • Loading branch information
bbortt committed Oct 12, 2024
1 parent 9573fd1 commit d6d7a57
Show file tree
Hide file tree
Showing 52 changed files with 228 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public AbstractWatchCommand(String name) {

@Override
public void execute(MixedOperation<T, L, R> operation, TestContext context) {
watch = operation.watch(new Watcher<T>() {
watch = operation.watch(new Watcher<>() {
@Override
public void eventReceived(Action action, T resource) {
if (results.isEmpty() && cachedResult == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.HashMap;
import java.util.Map;

import io.fabric8.kubernetes.api.model.KubernetesResource;
import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.kubernetes.command.*;
Expand Down Expand Up @@ -122,10 +121,7 @@ private Map<String,Object> createMessageHeaders(KubernetesCommand<?, ?> command)
Map<String, Object> headers = new HashMap<>();

headers.put(KubernetesMessageHeaders.COMMAND, command.getName());

for (Map.Entry<String, Object> entry : command.getParameters().entrySet()) {
headers.put(entry.getKey(), entry.getValue());
}
headers.putAll(command.getParameters());

return headers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void testInvalidPageType() throws Exception {
action.execute(context);
}

public class TestPage implements WebPage {
public static class TestPage implements WebPage {
@FindBy(id = "form")
private WebElement form;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public void shouldLoadSeleniumActions() throws IOException {
Assert.assertNull(clearCacheAction.getBrowser());
Assert.assertEquals(clearCacheAction.getName(), "selenium:clear-cache");

StopBrowserAction stopAction = (StopBrowserAction) result.getTestAction(actionIndex++);
StopBrowserAction stopAction = (StopBrowserAction) result.getTestAction(actionIndex);
Assert.assertNotNull(stopAction.getBrowser());
Assert.assertEquals(stopAction.getName(), "selenium:stop");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void shouldLoadSeleniumActions() throws IOException {
Assert.assertNull(clearCacheAction.getBrowser());
Assert.assertEquals(clearCacheAction.getName(), "selenium:clear-cache");

StopBrowserAction stopAction = (StopBrowserAction) result.getTestAction(actionIndex++);
StopBrowserAction stopAction = (StopBrowserAction) result.getTestAction(actionIndex);
Assert.assertNotNull(stopAction.getBrowser());
Assert.assertEquals(stopAction.getName(), "selenium:stop");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void shouldLoadSeleniumActions() throws IOException {
Assert.assertNull(clearCacheAction.getBrowser());
Assert.assertEquals(clearCacheAction.getName(), "selenium:clear-cache");

StopBrowserAction stopAction = (StopBrowserAction) result.getTestAction(actionIndex++);
StopBrowserAction stopAction = (StopBrowserAction) result.getTestAction(actionIndex);
Assert.assertNotNull(stopAction.getBrowser());
Assert.assertEquals(stopAction.getName(), "selenium:stop");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ public static List<String> getParameterList(String parameterString) {
for (int i = 0; i < parameterList.size(); i++) {
int next = i + 1;

String processed = parameterList.get(i);
StringBuilder processed = new StringBuilder(parameterList.get(i));

if (processed.startsWith("'") && !processed.endsWith("'")) {
if (processed.toString().startsWith("'") && !processed.toString().endsWith("'")) {
while (next < parameterList.size()) {
if (parameterString.contains(processed + ", " + parameterList.get(next))) {
processed += ", " + parameterList.get(next);
processed.append(", ").append(parameterList.get(next));
} else if (parameterString.contains(processed + "," + parameterList.get(next))) {
processed += "," + parameterList.get(next);
processed.append(",").append(parameterList.get(next));
} else if (parameterString.contains(processed + " , " + parameterList.get(next))) {
processed += " , " + parameterList.get(next);
processed.append(" , ").append(parameterList.get(next));
} else {
processed += parameterList.get(next);
processed.append(parameterList.get(next));
}

i++;
Expand All @@ -74,7 +74,7 @@ public static List<String> getParameterList(String parameterString) {

}

postProcessed.add(cutOffSingleQuotes(processed));
postProcessed.add(cutOffSingleQuotes(processed.toString()));
}

return postProcessed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static String replaceFunctionsInString(final String stringValue, TestCont

final String value = resolveFunction(variableNameBuf.toString(), context);

strBuffer.append(newString.substring(startIndex, searchIndex));
strBuffer.append(newString, startIndex, searchIndex);

if (enableQuoting) {
strBuffer.append("'" + value + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class FailureStackElement {
private String testFilePath;

/** Line number in XML test case where error happened */
private Long lineNumberStart = 0L;
private Long lineNumberStart;

/** Failing action in XML test case ends in this line */
private Long lineNumberEnd = 0L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void doExecute(TestContext context) {
}

//parse XML document and define XML source for transformation
Source xmlSource = null;
Source xmlSource;
if (xmlResourcePath != null) {
xmlSource = new StringSource(context.replaceDynamicContentInString(FileUtils.readToString(FileUtils.getFileResource(xmlResourcePath, context),
Charset.forName(context.replaceDynamicContentInString(xmlResourceCharset)))));
Expand All @@ -103,7 +103,7 @@ public void doExecute(TestContext context) {
}

//parse XSLT document and define XSLT source for transformation
Source xsltSource = null;
Source xsltSource;
if (xsltResourcePath != null) {
xsltSource = new StringSource(context.replaceDynamicContentInString(FileUtils.readToString(FileUtils.getFileResource(xsltResourcePath, context),
Charset.forName(context.replaceDynamicContentInString(xsltResourceCharset)))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String execute(List<String> parameterList, TestContext context) {
Calendar calendar = calendarProvider.getInstance();

SimpleDateFormat dateFormat;
String result = "";
String result;

if (parameterList.size() > 2) {
dateFormat = new SimpleDateFormat(parameterList.get(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public String execute(List<String> parameterList, TestContext context) {
double result = 0.0;

for (String token : parameterList) {
result += Double.valueOf(token);
result += Double.parseDouble(token);
}

return Double.valueOf(result).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Map.Entry;

import org.citrusframework.context.TestContext;
import org.citrusframework.util.StringUtils;

import static org.citrusframework.util.StringUtils.hasText;

Expand All @@ -34,7 +33,7 @@
public class MessageSelectorBuilder {

/** Selector string */
private String selectorString = "";
private String selectorString;

/**
* Constructor using fields.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.List;

import static java.util.Collections.singletonList;

/**
* @since 2.5
*/
Expand All @@ -46,17 +47,17 @@ public void shouldExtractControlParametersSuccessfully(String controlExpression,
public Object[][] validControlExpressions() {
return new Object[][]{
// {control-expression, expected-parameter-1, expected-parameter-2, ..}
{"'a'", Arrays.asList("a")},
{"'a',", Arrays.asList("a")},
{"'a','b'", Arrays.asList("a","b")},
{"'a','b',", Arrays.asList("a","b")},
{"'a,s','b',", Arrays.asList("a,s","b")},
{"'a)s','b',", Arrays.asList("a)s","b")},
{"'a's','b',", Arrays.asList("a's","b")},
{"''", Arrays.asList("")},
{"'',", Arrays.asList("")},
{"", Arrays.<String>asList()},
{null, Arrays.<String>asList()},
{"'a'", singletonList("a")},
{"'a',", singletonList("a")},
{"'a','b'", List.of("a","b")},
{"'a','b',", List.of("a","b")},
{"'a,s','b',", List.of("a,s","b")},
{"'a)s','b',", List.of("a)s","b")},
{"'a's','b',", List.of("a's","b")},
{"''", singletonList("")},
{"'',", singletonList("")},
{"", List.<String>of()},
{null, List.<String>of()},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,32 @@

package org.citrusframework.validation.matcher.core;

import java.util.Arrays;
import java.util.List;

import org.citrusframework.UnitTestSupport;
import org.citrusframework.exceptions.ValidationException;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.List;

import static java.util.Collections.singletonList;

public class ContainsIgnoreCaseValidationMatcherTest extends UnitTestSupport {

private ContainsIgnoreCaseValidationMatcher matcher = new ContainsIgnoreCaseValidationMatcher();

@Test
public void testValidateSuccess() {
matcher.validate("field", "This is a test", Arrays.asList("is a"), context);
matcher.validate("field", "This is a test", Arrays.asList("this"), context);
matcher.validate("field", "This is a test", Arrays.asList("TEST"), context);
matcher.validate("field", "This is a 0815test", Arrays.asList("0815"), context);
matcher.validate("field", "This is a test", Arrays.asList(" "), context);
matcher.validate("field", "This is a test", Arrays.asList(" IS A "), context);
matcher.validate("field", "This is a test", singletonList("is a"), context);
matcher.validate("field", "This is a test", singletonList("this"), context);
matcher.validate("field", "This is a test", singletonList("TEST"), context);
matcher.validate("field", "This is a 0815test", singletonList("0815"), context);
matcher.validate("field", "This is a test", singletonList(" "), context);
matcher.validate("field", "This is a test", singletonList(" IS A "), context);
}

@Test
public void testValidateError() {
assertException("field", "This is a test", Arrays.asList("0815"));
assertException("field", "This is a test", singletonList("0815"));
}

private void assertException(String fieldName, String value, List<String> control) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package org.citrusframework.validation.matcher.core;

import java.util.Arrays;
import java.util.List;

import org.citrusframework.UnitTestSupport;
import org.citrusframework.exceptions.ValidationException;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.List;

import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNullElse;

public class ContainsValidationMatcherTest extends UnitTestSupport {
Expand All @@ -32,17 +32,17 @@ public class ContainsValidationMatcherTest extends UnitTestSupport {

@Test
public void testValidateSuccess() {
matcher.validate("field", "This is a test", Arrays.asList("is a"), context);
matcher.validate("field", "This is a test", Arrays.asList("This"), context);
matcher.validate("field", "This is a test", Arrays.asList("test"), context);
matcher.validate("field", "This is a 0815test", Arrays.asList("0815"), context);
matcher.validate("field", "This is a test", Arrays.asList(" "), context);
matcher.validate("field", "This is a test", singletonList("is a"), context);
matcher.validate("field", "This is a test", singletonList("This"), context);
matcher.validate("field", "This is a test", singletonList("test"), context);
matcher.validate("field", "This is a 0815test", singletonList("0815"), context);
matcher.validate("field", "This is a test", singletonList(" "), context);
}

@Test
public void testValidateError() {
assertException("field", "This is a test", Arrays.asList("0815"));
assertException("field", null, Arrays.asList("control"));
assertException("field", "This is a test", singletonList("0815"));
assertException("field", null, singletonList("control"));
}

private void assertException(String fieldName, String value, List<String> control) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@

package org.citrusframework.validation.matcher.core;

import java.util.Arrays;

import org.citrusframework.UnitTestSupport;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.List;

public class CreateVariableValidationMatcherTest extends UnitTestSupport {

private CreateVariableValidationMatcher matcher = new CreateVariableValidationMatcher();
private final CreateVariableValidationMatcher matcher = new CreateVariableValidationMatcher();

@Test
public void testValidateSuccess() {
matcher.validate("field", "This is a test", Arrays.<String>asList(), context);
matcher.validate("field", "This is a test", List.of(), context);

Assert.assertEquals(context.getVariable("field"), "This is a test");

matcher.validate("field", "This is a 2nd test", Arrays.<String>asList(), context);
matcher.validate("field", "This is a 2nd test", List.of(), context);

Assert.assertEquals(context.getVariable("field"), "This is a 2nd test");

context.setVariable("foo", "bar");

matcher.validate("field", "Another test", Arrays.asList("foo"), context);
matcher.validate("field", "Another test", List.of("foo"), context);

Assert.assertEquals(context.getVariable("field"), "This is a 2nd test");
Assert.assertEquals(context.getVariable("foo"), "Another test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,29 @@

package org.citrusframework.validation.matcher.core;

import java.util.Arrays;
import java.util.List;

import org.citrusframework.UnitTestSupport;
import org.citrusframework.exceptions.ValidationException;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.List;

import static java.util.Collections.singletonList;

public class DatePatternValidationMatcherTest extends UnitTestSupport {

private DatePatternValidationMatcher matcher = new DatePatternValidationMatcher();

@Test
public void testValidateSuccess() {
matcher.validate("field", "2011-10-10", Arrays.asList("yyyy-MM-dd"), context);
matcher.validate("field", "10.10.2011", Arrays.asList("dd.MM.yyyy"), context);
matcher.validate("field", "2011-01-01T01:02:03", Arrays.asList("yyyy-MM-dd'T'HH:mm:ss"), context);
matcher.validate("field", "2011-10-10", singletonList("yyyy-MM-dd"), context);
matcher.validate("field", "10.10.2011", singletonList("dd.MM.yyyy"), context);
matcher.validate("field", "2011-01-01T01:02:03", singletonList("yyyy-MM-dd'T'HH:mm:ss"), context);
}

@Test
public void testValidateError() {
assertException("field", "201110-10", Arrays.asList("yy-MM-dd"));
assertException("field", "201110-10", singletonList("yy-MM-dd"));
}

private void assertException(String fieldName, String value, List<String> control) {
Expand Down
Loading

0 comments on commit d6d7a57

Please sign in to comment.