Skip to content

Commit

Permalink
Make default file encoding as "UTF-8" and add a command line option t…
Browse files Browse the repository at this point in the history
…o override it.
  • Loading branch information
itohro committed May 9, 2016
1 parent bd7a022 commit a354eda
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/main/java/org/eclipse/collections/tools/Converter.java
Expand Up @@ -12,6 +12,8 @@

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
Expand All @@ -31,17 +33,31 @@ public class Converter
"com\\.gs\\.collections", "org\\.eclipse\\.collections",
"com\\.goldmansachs", "org\\.eclipse\\.collections",
"gs-collections", "eclipse-collections");
public static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
private static Charset endoding = DEFAULT_ENCODING;

public static void main(String... args)
{
if (args.length != 1)
if (args.length < 1)
{
System.out.println("Usage: gsc-ec-converter <PATH_TO_PROJECT>");
System.out.println("Usage: gsc-ec-converter <PATH_TO_PROJECT> [File encoding]");
System.exit(1);
}
String path = args[0];
if (Files.exists(Paths.get(path)))
{
if (args.length == 2)
{
try
{
endoding = Charset.forName(args[1]);
}
catch (IllegalCharsetNameException | UnsupportedCharsetException e)
{
System.out.println("Invalid file encoding is passed: " + args[1]);
System.exit(1);
}
}
System.out.println("Running Eclipse Collections Converter against " + path + ".");
Converter.convert(path);
System.out.println("Done!");
Expand All @@ -64,7 +80,7 @@ private static void convert(String path)
try
{
boolean[] fileChanged = {false};
List<String> replacedLines = Files.lines(file, Charset.defaultCharset()).map(line -> {
List<String> replacedLines = Files.lines(file, endoding).map(line -> {
String newLine = Converter.replaceMatching(line);
fileChanged[0] = fileChanged[0] || !newLine.equals(line);
return newLine;
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/org/eclipse/collections/tools/ConverterTest.java
Expand Up @@ -78,7 +78,7 @@ public void main() throws Exception
}

@Test
public void invalidArg()
public void invalidArg1()
{
try
{
Expand All @@ -91,6 +91,20 @@ public void invalidArg()
}
}

@Test
public void invalidArg2()
{
try
{
Converter.main(this.testDir.getAbsolutePath(), "nonExistingEncoding");
Assert.fail("Converter should fail for non existing encoding");
}
catch (RuntimeException e)
{
Assert.assertEquals(1, ExitMock.getInstance().getStatus());
}
}

@Test
public void invalidPath()
{
Expand Down

0 comments on commit a354eda

Please sign in to comment.