Skip to content

Commit

Permalink
Merge branch 'dev_javaplugin'
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchumacherCapgemini committed Dec 7, 2020
2 parents 8bcac43 + 3f81412 commit d9842a2
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 77 deletions.
5 changes: 4 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ 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

# handle line endings for test files differently
cobigen/cobigen-core-parent/cobigen-core-api/src/test/resources/testdata/unittest/SystemUtilTest/TestWindowsLineEndings.txt eol=crlf
cobigen/cobigen-core-parent/cobigen-core-api/src/test/resources/testdata/unittest/SystemUtilTest/TestLinuxLineEndings.txt eol=lf
cobigen/cobigen-core-parent/cobigen-core-api/src/test/resources/testdata/unittest/SystemUtilTest/TestOsxLineEndings.txt eol=cr

cobigen/cobigen-javaplugin-parent/cobigen-javaplugin/src/test/resources/testdata/unittest/merger/BaseFile_Eol.java eol=lf
cobigen/cobigen-javaplugin-parent/cobigen-javaplugin/src/test/resources/testdata/unittest/merger/PatchFile_Eol.java eol=crlf
cobigen/cobigen-javaplugin-parent/cobigen-javaplugin/src/test/resources/testdata/unittest/merger/MergedFile_Eol.java eol=lf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>javaplugin-model</artifactId>
<version>7.0.0</version>
<version>7.1.0</version>
<name>CobiGen Javaplugin Model</name>
<packaging>jar</packaging>
<description>CobiGen - Java Plug-in Model</description>
Expand Down
8 changes: 4 additions & 4 deletions cobigen/cobigen-javaplugin-parent/cobigen-javaplugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>javaplugin</artifactId>
<name>CobiGen - Java Plug-in</name>
<version>7.0.0</version>
<version>7.1.0</version>
<packaging>jar</packaging>
<description>CobiGen - Java Plug-in</description>

Expand All @@ -27,7 +27,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>core-api</artifactId>
<version>7.0.0</version>
<version>7.1.0</version>
</dependency>

<dependency>
Expand All @@ -39,13 +39,13 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>core-test</artifactId>
<version>7.0.0</version>
<version>[7.0.0,)</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>core</artifactId>
<version>7.0.0</version>
<version>[7.0.0,)</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

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.javaplugin.inputreader.JavaParserUtil;
import com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableJavaClass;
import com.thoughtworks.qdox.model.JavaClass;
Expand Down Expand Up @@ -65,17 +69,22 @@ public String merge(File base, String patch, String targetCharset) throws MergeE

ModifyableJavaClass baseClass;
String lineDelimiter;
try (FileInputStream stream = new FileInputStream(base);
Path path = Paths.get(base.getAbsolutePath());

try (FileInputStream stream = new FileInputStream(path.toString());
BufferedInputStream bis = new BufferedInputStream(stream);
InputStreamReader reader = new InputStreamReader(bis, targetCharset)) {
lineDelimiter = determineLineDelimiter(bis, reader);

baseClass = (ModifyableJavaClass) JavaParserUtil.getFirstJavaClass(reader);
lineDelimiter = SystemUtil.determineLineDelimiter(path, targetCharset);

} catch (IOException e) {
throw new MergeException(base, "Cannot read base file.", e);
} catch (ParseException e) {
throw new MergeException(base, "The syntax of the base file is invalid. Error in line: " + e.getLine()
+ " / column: " + e.getColumn() + ": " + e.getMessage(), e);
}

ModifyableJavaClass patchClass;
try (StringReader reader = new StringReader(patch)) {

Expand All @@ -92,70 +101,7 @@ public String merge(File base, String patch, String targetCharset) throws MergeE
}

ModifyableJavaClass mergedClass = merge(baseClass, patchClass);
return consolidateLineEndings(mergedClass.getSource().getCodeBlock(), lineDelimiter);
}

/**
* @param bis
* The {@link BufferedInputStream} containing the input file
* @param reader
* The {@link InputStreamReader} iterating over the Stream
* @return The line delimiter corresponding to the input file
* @throws IOException
* If an exception occurs while processing the {@link BufferedInputStream} or the
* {@link InputStreamReader}
*/
private String determineLineDelimiter(BufferedInputStream bis, InputStreamReader reader) throws IOException {

bis.mark(0);
try {
while (reader.ready()) {
int nextChar = reader.read();
if (nextChar == '\r') {
nextChar = reader.read();
if (nextChar == '\n') {
return "\r\n";
}
return "\r";
} else if (nextChar == '\n') {
return "\n";
}
}
return null;
} finally {
emptyReader(reader);
bis.reset();
}
}

