forked from revelc/impsort-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revelcGH-30: Handle files that are empty, have an unknown newline, or…
… can't be parsed
- Loading branch information
Showing
11 changed files
with
213 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/net/revelc/code/impsort/EmptyFileException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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 java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Signals that the file denoted by this path is empty. | ||
* | ||
* <p> | ||
* This exception will be thrown by the {@link ImpSort#parseFile} when it encounters an empty file. | ||
* </p> | ||
*/ | ||
public class EmptyFileException extends IOException { | ||
private static final long serialVersionUID = -4202864513494828247L; | ||
|
||
private final Path path; | ||
|
||
/** | ||
* Constructs a {@code EmptyFileException} with {@code null} as its error detail message and the | ||
* specified path. | ||
* | ||
* @param path the path | ||
*/ | ||
public EmptyFileException(final Path path) { | ||
this.path = path; | ||
} | ||
|
||
/** | ||
* Returns the path. | ||
* | ||
* @return the path | ||
*/ | ||
public Path getPath() { | ||
return path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/net/revelc/code/impsort/UnknownLineEndingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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 java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Signals that the file denoted by this path has an unknown line ending. | ||
* | ||
* <p> | ||
* This exception will be thrown by the {@link ImpSort#parseFile} when it encounters a file with an | ||
* unknown line ending. | ||
* </p> | ||
*/ | ||
public class UnknownLineEndingException extends IOException { | ||
private static final long serialVersionUID = 4417291768648259852L; | ||
|
||
private final Path path; | ||
|
||
/** | ||
* Constructs a {@code UnknownLineEndingException} with {@code null} as its error detail message | ||
* and the specified path. | ||
* | ||
* @param path the path | ||
*/ | ||
public UnknownLineEndingException(final Path path) { | ||
this.path = path; | ||
} | ||
|
||
/** | ||
* Returns the path. | ||
* | ||
* @return the path | ||
*/ | ||
public Path getPath() { | ||
return path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 java.io.IOException; | ||
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(expected = EmptyFileException.class) | ||
public void test_empty_file() throws IOException, EmptyFileException, UnknownLineEndingException { | ||
Path p = | ||
Paths.get(System.getProperty("user.dir"), "src", "test", "resources", "EmptyFile.java"); | ||
new ImpSort(StandardCharsets.UTF_8, eclipseDefaults, true, true, LineEnding.AUTO).parseFile(p); | ||
} | ||
|
||
/** | ||
* Test successfully parsing file without any newline. | ||
*/ | ||
@Test(expected = UnknownLineEndingException.class) | ||
public void test_file_without_newline() | ||
throws IOException, EmptyFileException, UnknownLineEndingException { | ||
Path p = Paths.get(System.getProperty("user.dir"), "src", "test", "resources", | ||
"FileWithoutNewline.java"); | ||
new ImpSort(StandardCharsets.UTF_8, eclipseDefaults, true, true, LineEnding.AUTO).parseFile(p); | ||
} | ||
|
||
/** | ||
* Test successfully parsing file without any newline. | ||
*/ | ||
@Test(expected = IOException.class) | ||
public void test_invalid_file() | ||
throws IOException, EmptyFileException, UnknownLineEndingException { | ||
Path p = | ||
Paths.get(System.getProperty("user.dir"), "src", "test", "resources", "InvalidFile.java"); | ||
new ImpSort(StandardCharsets.UTF_8, eclipseDefaults, true, true, LineEnding.AUTO).parseFile(p); | ||
} | ||
|
||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!");}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class InvalidFile { | ||
public static void main(String[] args) { | ||
System.out.println("Hello, world!") | ||
} | ||
} |