Search in sources :

Example 1 with JavaMerger

use of com.devonfw.cobigen.javaplugin.merger.JavaMerger in project cobigen by devonfw.

the class JavaMergerTest method testConsistentLineEndings.

/**
 * Tests whether the output file does not contain different line endings
 *
 * @throws IOException test fails
 * @throws MergeException test fails
 * @author mbrunnli (04.06.2013)
 */
@Test
public void testConsistentLineEndings() throws IOException, MergeException {
    File baseFile = new File(testFileRootPath + "BaseFile_innerClass.java");
    File patchFile = new File(testFileRootPath + "PatchFile_innerClass.java");
    String mergedContents = new JavaMerger("", false).merge(baseFile, FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");
    boolean eol1 = mergedContents.contains("\r\n");
    mergedContents = mergedContents.replaceAll("\r\n", "");
    boolean eol2 = mergedContents.contains("\n");
    boolean eol3 = mergedContents.contains("\r");
    assertThat(eol1 ^ eol2 ^ eol3).isTrue();
}
Also used : JavaMerger(com.devonfw.cobigen.javaplugin.merger.JavaMerger) File(java.io.File) Test(org.junit.Test)

Example 2 with JavaMerger

use of com.devonfw.cobigen.javaplugin.merger.JavaMerger in project cobigen by devonfw.

the class JavaMergerTest method testMergeInheritanceRelation.

/**
 * Tests issue <a href=https://github.com/devonfw/cobigen/issues/39>#39</a>: inheritance relation should be merged
 * also if the base class (natively) extends java.lang.Object
 *
 * @throws IOException test fails
 * @throws MergeException test fails
 * @author mbrunnli (29.09.2014)
 */
@Test
public void testMergeInheritanceRelation() throws IOException, MergeException {
    File baseFile = new File(testFileRootPath + "BaseFile_inheritance.java");
    File patchFile = new File(testFileRootPath + "PatchFile_inheritance.java");
    try (FileReader fileReader = new FileReader(baseFile)) {
        JavaClass origClazz = getFirstJavaClass(fileReader);
        assertThat(origClazz.getSuperClass().getCanonicalName()).isEqualTo("java.lang.Object");
    }
    String mergedContents = new JavaMerger("", false).merge(baseFile, FileUtils.readFileToString(patchFile, Charset.forName("UTF-8")), "UTF-8");
    try (StringReader stringReader = new StringReader(mergedContents)) {
        JavaClass resultClazz = getFirstJavaClass(stringReader);
        assertThat(resultClazz.getSuperClass().getCanonicalName()).as("The merged result does not contain the expected inheritance relation 'extends HashMap<String,Long>'").isEqualTo("java.util.HashMap");
        assertThat(resultClazz.getSuperClass().getGenericValue()).as("The merged result does not contain the original inheritance declaration'extends HashMap<String,Long>'").isEqualTo("HashMap<String,Long>");
    }
}
Also used : JavaParserUtil.getFirstJavaClass(com.devonfw.cobigen.javaplugin.inputreader.JavaParserUtil.getFirstJavaClass) JavaClass(com.thoughtworks.qdox.model.JavaClass) JavaMerger(com.devonfw.cobigen.javaplugin.merger.JavaMerger) StringReader(java.io.StringReader) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.Test)

Example 3 with JavaMerger

use of com.devonfw.cobigen.javaplugin.merger.JavaMerger in project cobigen by devonfw.

the class JavaMergerTest method testReadingEncoding.

/**
 * Tests whether the contents will be rewritten after parsing and printing with QDox with the right encoding
 *
 * @throws IOException test fails
 * @throws MergeException test fails
 * @author mbrunnli (12.04.2013)
 */
