Skip to content

Commit

Permalink
Merge branch 'dev_propertyplugin'
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchumacherCapgemini committed Dec 4, 2020
2 parents 65419ae + 59105cc commit 4f9a619
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 37 deletions.
21 changes: 20 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist: bionic
os: linux
language: java
cache:
directories:
Expand Down Expand Up @@ -33,6 +34,24 @@ jobs:
script: ./scripts/travis-build-test.sh
if: (branch != gh-pages) AND (branch != dev_oomph_setup) AND (tag IS blank)
jdk: openjdk11
- name: Build & Test (openjdk11Windows)
language: bash
os: windows
cache:
directories:
- /c/ProgramData/chocolatey/lib/maven/apache-maven-3.6.3
- /c/Program Files/OpenJDK/openjdk-11.0.8_10
before_install:
- choco install openjdk11 --version 11.0.8.10 -y
- choco install maven --version 3.6.3 -y
- powershell refreshenv
- export PATH=${PATH}:"/c/ProgramData/chocolatey/lib/maven/apache-maven-3.6.3/bin"
- export PATH=${PATH}:"/c/Program Files/OpenJDK/openjdk-11.0.8_10/bin"
- export JAVA_HOME="/c/Program Files/OpenJDK/openjdk-11.0.8_10/"
install: []
script:
- ./scripts/travis-build-test.sh
if: (branch != gh-pages) AND (branch != dev_oomph_setup) AND (tag IS blank)
- name: Build docs as PDF
install: []
script: ./scripts/travis-build-docs.sh
Expand All @@ -53,4 +72,4 @@ jobs:
install: []
script: ./scripts/travis-sync-docs.sh
if: (branch = master) AND (type != pull_request) AND (fork = false) AND (tag IS blank)
jdk: openjdk11
jdk: openjdk11
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public JavaUtil() {

/**
* Returns the Object version of a Java primitive or the input if the input isn't a java primitive
*
*
* @param simpleType
* String
* @return the corresponding object wrapper type simple name of the input if the input is the name of a
Expand Down
2 changes: 1 addition & 1 deletion cobigen/cobigen-propertyplugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>propertyplugin</artifactId>
<packaging>jar</packaging>
<version>7.0.0</version>
<version>7.1.0</version>
<name>CobiGen - Property File Plug-In</name>
<description>CobiGen - Property File Plug-In</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand All @@ -19,16 +19,14 @@

import com.devonfw.cobigen.api.exception.MergeException;
import com.devonfw.cobigen.api.extension.Merger;
import com.devonfw.cobigen.api.util.SystemUtil;

/**
* The {@link PropertyMerger} merges two property files. One being provided as the base file and the second
* being provided as the file contents of the patch.
*/
public class PropertyMerger implements Merger {

/** Line separator, e.g. for windows '\r\n' */
public static final String LINE_SEPARATOR = java.lang.System.getProperty("line.separator");

/**
* Merger Type to be registered
*/
Expand Down Expand Up @@ -73,9 +71,11 @@ public String merge(File base, String patch, String targetCharset) throws MergeE
throw new MergeException(base, "Could not read generated patch.", e);
}
Set<Object> conflicts = getConflictingProperties(baseProperties, patchProperties);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(base), targetCharset));
return concatContents(conflicts, br, patch);
try (FileInputStream in = new FileInputStream(base);
InputStreamReader reader = new InputStreamReader(in, targetCharset)) {
BufferedReader br = new BufferedReader(reader);
String lineDelimiter = SystemUtil.determineLineDelimiter(base.toPath(), targetCharset);
return concatContents(conflicts, br, patch, lineDelimiter);
} catch (IOException e) {
throw new MergeException(base, "Could not read base file.", e);
}
Expand All @@ -91,16 +91,18 @@ public String merge(File base, String patch, String targetCharset) throws MergeE
* {@link BufferedReader} reading the base file
* @param patch
* which should be applied
* @param lineSeparator
* the line Separator to use for the file
* @return merged file contents
* @throws IOException
* if the base file could not be read oder accessed
* @author mbrunnli (11.03.2013)
*/
private String concatContents(Set<Object> conflicts, BufferedReader baseFileReader, String patch)
throws IOException {
private String concatContents(Set<Object> conflicts, BufferedReader baseFileReader, String patch,
String lineSeparator) throws IOException {

List<String> recordedComments = new LinkedList<>();
Map<String, String> collection = new HashMap<>();
Map<String, String> collection = new LinkedHashMap<>();
String line;
boolean lastLineWasComment = false;
int count = 0; // count is used below to maintain uniqueness in the hash
Expand All @@ -109,18 +111,18 @@ private String concatContents(Set<Object> conflicts, BufferedReader baseFileRead
line = line.trim();
// adding key of the respective value to the collection
if (line.startsWith("#")) {
collection.put("recordedComments" + count, LINE_SEPARATOR + line);
collection.put("recordedComments" + count, lineSeparator + line);
if (lastLineWasComment) {
String lastComment = recordedComments.remove(recordedComments.size() - 1);
recordedComments.add(lastComment + LINE_SEPARATOR + line);
recordedComments.add(lastComment + lineSeparator + line);
} else {
lastLineWasComment = true;
recordedComments.add(line);
}
} else {
lastLineWasComment = false;
if (!line.trim().isEmpty()) {
collection.put(line.substring(0, line.indexOf("=")), LINE_SEPARATOR + line);
collection.put(line.substring(0, line.indexOf("=")), lineSeparator + line);
}
}
count++;
Expand All @@ -131,28 +133,28 @@ private String concatContents(Set<Object> conflicts, BufferedReader baseFileRead
String lastObservedComment = null;
int observedEmptyLines = 0;
count = 0;
for (String patchLine : patch.split(LINE_SEPARATOR)) {
for (String patchLine : patch.split(lineSeparator)) {
m = p.matcher(patchLine);
if (m.matches()) {
// no conflicts
if (!conflicts.contains(m.group(1))) {
collection.put(m.group(1), LINE_SEPARATOR + patchLine);
collection.put(m.group(1), lineSeparator + patchLine);
observedEmptyLines = 0;
} else {
if (patchOverrides) { // override the original by patch file
// patchLine;
collection.put(m.group(1), LINE_SEPARATOR + patchLine);
collection.put(m.group(1), lineSeparator + patchLine);
observedEmptyLines = 0;
}
}
} else if (patchLine.startsWith("#")) {
// record comment over multiple lines
if (lastObservedComment != null) {
lastObservedComment += LINE_SEPARATOR + patchLine;
collection.put("lastObservedComment" + count, LINE_SEPARATOR + lastObservedComment);
lastObservedComment += lineSeparator + patchLine;
collection.put("lastObservedComment" + count, lineSeparator + lastObservedComment);
} else {
lastObservedComment = patchLine;
collection.put("lastObservedComment" + count, LINE_SEPARATOR + lastObservedComment);
collection.put("lastObservedComment" + count, lineSeparator + lastObservedComment);
}
} else {
if (lastObservedComment == null && patchLine.trim().isEmpty()) {
Expand All @@ -162,16 +164,16 @@ private String concatContents(Set<Object> conflicts, BufferedReader baseFileRead
// comment if not so
if (lastObservedComment != null && !recordedComments.contains(lastObservedComment)) {
for (int i = 0; i < observedEmptyLines; i++) {
collection.put("_blank" + count, LINE_SEPARATOR);
collection.put("_blank" + count, lineSeparator);
}
collection.put("lastObservedComment" + count, LINE_SEPARATOR + lastObservedComment);
collection.put("lastObservedComment" + count, lineSeparator + lastObservedComment);
}
lastObservedComment = null;
observedEmptyLines = 0;

if (!patchLine.trim().isEmpty()) {
// patchLine;
collection.put("patchLineNotEmpty" + count, LINE_SEPARATOR + patchLine);
collection.put("patchLineNotEmpty" + count, lineSeparator + patchLine);
observedEmptyLines = 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.devonfw.cobigen.propertyplugin;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.FileReader;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;

import com.devonfw.cobigen.propertyplugin.PropertyMerger;

import junit.framework.TestCase;

/**
Expand All @@ -32,10 +31,10 @@ public void testPropertyMergeOverride() throws Exception {
PropertyMerger pMerger = new PropertyMerger("", true);
String mergedPropFile =
pMerger.merge(base, IOUtils.toString(new FileReader(new File(testFileRootPath + "Name.ftl"))), "UTF-8");
Assert.assertTrue("NachNameOverride", mergedPropFile.contains("NachNameOverride"));
Assert.assertFalse("nachNameOriginal", mergedPropFile.contains("nachNameOriginal"));
Assert.assertTrue("FirstName", mergedPropFile.contains("firstName"));
Assert.assertTrue("lastName", mergedPropFile.contains("lastName"));
assertThat(mergedPropFile).contains("NachNameOverride");
assertThat(mergedPropFile).doesNotContain("nachNameOriginal");
assertThat(mergedPropFile).contains("firstName");
assertThat(mergedPropFile).contains("lastName");
}

/**
Expand All @@ -49,10 +48,10 @@ public void testPropertyMergeWithoutOverride() throws Exception {
PropertyMerger pMerger = new PropertyMerger("", false);
String mergedPropFile =
pMerger.merge(base, IOUtils.toString(new FileReader(new File(testFileRootPath + "Name.ftl"))), "UTF-8");
Assert.assertFalse("NachNameOverride", mergedPropFile.contains("NachNameOverride"));
Assert.assertTrue("nachNameOriginal", mergedPropFile.contains("nachNameOriginal"));
Assert.assertTrue("FirstName", mergedPropFile.contains("firstName"));
Assert.assertTrue("lastName", mergedPropFile.contains("lastName"));
assertThat(mergedPropFile).doesNotContain("NachNameOverride");
assertThat(mergedPropFile).contains("nachNameOriginal");
assertThat(mergedPropFile).contains("firstName");
assertThat(mergedPropFile).contains("lastName");
}

}
2 changes: 1 addition & 1 deletion documentation/master-cobigen.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ DISCLAIMER: All Cobigen plugins are compatible with the latest release of Devonf
* CobiGen - Java Plug-in v7.0.0
* CobiGen - XML Plug-in v7.0.0
* CobiGen - TypeScript Plug-in v7.1.0
* CobiGen - Property Plug-in v7.0.0
* CobiGen - Property Plug-in v7.1.0
* CobiGen - Text Merger v7.1.0
* CobiGen - JSON Plug-in v7.0.0
* CobiGen - HTML Plug-in v7.0.0
Expand Down

0 comments on commit 4f9a619

Please sign in to comment.