Skip to content

Commit c81bb06

Browse files
committed
Merge branch 'dev_core'
2 parents 4c8cfde + 5a553c3 commit c81bb06

File tree

11 files changed

+162
-33
lines changed

11 files changed

+162
-33
lines changed

cobigen/cobigen-core/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<artifactId>cobigen-core</artifactId>
66
<name>CobiGen</name>
7-
<version>2.1.0</version>
7+
<version>2.1.1</version>
88
<packaging>jar</packaging>
99

1010
<parent>
@@ -30,7 +30,7 @@
3030
<dependency>
3131
<groupId>org.freemarker</groupId>
3232
<artifactId>freemarker</artifactId>
33-
<version>2.3.20</version>
33+
<version>2.3.23</version>
3434
</dependency>
3535
<!-- Enables XPath for FreeMarker templates -->
3636
<dependency>

cobigen/cobigen-core/src/main/java/com/capgemini/cobigen/CobiGen.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class CobiGen {
7070
/**
7171
* Current version of the generation, needed for configuration file validation
7272
*/
73-
public static final String CURRENT_VERSION = "2.1.0";
73+
public static final String CURRENT_VERSION = "2.1.1";
7474

7575
/**
7676
* The {@link ContextConfiguration} for this instance
@@ -425,10 +425,9 @@ private List<IncrementTo> convertIncrements(List<Increment> increments, Trigger
425425
templates.add(new TemplateTo(template.getName(), template.getUnresolvedDestinationPath(),
426426
template.getMergeStrategy(), trigger, triggerInterpreter));
427427
}
428-
incrementTos
429-
.add(new IncrementTo(increment.getName(), increment.getDescription(), trigger.getId(),
430-
templates, convertIncrements(increment.getDependentIncrements(), trigger,
431-
triggerInterpreter)));
428+
incrementTos.add(new IncrementTo(increment.getName(), increment.getDescription(),
429+
trigger.getId(), templates, convertIncrements(increment.getDependentIncrements(), trigger,
430+
triggerInterpreter)));
432431
}
433432
return incrementTos;
434433
}

cobigen/cobigen-core/src/main/java/com/capgemini/cobigen/config/reader/TemplatesConfigurationReader.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,9 @@ private void scanTemplates(Path currentDirectory, String currentPath, TemplateSc
320320
+ templateNameWithoutExtension;
321321
if (observedTemplateNames.contains(templateName)) {
322322
throw new InvalidConfigurationException(
323-
"TemplateScan has detected two files with the same file name and thus with the same "
323+
"TemplateScan has detected two files with the same file name ("
324+
+ next.toString()
325+
+ ") and thus with the same "
324326
+ "template name. Continuing would result in an indeterministic behavior.\n"
325327
+ "For now, multiple files with the same name are not supported to be automatically "
326328
+ "configured with templateScans.");

cobigen/cobigen-core/src/main/java/com/capgemini/cobigen/config/resolver/PathExpressionResolver.java

+26-13
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ private void adaptVariables() {
5050

5151
HashMap<String, String> newVariables = new HashMap<>();
5252
for (String var : variables.keySet()) {
53-
newVariables.put(var, variables.get(var).replaceAll("\\.", "/"));
53+
String value = variables.get(var);
54+
if (value != null) {
55+
newVariables.put(var, value.replaceAll("\\.", "/"));
56+
} else {
57+
newVariables.put(var, value);
58+
}
5459
}
5560
variables = newVariables;
5661
}
@@ -91,26 +96,34 @@ public String evaluateExpressions(String in) throws UnknownContextVariableExcept
9196
Matcher m = p.matcher(in);
9297
StringBuffer out = new StringBuffer();
9398
while (m.find()) {
94-
if (variables.get(m.group(1)) == null) {
99+
if (!variables.containsKey(m.group(1))) {
95100
throw new UnknownContextVariableException(m.group(1));
96101
}
97-
if (m.group(2) != null) {
98-
boolean first = true;
99-
String modifiedValue = variables.get(m.group(1));
100-
for (String modifier : m.group(2).split("(\\?|#)")) {
101-
if (first) {
102-
first = false;
103-
continue; // ignore first as always empty due to beginning '?'
102+
103+
if (variables.get(m.group(1)) != null) {
104+
if (m.group(2) != null) {
105+
boolean first = true;
106+
String modifiedValue = variables.get(m.group(1));
107+
for (String modifier : m.group(2).split("(\\?|#)")) {
108+
if (first) {
109+
first = false;
110+
continue; // ignore first as always empty due to beginning '?'
111+
}
112+
modifiedValue = applyStringModifier(modifier, modifiedValue);
104113
}
105-
modifiedValue = applyStringModifier(modifier, modifiedValue);
114+
m.appendReplacement(out, modifiedValue);
115+
} else {
116+
m.appendReplacement(out, variables.get(m.group(1)));
106117
}
107-
m.appendReplacement(out, modifiedValue);
108118
} else {
109-
m.appendReplacement(out, variables.get(m.group(1)));
119+
m.appendReplacement(out, "");
110120
}
111121
}
112122
m.appendTail(out);
113-
return out.toString();
123+
124+
// Cleanup empty path segements
125+
String rawPath = out.toString();
126+
return rawPath.replaceAll("/+", "/");
114127
}
115128

116129
/**

cobigen/cobigen-core/src/main/java/com/capgemini/cobigen/model/JaxenXPathSupportNodeModel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public JaxenXPathSupportNodeModel(Node node) {
4040
try {
4141
useJaxenXPathSupport();
4242
} catch (Exception e) {
43-
LOG.error("{}", "Exception if the Jaxen classes are not present", e);
43+
LOG.error("Exception if the Jaxen classes are not present", e);
4444
}
4545
}
4646

cobigen/cobigen-core/src/main/java/com/capgemini/cobigen/validator/InputValidator.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static void validateInputsUnequalNull(Object... objects) {
7575
}
7676

7777
/**
78-
* Validates a {@link Map} of resolved variables for null keys and values
78+
* Validates a {@link Map} of resolved variables for null keys
7979
* @param resolvedVariables
8080
* to be validated
8181
* @author mbrunnli (10.04.2014)
@@ -90,10 +90,6 @@ public static void validateResolvedVariables(Map<String, String> resolvedVariabl
9090
throw new PluginProcessingException(
9191
"A Plug-In must not add entries with null keys into the resolved variables Map");
9292
}
93-
if (var.getValue() == null) {
94-
throw new PluginProcessingException(
95-
"A Plug-In must not add entries with null values into the resolved variables Map");
96-
}
9793
}
9894
}
9995

cobigen/cobigen-core/src/test/java/com/capgemini/cobigen/systemtest/TemplateScanTest.java

+82-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static org.mockito.internal.matchers.Any.ANY;
1313

1414
import java.io.File;
15+
import java.util.HashMap;
1516
import java.util.List;
1617

1718
import org.apache.commons.lang3.SystemUtils;
@@ -28,7 +29,6 @@
2829
import com.capgemini.cobigen.extension.to.TemplateTo;
2930
import com.capgemini.cobigen.pluginmanager.PluginRegistry;
3031
import com.capgemini.cobigen.systemtest.common.AbstractApiTest;
31-
import com.google.common.collect.ImmutableMap;
3232

3333
/**
3434
* Test suite for template-scan related system tests
@@ -62,10 +62,13 @@ public void testCorrectDestinationResoution() throws Exception {
6262
generationRootFolder.getAbsolutePath());
6363
List<TemplateTo> templates = target.getMatchingTemplates(input);
6464
Assert.assertNotNull(templates);
65-
Assert.assertEquals(1, templates.size());
65+
66+
TemplateTo targetTemplate =
67+
getTemplateById(templates, "prefix_${variables.component#cap_first#replace('1','ONE')}.java");
68+
Assert.assertNotNull(targetTemplate);
6669

6770
// Execution
68-
target.generate(input, templates.get(0), false);
71+
target.generate(input, targetTemplate, false);
6972

7073
// Validation
7174
Assert.assertTrue(new File(generationRootFolder.getAbsolutePath() + SystemUtils.FILE_SEPARATOR
@@ -95,6 +98,76 @@ public void testScanTemplatesFromArchivFile() throws Exception {
9598
assertThat(templates.size(), equalTo(7));
9699
}
97100

101+
/**
102+
* Tests the correct destination resolution for resources obtained by template-scans in the case of an
103+
* empty path element
104+
* @throws Exception
105+
* test fails
106+
* @author mbrunnli (20.12.2015)
107+
*/
108+
@Test
109+
public void testCorrectDestinationResoution_emptyPathElement() throws Exception {
110+
Object input = createTestInputAndConfigureMock();
111+
112+
File generationRootFolder = tmpFolder.newFolder("generationRootFolder");
113+
// Useful to see generates if necessary, comment the generationRootFolder above then
114+
// File generationRootFolder = new File(testFileRootPath + "generates");
115+
116+
// pre-processing
117+
File templatesFolder = new File(testFileRootPath);
118+
CobiGen target = new CobiGen(templatesFolder.toURI());
119+
target.setContextSetting(ContextSetting.GenerationTargetRootPath,
120+
generationRootFolder.getAbsolutePath());
121+
List<TemplateTo> templates = target.getMatchingTemplates(input);
122+
Assert.assertNotNull(templates);
123+
124+
TemplateTo targetTemplate = getTemplateById(templates, "prefix_Test.java");
125+
Assert.assertNotNull(targetTemplate);
126+
127+
// Execution
128+
target.generate(input, targetTemplate, false);
129+
130+
// Validation
131+
Assert.assertTrue(new File(generationRootFolder.getAbsolutePath() + SystemUtils.FILE_SEPARATOR
132+
+ "src" + SystemUtils.FILE_SEPARATOR + "main" + SystemUtils.FILE_SEPARATOR + "java"
133+
+ SystemUtils.FILE_SEPARATOR + "base" + SystemUtils.FILE_SEPARATOR + "Test.java").exists());
134+
}
135+
136+
/**
137+
* Tests the correct destination resolution for resources obtained by template-scans in the case of
138+
* multiple empty path elements
139+
* @throws Exception
140+
* test fails
141+
* @author mbrunnli (20.12.2015)
142+
*/
143+
@Test
144+
public void testCorrectDestinationResoution_emptyPathElements() throws Exception {
145+
Object input = createTestInputAndConfigureMock();
146+
147+
File generationRootFolder = tmpFolder.newFolder("generationRootFolder");
148+
// Useful to see generates if necessary, comment the generationRootFolder above then
149+
// File generationRootFolder = new File(testFileRootPath + "generates");
150+
151+
// pre-processing
152+
File templatesFolder = new File(testFileRootPath);
153+
CobiGen target = new CobiGen(templatesFolder.toURI());
154+
target.setContextSetting(ContextSetting.GenerationTargetRootPath,
155+
generationRootFolder.getAbsolutePath());
156+
List<TemplateTo> templates = target.getMatchingTemplates(input);
157+
Assert.assertNotNull(templates);
158+
159+
TemplateTo targetTemplate = getTemplateById(templates, "prefix_MultiEmpty.java");
160+
Assert.assertNotNull(targetTemplate);
161+
162+
// Execution
163+
target.generate(input, targetTemplate, false);
164+
165+
// Validation
166+
Assert.assertTrue(new File(generationRootFolder.getAbsolutePath() + SystemUtils.FILE_SEPARATOR
167+
+ "src" + SystemUtils.FILE_SEPARATOR + "main" + SystemUtils.FILE_SEPARATOR + "java"
168+
+ SystemUtils.FILE_SEPARATOR + "base" + SystemUtils.FILE_SEPARATOR + "MultiEmpty.java").exists());
169+
}
170+
98171
/**
99172
* Creates simple to debug test data, which includes only one object as input. A
100173
* {@link ITriggerInterpreter TriggerInterpreter} will be mocked with all necessary supplier classes to
@@ -140,16 +213,19 @@ public String toString() {
140213
.thenReturn(true);
141214

142215
// Simulate variable resolving of any plug-in
216+
HashMap<String, String> variables = new HashMap<>(3);
217+
variables.put("rootPackage", "com.capgemini");
218+
variables.put("component", "comp1");
219+
variables.put("detail", null);
220+
143221
when(
144222
matcher.resolveVariables(
145223
argThat(new MatcherToMatcher(equalTo("fqn"), ANY, sameInstance(input))),
146224
argThat(hasItemsInList(
147225
//
148226
new VariableAssignmentToMatcher(equalTo("regex"), equalTo("rootPackage"), equalTo("1")),
149227
new VariableAssignmentToMatcher(equalTo("regex"), equalTo("entityName"), equalTo("3"))))))
150-
.thenReturn(
151-
ImmutableMap.<String, String> builder().put("rootPackage", "com.capgemini")
152-
.put("component", "comp1").build());
228+
.thenReturn(variables);
153229

154230
PluginRegistry.registerTriggerInterpreter(triggerInterpreter);
155231

cobigen/cobigen-core/src/test/java/com/capgemini/cobigen/systemtest/common/AbstractApiTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.capgemini.cobigen.systemtest.common;
22

3+
import java.util.Collection;
4+
35
import org.junit.Rule;
46
import org.junit.rules.TemporaryFolder;
57

8+
import com.capgemini.cobigen.extension.to.TemplateTo;
9+
610
/**
711
*
812
* @author mbrunnli (07.12.2014)
@@ -20,4 +24,22 @@ public class AbstractApiTest {
2024
*/
2125
@Rule
2226
public TemporaryFolder tmpFolder = new TemporaryFolder();
27+
28+
/**
29+
* Search for template by id
30+
* @param templates
31+
* list of templates
32+
* @param id
33+
* to search for
34+
* @return the first template, with the given id or <code>null</code> if not found
35+
* @author mbrunnli (Dec 20, 2015)
36+
*/
37+
public TemplateTo getTemplateById(Collection<TemplateTo> templates, String id) {
38+
for (TemplateTo template : templates) {
39+
if (template.getId().equals(id)) {
40+
return template;
41+
}
42+
}
43+
return null;
44+
}
2345
}