@Test
public void testReadingEncoding() throws IOException, MergeException {
    File baseFile = new File(testFileRootPath + "BaseFile_encoding_UTF-8.java");
    File patchFile = new File(testFileRootPath + "PatchFile_encoding.java");
    String mergedContents = new JavaMerger("", false).merge(baseFile, FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");
    JavaSource mergedSource;
    try (StringReader stringReader = new StringReader(mergedContents)) {
        mergedSource = getFirstJavaClass(stringReader).getSource();
        assertThat(mergedSource.toString().contains("enthält")).isTrue();
    }
    baseFile = new File(testFileRootPath + "BaseFile_encoding_ISO-8859-1.java");
    mergedContents = new JavaMerger("", false).merge(baseFile, FileUtils.readFileToString(patchFile, StandardCharsets.ISO_8859_1), "ISO-8859-1");
    try (StringReader stringReader = new StringReader(mergedContents)) {
        mergedSource = getFirstJavaClass(stringReader).getSource();
        assertThat(mergedSource.toString()).contains("enthält");
    }
}
Also used : JavaMerger(com.devonfw.cobigen.javaplugin.merger.JavaMerger) JavaSource(com.thoughtworks.qdox.model.JavaSource) StringReader(java.io.StringReader) File(java.io.File) Test(org.junit.Test)

Example 4 with JavaMerger

use of com.devonfw.cobigen.javaplugin.merger.JavaMerger in project cobigen by devonfw.

the class JavaMergerTest method testMergePropertyAnnotation.

/**
 * Tests merging a property annotation into the baseFile
 *
 * @throws IOException shouldn't happen
 * @throws MergeException shouldn't happen either
 */
@Test
public void testMergePropertyAnnotation() throws IOException, MergeException {
    File baseFile = new File(testFileRootPath + "BaseFile_PropertyAnnotation.java");
    File patchFile = new File(testFileRootPath + "PatchFile_PropertyAnnotation.java");
    // with override
    String mergedContents = new JavaMerger("", false).merge(baseFile, FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");
    assertThat(mergedContents).contains("@Column(name=\"USER\")");
    assertThat(mergedContents).contains("@Column(name=\"NAME\")");
    // without override
    mergedContents = new JavaMerger("", true).merge(baseFile, FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");
    assertThat(mergedContents).contains("@Column(name=  \"USERNAME\")");
    assertThat(mergedContents).contains("@Column(name=\"NAME\")");
}
Also used : JavaMerger(com.devonfw.cobigen.javaplugin.merger.JavaMerger) File(java.io.File) Test(org.junit.Test)

Example 5 with JavaMerger

use of com.devonfw.cobigen.javaplugin.merger.JavaMerger in project cobigen by devonfw.

the class JavaMergerTest method testMergeMethodAnnotation.

/**
 * Tests merging a Method annotation into the baseFile
 *
 * @throws IOException shouldn't happen
 * @throws MergeException shouldn't happen either
 */
@Test
public void testMergeMethodAnnotation() throws IOException, MergeException {
    File baseFile = new File(testFileRootPath + "BaseFile_MethodAnnotation.java");
    File patchFile = new File(testFileRootPath + "PatchFile_MethodAnnotation.java");
    // with override
    String mergedContents = new JavaMerger("", true).merge(baseFile, FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");
    assertThat(mergedContents).contains("@Column(name=\"USERNAME\")");
    assertThat(mergedContents).contains("@Column(name=\"USER\")");
    // without override
    mergedContents = new JavaMerger("", false).merge(baseFile, FileUtils.readFileToString(patchFile, StandardCharsets.UTF_8), "UTF-8");
    assertThat(mergedContents).contains("@Column(name=\"NAME\")");
    assertThat(mergedContents).contains("@Column(name=\"USER\")");
}
Also used : JavaMerger(com.devonfw.cobigen.javaplugin.merger.JavaMerger) File(java.io.File) Test(org.junit.Test)

Aggregations

JavaMerger (com.devonfw.cobigen.javaplugin.merger.JavaMerger)11 File (java.io.File)10 Test (org.junit.Test)10 StringReader (java.io.StringReader)3 JavaParserUtil.getFirstJavaClass (com.devonfw.cobigen.javaplugin.inputreader.JavaParserUtil.getFirstJavaClass)2 JavaClass (com.thoughtworks.qdox.model.JavaClass)2 JavaSource (com.thoughtworks.qdox.model.JavaSource)2 Merger (com.devonfw.cobigen.api.extension.Merger)1 ModifyableClassLibraryBuilder (com.devonfw.cobigen.javaplugin.merger.libextension.ModifyableClassLibraryBuilder)1 ClassLibraryBuilder (com.thoughtworks.qdox.library.ClassLibraryBuilder)1 JavaMethod (com.thoughtworks.qdox.model.JavaMethod)1 FileInputStream (java.io.FileInputStream)1 FileReader (java.io.FileReader)1