Search in sources :

Example 1 with ConfigurationHolder

use of com.devonfw.cobigen.impl.config.ConfigurationHolder in project cobigen by devonfw.

the class TemplatesConfigurationReaderTest method testTemplateRefOutsideCurrentFile.

/**
 * Tests the correct resolution of TemplateRef from outside the current templates file.
 */
@Test
public void testTemplateRefOutsideCurrentFile() {
    // given
    Trigger trigger = new Trigger("testingTrigger", "asdf", "valid_external_templateref", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    Path configPath = Paths.get(new File(testFileRootPath).toURI());
    ConfigurationHolder configurationHolder = new ConfigurationHolder(configPath.toUri());
    TemplatesConfiguration templatesConfiguration = configurationHolder.readTemplatesConfiguration(trigger);
    Map<String, Increment> increments = templatesConfiguration.getIncrements();
    assertThat(templatesConfiguration.getTrigger().getId()).isEqualTo("testingTrigger");
    // validation
    Increment incrementThree = increments.get("3");
    LinkedList<String> templateNamesThree = new LinkedList<>();
    for (Template tmplate : incrementThree.getTemplates()) {
        templateNamesThree.add(tmplate.getName());
    }
    assertThat(templateNamesThree).containsExactly("templateDecl");
    Increment incrementFour = increments.get("4");
    LinkedList<String> templateNamesFour = new LinkedList<>();
    for (Template tmplate : incrementFour.getTemplates()) {
        templateNamesFour.add(tmplate.getName());
    }
    assertThat(templateNamesFour).containsExactly("ExplicitlyDefined");
}
Also used : Path(java.nio.file.Path) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) TemplatesConfiguration(com.devonfw.cobigen.impl.config.TemplatesConfiguration) LinkedList(java.util.LinkedList) Template(com.devonfw.cobigen.impl.config.entity.Template) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Increment(com.devonfw.cobigen.impl.config.entity.Increment) File(java.io.File) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 2 with ConfigurationHolder

use of com.devonfw.cobigen.impl.config.ConfigurationHolder in project cobigen by devonfw.

the class TemplatesConfigurationReaderTest method testInvalidTemplateRefOutsideCurrentFile.

/**
 * Tests the correct detection of invalid external increment reference.
 *
 * @throws InvalidConfigurationException expected
 */
@Test(expected = InvalidConfigurationException.class)
public void testInvalidTemplateRefOutsideCurrentFile() {
    Path config = Paths.get(new File(testFileRootPath).toURI());
    new ContextConfigurationReader(config);
    // given
    ConfigurationHolder configurationHolder = new ConfigurationHolder(config.toUri());
    TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "faulty_invalid_external_templateref", configurationHolder);
    Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    // when
    Map<String, Template> templates = target.loadTemplates(trigger);
    target.loadIncrements(templates, trigger);
}
Also used : Path(java.nio.file.Path) ContextConfigurationReader(com.devonfw.cobigen.impl.config.reader.ContextConfigurationReader) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) Template(com.devonfw.cobigen.impl.config.entity.Template) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 3 with ConfigurationHolder

use of com.devonfw.cobigen.impl.config.ConfigurationHolder in project cobigen by devonfw.

the class TemplatesConfigurationReaderTest method testInvalidIncrementRefOutsideCurrentFile.

/**
 * Tests the correct detection of invalid external increment reference.
 *
 * @throws InvalidConfigurationException expected
 */
@Test(expected = InvalidConfigurationException.class)
public void testInvalidIncrementRefOutsideCurrentFile() {
    Path config = Paths.get(new File(testFileRootPath).toURI());
    new ContextConfigurationReader(config);
    // given
    ConfigurationHolder configurationHolder = new ConfigurationHolder(config.toUri());
    TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "faulty_invalid_external_incrementref", configurationHolder);
    Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    // when
    Map<String, Template> templates = target.loadTemplates(trigger);
    target.loadIncrements(templates, trigger);
}
Also used : Path(java.nio.file.Path) ContextConfigurationReader(com.devonfw.cobigen.impl.config.reader.ContextConfigurationReader) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) Template(com.devonfw.cobigen.impl.config.entity.Template) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 4 with ConfigurationHolder

