From 9a21312ee5ace47f5c814321d02e62f63b97928d Mon Sep 17 00:00:00 2001 From: Malte Brunnlieb Date: Mon, 14 Sep 2020 18:34:36 +0200 Subject: [PATCH 1/9] #1238 Set next development version --- cobigen/cobigen-textmerger/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cobigen/cobigen-textmerger/pom.xml b/cobigen/cobigen-textmerger/pom.xml index ba7c95ba8f..ccfac3a293 100644 --- a/cobigen/cobigen-textmerger/pom.xml +++ b/cobigen/cobigen-textmerger/pom.xml @@ -2,7 +2,7 @@ 4.0.0 textmerger jar - 7.0.0 + 7.0.1-SNAPSHOT CobiGen - Text Merger Plug-in CobiGen - Text Merger Plug-in From f08888467386e6afd77b64153eaaa1b102fe35b2 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Fri, 18 Sep 2020 14:43:04 +0200 Subject: [PATCH 2/9] bugfix #1242 replaced system line delimiters with line delimiters detected in base file --- .../cobigen/textmerger/TextAppender.java | 91 +++++++++++++------ .../textmerger/anchorextension/MergeUtil.java | 25 ++--- .../cobigen/textmerger/MergeUtilTest.java | 29 +++--- 3 files changed, 91 insertions(+), 54 deletions(-) diff --git a/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/TextAppender.java b/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/TextAppender.java index 3c36b4aa68..737688f85d 100644 --- a/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/TextAppender.java +++ b/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/TextAppender.java @@ -1,7 +1,10 @@ package com.devonfw.cobigen.textmerger; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; @@ -11,6 +14,7 @@ import com.devonfw.cobigen.api.exception.MergeException; import com.devonfw.cobigen.api.extension.Merger; +import com.devonfw.cobigen.api.util.SystemUtil; import com.devonfw.cobigen.textmerger.anchorextension.Anchor; import com.devonfw.cobigen.textmerger.anchorextension.MergeStrategy; import com.devonfw.cobigen.textmerger.anchorextension.MergeUtil; @@ -26,6 +30,9 @@ public class TextAppender implements Merger { */ private String type; + /** + * Default merge strategy + */ private MergeStrategy defaultStrat; /** @@ -68,13 +75,24 @@ public String getType() { @Override public String merge(File base, String patch, String targetCharset) throws MergeException { String mergedString; + String lineDelimiter; try { mergedString = FileUtils.readFileToString(base, targetCharset); + try (FileInputStream stream = new FileInputStream(base); + BufferedInputStream bis = new BufferedInputStream(stream); + InputStreamReader reader = new InputStreamReader(bis, targetCharset)) { + lineDelimiter = SystemUtil.determineLineDelimiter(bis, reader); + } catch (IOException e) { + throw new MergeException(base, "Could not read base file.", e); + } } catch (IOException e) { throw new MergeException(base, "Could not read base file.", e); } try { - mergedString = merge(mergedString, patch); + if (lineDelimiter.isEmpty()) { + lineDelimiter = System.lineSeparator(); + } + mergedString = merge(mergedString, patch, lineDelimiter); } catch (Exception e) { throw new MergeException(base, e.getMessage(), e); } @@ -87,26 +105,28 @@ public String merge(File base, String patch, String targetCharset) throws MergeE * target {@link String} to be merged into * @param patch * {@link String} patch, which should be applied to the base file + * @param lineDelimiter + * String line delimiter to use * @return Merged text (not null) * @throws Exception * When there is some problem about anchors */ - public String merge(String base, String patch) throws Exception { + public String merge(String base, String patch, String lineDelimiter) throws Exception { String mergedString = ""; if (MergeUtil.hasAnchors(patch)) { - LinkedHashMap splitBase = MergeUtil.splitByAnchors(base, defaultStrat); - LinkedHashMap splitPatch = MergeUtil.splitByAnchors(patch, defaultStrat); + LinkedHashMap splitBase = MergeUtil.splitByAnchors(base, defaultStrat, lineDelimiter); + LinkedHashMap splitPatch = MergeUtil.splitByAnchors(patch, defaultStrat, lineDelimiter); String footer = "footer"; String header = "header"; String toAppend = ""; if (MergeUtil.hasKeyMatchingDocumentPart(header, splitBase)) { - toAppend = MergeUtil.appendText(toAppend, header, splitBase, true, true); + toAppend = MergeUtil.appendText(toAppend, header, splitBase, true, true, lineDelimiter); mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, MergeUtil.getKeyMatchingDocumentPart(header, splitBase)); } else if (MergeUtil.hasKeyMatchingDocumentPart(header, splitPatch)) { - toAppend = MergeUtil.appendText(toAppend, header, splitPatch, true, true); + toAppend = MergeUtil.appendText(toAppend, header, splitPatch, true, true, lineDelimiter); mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, MergeUtil.getKeyMatchingDocumentPart(header, splitPatch)); } @@ -127,16 +147,20 @@ public String merge(String base, String patch) throws Exception { if (tmpAnchor.getNewlineName().matches("(newline_).+")) { switch (tmpAnchor.getNewlineName().toLowerCase()) { case "newline_appendbefore": - toAppend += MergeUtil.appendText(toAppend, docPart, splitBase, false, false); - toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true); + toAppend += MergeUtil.appendText(toAppend, docPart, splitBase, false, false, + lineDelimiter); + toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true, + lineDelimiter); mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, tmpAnchor); break; case "newline_appendafter": case "newline_append": - toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false); - toAppend += System.lineSeparator(); - toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true); + toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false, + lineDelimiter); + toAppend += lineDelimiter; + toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true, + lineDelimiter); mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, tmpAnchor); break; @@ -147,18 +171,22 @@ public String merge(String base, String patch) throws Exception { } else if (tmpAnchor.getNewlineName().matches(".*(newline)")) { switch (tmpAnchor.getNewlineName().toLowerCase()) { case "appendbefore_newline": - toAppend += System.lineSeparator(); - toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false); - toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true); + toAppend += lineDelimiter; + toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false, + lineDelimiter); + toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true, + lineDelimiter); mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, tmpAnchor); break; case "newline": case "appendafter_newline": case "append_newline": - toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false); - toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true); - toAppend += System.lineSeparator(); + toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false, + lineDelimiter); + toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true, + lineDelimiter); + toAppend += lineDelimiter; mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, tmpAnchor); break; @@ -169,25 +197,31 @@ public String merge(String base, String patch) throws Exception { } else { switch (mergeStrat) { case APPENDBEFORE: - toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false); - toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true); + toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false, + lineDelimiter); + toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true, + lineDelimiter); mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, tmpAnchor); break; case APPENDAFTER: case APPEND: - toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false); - toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true); + toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false, + lineDelimiter); + toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true, + lineDelimiter); mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, tmpAnchor); break; case OVERRIDE: - toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true); + toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true, + lineDelimiter); mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, tmpAnchor); break; case NOMERGE: - toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, true); + toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, true, + lineDelimiter); mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, tmpAnchor); break; @@ -203,22 +237,23 @@ public String merge(String base, String patch) throws Exception { } } else { - toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, true); + toAppend = + MergeUtil.appendText(toAppend, docPart, splitBase, false, true, lineDelimiter); mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, tmpAnchor); } } else if (MergeUtil.hasKeyMatchingDocumentPart(docPart, splitPatch)) { - toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true); + toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true, lineDelimiter); mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase, tmpAnchor); } } } else { if (MergeUtil.hasKeyMatchingDocumentPart(footer, splitBase)) { - toAppend = MergeUtil.appendText(toAppend, footer, splitBase, false, true); + toAppend = MergeUtil.appendText(toAppend, footer, splitBase, false, true, lineDelimiter); mergedString += toAppend; } else if (MergeUtil.hasKeyMatchingDocumentPart(footer, splitPatch)) { - toAppend = MergeUtil.appendText(toAppend, footer, splitPatch, false, true); + toAppend = MergeUtil.appendText(toAppend, footer, splitPatch, false, true, lineDelimiter); mergedString += toAppend; } break; @@ -231,7 +266,7 @@ public String merge(String base, String patch) throws Exception { mergedString = patch; return mergedString; } else if (withNewLineBeforehand) { - mergedString += System.lineSeparator(); + mergedString += lineDelimiter; } mergedString += patch; } diff --git a/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/anchorextension/MergeUtil.java b/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/anchorextension/MergeUtil.java index 025386dd4c..72867f9f14 100644 --- a/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/anchorextension/MergeUtil.java +++ b/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/anchorextension/MergeUtil.java @@ -57,12 +57,14 @@ public class MergeUtil { * The string that is to be split by anchors * @param defaultMergeStrategy * the default merge strategy to fall back + * @param lineDelimiter + * String line delimiter to use * @return a LinkedHashMap which contains anchors as keys and the following text as values * @throws Exception * when an anchor contains a wrong definition */ - public static LinkedHashMap splitByAnchors(String toSplit, MergeStrategy defaultMergeStrategy) - throws Exception { + public static LinkedHashMap splitByAnchors(String toSplit, MergeStrategy defaultMergeStrategy, + String lineDelimiter) throws Exception { LinkedHashMap result = new LinkedHashMap<>(); toSplit.trim(); String firstLine = @@ -75,7 +77,7 @@ public static LinkedHashMap splitByAnchors(String toSplit, Merge + "https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#error-list " + "for more details"); } - toSplit = toSplit + System.lineSeparator() + "anchor:::anchorend" + System.lineSeparator(); + toSplit = toSplit + lineDelimiter + "anchor:::anchorend" + lineDelimiter; Anchor anchor; int anchorCount = 0; Pattern regex = Pattern.compile(anchorRegex); @@ -168,26 +170,27 @@ public static Anchor getKeyMatchingDocumentPart(String docPart, Map m, boolean before, - boolean withKey) { + public static String appendText(String text, String docPart, Map m, boolean before, boolean withKey, + String lineDelimiter) { Anchor key = MergeUtil.getKeyMatchingDocumentPart(docPart, m); if (key.getNewlineName().equalsIgnoreCase("newline_appendbefore") && withKey) { - return System.lineSeparator() + key.getAnchor() + System.lineSeparator() + System.lineSeparator() - + m.get(key) + text; + return lineDelimiter + key.getAnchor() + lineDelimiter + lineDelimiter + m.get(key) + text; } else if (before) { if (withKey) { - return System.lineSeparator() + key.getAnchor() + System.lineSeparator() + m.get(key) + text; + return lineDelimiter + key.getAnchor() + lineDelimiter + m.get(key) + text; } else { - return System.lineSeparator() + m.get(key) + text; + return lineDelimiter + m.get(key) + text; } } else { if (withKey) { - return System.lineSeparator() + key.getAnchor() + text + System.lineSeparator() + m.get(key); + return lineDelimiter + key.getAnchor() + text + lineDelimiter + m.get(key); } else { - return text + System.lineSeparator() + m.get(key); + return text + lineDelimiter + m.get(key); } } } diff --git a/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/MergeUtilTest.java b/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/MergeUtilTest.java index d049b2a197..8e64cc933b 100644 --- a/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/MergeUtilTest.java +++ b/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/MergeUtilTest.java @@ -61,7 +61,7 @@ public void testProperMappingOfAnchorsAndText() { + "// anchor:test2213123:append:anchorend" + System.lineSeparator() + " Lorem ipsum dolor sit amet"; Map result = new LinkedHashMap<>(); try { - result = MergeUtil.splitByAnchors(testString, testStrat); + result = MergeUtil.splitByAnchors(testString, testStrat, System.lineSeparator()); } catch (Exception e) { fail("Expected no Exception, got the following Exception instead: " + e.getMessage()); } @@ -80,7 +80,7 @@ public void testProperMappingOfAnchorsAndTextWhenAnchorDoesntHaveEnoughPartsAndN + System.lineSeparator() + "// anchor:anotherone:hgfds:anchorend" + System.lineSeparator() + "test3" + System.lineSeparator() + "// anchor:footer:append:anchorend" + System.lineSeparator() + "test4"; try { - MergeUtil.splitByAnchors(testString, testStrat); + MergeUtil.splitByAnchors(testString, testStrat, System.lineSeparator()); failBecauseExceptionWasNotThrown(Exception.class); } catch (Exception e) { @@ -111,7 +111,7 @@ public void testProperMappingOfAnchorsAndTextAddsDefaultMergeStrategy() { Map test = new LinkedHashMap<>(); try { - test = MergeUtil.splitByAnchors(testString, testStrat); + test = MergeUtil.splitByAnchors(testString, testStrat, System.lineSeparator()); assertThat(test).isEqualTo(expected); } catch (Exception e) { fail("Expected no Exception, got the following Exception instead: " + e.getMessage()); @@ -130,20 +130,19 @@ public void throwsExceptionWhenNoAnchorAsFirstLine() { String test2 = "blabla" + System.lineSeparator() + "// anchor:test2:test2:anchorend" + System.lineSeparator() + "test2"; try { - MergeUtil.splitByAnchors(test, testStrat); + MergeUtil.splitByAnchors(test, testStrat, System.lineSeparator()); } catch (Exception e) { fail("Expected no Exception, got the following Exception instead: " + e.getClass()); } try { - MergeUtil.splitByAnchors(test2, testStrat); + MergeUtil.splitByAnchors(test2, testStrat, System.lineSeparator()); failBecauseExceptionWasNotThrown(Exception.class); } catch (Exception e) { assertThat(e).hasMessage( "Incorrect document structure. Anchors are defined but there is no anchor at the start of the document.\n" + "See https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#general and " - + "https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#error-list " - + "for more details"); + + "https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#error-list " + "for more details"); } } @@ -179,17 +178,17 @@ public void testProperAppendingOfText() { String testString = "", testString2 = "", testString3 = "", testString4 = ""; - testString = MergeUtil.appendText(testString, "test", test, false, false); - testString = MergeUtil.appendText(testString, "test", test2, true, true); + testString = MergeUtil.appendText(testString, "test", test, false, false, System.lineSeparator()); + testString = MergeUtil.appendText(testString, "test", test2, true, true, System.lineSeparator()); - testString2 = MergeUtil.appendText(testString2, "test", test, false, false); - testString2 = MergeUtil.appendText(testString2, "test", test2, false, true); + testString2 = MergeUtil.appendText(testString2, "test", test, false, false, System.lineSeparator()); + testString2 = MergeUtil.appendText(testString2, "test", test2, false, true, System.lineSeparator()); - testString3 = MergeUtil.appendText(testString3, "test", test, false, false); - testString3 = MergeUtil.appendText(testString3, "test", test2, true, false); + testString3 = MergeUtil.appendText(testString3, "test", test, false, false, System.lineSeparator()); + testString3 = MergeUtil.appendText(testString3, "test", test2, true, false, System.lineSeparator()); - testString4 = MergeUtil.appendText(testString4, "test", test, false, false); - testString4 = MergeUtil.appendText(testString4, "test", test2, false, false); + testString4 = MergeUtil.appendText(testString4, "test", test, false, false, System.lineSeparator()); + testString4 = MergeUtil.appendText(testString4, "test", test2, false, false, System.lineSeparator()); assertThat(testString).isEqualTo(System.lineSeparator() + "// anchor:test:append:anchorend" + System.lineSeparator() + "test2" + System.lineSeparator() + "test1"); From 16132a4d2a60c8afc1d5a1d1a0b8e45ae9c5f4ad Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Fri, 25 Sep 2020 13:26:26 +0200 Subject: [PATCH 3/9] #1242 added another test to check if line endings get normalized to the base file if line endings of base and patch file differ --- .../cobigen/textmerger/TextAppenderTest.java | 23 +++++++++++++++---- .../base/TestBaseLineDelimiter.txt | 2 ++ .../merged/MergedBaseLineDelimiter.txt | 3 +++ .../patch/PatchBaseLineDelimiter.txt | 2 ++ 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 cobigen/cobigen-textmerger/src/test/resources/anchortests/base/TestBaseLineDelimiter.txt create mode 100644 cobigen/cobigen-textmerger/src/test/resources/anchortests/merged/MergedBaseLineDelimiter.txt create mode 100644 cobigen/cobigen-textmerger/src/test/resources/anchortests/patch/PatchBaseLineDelimiter.txt diff --git a/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/TextAppenderTest.java b/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/TextAppenderTest.java index 96e0904b07..b8405cb4a9 100644 --- a/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/TextAppenderTest.java +++ b/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/TextAppenderTest.java @@ -9,7 +9,6 @@ import org.junit.Test; import com.devonfw.cobigen.api.exception.MergeException; -import com.devonfw.cobigen.textmerger.TextAppender; /** * @@ -276,8 +275,7 @@ public void testAnchorMerge_testNoAnchorAtStartOfBase() throws Exception { assertThat(e).hasMessage(getMergeExceptionMessage(file, "Incorrect document structure. Anchors are defined but there is no anchor at the start of the document.\n" + "See https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#general and " - + "https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#error-list " - + "for more details")); + + "https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#error-list " + "for more details")); } } @@ -299,11 +297,26 @@ public void testAnchorMerge_testNoAnchorAtStartOfPatch() throws Exception { assertThat(e).hasMessage(getMergeExceptionMessage(file, "Incorrect document structure. Anchors are defined but there is no anchor at the start of the document.\n" + "See https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#general and " - + "https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#error-list " - + "for more details")); + + "https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#error-list " + "for more details")); } } + /** + * Tests if line endings of base and patch file get normalized to base file if line endings of base and + * patch file differ + * @throws Exception + * if errors occur while merging + */ + @Test + public void testMerge_normalizeToBaseLineDelimiter() throws Exception { + TextAppender appender = new TextAppender("textmerge_append", false); + String mergedString = appender.merge(new File(testFileRootPath + "anchortests/base/TestBaseLineDelimiter.txt"), + FileUtils.readFileToString(new File(testFileRootPath + "anchortests/patch/PatchBaseLineDelimiter.txt")), + "UTF-8"); + assertThat(mergedString).isEqualTo( + FileUtils.readFileToString(new File(testFileRootPath + "anchortests/merged/MergedBaseLineDelimiter.txt"))); + } + /** * Helper method that creates a String matching the usual look of a merge exception * @param base diff --git a/cobigen/cobigen-textmerger/src/test/resources/anchortests/base/TestBaseLineDelimiter.txt b/cobigen/cobigen-textmerger/src/test/resources/anchortests/base/TestBaseLineDelimiter.txt new file mode 100644 index 0000000000..0a207c060e --- /dev/null +++ b/cobigen/cobigen-textmerger/src/test/resources/anchortests/base/TestBaseLineDelimiter.txt @@ -0,0 +1,2 @@ +a +b \ No newline at end of file diff --git a/cobigen/cobigen-textmerger/src/test/resources/anchortests/merged/MergedBaseLineDelimiter.txt b/cobigen/cobigen-textmerger/src/test/resources/anchortests/merged/MergedBaseLineDelimiter.txt new file mode 100644 index 0000000000..f916f2839d --- /dev/null +++ b/cobigen/cobigen-textmerger/src/test/resources/anchortests/merged/MergedBaseLineDelimiter.txt @@ -0,0 +1,3 @@ +a +bc +d \ No newline at end of file diff --git a/cobigen/cobigen-textmerger/src/test/resources/anchortests/patch/PatchBaseLineDelimiter.txt b/cobigen/cobigen-textmerger/src/test/resources/anchortests/patch/PatchBaseLineDelimiter.txt new file mode 100644 index 0000000000..7abb43b567 --- /dev/null +++ b/cobigen/cobigen-textmerger/src/test/resources/anchortests/patch/PatchBaseLineDelimiter.txt @@ -0,0 +1,2 @@ +c +d \ No newline at end of file From d7e1102922cc379171619417a7b37c22b9ead3c1 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Tue, 29 Sep 2020 20:15:05 +0200 Subject: [PATCH 4/9] moved line delimiter test files added extra eol conditions for line delimiter test files to .gitattributes replaced inputstream based determineLineDelimiter method with Path based one implemented latest StringUtil consolidateLineEndings method from core-api (adjusts line endings of patch file to base file) --- .gitattributes | 5 ++++ .../cobigen/textmerger/TextAppender.java | 28 +++++++++---------- .../cobigen/textmerger/TextAppenderTest.java | 9 +++--- .../merged => }/MergedBaseLineDelimiter.txt | 0 .../patch => }/PatchBaseLineDelimiter.txt | 0 .../base => }/TestBaseLineDelimiter.txt | 0 6 files changed, 23 insertions(+), 19 deletions(-) rename cobigen/cobigen-textmerger/src/test/resources/{anchortests/merged => }/MergedBaseLineDelimiter.txt (100%) rename cobigen/cobigen-textmerger/src/test/resources/{anchortests/patch => }/PatchBaseLineDelimiter.txt (100%) rename cobigen/cobigen-textmerger/src/test/resources/{anchortests/base => }/TestBaseLineDelimiter.txt (100%) diff --git a/.gitattributes b/.gitattributes index a2517b6f44..7b7b90de9e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,6 +3,11 @@ * text eol=lf *.bat eol=crlf +# special line ending handling for test files +/cobigen/cobigen-textmerger/src/test/resources/TestBaseLineDelimter.txt eol=lf +/cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimter.txt eol=crlf +/cobigen/cobigen-textmerger/src/test/resources/MergeBaseLineDelimter.txt eol=lf + # # The above will handle all files NOT found below # diff --git a/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/TextAppender.java b/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/TextAppender.java index 737688f85d..e2ba0bafca 100644 --- a/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/TextAppender.java +++ b/cobigen/cobigen-textmerger/src/main/java/com/devonfw/cobigen/textmerger/TextAppender.java @@ -1,10 +1,9 @@ package com.devonfw.cobigen.textmerger; -import java.io.BufferedInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStreamReader; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; @@ -14,6 +13,7 @@ import com.devonfw.cobigen.api.exception.MergeException; import com.devonfw.cobigen.api.extension.Merger; +import com.devonfw.cobigen.api.util.StringUtil; import com.devonfw.cobigen.api.util.SystemUtil; import com.devonfw.cobigen.textmerger.anchorextension.Anchor; import com.devonfw.cobigen.textmerger.anchorextension.MergeStrategy; @@ -75,24 +75,24 @@ public String getType() { @Override public String merge(File base, String patch, String targetCharset) throws MergeException { String mergedString; - String lineDelimiter; + String lineDelimiterBase; + + Path path = Paths.get(base.getAbsolutePath()); + try { mergedString = FileUtils.readFileToString(base, targetCharset); - try (FileInputStream stream = new FileInputStream(base); - BufferedInputStream bis = new BufferedInputStream(stream); - InputStreamReader reader = new InputStreamReader(bis, targetCharset)) { - lineDelimiter = SystemUtil.determineLineDelimiter(bis, reader); - } catch (IOException e) { - throw new MergeException(base, "Could not read base file.", e); - } + lineDelimiterBase = SystemUtil.determineLineDelimiter(path, targetCharset); + } catch (IOException e) { throw new MergeException(base, "Could not read base file.", e); } try { - if (lineDelimiter.isEmpty()) { - lineDelimiter = System.lineSeparator(); + if (lineDelimiterBase.isEmpty()) { + lineDelimiterBase = System.lineSeparator(); } - mergedString = merge(mergedString, patch, lineDelimiter); + + mergedString = + merge(mergedString, StringUtil.consolidateLineEndings(patch, lineDelimiterBase), lineDelimiterBase); } catch (Exception e) { throw new MergeException(base, e.getMessage(), e); } diff --git a/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/TextAppenderTest.java b/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/TextAppenderTest.java index b8405cb4a9..c3562a4401 100644 --- a/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/TextAppenderTest.java +++ b/cobigen/cobigen-textmerger/src/test/java/com/devonfw/cobigen/textmerger/TextAppenderTest.java @@ -310,11 +310,10 @@ public void testAnchorMerge_testNoAnchorAtStartOfPatch() throws Exception { @Test public void testMerge_normalizeToBaseLineDelimiter() throws Exception { TextAppender appender = new TextAppender("textmerge_append", false); - String mergedString = appender.merge(new File(testFileRootPath + "anchortests/base/TestBaseLineDelimiter.txt"), - FileUtils.readFileToString(new File(testFileRootPath + "anchortests/patch/PatchBaseLineDelimiter.txt")), - "UTF-8"); - assertThat(mergedString).isEqualTo( - FileUtils.readFileToString(new File(testFileRootPath + "anchortests/merged/MergedBaseLineDelimiter.txt"))); + String mergedString = appender.merge(new File(testFileRootPath + "TestBaseLineDelimiter.txt"), + FileUtils.readFileToString(new File(testFileRootPath + "PatchBaseLineDelimiter.txt")), "UTF-8"); + assertThat(mergedString) + .isEqualTo(FileUtils.readFileToString(new File(testFileRootPath + "MergedBaseLineDelimiter.txt"))); } /** diff --git a/cobigen/cobigen-textmerger/src/test/resources/anchortests/merged/MergedBaseLineDelimiter.txt b/cobigen/cobigen-textmerger/src/test/resources/MergedBaseLineDelimiter.txt similarity index 100% rename from cobigen/cobigen-textmerger/src/test/resources/anchortests/merged/MergedBaseLineDelimiter.txt rename to cobigen/cobigen-textmerger/src/test/resources/MergedBaseLineDelimiter.txt diff --git a/cobigen/cobigen-textmerger/src/test/resources/anchortests/patch/PatchBaseLineDelimiter.txt b/cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt similarity index 100% rename from cobigen/cobigen-textmerger/src/test/resources/anchortests/patch/PatchBaseLineDelimiter.txt rename to cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt diff --git a/cobigen/cobigen-textmerger/src/test/resources/anchortests/base/TestBaseLineDelimiter.txt b/cobigen/cobigen-textmerger/src/test/resources/TestBaseLineDelimiter.txt similarity index 100% rename from cobigen/cobigen-textmerger/src/test/resources/anchortests/base/TestBaseLineDelimiter.txt rename to cobigen/cobigen-textmerger/src/test/resources/TestBaseLineDelimiter.txt From 0f6e11c47eea55694fb8b3418bbb1bc2fc407c25 Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 1 Oct 2020 17:43:44 +0200 Subject: [PATCH 5/9] fixed .gitattributes file paths for eol exclusions changed line endings of patch file once --- .gitattributes | 10 +++++----- .../src/test/resources/PatchBaseLineDelimiter.txt | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.gitattributes b/.gitattributes index 7b7b90de9e..9ae14fa217 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,11 +3,6 @@ * text eol=lf *.bat eol=crlf -# special line ending handling for test files -/cobigen/cobigen-textmerger/src/test/resources/TestBaseLineDelimter.txt eol=lf -/cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimter.txt eol=crlf -/cobigen/cobigen-textmerger/src/test/resources/MergeBaseLineDelimter.txt eol=lf - # # The above will handle all files NOT found below # @@ -51,3 +46,8 @@ *.pdf binary *.exe binary *.zip binary + +# special line ending handling for test files +cobigen/cobigen-textmerger/src/test/resources/TestBaseLineDelimiter.txt eol=lf +cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt eol=crlf +cobigen/cobigen-textmerger/src/test/resources/MergeBaseLineDelimiter.txt eol=lf diff --git a/cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt b/cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt index 7abb43b567..0a207c060e 100644 --- a/cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt +++ b/cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt @@ -1,2 +1,2 @@ -c -d \ No newline at end of file +a +b \ No newline at end of file From 176f0a5d5ba20f5104140641a4df04e217814b1a Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Thu, 1 Oct 2020 17:44:40 +0200 Subject: [PATCH 6/9] changed line endings of path file again --- .../src/test/resources/PatchBaseLineDelimiter.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt b/cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt index 0a207c060e..7abb43b567 100644 --- a/cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt +++ b/cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt @@ -1,2 +1,2 @@ -a -b \ No newline at end of file +c +d \ No newline at end of file From 4d2d2332090560037146f460e387a4b1ea58d3ae Mon Sep 17 00:00:00 2001 From: MikeSchumacher Date: Fri, 4 Dec 2020 10:27:43 +0100 Subject: [PATCH 7/9] #1284 set release snapshot version --- cobigen/cobigen-textmerger/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cobigen/cobigen-textmerger/pom.xml b/cobigen/cobigen-textmerger/pom.xml index ccfac3a293..f7f4793f45 100644 --- a/cobigen/cobigen-textmerger/pom.xml +++ b/cobigen/cobigen-textmerger/pom.xml @@ -2,7 +2,7 @@ 4.0.0 textmerger jar - 7.0.1-SNAPSHOT + 7.1.0-SNAPSHOT CobiGen - Text Merger Plug-in CobiGen - Text Merger Plug-in From a6635f586e37dc858f98198661125cd44fe8f99e Mon Sep 17 00:00:00 2001 From: MikeSchumacher Date: Fri, 4 Dec 2020 10:28:23 +0100 Subject: [PATCH 8/9] #1284 update wiki docs --- documentation/master-cobigen.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/master-cobigen.asciidoc b/documentation/master-cobigen.asciidoc index dd115ef389..4c5aaea75b 100644 --- a/documentation/master-cobigen.asciidoc +++ b/documentation/master-cobigen.asciidoc @@ -22,7 +22,7 @@ DISCLAIMER: All Cobigen plugins are compatible with the latest release of Devonf * CobiGen - XML Plug-in v4.2.0 * CobiGen - TypeScript Plug-in v2.4.4 * CobiGen - Property Plug-in v2.1.0 -* CobiGen - Text Merger v7.0.0 +* CobiGen - Text Merger v7.1.0 * CobiGen - JSON Plug-in v2.1.0 * CobiGen - HTML Plug-in v2.1.0 * CobiGen - Open API Plug-in v2.4.0 From 74c86c80fd260ef3b1aefb302b9fc9f10725b2f0 Mon Sep 17 00:00:00 2001 From: MikeSchumacher Date: Fri, 4 Dec 2020 10:28:25 +0100 Subject: [PATCH 9/9] #1284 Set release version --- cobigen/cobigen-textmerger/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cobigen/cobigen-textmerger/pom.xml b/cobigen/cobigen-textmerger/pom.xml index f7f4793f45..71f7bedac0 100644 --- a/cobigen/cobigen-textmerger/pom.xml +++ b/cobigen/cobigen-textmerger/pom.xml @@ -2,7 +2,7 @@ 4.0.0 textmerger jar - 7.1.0-SNAPSHOT + 7.1.0 CobiGen - Text Merger Plug-in CobiGen - Text Merger Plug-in