From 5c328d629fe8760d9e5458c1438bae797755cdbb Mon Sep 17 00:00:00 2001 From: jan-vcapgemini Date: Mon, 7 Aug 2023 19:47:40 +0200 Subject: [PATCH 01/19] fixed code issues fixed deserializer of read method in TemplatesConfigurationReader fixed GenerationProcessor fixed TemplatesConfigurationReaderTest fixed TemplateSetReaderTest fixed TemplateClassTest --- .../reader/TemplatesConfigurationReader.java | 3 +- .../generator/GenerationProcessorImpl.java | 18 +- .../config/reader/TemplateSetReaderTest.java | 20 +- .../TemplatesConfigurationReaderTest.java | 463 ++++++++++-------- .../config/utils/TemplateClassTest.java | 6 +- 5 files changed, 276 insertions(+), 234 deletions(-) diff --git a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/reader/TemplatesConfigurationReader.java b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/reader/TemplatesConfigurationReader.java index 880cd2fa82..20003e673e 100644 --- a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/reader/TemplatesConfigurationReader.java +++ b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/config/reader/TemplatesConfigurationReader.java @@ -115,9 +115,10 @@ public TemplatesConfigurationReader(Path configFilePath, ConfigurationReader con this.configurationReader = configurationReader; // Path rootTemplatePath = configFilePath.getParent(); // Path templateLocation = findTemplateRootPath(projectRoot, templateFolder, rootTemplatePath); + this.configFilePath = configFilePath; this.rootTemplateFolder = TemplateFolder.create(configFilePath.getParent()); - this.configNode = deserialize(this.configFilePath, + this.configNode = deserialize(configFilePath, com.devonfw.cobigen.impl.config.entity.io.TemplatesConfiguration.class, TemplatesConfigurationVersion.class, "templatesConfiguration"); } diff --git a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/generator/GenerationProcessorImpl.java b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/generator/GenerationProcessorImpl.java index 8ca886e0c7..5c748b3c28 100644 --- a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/generator/GenerationProcessorImpl.java +++ b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/generator/GenerationProcessorImpl.java @@ -161,8 +161,9 @@ public GenerationReportTo generate(Object input, List templatesToBePrepended = flatten(generableArtifacts); progressCallback.accept("Prepend Templates Classloader", 10); - inputProjectClassLoader = prependTemplatesClassloader(inputProjectClassLoader); + inputProjectClassLoader = prependTemplatesClassloader(inputProjectClassLoader, templatesToBePrepended); // initialize this.forceOverride = forceOverride; @@ -255,17 +256,26 @@ public GenerationReportTo generate(Object input, List templatesToBePrepended) { Path configLocation = Paths.get(this.configurationHolder.getConfigurationLocation()); ClassLoader combinedClassLoader = inputProjectClassLoader != null ? inputProjectClassLoader : Thread.currentThread().getContextClassLoader(); - if (!this.configurationHolder.getUtilsLocation().isEmpty()) { - List utilsLocations = this.configurationHolder.getUtilsLocation(); + List utilsLocations = new ArrayList<>(); + for (TemplateTo template : templatesToBePrepended) { + Trigger trigger = this.configurationHolder.getContextConfiguration().getTrigger(template.getTriggerId()); + if (!this.configurationHolder.getUtilsLocation(trigger).isEmpty()) { + utilsLocations = this.configurationHolder.getUtilsLocation(trigger); + } + } + + if (!utilsLocations.isEmpty()) { Path cpCacheFile = null; try { List urlList = new ArrayList<>(); diff --git a/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplateSetReaderTest.java b/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplateSetReaderTest.java index 9870549a5e..e02004ae57 100644 --- a/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplateSetReaderTest.java +++ b/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplateSetReaderTest.java @@ -9,6 +9,8 @@ import java.nio.file.Path; import java.nio.file.Paths; +import com.devonfw.cobigen.impl.config.ContextConfiguration; +import com.devonfw.cobigen.impl.config.entity.io.TemplateSetConfiguration; import org.apache.commons.io.FileUtils; import org.junit.Ignore; import org.junit.Rule; @@ -54,7 +56,7 @@ public void testErrorOnInvalidConfiguration() throws InvalidConfigurationExcepti // when assertThatThrownBy(() -> { - new TemplateSetConfiguration(TEST_FILE_ROOT_PATH.resolve("faulty")); + new ContextConfiguration(null, null, TEST_FILE_ROOT_PATH.resolve("faulty")); }).isInstanceOf(InvalidConfigurationException.class) .hasMessage(TEST_FILE_ROOT_PATH.resolve("faulty").toAbsolutePath() + ":\n" @@ -74,7 +76,7 @@ public void testInvalidTemplateSets() throws InvalidConfigurationException { assertThatThrownBy(() -> { - new TemplateSetConfiguration(INVALID_CONFIGURATION_PATH); + new ContextConfiguration(null, null,INVALID_CONFIGURATION_PATH); }).isInstanceOf(InvalidConfigurationException.class).hasMessage(INVALID_CONFIGURATION_PATH.toAbsolutePath() + ":\n" + "Could not find any template-set configuration file in the given folder."); @@ -96,7 +98,7 @@ public void testTemplateSetsDuplicatedThrowsError() throws Exception { Path templateSetPathAdapted = TEST_FILE_ROOT_PATH.resolve("valid_template_sets_duplicated"); FileUtils.copyDirectory(templateSetPathAdapted.toFile(), folder); withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> { - TemplateSetConfiguration templateSetConfiguration = new TemplateSetConfiguration( + ContextConfiguration templateSetConfiguration = new ContextConfiguration(null, null, folder.toPath().resolve("template-sets")); // TODO add check for proper exception message }); @@ -115,9 +117,9 @@ public void testTemplateSetsDownloaded() throws Exception { Path templateSetPath = TEST_FILE_ROOT_PATH.resolve("valid_template_sets_downloaded/"); FileUtils.copyDirectory(templateSetPath.toFile(), folder); withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> { - TemplateSetConfiguration templateSetConfiguration = new TemplateSetConfiguration( + ContextConfiguration templateSetConfiguration = new ContextConfiguration(null, null, folder.toPath().resolve("template-sets")); - assertThat(templateSetConfiguration.getTemplatesConfigurations().size()).isEqualTo(1); + assertThat(templateSetConfiguration.getTriggers().size()).isEqualTo(1); }); } @@ -136,10 +138,10 @@ public void testTemplateSetsAdaptedAndDownloaded() throws Exception { FileUtils.copyDirectory(templateSetPath.toFile(), folder); withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> { - TemplateSetConfiguration templateSetConfiguration = new TemplateSetConfiguration( + ContextConfiguration templateSetConfiguration = new ContextConfiguration(null, null, folder.toPath().resolve("template-sets")); - assertThat(templateSetConfiguration.getTemplatesConfigurations().size()).isEqualTo(3); + assertThat(templateSetConfiguration.getTriggers().size()).isEqualTo(3); }); } @@ -161,10 +163,10 @@ public void testGetTemplatesWithInvalidAdaptedFolder() throws Exception { withEnvironmentVariable(ConfigurationConstants.CONFIG_ENV_HOME, folder.getAbsolutePath()).execute(() -> { - TemplateSetConfiguration templateSetConfiguration = new TemplateSetConfiguration( + ContextConfiguration templateSetConfiguration = new ContextConfiguration(null, null, folder.toPath().resolve("template-sets")); - assertThat(templateSetConfiguration.getTemplatesConfigurations().size()).isEqualTo(3); + assertThat(templateSetConfiguration.getTriggers().size()).isEqualTo(3); }); } diff --git a/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplatesConfigurationReaderTest.java b/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplatesConfigurationReaderTest.java index de3f5da8ae..83f5cd4cd1 100644 --- a/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplatesConfigurationReaderTest.java +++ b/cobigen/cobigen-core/src/test/java/com/devonfw/cobigen/unittest/config/reader/TemplatesConfigurationReaderTest.java @@ -11,6 +11,7 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.Map; +import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.junit.Test; @@ -23,7 +24,6 @@ import com.devonfw.cobigen.impl.config.entity.Matcher; import com.devonfw.cobigen.impl.config.entity.Template; import com.devonfw.cobigen.impl.config.entity.Trigger; -import com.devonfw.cobigen.impl.config.entity.io.TemplateExtension; import com.devonfw.cobigen.impl.config.entity.io.TemplateScan; import com.devonfw.cobigen.impl.config.reader.ContextConfigurationReader; import com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader; @@ -49,15 +49,18 @@ public class TemplatesConfigurationReaderTest extends AbstractUnitTest { @Test public void testTemplatesOfAPackageRetrieval() throws Exception { - TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), - "valid"); + TemplatesConfigurationReader target = new TemplatesConfigurationReader( + new File(testFileRootPath).toPath().resolve("valid").resolve("templates.xml")); Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList(), new LinkedList()); Template templateMock = mock(Template.class); HashMap templates = new HashMap<>(); templates.put("resources_resources_spring_common", templateMock); - target.loadIncrements(templates, trigger); + TemplatesConfiguration templatesConfiguration = target.read(trigger); + // templatesConfiguration.loadIncrements(templates, trigger); + Set