cobigen/cobigen-core/src/test/java/com/capgemini/cobigen/unittest/config/reader/TemplatesConfigurationReaderTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.capgemini.cobigen.unittest.config.reader;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.fail;
46
import static org.mockito.Mockito.mock;
57

68
import java.io.File;
@@ -372,4 +374,23 @@ public void testIncrementComposition_combiningAllPossibleReferences() {
372374
"prefix_scanned", "scanned", "prefix_scanned2");
373375

374376
}
377+
378+
/**
379+
* Test for <a href="https://github.com/devonfw/tools-cobigen/issues/167">Issue 167</a>. Tests if the
380+
* exception message from {@link #testErrorOnDuplicateScannedIds()} contains the name of the file causing
381+
* the exception
382+
*
383+
* @author sholzer (Dec 18, 2015)
384+
*/
385+
@Test
386+
public void testExceptionMessageForDuplicateTemplateNames() {
387+
String message = "";
388+
try {
389+
testErrorOnDuplicateScannedIds();
390+
fail("An Exception should have been thrown");
391+
} catch (Exception e) {
392+
message = e.getMessage();
393+
}
394+
assertFalse(message.indexOf("Bar") == -1);
395+
}
375396
}

cobigen/cobigen-core/src/test/resources/testdata/systemtest/TemplateScanTest/test/templates/base/${variables.detail}/${variables.detail}/MultiEmpty.java.ftl

Whitespace-only changes.

cobigen/cobigen-core/src/test/resources/testdata/systemtest/TemplateScanTest/test/templates/base/${variables.detail}/Test.java.ftl

Whitespace-only changes.

0 commit comments

Comments
 (0)