use of com.devonfw.cobigen.impl.config.ConfigurationHolder in project cobigen by devonfw.

the class TemplateClassTest method testResolveUtilClassesFromJarArchive.

/**
 * Tests if the template utility classes can be loaded from a jar archive
 *
 * @throws IOException test fails
 */
@Test
public void testResolveUtilClassesFromJarArchive() throws IOException {
    String filename = "archive";
    Path path = Paths.get(TEST_FILES_ROOT_PATH + filename + File.separator + "templates.jar");
    ClassLoader inputClassLoader = URLClassLoader.newInstance(new URL[] { path.toUri().toURL() }, getClass().getClassLoader());
    List<Class<?>> classes = ConfigurationClassLoaderUtil.resolveUtilClasses(new ConfigurationHolder(path.toUri()), inputClassLoader);
    assertThat(classes).isNotEmpty();
    assertThat(classes.get(0).getName()).contains("IDGenerator");
}
Also used : TemplatePath(com.devonfw.cobigen.impl.config.entity.TemplatePath) Path(java.nio.file.Path) URLClassLoader(java.net.URLClassLoader) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) Test(org.junit.Test) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest)

Example 5 with ConfigurationHolder

use of com.devonfw.cobigen.impl.config.ConfigurationHolder in project cobigen by devonfw.

the class CobiGenFactory method create.

/**
 * Creates a new {@link CobiGen} while searching a valid configuration at the given path
 *
 * @param configFileOrFolder the root folder containing the context.xml and all templates, configurations etc.
 * @return a new instance of {@link CobiGen}
 * @throws InvalidConfigurationException if the context configuration could not be read properly.
 */
public static CobiGen create(URI configFileOrFolder) throws InvalidConfigurationException {
    Objects.requireNonNull(configFileOrFolder, "The URI pointing to the configuration could not be null.");
    ConfigurationHolder configurationHolder = new ConfigurationHolder(configFileOrFolder);
    BeanFactory beanFactory = new BeanFactory();
    beanFactory.addManuallyInitializedBean(configurationHolder);
    CobiGen createBean = beanFactory.createBean(CobiGen.class);
    // Notifies all plugins of new template root path
    PluginRegistry.notifyPlugins(configurationHolder.getConfigurationPath());
    return createBean;
}
Also used : BeanFactory(com.devonfw.cobigen.impl.aop.BeanFactory) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) CobiGen(com.devonfw.cobigen.api.CobiGen)

Aggregations

ConfigurationHolder (com.devonfw.cobigen.impl.config.ConfigurationHolder)8 AbstractUnitTest (com.devonfw.cobigen.unittest.config.common.AbstractUnitTest)7 Path (java.nio.file.Path)7 Test (org.junit.Test)7 ContainerMatcher (com.devonfw.cobigen.impl.config.entity.ContainerMatcher)4 Matcher (com.devonfw.cobigen.impl.config.entity.Matcher)4 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)4 File (java.io.File)4 Template (com.devonfw.cobigen.impl.config.entity.Template)3 TemplatePath (com.devonfw.cobigen.impl.config.entity.TemplatePath)3 TemplatesConfiguration (com.devonfw.cobigen.impl.config.TemplatesConfiguration)2 Increment (com.devonfw.cobigen.impl.config.entity.Increment)2 ContextConfigurationReader (com.devonfw.cobigen.impl.config.reader.ContextConfigurationReader)2 TemplatesConfigurationReader (com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader)2 URLClassLoader (java.net.URLClassLoader)2 CobiGen (com.devonfw.cobigen.api.CobiGen)1 BeanFactory (com.devonfw.cobigen.impl.aop.BeanFactory)1 LinkedList (java.util.LinkedList)1