Skip to content

Commit

Permalink
Fix Sonar warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ksclarke committed Nov 19, 2024
1 parent 47ab790 commit 118afe5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/main/java/info/freelibrary/iiif/webrepl/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.CodeSource;
import java.time.Duration;
import java.util.List;
Expand Down Expand Up @@ -159,7 +160,7 @@ private static class JPv3Handler implements Handler {
}

@Override
@SuppressWarnings({ PMD.COGNITIVE_COMPLEXITY, PMD.SYSTEM_PRINTLN, PMD.COGNITIVE_COMPLEXITY,
@SuppressWarnings({ PMD.COGNITIVE_COMPLEXITY, PMD.SYSTEM_PRINTLN, Sonar.COGNITIVE_COMPLEXITY,
Sonar.SYSTEM_OUT_ERR })
public void handle(final Request aRequest, final Consumer<Response> aCallback) {
final String uri = aRequest.uri();
Expand Down Expand Up @@ -228,9 +229,7 @@ public void handle(final Request aRequest, final Consumer<Response> aCallback) {
response = getResponse(NOT_FOUND, TEXT_CONTENT_TYPE, EMPTY_BODY);
}
}
default -> {
response = getResponse(METHOD_NOT_ALLOWED, TEXT_CONTENT_TYPE, EMPTY_BODY);
}
default -> response = getResponse(METHOD_NOT_ALLOWED, TEXT_CONTENT_TYPE, EMPTY_BODY);
}

aCallback.accept(response);
Expand Down Expand Up @@ -293,22 +292,17 @@ private String getImports() throws IOException {
* @throws IOException If there is trouble reading the imports file
*/
private String getImports(final String aSnippet) throws IOException {
File importsFile = new File("/etc/jshell/imports.jsh");
File importsFile = Path.of("/etc/jshell/imports.jsh").toFile();

// Check to see if we're running from the Maven build
if (!importsFile.exists()) {
importsFile = new File("src/main/docker/imports.jsh");
importsFile = Path.of("src/main/docker/imports.jsh").toFile();
}

try (BufferedReader reader = Files.newBufferedReader(importsFile.toPath())) {
try (final BufferedReader reader = Files.newBufferedReader(importsFile.toPath())) {
return reader.lines().filter(line -> !line.isBlank()).filter(line -> {
final String className = line.substring(line.lastIndexOf('.') + 1, line.length() - 1);

if (aSnippet == null || aSnippet.contains(className)) {
return true;
}

return false;
return aSnippet == null || aSnippet.contains(className);
}).collect(Collectors.joining(EOL)) + EOL;
}
}
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/info/freelibrary/iiif/webrepl/DiagConsumerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

package info.freelibrary.iiif.webrepl;

import static info.freelibrary.util.Constants.EMPTY;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import jdk.jshell.JShell;
import jdk.jshell.SnippetEvent;

/**
* Tests of <code>DiagConsumer</code>.
*/
class DiagConsumerTest {

/** The expected output from the test. */
private static final String EXPECTED = """
Could not parse:
1 int x = [["not a number"]];
Reason: incompatible types: java.lang.String cannot be converted to int
""";

/**
* Tests the <code>DiagConsumer</code> constructor.
*/
@Test
final void testAcceptDiagConsumer() {
try (JShell jshell = JShell.create()) {
final StringBuilder builder = new StringBuilder("int x = \"not a number\";");
final DiagConsumer consumer = new DiagConsumer(builder);

for (final SnippetEvent event : jshell.eval(builder.toString())) {
jshell.diagnostics(event.snippet()).forEach(consumer::accept);
}

assertEquals(EXPECTED, builder.toString());
}
}

/**
* Tests the <code>DiagConsumer</code> constructor.
*/
@Test
final void testConstructor() {
final StringBuilder builder = new StringBuilder();

new DiagConsumer(builder);

assertEquals(EMPTY, builder.toString().trim());
}
}

0 comments on commit 118afe5

Please sign in to comment.