KiltReformatter { public void reorderByKey(final FileMatcher fileMatcher, final AttachCommentsTo attachCommentsTo, final Charset charset) { Require.nonNull(fileMatcher); Require.nonNull(attachCommentsTo); Require.nonNull(charset); final Reformatter reformatter= new Reformatter( ReformatOptions.create() .with(charset) .with(attachCommentsTo)); final Set<File> propertyFiles= fileMatcher.findMatchingFiles(); LOGGER.log(Level.INFO, "Reordering entries in the following files: {}", propertyFiles); propertyFiles.stream().forEach(_f -> { final PropertyFile pf= PropertyFile.from(_f, charset); reformatter.reorderByKey(pf); pf.overwrite(_f, APRON_OPTIONS.with(charset)); }); }  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 testReorder_orderByName() throws IOException { final Path propertiesRootDirectory= this.tmpFolder.getRoot().toPath(); final File f1= createI18nBundle(propertiesRootDirectory, "" + "keyC = valueC\n" + "keyB = valueB\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyD = valueD\n" ); final File f2= createI18nBundle(propertiesRootDirectory, "" + "keyE = valueE\n" + "keyD = valueD\n" + "keyC = valueC\n" + "keyB = valueB\n" + "keyA = valueA\n" ); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); new KiltReformatter() .reorderByKey(fileMatcher, AttachCommentsTo.NEXT_PROPERTY, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "keyA = valueA\n" + "keyB = valueB\n" + "keyC = valueC\n" + "keyD = valueD\n" + "keyE = valueE\n" ); assertThat(contentOf(f2)).isEqualTo("" + "keyA = valueA\n" + "keyB = valueB\n" + "keyC = valueC\n" + "keyD = valueD\n" + "keyE = valueE\n" ); }
@Test public void testReorder_attachCommentToNext() throws IOException { final Path propertiesRootDirectory= this.tmpFolder.getRoot().toPath(); final File f1= createI18nBundle(propertiesRootDirectory, "" + "# Comment 1\n" + "key_F = F\n" + "key_L = L\n" + "\n" + "# Comment 2\n" + "key_B = B\n" + "# Comment 3\n" + "key_A = A\n" ); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); new KiltReformatter() .reorderByKey(fileMatcher, AttachCommentsTo.NEXT_PROPERTY, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "# Comment 3\n" + "key_A = A\n" + "\n" + "# Comment 2\n" + "key_B = B\n" + "# Comment 1\n" + "key_F = F\n" + "key_L = L\n" ); }
@Test public void testReorder_attachCommentToPrev() throws IOException { final Path propertiesRootDirectory= this.tmpFolder.getRoot().toPath(); final File f1= createI18nBundle(propertiesRootDirectory, "" + "# Comment 1\n" + "key_F = F\n" + "key_L = L\n" + "\n" + "# Comment 2\n" + "key_B = B\n" + "# Comment 3\n" + "key_A = A\n" ); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); new KiltReformatter() .reorderByKey(fileMatcher, AttachCommentsTo.PREV_PROPERTY, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "# Comment 1\n" + "key_A = A\n" + "key_B = B\n" + "# Comment 3\n" + "key_F = F\n" + "key_L = L\n" + "\n" + "# Comment 2\n" ); }
@Test public void testReorder_attachCommentToLine() throws IOException { final Path propertiesRootDirectory= this.tmpFolder.getRoot().toPath(); final File f1= createI18nBundle(propertiesRootDirectory, "" + "# Comment 1\n" + "key_F = F\n" + "key_L = L\n" + "\n" + "# Comment 2\n" + "key_B = B\n" + "# Comment 3\n" + "key_A = A\n" ); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); new KiltReformatter() .reorderByKey(fileMatcher, AttachCommentsTo.ORIG_LINE, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "# Comment 1\n" + "key_A = A\n" + "key_B = B\n" + "\n" + "# Comment 2\n" + "key_F = F\n" + "# Comment 3\n" + "key_L = L\n" ); }
@Test public void testReorder_multilineProperties() throws IOException { final Path propertiesRootDirectory= this.tmpFolder.getRoot().toPath(); final File f1= createI18nBundle(propertiesRootDirectory, "" + "key\\ \\\n" + " 2\\\n" + " = value \\\n" + " 2\n" + "\n" + " # some comment\n" + "key\\ \\\n" + " 1\\\n" + " = value \\\n" + " 1\n" + "key\\ \\\n" + " 3\\\n" + " = value \\\n" + " 3\n" ); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); new KiltReformatter() .reorderByKey(fileMatcher, AttachCommentsTo.ORIG_LINE, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "key\\ \\\n" + " 1\\\n" + " = value \\\n" + " 1\n" + "\n" + " # some comment\n" + "key\\ \\\n" + " 2\\\n" + " = value \\\n" + " 2\n" + "key\\ \\\n" + " 3\\\n" + " = value \\\n" + " 3\n" ); }
@Test public void testReorder_noFiles() throws IOException { final FileMatcher fileMatcher= new FileMatcher(Paths.get(""), new String[]{""}, new String[]{""}); new KiltReformatter() .reorderByKey(fileMatcher, AttachCommentsTo.NEXT_PROPERTY, UTF_8); }