Skip to content

Commit

Permalink
revelcGH-30: Handle files that are empty or lack a newline
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Sep 5, 2020
1 parent 512fda0 commit fc86a6a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@
<plugin>
<groupId>org.apache.rat</groupId>
<artifactId>apache-rat-plugin</artifactId>
<configuration>
<excludes>
<exclude>src/test/resources/EmptyFile.java</exclude>
<exclude>src/test/resources/FileWithoutNewline.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>check-licenses</id>
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/net/revelc/code/impsort/ImpSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,25 @@ public ImpSort(final Charset sourceEncoding, final Grouper grouper, final boolea

public Result parseFile(final Path path) throws IOException {
String file = new String(Files.readAllBytes(path), sourceEncoding);
LineEnding fileLineEnding = LineEnding.determineLineEnding(file);
LineEnding impLineEnding;
if (lineEnding == LineEnding.KEEP) {
impLineEnding = fileLineEnding;
} else {
impLineEnding = lineEnding;
}
List<String> fileLines = Arrays.asList(file.split(fileLineEnding.getChars()));
ParseResult<CompilationUnit> parseResult = new JavaParser().parse(file);
CompilationUnit unit =
parseResult.getResult().orElseThrow(() -> new IOException("Unable to parse " + path));
Position packagePosition =
unit.getPackageDeclaration().map(p -> p.getEnd().get()).orElse(unit.getBegin().get());
NodeList<ImportDeclaration> importDeclarations = unit.getImports();
LineEnding impLineEnding = lineEnding;
List<String> fileLines = Collections.emptyList();
if (!file.isEmpty()) {
LineEnding fileLineEnding = LineEnding.determineLineEnding(file);
if (lineEnding == LineEnding.KEEP) {
impLineEnding = fileLineEnding;
}
if (lineEnding == LineEnding.UNKNOWN) {
fileLines = Collections.singletonList(file);
} else {
fileLines = Arrays.asList(file.split(fileLineEnding.getChars()));
}
}
if (importDeclarations.isEmpty()) {
return new Result(path, sourceEncoding, fileLines, 0, fileLines.size(), "", "",
Collections.emptyList(), impLineEnding);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/revelc/code/impsort/LineEnding.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
public enum LineEnding {

AUTO(System.lineSeparator()), KEEP(null), LF("\n"), CRLF("\r\n"), CR("\r"), UNKNOWN(null);
AUTO(System.lineSeparator()), KEEP(null), LF("\n"), CRLF("\r\n"), CR("\r"), UNKNOWN("");

private final String chars;

Expand Down
58 changes: 58 additions & 0 deletions src/test/java/net/revelc/code/impsort/FileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.revelc.code.impsort;

import static org.junit.Assert.assertTrue;

import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Test;

/**
* Test class for special file cases.
*/
public class FileTest {

private static Grouper eclipseDefaults =
new Grouper("java.,javax.,org.,com.", "", false, false, true);

/**
* Test successfully parsing empty (0 byte) file.
*/
@Test
public void test_empty_file() throws Exception {
Path p =
Paths.get(System.getProperty("user.dir"), "src", "test", "resources", "EmptyFile.java");
Result result =
new ImpSort(StandardCharsets.UTF_8, eclipseDefaults, true, true, LineEnding.AUTO)
.parseFile(p);
assertTrue(result.isSorted());
}

/**
* Test successfully parsing file without any newline.
*/
@Test
public void test_file_without_newline() throws Exception {
Path p = Paths.get(System.getProperty("user.dir"), "src", "test", "resources",
"FileWithoutNewline.java");
Result result =
new ImpSort(StandardCharsets.UTF_8, eclipseDefaults, false, true, LineEnding.AUTO)
.parseFile(p);
assertTrue(result.isSorted());
}

}
Empty file.
1 change: 1 addition & 0 deletions src/test/resources/FileWithoutNewline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import java.lang.System;public class FileWithoutNewline{public static void main(String[] args){System.out.println("Hello, world!");}}

0 comments on commit fc86a6a

Please sign in to comment.