/**
* @param reader
* The {@link InputStreamReader} that is to be emptied
* @throws IOException
* If an exception occurs while processing the {@link InputStreamReader}
*/
private void emptyReader(InputStreamReader reader) throws IOException {
while (reader.ready()) {
reader.read();
}

}

/**
* Consolidates all line endings to the System default
*
* @param codeBlock
* which should be consolidate
* @param lineDelimiter
* the line delimiter of the file or null if none
* @return the consolidated code block
* @author mbrunnli (04.06.2013)
*/
private String consolidateLineEndings(String codeBlock, String lineDelimiter) {
if (lineDelimiter != null) {
return codeBlock.replaceAll("\r\n|\r|\n", lineDelimiter);
}
return codeBlock;
return StringUtil.consolidateLineEndings(mergedClass.getSource().getCodeBlock(), lineDelimiter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.devonfw.cobigen.api.exception.MergeException;
import com.devonfw.cobigen.javaplugin.merger.JavaMerger;
import com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder;
import com.google.common.io.Files;
import com.thoughtworks.qdox.library.ClassLibraryBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaConstructor;
Expand Down Expand Up @@ -318,6 +317,24 @@ public void testConsistentLineEndings() throws IOException, MergeException {
assertThat(eol1 ^ eol2 ^ eol3).isTrue();
}

/**
* Tests whether the output file contains line endings of base file (linux line endings)
* @throws IOException
* test fails
* @throws MergeException
* test fails
*/
@Test
public void testBaseLineEndings() throws IOException, MergeException {
File baseFile = new File(testFileRootPath + "BaseFile_Eol.java");
File patchFile = new File(testFileRootPath + "PatchFile_Eol.java");
File mergedFile = new File(testFileRootPath + "MergedFile_Eol.java");
String expectedContent = FileUtils.readFileToString(mergedFile);
String mergedContents =
new JavaMerger("", false).merge(baseFile, FileUtils.readFileToString(patchFile), "UTF-8");
assertThat(mergedContents).isEqualTo(expectedContent);
}

/**
* Tests whether all generics of the original file will be existent after merging
* @throws IOException
Expand Down Expand Up @@ -358,7 +375,7 @@ public void testMergeMethodsWithoutExtendingMethodBodyWithWhitespaces() throws I
JavaClass origClazz = source.getClasses().get(0);

String mergedContents =
new JavaMerger("", true).merge(file, Files.toString(file, Charset.forName("UTF-8")), "UTF-8");
new JavaMerger("", true).merge(file, FileUtils.readFileToString(file, Charset.forName("UTF-8")), "UTF-8");

classLibraryBuilder = new ModifyableClassLibraryBuilder();
source = classLibraryBuilder.addSource(new StringReader(mergedContents));
Expand Down Expand Up @@ -387,8 +404,8 @@ public void testMergeInheritanceRelation() throws IOException, MergeException {
JavaClass origClazz = getFirstJavaClass(new FileReader(baseFile));
assertThat(origClazz.getSuperClass().getCanonicalName()).isEqualTo("java.lang.Object");

String mergedContents =
new JavaMerger("", false).merge(baseFile, Files.toString(patchFile, Charset.forName("UTF-8")), "UTF-8");
String mergedContents = new JavaMerger("", false).merge(baseFile,
FileUtils.readFileToString(patchFile, Charset.forName("UTF-8")), "UTF-8");

JavaClass resultClazz = getFirstJavaClass(new StringReader(mergedContents));
assertThat(resultClazz.getSuperClass().getCanonicalName())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.devonfw;

public class FooBar {

private int baseField = 0;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.devonfw;

public class FooBar {

private int baseField = 0;

private int patchField = 0;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.devonfw;

public class FooBar
{

private int baseField = 1;
private int patchField = 0;

}
2 changes: 1 addition & 1 deletion documentation/master-cobigen.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ DISCLAIMER: All Cobigen plugins are compatible with the latest release of Devonf
---

* CobiGen v7.1.0
* CobiGen - Java Plug-in v7.0.0
* CobiGen - Java Plug-in v7.1.0
* CobiGen - XML Plug-in v7.0.0
* CobiGen - TypeScript Plug-in v7.1.0
* CobiGen - Property Plug-in v7.1.0
Expand Down

0 comments on commit d9842a2

Please sign in to comment.