Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1452 Implemented new configuration reader to read the template sets #1504

Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.codehaus.plexus.archiver.tar.TarGZipUnArchiver;
import org.codehaus.plexus.logging.console.ConsoleLoggerManager;
import org.codehaus.plexus.util.Os;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.slf4j.LoggerFactory;
Expand All @@ -32,23 +37,84 @@ public class AbstractCliTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

/** Temporary directory for the templates project */
@ClassRule
public static TemporaryFolder tempFolderTemplates = new TemporaryFolder();

/** Current home directory */
protected Path currentHome;

/** The devon4j-templates development folder */
/** The templates development folder */
protected static Path devTemplatesPath;

/** A temp directory containing the templates development folder */
protected static Path devTemplatesPathTemp;

/**
* Determine the devon4j-templates development folder
* Determine the templates development folder and create a copy of it in the temp directory
*
* @throws URISyntaxException if the path could not be created properly
* @throws IOException if accessing a template directory directory fails
*/
@BeforeClass
public static void determineDevTemplatesPath() throws URISyntaxException {
public static void determineDevTemplatesPath() throws URISyntaxException, IOException {

devTemplatesPath = new File(AbstractCliTest.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.getParentFile().getParentFile().getParentFile().getParentFile().toPath().resolve("cobigen-templates");

Path utilsPom = new File(AbstractCliTest.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.getParentFile().getParentFile().getParentFile().getParentFile().toPath().resolve("cobigen-templates")
.resolve("templates-devon4j");
.resolve("templates-devon4j-tests/src/test/resources/utils/pom.xml");

// create a temporary directory cobigen-templates/template-sets/adapted containing the template sets
Path tempFolderPath = tempFolderTemplates.getRoot().toPath();
Path cobigenTemplatePath = tempFolderPath.resolve("cobigen-templates");
if (!Files.exists(cobigenTemplatePath)) {
Files.createDirectory(cobigenTemplatePath);

devTemplatesPathTemp = cobigenTemplatePath.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);
Path templateSetsAdaptedFolder = devTemplatesPathTemp.resolve(ConfigurationConstants.ADAPTED_FOLDER);
Files.createDirectory(devTemplatesPathTemp);
Files.createDirectory(templateSetsAdaptedFolder);

FileUtils.copyDirectory(devTemplatesPath.toFile(), templateSetsAdaptedFolder.toFile());

List<Path> devTemplateSets = new ArrayList<>();
try (Stream<Path> files = Files.list(templateSetsAdaptedFolder)) {
files.forEach(path -> {
devTemplateSets.add(path);
});
}

for (Path path : devTemplateSets) {
if (Files.isDirectory(path)) {
Path resourcesFolder = path.resolve("src/main/resources");
Path templatesFolder = path.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER);
if (Files.exists(resourcesFolder) && !Files.exists(templatesFolder)) {
try {
Files.move(resourcesFolder, templatesFolder);
} catch (IOException e) {
throw new IOException("Error moving directory " + resourcesFolder, e);
}
}

if (path.getFileName().toString().equals("templates-devon4j-utils")) {
if (Files.exists(path.resolve("pom.xml"))) {
try {
Files.delete(path.resolve("pom.xml"));
} catch (IOException e) {
throw new IOException("Error deleting file " + path.resolve("pom.xml"), e);
}
}
try {
Files.copy(utilsPom, path.resolve("pom.xml"));
} catch (IOException e) {
throw new IOException("Error copying file " + utilsPom, e);
}
}
}
}
}
}

/**
Expand All @@ -71,7 +137,7 @@ public void runWithLatestTemplates() throws IOException {

Path configFile = this.currentHome.resolve(ConfigurationConstants.COBIGEN_CONFIG_FILE);
Files.write(configFile,
(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATES_PATH + "=" + devTemplatesPath.toString()).getBytes());
(ConfigurationConstants.CONFIG_PROPERTY_TEMPLATES_PATH + "=" + devTemplatesPathTemp.toString()).getBytes());
}

/**
Expand Down Expand Up @@ -129,7 +195,7 @@ protected void execute(String[] args, boolean useDevTemplates, boolean assureFai
debugArgs = Arrays.copyOf(debugArgs, debugArgs.length + 3);
debugArgs[debugArgs.length - 3] = "-v";
debugArgs[debugArgs.length - 2] = "-tp";
debugArgs[debugArgs.length - 1] = devTemplatesPath.toString();
debugArgs[debugArgs.length - 1] = devTemplatesPathTemp.toString();
} else {
debugArgs = Arrays.copyOf(debugArgs, debugArgs.length + 1);
debugArgs[debugArgs.length - 1] = "-v";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Before;
import org.junit.Test;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
Expand All @@ -16,6 +19,29 @@
*/
public class AdaptTemplatesCommandTest extends AbstractCliTest {

/**
* Simulate the download of the template set jars, as this not yet implemented. This method can be removed later
*
* @throws URISyntaxException if the path could not be created properly
* @throws IOException if accessing a directory or file fails
*/
@Before
public void initAdaptTemplatesTest() throws URISyntaxException, IOException {

Path cliSystemTestPath = new File(
AdaptTemplatesCommandTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile()
.getParentFile().toPath();
Path templateJar = cliSystemTestPath.resolve("src/test/resources/testdata/crud-java-server-app.jar");
if (Files.exists(templateJar)) {
Path downloadedTemplateSetsPath = this.currentHome.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER)
.resolve(ConfigurationConstants.DOWNLOADED_FOLDER);
if (!Files.exists(downloadedTemplateSetsPath)) {
Files.createDirectories(downloadedTemplateSetsPath);
}
Files.copy(templateJar, downloadedTemplateSetsPath.resolve(templateJar.getFileName()));
}
}

/**
* Checks if adapt-templates command successfully created cobigen templates folder and its sub folders
*
Expand All @@ -29,19 +55,21 @@ public void adaptTemplatesTest() throws Exception {

execute(args, false);

Path cobigenTemplatesFolderPath = this.currentHome.resolve(ConfigurationConstants.TEMPLATES_FOLDER)
.resolve(ConfigurationConstants.COBIGEN_TEMPLATES);
assertThat(cobigenTemplatesFolderPath).exists();
// check if templates exist
assertThat(Paths
.get(cobigenTemplatesFolderPath.toString() + File.separator + ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER))
.exists();
Path cobigenTemplateSetsFolderPath = this.currentHome.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);
Path downloadedTemplateSetsFolderPath = cobigenTemplateSetsFolderPath
.resolve(ConfigurationConstants.DOWNLOADED_FOLDER);
Path adaptedTemplateSetsFolderPath = cobigenTemplateSetsFolderPath.resolve(ConfigurationConstants.ADAPTED_FOLDER);

assertThat(cobigenTemplateSetsFolderPath).exists();
assertThat(downloadedTemplateSetsFolderPath).exists();
assertThat(adaptedTemplateSetsFolderPath).exists();

// check if adapted template set exists
Path templateSet = adaptedTemplateSetsFolderPath.resolve("crud-java-server-app");
assertThat(templateSet).exists();
// check if context configuration exists
assertThat(Paths
.get(cobigenTemplatesFolderPath.toString() + File.separator + ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER
+ File.separator + ConfigurationConstants.CONTEXT_CONFIG_FILENAME)).exists();
// check if sources of utility classes exist
assertThat(Paths.get(cobigenTemplatesFolderPath.toString() + File.separator + "src" + File.separator + "main"
+ File.separator + "java")).exists();
assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)).exists();
assertThat(templateSet.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER)
.resolve(ConfigurationConstants.CONTEXT_CONFIG_FILENAME)).exists();
}
}
Binary file not shown.
5 changes: 0 additions & 5 deletions cobigen-cli/cli/src/main/resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@
<artifactId>jsonplugin</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>templates-devon4j</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package com.devonfw.cobigen.templates.devon4j.test.templates;

import java.io.File;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.devonfw.cobigen.api.constants.ConfigurationConstants;
import com.devonfw.cobigen.maven.test.AbstractMavenTest;
import com.devonfw.cobigen.templates.devon4j.config.constant.MavenMetadata;

Expand All @@ -15,6 +25,80 @@ public class TemplatesGenerationTest extends AbstractMavenTest {
/** Root of all test resources of this test suite */
public static final String TEST_RESOURCES_ROOT = "src/test/resources/testdata/templatetest/";

/** Temporary files rule to create temporary folders */
@ClassRule
public static TemporaryFolder tempFolder = new TemporaryFolder();

/** The templates development folder */
protected static Path templatesProject;

/** The templates development folder */
protected static Path templatesProjectTemporary;

/**
* Creates a copy of the templates project in the temp directory
*
* @throws URISyntaxException if the path could not be created properly
* @throws IOException if accessing a directory or file fails
*/
@BeforeClass
public static void setupDevTemplates() throws URISyntaxException, IOException {

templatesProject = new File(
TemplatesGenerationTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile()
.getParentFile().getParentFile().toPath();

Path utilsPom = new File(TemplatesGenerationTest.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.getParentFile().getParentFile().toPath().resolve("src/test/resources/utils/pom.xml");

// create a temporary directory cobigen-templates/template-sets/adapted containing the template sets
Path tempFolderPath = tempFolder.getRoot().toPath();
Path cobigenTemplatePath = tempFolderPath.resolve("cobigen-templates");
if (!Files.exists(cobigenTemplatePath)) {
Files.createDirectory(cobigenTemplatePath);

templatesProjectTemporary = cobigenTemplatePath.resolve(ConfigurationConstants.TEMPLATE_SETS_FOLDER);
Path templateSetsAdaptedFolder = templatesProjectTemporary.resolve(ConfigurationConstants.ADAPTED_FOLDER);
Files.createDirectory(templatesProjectTemporary);
Files.createDirectory(templateSetsAdaptedFolder);

FileUtils.copyDirectory(templatesProject.toFile(), templateSetsAdaptedFolder.toFile());

try (Stream<Path> files = Files.list(templateSetsAdaptedFolder)) {
files.forEach(path -> {
if (Files.isDirectory(path)) {
Path resourcesFolder = path.resolve("src/main/resources");
Path templatesFolder = path.resolve(ConfigurationConstants.TEMPLATE_RESOURCE_FOLDER);
if (Files.exists(resourcesFolder) && !Files.exists(templatesFolder)) {
try {
Files.move(resourcesFolder, templatesFolder);
} catch (IOException e) {
e.printStackTrace();
}
}
}

// Replace the pom.xml in the utils project. Needed so that the utils project in the temp directory is build
// properly
if (path.getFileName().toString().equals("templates-devon4j-utils")) {
if (Files.exists(path.resolve("pom.xml"))) {
try {
Files.delete(path.resolve("pom.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Files.copy(utilsPom, path.resolve("pom.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
}

/**
* Test successful generation of all templates based on an entity
*
Expand All @@ -24,7 +108,7 @@ public class TemplatesGenerationTest extends AbstractMavenTest {
public void testAllTemplatesGeneration_EntityInput() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesEntityInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

/**
Expand All @@ -36,7 +120,7 @@ public void testAllTemplatesGeneration_EntityInput() throws Exception {
public void testAllTemplatesGeneration_EtoInput() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesEtoInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

/**
Expand All @@ -48,7 +132,7 @@ public void testAllTemplatesGeneration_EtoInput() throws Exception {
public void testAllTemplatesGeneration_OpenApiInput() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesOpenApiInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

/**
Expand All @@ -60,7 +144,7 @@ public void testAllTemplatesGeneration_OpenApiInput() throws Exception {
public void testAllTemplatesGeneration_RestServiceInput() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesRestServiceInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

/**
Expand All @@ -72,7 +156,7 @@ public void testAllTemplatesGeneration_RestServiceInput() throws Exception {
public void testAllTemplatesGeneration_ToInput() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesToInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

/**
Expand All @@ -84,7 +168,7 @@ public void testAllTemplatesGeneration_ToInput() throws Exception {
public void testAllTemplatesGeneration_XML() throws Exception {

File testProject = new File(TEST_RESOURCES_ROOT + "TestAllTemplatesXMLInput/");
runMavenInvoker(testProject, new File("").getAbsoluteFile(), MavenMetadata.LOCAL_REPO);
runMavenInvoker(testProject, templatesProjectTemporary.toFile(), MavenMetadata.LOCAL_REPO);
}

}
Loading