Skip to content

Commit

Permalink
Merge pull request #179 from oracle/resolve-sonar-issues
Browse files Browse the repository at this point in the history
Resolve simple Sonar code issues
  • Loading branch information
rjeberhard authored Apr 19, 2022
2 parents 172aea1 + 9845c19 commit 6dd5f91
Show file tree
Hide file tree
Showing 32 changed files with 337 additions and 317 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020, 2021, Oracle and/or its affiliates.
// Copyright (c) 2020, 2022, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.wls.buildhelper;
Expand Down Expand Up @@ -26,7 +26,7 @@
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

public class BuildHelperMojoTest {
class BuildHelperMojoTest {

private final BuildHelperMojo mojo = new BuildHelperMojo();
private final List<Memento> mementos = new ArrayList<>();
Expand All @@ -46,55 +46,55 @@ public void tearDown() {
}

@Test
public void helperImplementsMojo() {
void helperImplementsMojo() {
assertThat(mojo, Matchers.instanceOf(AbstractMojo.class));
}

@Test
public void mojoHasGoalAnnotation() {
void mojoHasGoalAnnotation() {
assertThat(mojoTestSupport.getClassAnnotation(), notNullValue());
}

@Test
public void mojoAnnotatedWithName() {
void mojoAnnotatedWithName() {
assertThat(mojoTestSupport.getClassAnnotation().get("name"), equalTo("copy"));
}

@Test
public void mojoAnnotatedWithDefaultPhase() {
void mojoAnnotatedWithDefaultPhase() {
assertThat(mojoTestSupport.getClassAnnotation().get("defaultPhase"), equalTo(PROCESS_RESOURCES));
}

@Test
public void hasRequiredSourceFileParameter() throws NoSuchFieldException {
void hasRequiredSourceFileParameter() throws NoSuchFieldException {
assertThat(mojoTestSupport.getParameterField("sourceFile").getType(), equalTo(String.class));
assertThat(mojoTestSupport.getParameterAnnotation("sourceFile").get("required"), is(true));
}

@Test
public void hasAnnotatedTargetFileField_withNoDefault() throws NoSuchFieldException {
void hasAnnotatedTargetFileField_withNoDefault() throws NoSuchFieldException {
assertThat(mojoTestSupport.getParameterField("targetFile").getType(), equalTo(File.class));
assertThat(mojoTestSupport.getParameterAnnotation("targetFile").get("defaultValue"), nullValue());
}

@Test
public void targetFileField_isRequired() throws NoSuchFieldException {
void targetFileField_isRequired() throws NoSuchFieldException {
assertThat(mojoTestSupport.getParameterAnnotation("targetFile").get("required"), is(true));
}

@Test
public void hasAnnotatedUserDirFileField_withNullDefault() throws NoSuchFieldException {
void hasAnnotatedUserDirFileField_withNullDefault() throws NoSuchFieldException {
assertThat(mojoTestSupport.getParameterField("userDir").getType(), equalTo(File.class));
assertThat(mojoTestSupport.getParameterAnnotation("userDir").get("defaultValue"), nullValue());
}

@Test
public void userDirField_isNotRequired() throws NoSuchFieldException {
void userDirField_isNotRequired() throws NoSuchFieldException {
assertThat(mojoTestSupport.getParameterAnnotation("userDir").get("required"), nullValue());
}

@Test
public void whenSourceAndTargetAbsolute_useAbsolutePaths() throws Exception {
void whenSourceAndTargetAbsolute_useAbsolutePaths() throws Exception {
setMojoParameter("sourceFile", "/root/source");
setMojoParameter("targetFile", new File("/root/target"));

Expand All @@ -105,7 +105,7 @@ public void whenSourceAndTargetAbsolute_useAbsolutePaths() throws Exception {
}

@Test
public void whenSourcePathIsRelative_computeAbsoluteRelativeToUserDir() throws Exception {
void whenSourcePathIsRelative_computeAbsoluteRelativeToUserDir() throws Exception {
setMojoParameter("sourceFile", "source");
setMojoParameter("targetFile", new File("/root/target"));
setMojoParameter("userDir", new File("/root/nested"));
Expand All @@ -117,7 +117,7 @@ public void whenSourcePathIsRelative_computeAbsoluteRelativeToUserDir() throws E
}

@Test
public void whenSourcePathIsRelativeAndNoUserDir_useSystemProperty() throws Exception {
void whenSourcePathIsRelativeAndNoUserDir_useSystemProperty() throws Exception {
System.setProperty("user.dir", "/user");
setMojoParameter("sourceFile", "source");
setMojoParameter("targetFile", new File("/root/target"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021, Oracle and/or its affiliates.
// Copyright (c) 2021, 2022, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.wls.exporter;
Expand All @@ -8,6 +8,10 @@

public class ConfigurationDisplay {

private ConfigurationDisplay() {
// no-op
}

public static void displayConfiguration(OutputStream outputStream) {
try (PrintStream ps = new PrintStream(outputStream)) {
ps.println("<p>Current Configuration</p>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
* @author Russell Gold
*/
class DemoInputs {

private DemoInputs() {
// no-op
}

@SuppressWarnings("unused")
static final String YAML_STRING = "---\n" +
"startDelaySeconds: 5\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
*/
public class LiveConfiguration {

private LiveConfiguration() {
// no-op
}

/** The address used to access WLS (cannot use the address found in the request due to potential server-side request forgery. */
static final String WLS_HOST;

Expand Down Expand Up @@ -190,7 +194,7 @@ public static void updateConfiguration() {
installNewConfiguration(updater.getUpdate());
}

private synchronized static void installNewConfiguration(ConfigurationUpdate update) {
private static synchronized void installNewConfiguration(ConfigurationUpdate update) {
if (update.getTimestamp() > timestamp) {
getConfig().replace(toConfiguration(update.getConfiguration()));
timestamp = update.getTimestamp();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017, 2020, Oracle and/or its affiliates.
// Copyright (c) 2017, 2022, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.wls.exporter.domain;
Expand All @@ -13,6 +13,9 @@
*/
public class MapUtils {

private MapUtils() {
// no-op
}
private static final String ILLEGAL_VALUE_FORMAT = "Illegal value for %s: %s. Value must be %s";

/**
Expand Down Expand Up @@ -51,8 +54,8 @@ static Boolean getBooleanValue(Map<String, Object> map, String key) {
throw createBadTypeException(key, value, "a boolean");
}

private final static String[] TRUE_VALUES = {"true", "t", "yes", "on", "y"};
private final static String[] FALSE_VALUES = {"false", "f", "no", "off", "n"};
private static final String[] TRUE_VALUES = {"true", "t", "yes", "on", "y"};
private static final String[] FALSE_VALUES = {"false", "f", "no", "off", "n"};

private static boolean inValues(Object candidate, String... matches) {
for (String match : matches)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017, 2020, Oracle and/or its affiliates.
// Copyright (c) 2017, 2022, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.wls.exporter.domain;
Expand All @@ -11,6 +11,11 @@
* @author Russell Gold
*/
public class SnakeCaseUtil {

private SnakeCaseUtil() {
// no-op
}

private static final Pattern SNAKE_CASE_PATTERN = Pattern.compile("([a-z0-9])([A-Z])");

static String convert(String s) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021, Oracle and/or its affiliates.
// Copyright (c) 2021, 2022, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.wls.exporter.webapp;
Expand All @@ -12,6 +12,10 @@

public class ServletUtils {

private ServletUtils() {
// no-op
}

/** The path to the configuration file within the web application. */
public static final String CONFIG_YML = "/config.yml";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ConfigurationFormCallTest {
class ConfigurationFormCallTest {

private static final String CONFIGURATION =
"hostName: " + HOST_NAME + "\n" +
Expand Down Expand Up @@ -72,7 +72,7 @@ public void setUp() {
}

@Test
public void whenNoConfigurationSpecified_reportFailure() {
void whenNoConfigurationSpecified_reportFailure() {
assertThrows(RuntimeException.class, () -> handleConfigurationFormCall(context));
}

Expand All @@ -83,35 +83,35 @@ private void handleConfigurationFormCall(InvocationContextStub context) throws I
}

@Test
public void whenRequestUsesHttp_authenticateWithHttp() throws Exception {
void whenRequestUsesHttp_authenticateWithHttp() throws Exception {
handleConfigurationFormCall(context.withConfigurationForm("replace", CONFIGURATION));

assertThat(factory.getClientUrl(), startsWith("http:"));
}

@Test
public void whenRequestUsesHttps_authenticateWithHttps() throws Exception {
void whenRequestUsesHttps_authenticateWithHttps() throws Exception {
handleConfigurationFormCall(context.withHttps().withConfigurationForm("replace", CONFIGURATION));

assertThat(factory.getClientUrl(), startsWith("https:"));
}

@Test
public void afterUploadWithReplace_useNewConfiguration() throws Exception {
void afterUploadWithReplace_useNewConfiguration() throws Exception {
handleConfigurationFormCall(context.withConfigurationForm("replace", CONFIGURATION));

assertThat(LiveConfiguration.asString(), equalTo(CONFIGURATION));
}

@Test
public void afterUpload_redirectToMainPage() throws Exception {
void afterUpload_redirectToMainPage() throws Exception {
handleConfigurationFormCall(context.withConfigurationForm("replace", CONFIGURATION));

assertThat(context.getRedirectLocation(), equalTo(WebAppConstants.MAIN_PAGE));
}

@Test
public void whenRestPortInaccessible_switchToSpecifiedPort() throws Exception {
void whenRestPortInaccessible_switchToSpecifiedPort() throws Exception {
LiveConfiguration.loadFromString(CONFIGURATION_WITH_REST_PORT);
factory.throwConnectionFailure("localhost", REST_PORT);

Expand All @@ -121,14 +121,14 @@ public void whenRestPortInaccessible_switchToSpecifiedPort() throws Exception {
}

@Test
public void afterUploadWithAppend_useCombinedConfiguration() throws Exception {
void afterUploadWithAppend_useCombinedConfiguration() throws Exception {
handleConfigurationFormCall(context.withConfigurationForm("append", ADDED_CONFIGURATION));

assertThat(LiveConfiguration.asString(), equalTo(COMBINED_CONFIGURATION));
}

@Test
public void whenSelectedFileIsNotYaml_reportError() throws Exception {
void whenSelectedFileIsNotYaml_reportError() throws Exception {
handleConfigurationFormCall(context.withConfigurationForm("replace", NON_YAML));

assertThat(context.getResponse(), containsString(ConfigurationException.NOT_YAML_FORMAT));
Expand All @@ -138,7 +138,7 @@ public void whenSelectedFileIsNotYaml_reportError() throws Exception {
"this is not yaml\n";

@Test
public void whenSelectedFileHasPartialYaml_reportError() throws Exception {
void whenSelectedFileHasPartialYaml_reportError() throws Exception {
handleConfigurationFormCall(context.withConfigurationForm("replace", PARTIAL_YAML));

assertThat(context.getResponse(), containsString(ConfigurationException.BAD_YAML_FORMAT));
Expand All @@ -148,7 +148,7 @@ public void whenSelectedFileHasPartialYaml_reportError() throws Exception {
"queries:\nkey name\n";

@Test
public void whenSelectedFileHasBadBooleanValue_reportError() throws Exception {
void whenSelectedFileHasBadBooleanValue_reportError() throws Exception {
handleConfigurationFormCall(context.withConfigurationForm("append", ADDED_CONFIGURATION_WITH_BAD_BOOLEAN));

assertThat(context.getResponse(), containsString(BAD_BOOLEAN_STRING));
Expand All @@ -162,14 +162,14 @@ public void whenSelectedFileHasBadBooleanValue_reportError() throws Exception {
" values: [age, sex]\n";

@Test
public void afterSelectedFileHasBadBooleanValue_configurationIsUnchanged() throws Exception {
void afterSelectedFileHasBadBooleanValue_configurationIsUnchanged() throws Exception {
handleConfigurationFormCall(context.withConfigurationForm("append", ADDED_CONFIGURATION_WITH_BAD_BOOLEAN));

assertThat(LiveConfiguration.asString(), equalTo(CONFIGURATION));
}

@Test
public void whenServerSends403StatusOnGet_returnToClient() throws Exception {
void whenServerSends403StatusOnGet_returnToClient() throws Exception {
factory.reportNotAuthorized();

handleConfigurationFormCall(context.withConfigurationForm("replace", CONFIGURATION));
Expand All @@ -178,7 +178,7 @@ public void whenServerSends403StatusOnGet_returnToClient() throws Exception {
}

@Test
public void whenServerSends401StatusOnGet_returnToClient() throws Exception {
void whenServerSends401StatusOnGet_returnToClient() throws Exception {
factory.reportAuthenticationRequired("Test-Realm");

handleConfigurationFormCall(context.withConfigurationForm("replace", CONFIGURATION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ private void handleConfigurationPutCall(InvocationContextStub context) throws IO
}

@Test
public void whenSpecifiedConfigurationHasBadBooleanValue_reportError() throws Exception {
void whenSpecifiedConfigurationHasBadBooleanValue_reportError() throws Exception {
handleConfigurationPutCall(context.withConfiguration("application/yaml", CONFIGURATION_WITH_BAD_BOOLEAN));

assertThat(context.getResponseStatus(), equalTo(HttpURLConnection.HTTP_BAD_REQUEST));
}

@Test
public void updateSpecifiedConfiguration() throws Exception {
void updateSpecifiedConfiguration() throws Exception {
handleConfigurationPutCall(context.withConfiguration("application/yaml", YAML_CONFIGURATION));

assertThat(LiveConfiguration.asString(), equalTo(YAML_CONFIGURATION));
}

@Test
public void updateConfigurationWithJson() throws Exception {
void updateConfigurationWithJson() throws Exception {
handleConfigurationPutCall(context.withConfiguration("application/json", JSON_CONFIGURATION));

assertThat(LiveConfiguration.asString(), equalTo(YAML_CONFIGURATION));
Expand Down
Loading

0 comments on commit 6dd5f91

Please sign in to comment.