KiltReformatter { public void reorderByTemplate(final File template, final FileMatcher fileMatcher, final AttachCommentsTo attachCommentsTo, final Charset charset) { Require.nonNull(fileMatcher); Require.nonNull(attachCommentsTo); Require.nonNull(charset); final Set<File> propertyFiles= fileMatcher.findMatchingFiles(); LOGGER.log(Level.INFO, "Reordering entries by template {} in the following files: {}", template, propertyFiles); final PropertyFile reference= PropertyFile.from(template, charset); final Set<RememberingPropertyFile> pfs= propertyFiles.stream() .filter(_f -> { if (_f.getAbsolutePath().equals(template.getAbsolutePath())) { LOGGER.log(Level.DEBUG, "Ignoring property file "+_f.getAbsolutePath()+" because it is the same as the reference template."); return false; } else { return true; } }) .map(_f -> { return RememberingPropertyFile.from(_f, charset); }) .collect(Collectors.toSet()); final Reformatter reformatter= new Reformatter( ReformatOptions.create() .with(charset) .with(attachCommentsTo)); for (final RememberingPropertyFile rpf : pfs) { reformatter.reorderByTemplate(reference, rpf.propertyFile); rpf.propertyFile.overwrite(rpf.actualFile, 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_orderByLanguage() 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() .reorderByTemplate(f1, fileMatcher, AttachCommentsTo.NEXT_PROPERTY, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "keyC = valueC\n" + "keyB = valueB\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyD = valueD\n" ); assertThat(contentOf(f2)).isEqualTo("" + "keyC = valueC\n" + "keyB = valueB\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyD = valueD\n" ); }
@Test @Ignore(value = "Reordering multiple files at one is currently not implemented. " + "The reason is that the current format for i18include doesn't really allow an order of the " + "input files.") public void testReorder_orderByLanguage_multipleFiles() 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" + "keyZ = valueZ\n" + "keyB = valueB\n" + "keyY = valueY\n" + "keyA = valueA\n" );final File f3= createI18nBundle(propertiesRootDirectory, "" + "keyY = valueY\n" + "keyE = valueE\n" + "keyC = valueC\n" + "keyA = valueA\n" + "keyZ = valueZ\n" ); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); new KiltReformatter() .reorderByTemplate(f1, fileMatcher, AttachCommentsTo.NEXT_PROPERTY, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "keyC = valueC\n" + "keyB = valueB\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyD = valueD\n" ); assertThat(contentOf(f2)).isEqualTo("" + "keyC = valueC\n" + "keyB = valueB\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyD = valueD\n" + "keyZ = valueZ\n" + "keyY = valueY\n" ); assertThat(contentOf(f3)).isEqualTo("" + "keyC = valueC\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyZ = valueZ\n" + "keyY = valueY\n" ); }
@Test public void testReorder_orderByLanguage_multipleFiles_Separately() 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" + "keyZ = valueZ\n" + "keyB = valueB\n" + "keyY = valueY\n" + "keyA = valueA\n" );final File f3= createI18nBundle(propertiesRootDirectory, "" + "keyY = valueY\n" + "keyE = valueE\n" + "keyC = valueC\n" + "keyA = valueA\n" + "keyZ = valueZ\n" ); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); new KiltReformatter() .reorderByTemplate(f1, fileMatcher, AttachCommentsTo.NEXT_PROPERTY, UTF_8); new KiltReformatter() .reorderByTemplate(f2, fileMatcher, AttachCommentsTo.NEXT_PROPERTY, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "keyC = valueC\n" + "keyB = valueB\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyD = valueD\n" ); assertThat(contentOf(f2)).isEqualTo("" + "keyC = valueC\n" + "keyB = valueB\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyD = valueD\n" + "keyZ = valueZ\n" + "keyY = valueY\n" ); assertThat(contentOf(f3)).isEqualTo("" + "keyC = valueC\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyZ = valueZ\n" + "keyY = valueY\n" ); }
@Test public void testReorder_orderByLanguage_SameFileTwice() 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() .reorderByTemplate(f1, fileMatcher, AttachCommentsTo.NEXT_PROPERTY, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "keyC = valueC\n" + "keyB = valueB\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyD = valueD\n" ); assertThat(contentOf(f2)).isEqualTo("" + "keyC = valueC\n" + "keyB = valueB\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyD = valueD\n" ); }
@Test public void testReorder_orderByLanguage_NothingInCommon() 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, "" + "keyZ = valueZ\n" + "keyY = valueY\n" + "keyX = valueX\n" ); final FileMatcher fileMatcher= new FileMatcher(propertiesRootDirectory, new String[]{"**/*.properties"}, new String[]{""}); new KiltReformatter() .reorderByTemplate(f1, fileMatcher, AttachCommentsTo.NEXT_PROPERTY, UTF_8); assertThat(contentOf(f1)).isEqualTo("" + "keyC = valueC\n" + "keyB = valueB\n" + "keyA = valueA\n" + "keyE = valueE\n" + "keyD = valueD\n" ); assertThat(contentOf(f2)).isEqualTo("" + "keyZ = valueZ\n" + "keyY = valueY\n" + "keyX = valueX\n" ); }