Skip to content

Commit

Permalink
Compiling with Java release 8
Browse files Browse the repository at this point in the history
Replaced Java 11 Files.readString with an equivalent FilesUtil.readString
  • Loading branch information
cedricvidal authored and jdubois committed Oct 6, 2023
1 parent bf4fa47 commit d8d5b9f
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 6 deletions.
48 changes: 47 additions & 1 deletion cli/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>8</release>
<annotationProcessorPaths>
<path>
<groupId>info.picocli</groupId>
Expand Down Expand Up @@ -45,7 +46,7 @@
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<version>3.5.1</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -87,6 +88,51 @@
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.3</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit-platform-engine</artifactId>
<groupId>org.junit.platform</groupId>
</exclusion>
<exclusion>
<artifactId>junit-jupiter-api</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
<exclusion>
<artifactId>apiguardian-api</artifactId>
<groupId>org.apiguardian</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>byte-buddy</artifactId>
<groupId>net.bytebuddy</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.1.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
22 changes: 22 additions & 0 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
<java.version>17</java.version>
<picocli.version>4.7.5</picocli.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.1.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>info.picocli</groupId>
Expand All @@ -26,6 +37,16 @@
<artifactId>picocli-jansi-graalvm</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -35,6 +56,7 @@
<!-- annotationProcessorPaths requires maven-compiler-plugin version 3.5 or higher -->
<version>3.11.0</version>
<configuration>
<release>8</release>
<annotationProcessorPaths>
<path>
<groupId>info.picocli</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.concurrent.Callable;
import java.util.stream.Stream;

import io.github.nubesgen.cli.util.FilesUtil;
import io.github.nubesgen.cli.util.Output;

@CommandLine.Command(name = "scan", description = "Scan the current project to find the technologies it uses")
Expand Down Expand Up @@ -59,7 +60,7 @@ public static String scan(String workingDirectory) {
try {
if (mavenFile.exists()) {
Output.printInfo("Maven detected");
testFile = Files.readString(mavenFile.toPath());
testFile = FilesUtil.readString(mavenFile.toPath());
if (testFile.contains("spring-cloud-starter-function") || testFile.contains("azure-functions-java-library")) {
Output.printInfo("Project type detected: Azure Functions");
getRequest = "?application=FUNCTION.consumption";
Expand All @@ -81,7 +82,7 @@ public static String scan(String workingDirectory) {
getRequest = JavaScanner.javaAddOnScanner(testFile, getRequest);
} else if (gradleFile.exists()) {
Output.printInfo("Gradle project detected");
testFile = Files.readString(gradleFile.toPath());
testFile = FilesUtil.readString(gradleFile.toPath());
if (testFile.contains("spring-cloud-starter-function") || testFile.contains("azure-functions-java-library")) {
Output.printInfo("Project type detected: Azure Functions");
getRequest = "?application=FUNCTION.consumption";
Expand All @@ -100,19 +101,19 @@ public static String scan(String workingDirectory) {
getRequest = JavaScanner.javaAddOnScanner(testFile, getRequest);
} else if (nodejsFile.exists()) {
Output.printInfo("NodeJS project detected");
testFile = Files.readString(nodejsFile.toPath());
testFile = FilesUtil.readString(nodejsFile.toPath());
getRequest += "&runtime=NODEJS";
getRequest = NodeJsScanner.nodejsDatabaseScanner(testFile, getRequest);
getRequest = NodeJsScanner.nodejsAddOnScanner(testFile, getRequest);
} else if (dotnetFile.isPresent()) {
Output.printInfo(".NET project detected");
testFile = Files.readString(dotnetFile.get());
testFile = FilesUtil.readString(dotnetFile.get());
getRequest += "&runtime=DOTNET";
getRequest = DotNetScanner.dotnetDatabaseScanner(testFile, getRequest);
getRequest = DotNetScanner.dotnetAddOnScanner(testFile, getRequest);
} else if (pythonFile.exists()) {
Output.printInfo("Python project detected");
testFile = Files.readString(pythonFile.toPath());
testFile = FilesUtil.readString(pythonFile.toPath());
getRequest += "&runtime=PYTHON";
getRequest = PythonScanner.pythonDatabaseScanner(testFile, getRequest);
getRequest = PythonScanner.pythonAddOnScanner(testFile, getRequest);
Expand Down
37 changes: 37 additions & 0 deletions cli/src/main/java/io/github/nubesgen/cli/util/FilesUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.nubesgen.cli.util;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Java 8 compatible Files helpers.
*/
public class FilesUtil {

/**
* Reads all content from a file into a string, decoding from bytes to characters using the UTF-8 charset.
* The method ensures that the file is closed when all content have been read or an I/O error, or other
* runtime exception, is thrown.
*
* Reproduces the behavior of Java 11 Paths.readString(Path).
*
* @param path the path to the file
* @return a String containing the content read from the file
* @throws IOException if an I/O error occurs reading from the file or a malformed or unmappable byte sequence is read
*/
public static String readString(final Path path) throws IOException {
final StringBuilder resultStringBuilder = new StringBuilder();
try (final BufferedReader reader = Files.newBufferedReader(path, UTF_8)) {
String line;
while ((line = reader.readLine()) != null) {
resultStringBuilder.append(line).append("\n");
}
}
return resultStringBuilder.toString();
}

}
28 changes: 28 additions & 0 deletions cli/src/test/java/io/github/nubesgen/cli/util/FilesUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.nubesgen.cli.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.NoSuchFileException;

import org.junit.jupiter.api.Test;
class FilesUtilTest {

@Test
public void readStringOk() throws URISyntaxException, IOException {
final Path path = Paths.get(getClass().getResource("/util/content.txt").toURI());
final String content = FilesUtil.readString(path);
assertThat(content).isEqualTo("Nubes\nGen\n");
}

@Test
public void readStringNonExistentThrowsFileNotFoundException() throws URISyntaxException, IOException {
final Path path = Paths.get("nonexistent");
assertThatThrownBy(() -> FilesUtil.readString(path)).isInstanceOf(NoSuchFileException.class);
}

}
2 changes: 2 additions & 0 deletions cli/src/test/resources/util/content.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Nubes
Gen

0 comments on commit d8d5b9f

Please sign in to comment.