Skip to content

Commit

Permalink
Merge branch 'dev_core'
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchumacherCapgemini committed Dec 7, 2020
2 parents 4f9a619 + 943251f commit 8bcac43
Show file tree
Hide file tree
Showing 17 changed files with 427 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@
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
7 changes: 7 additions & 0 deletions cobigen/cobigen-core-parent/cobigen-core-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>

<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>core-test</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ public class ConfigurationConstants {

/** Name of the templates folder */
public static final String TEMPLATES_FOLDER = "templates";


/** Name of the extracted templates project */
public static final String COBIGEN_TEMPLATES = "CobiGen_Templates";

/** Name of the environment variable pointing to cobigen configuration file */
public static final String COBIGEN_CONFIG_DIR = "COBIGEN_CONFIG_DIR";

/** Name of cobigen configuration file */
public static final String COBIGEN_CONFIG_FILE = ".cobigen";

/**Name of configuration key for location of templates. It could be Cobigen_Templates folder, an artifact or a maven dependency*/
public static final String COBIGEN_CONFIG_TEMPLATES_LOCATION_KEY = "cobigen.templates.templates_location";

/** Resource folder containing templates */
public static final String TEMPLATE_RESOURCE_FOLDER = "src/main/templates";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ public static String uncapFirst(String in) {
return in.substring(0, 1).toLowerCase() + in.substring(1, in.length());
}

/**
* Consolidates all line endings to the given one
*
* @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)
*/
public static String consolidateLineEndings(String codeBlock, String lineDelimiter) {
if (lineDelimiter != null) {
return codeBlock.replaceAll("\r\n|\r|\n", lineDelimiter);
}
return codeBlock;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.devonfw.cobigen.api.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;

/**
* This util class provides system properties
*/
Expand All @@ -15,4 +21,63 @@ public class SystemUtil {
*/
public static final String LINE_SEPARATOR = java.lang.System.getProperty("line.separator");

/**
* Determines the line delimiter
*
* @param path
* The path containing the input file
* @param targetCharset
* target char set of the file to be read and write
* @return The line delimiter corresponding to the input file
* @throws IOException
* If an exception occurs while processing the {@link BufferedInputStream} or the
* {@link InputStreamReader}
*/
public static String determineLineDelimiter(Path path, String targetCharset) throws IOException {

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

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();
}

} catch (IOException e) {
throw new IOException("Could not read file:" + path.toString(), e);
}

}

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

}

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

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

import org.junit.Test;

import com.devonfw.cobigen.api.util.StringUtil;

/**
*
*/
public class StringUtilTest {

/** Testdata root path */
private static final String testdataRoot = "src/test/resources/testdata/unittest/StringUtilTest";

/**
* Tests whether consolidateLineEndings returns proper line endings for Windows, Linux and Osx
* @throws Exception
* test fails
*/
@Test
public void testConsolidateLineEndings() throws Exception {
String origin = "\n";
String origin2 = "\r\n";
String lineDelimiterWindows = "\r\n";
String lineDelimiterLinux = "\n";
String lineDelimiterOsx = "\r";
String consolidatedWindows = StringUtil.consolidateLineEndings(origin, lineDelimiterWindows);
String consolidatedLinux = StringUtil.consolidateLineEndings(origin2, lineDelimiterLinux);
String consolidatedOsx = StringUtil.consolidateLineEndings(origin, lineDelimiterOsx);
assertThat(consolidatedWindows).isEqualTo(lineDelimiterWindows);
assertThat(consolidatedLinux).isEqualTo(lineDelimiterLinux);
assertThat(consolidatedOsx).isEqualTo(lineDelimiterOsx);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.devonfw.cobigen.api;

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

import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Test;

import com.devonfw.cobigen.api.util.SystemUtil;

/** Test suite for {@link SystemUtil}. */
public class SystemUtilTest {

/** Testdata root path */
private static final String testdataRoot = "src/test/resources/testdata/unittest/SystemUtilTest";

/**
* Tests whether determineLineDelimiter returns Linux line endings
* @throws Exception
* test fails
*/
@Test
public void testDetermineLineDelimiterLinux() throws Exception {
Path path = Paths.get(testdataRoot, "TestLinuxLineEndings.txt");
String targetCharset = "UTF-8";
String lineEnding = SystemUtil.determineLineDelimiter(path, targetCharset);
assertThat(lineEnding).isEqualTo("\n");
}

/**
* Tests whether determineLineDelimiter returns Windows line endings
* @throws Exception
* test fails
*/
@Test
public void testDetermineLineDelimiterWindows() throws Exception {
Path path = Paths.get(testdataRoot, "TestWindowsLineEndings.txt");
String targetCharset = "UTF-8";
String lineEnding = SystemUtil.determineLineDelimiter(path, targetCharset);
assertThat(lineEnding).isEqualTo("\r\n");
}

/**
* Tests whether determineLineDelimiter returns Osx line endings
* @throws Exception
* test fails
*/
@Test
public void testDetermineLineDelimiterOsx() throws Exception {
Path path = Paths.get(testdataRoot, "TestOsxLineEndings.txt");
String targetCharset = "UTF-8";
String lineEnding = SystemUtil.determineLineDelimiter(path, targetCharset);
assertThat(lineEnding).isEqualTo("\r");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a
b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ef
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
c
d
4 changes: 2 additions & 2 deletions cobigen/cobigen-core-parent/cobigen-core-systemtest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>tempeng-freemarker</artifactId>
<version>7.0.0-SNAPSHOT</version>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>javaplugin</artifactId>
<version>7.0.0-SNAPSHOT</version>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
8 changes: 8 additions & 0 deletions cobigen/cobigen-core-parent/cobigen-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>

<!-- collection of functions to test code which uses java.lang.System -->
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-lambda</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>

<!-- Needed for JDK 9+ -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.devonfw.cobigen.impl.config.ContextConfiguration;
import com.devonfw.cobigen.impl.extension.PluginRegistry;
import com.devonfw.cobigen.impl.healthcheck.HealthCheckImpl;
import com.devonfw.cobigen.impl.util.ConfigurationUtil;
import com.devonfw.cobigen.impl.util.FileSystemUtil;

/**
Expand Down Expand Up @@ -46,6 +47,24 @@ public static CobiGen create(URI configFileOrFolder) throws InvalidConfiguration
return createBean;
}

/**
* Creates a new {@link CobiGen}
*
* @return a new instance of {@link CobiGen}
* @throws IOException
* if the {@link URI} points to a file or folder, which could not be read.
* @throws InvalidConfigurationException
* if the context configuration could not be read properly.
*/
public static CobiGen create() throws InvalidConfigurationException, IOException {
URI configFileOrFolder = ConfigurationUtil.findTemplatesLocation();
if (configFileOrFolder == null) {
throw new InvalidConfigurationException(
"No valid templates can be found. Please configure your cobigen configuration file properly or place the templates in cobigen home directory. Creating CobiGen instance aborted.");
}
return create(configFileOrFolder);
}

/**
* Creates a new {@link HealthCheck}.
* @return a new {@link HealthCheck} instance
Expand Down
Loading

0 comments on commit 8bcac43

Please sign in to comment.