From a354eda9d0a35ea99c6d450bdef923489a23f0f4 Mon Sep 17 00:00:00 2001 From: Hiroshi Ito Date: Mon, 9 May 2016 17:02:37 +0900 Subject: [PATCH] Make default file encoding as "UTF-8" and add a command line option to override it. --- .../eclipse/collections/tools/Converter.java | 22 ++++++++++++++++--- .../collections/tools/ConverterTest.java | 16 +++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/eclipse/collections/tools/Converter.java b/src/main/java/org/eclipse/collections/tools/Converter.java index 4288fab..05a0948 100644 --- a/src/main/java/org/eclipse/collections/tools/Converter.java +++ b/src/main/java/org/eclipse/collections/tools/Converter.java @@ -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; @@ -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 "); + System.out.println("Usage: gsc-ec-converter [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!"); @@ -64,7 +80,7 @@ private static void convert(String path) try { boolean[] fileChanged = {false}; - List replacedLines = Files.lines(file, Charset.defaultCharset()).map(line -> { + List replacedLines = Files.lines(file, endoding).map(line -> { String newLine = Converter.replaceMatching(line); fileChanged[0] = fileChanged[0] || !newLine.equals(line); return newLine; diff --git a/src/test/java/org/eclipse/collections/tools/ConverterTest.java b/src/test/java/org/eclipse/collections/tools/ConverterTest.java index d0cabf5..b8d7e6b 100644 --- a/src/test/java/org/eclipse/collections/tools/ConverterTest.java +++ b/src/test/java/org/eclipse/collections/tools/ConverterTest.java @@ -78,7 +78,7 @@ public void main() throws Exception } @Test - public void invalidArg() + public void invalidArg1() { try { @@ -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() {