KiltReformatter { public void reformat(final FileMatcher fileMatcher, final String formatString, final boolean reformatKeyAndValue, final Charset charset) { Require.nonNull(fileMatcher); Require.nonNull(formatString); Require.nonNull(charset); final Set<File> propertyFiles= fileMatcher.findMatchingFiles(); LOGGER.log(Level.INFO, "Reformatting entries in the following files: {}", propertyFiles); final Reformatter reformatter= new Reformatter( ReformatOptions.create() .with(charset) .withFormat(formatString) .withReformatKeyAndValue(reformatKeyAndValue)); for (final File propertyFile : propertyFiles) { reformatter.reformat(propertyFile); } }  void reformat(final FileMatcher fileMatcher, final String formatString, final boolean reformatKeyAndValue, final Charset charset); void reorderByKey(final FileMatcher fileMatcher, final AttachCommentsTo attachCommentsTo, final Charset charset); void reorderByTemplate(final File template, final FileMatcher fileMatcher, final AttachCommentsTo attachCommentsTo, final Charset charset);  }
@Test public void testReformat_differentFormat() throws IOException { final Path propertiesRootDirectory= this.tmpFolder.getRoot().toPath(); final File f1= createI18nBundle(propertiesRootDirectory, "" + "key1 = value1\n" + "key2 : value2\r" + "key3 value3\r\n"); final File f2= createI18nBundle(propertiesRootDirectory, "" + " key1 = value1\n" + " key2 : value2\r" + "\tkey3 value3\r\n"); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); new KiltReformatter() .reformat(fileMatcher, "<key>\\t=\\t<value>\\n", false, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "key1\t=\tvalue1\n" + "key2\t=\tvalue2\n" + "key3\t=\tvalue3\n"); assertThat(contentOf(f2)).isEqualTo("" + "key1\t=\tvalue1\n" + "key2\t=\tvalue2\n" + "key3\t=\tvalue3\n"); }
@Test public void testReformat_invalidFormat() throws IOException { final Path propertiesRootDirectory= this.tmpFolder.getRoot().toPath(); final File f1= createI18nBundle(propertiesRootDirectory, "" + "key1 = value1\n" + "key2 : value2\r" + "key3 value3\r\n"); final File f2= createI18nBundle(propertiesRootDirectory, "" + " key1 = value1\n" + " key2 : value2\r" + "\tkey3 value3\r\n"); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); assertThatExceptionOfType(InvalidFormatException.class) .isThrownBy(() -> { new KiltReformatter().reformat(fileMatcher, "<key> = <valueismissing>\\n", false, UTF_8);}); }
@Test public void testReformat_multilineProperties() throws IOException { final Path propertiesRootDirectory= this.tmpFolder.getRoot().toPath(); final File f1= createI18nBundle(propertiesRootDirectory, "" + "\tkey\\ \\\n" + " one\\\n" + " : value \\\n" + " 1\n" + "key\\ \\\n" + " two\\\r" + " = \t value \\\r" + " 2\n" ); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); new KiltReformatter() .reformat(fileMatcher, "<key> = <value>\\n", false, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "key\\ \\\n" + " one\\\n" + " = value \\\n" + " 1\n" + "key\\ \\\n" + " two\\\r" + " = value \\\r" + " 2\n" ); }
@Test public void testReformat_multilineProperties_ToSingleLine() throws IOException { final Path propertiesRootDirectory= this.tmpFolder.getRoot().toPath(); final File f1= createI18nBundle(propertiesRootDirectory, "" + "\tkey\\ \\\n" + " one\\\n" + " : value \\\n" + " 1\n" + "key\\ \\\n" + " two\\\r" + " = \t value \\\r" + " 2\n" ); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); new KiltReformatter() .reformat(fileMatcher, "<key> = <value>\\n", true, UTF_8); System.out.println("SOLL:\n" + "key one = value 1\n" + "key two = value 2\n" ); System.out.println("IST:\n" + contentOf(f1)); assertThat(contentOf(f1)).isEqualTo("" + "key\\ one = value 1\n" + "key\\ two = value 2\n" ); }