IgnoredSectionsStore { public String revertIgnoredSections(String sortedXml) { Matcher matcher = InstructionType.TOKEN_PATTERN.matcher(sortedXml); StringBuffer returnValue = new StringBuffer(); while (matcher.find()) { int index = Integer.parseInt(matcher.group(1)); String replacement = ignoredSections.get(index); String oneBackslashBeforeBackslash = replacement.replace("\\", "\\\\"); String oneBackslashBeforeDollar = oneBackslashBeforeBackslash.replace("$", "\\$"); matcher.appendReplacement(returnValue, oneBackslashBeforeDollar); } matcher.appendTail(returnValue); return returnValue.toString(); }  String replaceIgnoredSections(String originalXml); String revertIgnoredSections(String sortedXml);  }
@Test void revertTokensInOrderShouldWork() { String xml = "abc<?sortpom token='0'?>cbaabc<?sortpom token='1'?>cba"; ignoredSections.add("<?sortpom ignore?>def1<?sortpom resume?>"); ignoredSections.add("<?SORTPOM Ignore?>def2<?sortPom reSUME?>"); String replaced = ignoredSectionsStore.revertIgnoredSections(xml); assertThat(replaced, is("abc<?sortpom ignore?>def1<?sortpom resume?>cbaabc<?SORTPOM Ignore?>def2<?sortPom reSUME?>cba")); }
@Test void revertTokensInRearrangedOrderShouldPlaceTextInRightOrder() { String xml = "abc<?sortpom token='1'?>cbaabc<?sortpom token='0'?>cba"; ignoredSections.add("<?sortpom ignore?>def0<?sortpom resume?>"); ignoredSections.add("<?SORTPOM Ignore?>def1<?sortPom reSUME?>"); String replaced = ignoredSectionsStore.revertIgnoredSections(xml); assertThat(replaced, is("abc<?SORTPOM Ignore?>def1<?sortPom reSUME?>cbaabc<?sortpom ignore?>def0<?sortpom resume?>cba")); }
@Test void revertTokensInMultipleLinesShouldPlaceTextInRightOrder() { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http: "\n" + " <modelVersion>4.0.0</modelVersion>\n" + "\n" + " <!-- Basics -->\n" + " <groupId>something</groupId>\n" + " <artifactId>maven-sortpom-sorter</artifactId>\n" + " <version>1.0</version>\n" + " <packaging>jar</packaging>\n" + "\n" + " <dependencies>\n" + " <dependency>\n" + " <groupId>commons-io</groupId>\n" + " <artifactId>commons-io</artifactId>\n" + " <?sortpom token='1'?>\n" + " </dependency>\n" + " <dependency>\n" + " <groupId>org.jdom</groupId>\n" + " <artifactId>jdom</artifactId>\n" + " <version>1.1.3</version>\n" + " </dependency>\n" + "\n" + " <!-- Test dependencies -->\n" + " <dependency>\n" + " <groupId>com.google.code.reflection-utils</groupId>\n" + " <artifactId>reflection-utils</artifactId>\n" + " <version>0.0.1</version>\n" + " <scope>test</scope>\n" + " </dependency>\n" + " <dependency>\n" + " <groupId>junit</groupId>\n" + " <artifactId>junit</artifactId>\n" + " <?sortpom token='0'?>\n" + " <scope>test</scope>\n" + " </dependency>\n" + " </dependencies>\n" + " <name>SortPom Sorter</name>\n" + " <description>The sorting functionality</description>\n" + "\n" + "</project>\n"; ignoredSections.add("<?sortpom ignore?>\n" + " <version>4.11</version><!--$NO-MVN-MAN-VER$ -->\n" + " <?sortpom resume?>"); ignoredSections.add("<?sortpom ignore?>\n" + " <version>2.1</version><!--$NO-MVN-MAN-VER$ -->\n" + " <?sortpom resume?>"); String replaced = ignoredSectionsStore.revertIgnoredSections(xml); assertThat(replaced, is("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http: "\n" + " <modelVersion>4.0.0</modelVersion>\n" + "\n" + " <!-- Basics -->\n" + " <groupId>something</groupId>\n" + " <artifactId>maven-sortpom-sorter</artifactId>\n" + " <version>1.0</version>\n" + " <packaging>jar</packaging>\n" + "\n" + " <dependencies>\n" + " <dependency>\n" + " <groupId>commons-io</groupId>\n" + " <artifactId>commons-io</artifactId>\n" + " <?sortpom ignore?>\n" + " <version>2.1</version><!--$NO-MVN-MAN-VER$ -->\n" + " <?sortpom resume?>\n" + " </dependency>\n" + " <dependency>\n" + " <groupId>org.jdom</groupId>\n" + " <artifactId>jdom</artifactId>\n" + " <version>1.1.3</version>\n" + " </dependency>\n" + "\n" + " <!-- Test dependencies -->\n" + " <dependency>\n" + " <groupId>com.google.code.reflection-utils</groupId>\n" + " <artifactId>reflection-utils</artifactId>\n" + " <version>0.0.1</version>\n" + " <scope>test</scope>\n" + " </dependency>\n" + " <dependency>\n" + " <groupId>junit</groupId>\n" + " <artifactId>junit</artifactId>\n" + " <?sortpom ignore?>\n" + " <version>4.11</version><!--$NO-MVN-MAN-VER$ -->\n" + " <?sortpom resume?>\n" + " <scope>test</scope>\n" + " </dependency>\n" + " </dependencies>\n" + " <name>SortPom Sorter</name>\n" + " <description>The sorting functionality</description>\n" + "\n" + "</project>\n")); }