AndroidStringDocumentMapper { public List<TextUnitDTO> mapToTextUnits(AndroidStringDocument document) { return mapToTextUnitStream(document).collect(Collectors.toList()); } AndroidStringDocumentMapper(String pluralSeparator,
                                       String assetDelimiter,
                                       String locale,
                                       String repositoryName);  AndroidStringDocumentMapper(String pluralSeparator, String assetDelimiter); AndroidStringDocument readFromTextUnits(List<TextUnitDTO> textUnits, boolean useSource); AndroidStringDocument readFromSourceTextUnits(List<TextUnitDTO> textUnits); AndroidStringDocument readFromTargetTextUnits(List<TextUnitDTO> textUnits); List<TextUnitDTO> mapToTextUnits(AndroidStringDocument document); Stream<TextUnitDTO> mapToTextUnitStream(AndroidStringDocument document);  }
@Test public void testMapToTextUnitsFromEmptyDocument() { mapper = new AndroidStringDocumentMapper("", assetDelimiter); document = new AndroidStringDocument(); textUnits = mapper.mapToTextUnits(document); assertThat(textUnits).isEmpty(); }
@Test public void testMapToTextUnitsFromSingularsDocument() { mapper = new AndroidStringDocumentMapper("", assetDelimiter); document = new AndroidStringDocument(); document.addSingular(new AndroidSingular(10L, "string/path1#@#name1", "content1", "comment1")); document.addSingular(new AndroidSingular(11L, "string/path2#@#name2", "content2", "comment2")); textUnits = mapper.mapToTextUnits(document); assertThat(textUnits).isNotEmpty(); assertThat(textUnits).hasSize(2); assertThat(textUnits).extracting("tmTextUnitId", "name", "assetPath", "target") .contains(Tuple.tuple(10L, "name1", "string/path1", "content1")) .contains(Tuple.tuple(11L, "name2", "string/path2", "content2")); }
@Test public void testMapToTextUnitsFromPluralsDocument() { String locale = "pt-BR"; String repo = "brazil"; mapper = new AndroidStringDocumentMapper("_", assetDelimiter, locale, repo); document = new AndroidStringDocument(); AndroidPlural.AndroidPluralBuilder builder1 = AndroidPlural.builder(); builder1.setComment("comment1"); builder1.setName("string/path1#@#name1"); builder1.addItem(new AndroidPluralItem(10L, ONE, "one1_content")); builder1.addItem(new AndroidPluralItem(11L, MANY, "many1_content")); document.addPlural(builder1.build()); AndroidPlural.AndroidPluralBuilder builder2 = AndroidPlural.builder(); builder2.setComment("comment2"); builder2.setName("string/path2#@#name2"); builder2.addItem(new AndroidPluralItem(21L, ONE, "one2_content")); builder2.addItem(new AndroidPluralItem(22L, TWO, "two2_content")); builder2.addItem(new AndroidPluralItem(23L, MANY, "many2_content")); document.addPlural(builder2.build()); AndroidPlural.AndroidPluralBuilder builder3 = AndroidPlural.builder(); builder3.setComment("comment3"); builder3.setName("string/path3#@#name3"); builder3.addItem(new AndroidPluralItem(30L, ZERO, "zero3_content")); builder3.addItem(new AndroidPluralItem(31L, ONE, "one3_content")); builder3.addItem(new AndroidPluralItem(32L, TWO, "two3_content")); builder3.addItem(new AndroidPluralItem(33L, FEW, "few3_content")); builder3.addItem(new AndroidPluralItem(34L, MANY, "many3_content")); builder3.addItem(new AndroidPluralItem(35L, OTHER, "other3_content")); document.addPlural(builder3.build()); textUnits = mapper.mapToTextUnits(document); assertThat(textUnits).hasSize(11); assertThat(textUnits) .extracting("tmTextUnitId", "name", "pluralForm", "assetPath", "target", "targetLocale", "repositoryName") .contains(tuple(10L, "name1_one", "one", "string/path1", "one1_content", locale, repo)) .contains(tuple(11L, "name1_many", "many", "string/path1", "many1_content", locale, repo)) .contains(tuple(21L, "name2_one", "one", "string/path2", "one2_content", locale, repo)) .contains(tuple(22L, "name2_two", "two", "string/path2", "two2_content", locale, repo)) .contains(tuple(23L, "name2_many", "many", "string/path2", "many2_content", locale, repo)) .contains(tuple(30L, "name3_zero", "zero", "string/path3", "zero3_content", locale, repo)) .contains(tuple(31L, "name3_one", "one", "string/path3", "one3_content", locale, repo)) .contains(tuple(32L, "name3_two", "two", "string/path3", "two3_content", locale, repo)) .contains(tuple(33L, "name3_few", "few", "string/path3", "few3_content", locale, repo)) .contains(tuple(34L, "name3_many", "many", "string/path3", "many3_content", locale, repo)) .contains(tuple(35L, "name3_other", "other", "string/path3", "other3_content", locale, repo)); }
@Test public void testReadingTextUnitsFromFile() throws Exception { String path = Resources.getResource("com/box/l10n/mojito/android/strings/test_resources_file.xml").getPath(); mapper = new AndroidStringDocumentMapper("_", assetDelimiter); textUnits = mapper.mapToTextUnits(AndroidStringDocumentReader.fromFile(path)); assertThat(textUnits).isNotEmpty(); assertThat(textUnits).extracting("assetPath").containsOnly("Mojito/src/main/res/values/strings.xml"); assertThat(textUnits).filteredOn(tu -> tu.getName().equalsIgnoreCase("some_string")) .extracting("target", "comment") .containsOnly(tuple("Dela...", "Some string")); assertThat(textUnits).filteredOn(tu -> tu.getName().equalsIgnoreCase("show_options")) .extracting("target", "comment") .containsOnly(tuple("Mer \" dela", "Options")); assertThat(textUnits).filteredOn(tu -> tu.getName().equalsIgnoreCase("without_comment")) .extracting("target", "comment") .containsOnly(tuple("ei ' kommentteja", null)); assertThat(textUnits).filteredOn(tu -> tu.getName().equalsIgnoreCase("line_break")) .extracting("target", "comment") .containsOnly(tuple("salto de \nlinea", null)); assertThat(textUnits).filteredOn(tu -> tu.getName().startsWith("test_plural_hindi")) .extracting("name", "pluralForm", "target") .contains(tuple("test_plural_hindi_one", "one", "%1$d @ बहुवचन")) .contains(tuple("test_plural_hindi_other", "other", "%1$d @ विलक्षण")); assertThat(textUnits).filteredOn(tu -> tu.getName().startsWith("plural_russian")) .extracting("name", "pluralForm", "target") .contains(tuple("plural_russian_one", "one", "одно слово")) .contains(tuple("plural_russian_many", "many", "больше слов")); }