Skip to content
Snippets Groups Projects
Commit d4656bb2 authored by Christian Wulf's avatar Christian Wulf
Browse files

added tests, fixed grammar bug

parent 2746b287
No related branches found
No related tags found
No related merge requests found
Showing
with 664 additions and 514 deletions
......@@ -15,7 +15,7 @@
</appenders>
<loggers>
<logger name="de.chw.css.script.ProductLineTemplateEngine" level="off" additivity="false">
<logger name="de.chw.css.script.ProductLineGenerator" level="off" additivity="false">
<appender-ref ref="Console" />
<!-- <appender-ref ref="File" /> -->
</logger>
......
......@@ -54,10 +54,11 @@ fragment HEX_DIGIT
| 'a'..'f'
;
COMMENT_START: '/*' NEW_LINE;
COMMENT_END: '*/' NEW_LINE;
TEMPLATE: 'TEMPLATE' NEW_LINE;
TEMPLATE_END: 'TEMPLATE END' NEW_LINE;
COMMENT_START: '/*';
COMMENT_END: '*/';
TEMPLATE: 'TEMPLATE';
TEMPLATE_END: 'TEMPLATE END';
PRODUCTLINE: 'PRODUCTLINE';
VARIABLE: 'VARIABLE';
NEW_LINE
......@@ -69,7 +70,7 @@ NEW_LINE
| '\u2029' //'<Paragraph Separator Character (U+2029)>'
)
;
VALUE: '/*' | '*/' | 'TEMPLATE' | 'TEMPLATE END' | .;
VALUE: .;
//'<A Unicode Character Of Classes Lu, Ll, Lt, Lm, Lo, Or Nl>'
// WARNING: ignores Unicode_escape_sequence
......
......@@ -15,14 +15,14 @@ package de.chw.css.script;
}
css
: product_line_definitions? c=content
: ((COMMENT_START NEW_LINE TEMPLATE) => product_line_definitions)? c=content
-> ^(CSS product_line_definitions? VALUE[$c.text])
;
content: (NEW_LINE | VALUE)+;
content: (COMMENT_START | COMMENT_END | TEMPLATE | TEMPLATE_END | NEW_LINE | VALUE)+;
product_line_definitions
: COMMENT_START! TEMPLATE^ product_lines TEMPLATE_END! COMMENT_END!
: COMMENT_START! NEW_LINE! TEMPLATE^ NEW_LINE! product_lines TEMPLATE_END! NEW_LINE! COMMENT_END! NEW_LINE!
;
product_lines: product_line*;
......@@ -41,4 +41,4 @@ variable_declaration
identifier: VALUE+;
values: (NEW_LINE | VALUE)+;
\ No newline at end of file
values: (COMMENT_START | COMMENT_END | NEW_LINE | VALUE)+;
\ No newline at end of file
/*
TEMPLATE
PRODUCTLINE default
VARIABLE groupId
cockpit
PRODUCTLINE fm
VARIABLE groupId
fmcockpit
PRODUCTLINE bm
VARIABLE groupId
bmcockpit
TEMPLATE END
*/
dataSource {
pooled = true
driverClassName = "org.postgresql.Driver"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
//url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
//driverClassName = "org.h2.Driver"
//username = "sa"
//password = ""
url = "jdbc:postgresql://127.0.0.1:5432//* groupId */"
username = "postgres"
password = "ESNAdmin2012"
dialect = org.hibernatespatial.postgis.PostgisDialect
}
}
test {
dataSource {
dbCreate = "create-drop"
url = "jdbc:postgresql://127.0.0.1:5432//* groupId */"
username = "postgres"
password = "ESNAdmin2012"
dialect = org.hibernatespatial.postgis.PostgisDialect
}
}
production {
dataSource {
dbCreate = "create-drop"
url = "jdbc:postgresql://127.0.0.1:5432//* groupId */"
username = "postgres"
password = "ESNAdmin2012"
dialect = org.hibernatespatial.postgis.PostgisDialect
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}
/* Added by the Hibernate Spatial Plugin. */
dataSource {
dialect = org.hibernatespatial.postgis.PostgisDialect
}
dataSource {
pooled = true
driverClassName = "org.postgresql.Driver"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
//url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
//driverClassName = "org.h2.Driver"
//username = "sa"
//password = ""
url = "jdbc:postgresql://127.0.0.1:5432//* groupId */"
username = "postgres"
password = "ESNAdmin2012"
dialect = org.hibernatespatial.postgis.PostgisDialect
}
}
test {
dataSource {
dbCreate = "create-drop"
url = "jdbc:postgresql://127.0.0.1:5432//* groupId */"
username = "postgres"
password = "ESNAdmin2012"
dialect = org.hibernatespatial.postgis.PostgisDialect
}
}
production {
dataSource {
dbCreate = "create-drop"
url = "jdbc:postgresql://127.0.0.1:5432//* groupId */"
username = "postgres"
password = "ESNAdmin2012"
dialect = org.hibernatespatial.postgis.PostgisDialect
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}
/* Added by the Hibernate Spatial Plugin. */
dataSource {
dialect = org.hibernatespatial.postgis.PostgisDialect
}
dataSource {
pooled = true
driverClassName = "org.postgresql.Driver"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
//url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
//driverClassName = "org.h2.Driver"
//username = "sa"
//password = ""
url = "jdbc:postgresql://127.0.0.1:5432/fmcockpit"
username = "postgres"
password = "ESNAdmin2012"
dialect = org.hibernatespatial.postgis.PostgisDialect
}
}
test {
dataSource {
dbCreate = "create-drop"
url = "jdbc:postgresql://127.0.0.1:5432/fmcockpit"
username = "postgres"
password = "ESNAdmin2012"
dialect = org.hibernatespatial.postgis.PostgisDialect
}
}
production {
dataSource {
dbCreate = "create-drop"
url = "jdbc:postgresql://127.0.0.1:5432/fmcockpit"
username = "postgres"
password = "ESNAdmin2012"
dialect = org.hibernatespatial.postgis.PostgisDialect
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}
/* Added by the Hibernate Spatial Plugin. */
dataSource {
dialect = org.hibernatespatial.postgis.PostgisDialect
}
......@@ -4,9 +4,16 @@ import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ProductLineGenerator {
private final static Logger LOG = LogManager.getLogger(ProductLineGenerator.class);
private String content;
private Map<String, String> variables;
......@@ -18,13 +25,24 @@ public class ProductLineGenerator {
/**
*
* @param pattern
* including the keyword var that is replaced by the particular
* variable names
* including the keyword <code>var</code> that is replaced by the
* particular variable names
*/
public void replaceVariables(String pattern) {
for (String key : variables.keySet()) {
String regex = pattern.replace("var", key);
content = content.replaceAll(regex, variables.get(key));
// we do not use a simple String.replaceAll, because we want to log the single replacements
Matcher matcher = Pattern.compile(regex).matcher(content);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String group = matcher.group();
String replacement = variables.get(key);
matcher.appendReplacement(sb, replacement);
LOG.debug("Replaced: " + group + "\n\tby " + replacement);
}
matcher.appendTail(sb);
content = sb.toString();
}
}
......
......@@ -13,6 +13,8 @@ import org.apache.logging.log4j.Logger;
public class ProductLineTemplateEngine {
private final static Logger LOG = LogManager.getLogger(ProductLineTemplateEngine.class);
// patterns for predicate and corresponding generate methods
private static final String BINARY_FILENAME_PATTERN = "_template_default\\.(\\w+)\\z";
private String chosenProduct;
......@@ -28,11 +30,13 @@ public class ProductLineTemplateEngine {
return (success) ? outputFilename : null;
}
public void generateBinaryFile(File file) throws IOException {
String regex = "_template_default\\.(\\w+)\\z";
public boolean isBinaryFile(File file) {
return file.getName().matches(BINARY_FILENAME_PATTERN);
}
public void generateBinaryFile(File file) throws IOException {
final String defaultTemplateFilename = file.getAbsolutePath();
Matcher matcher = Pattern.compile(regex).matcher(defaultTemplateFilename);
Matcher matcher = Pattern.compile(BINARY_FILENAME_PATTERN).matcher(defaultTemplateFilename);
String templateFilename = matcher.replaceFirst("_template_" + chosenProduct + ".$1");
String outputFilename = matcher.replaceFirst(".$1");
......
......@@ -12,7 +12,7 @@ import org.antlr.runtime.RecognitionException;
import org.junit.Test;
public class CssTemplateEngineTest {
public class CssTemplateEngineLexerTest {
@Test
public void testStandardCss() throws IOException, RecognitionException {
......@@ -37,7 +37,7 @@ public class CssTemplateEngineTest {
List<?> tokens = tokenStream.getTokens();
// assertEquals(3, tokens.size());
assertEquals(189, tokens.size());
assertEquals(193, tokens.size());
assertEquals(CssTemplateEngineLexer.COMMENT_START, ((CommonToken) tokens.get(0)).getType());
// assertEquals(CssTemplateEngineLexer.TEMPLATE, ((CommonToken) tokens.get(1)).getType());
// TODO
......
package de.chw.css.script;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import org.antlr.runtime.RecognitionException;
......@@ -11,34 +12,46 @@ import de.chw.util.TestHelper;
public class ProductLineGeneratorTest {
private void testProductLineGenerator(String filenamePrefix) throws IOException {
ProductLineTemplateReader reader = new ProductLineTemplateReader(filenamePrefix + ".css", "fm");
private void testProductLineGenerator(final String filenamePrefix, final String ext) throws IOException {
final String generatedFilename = filenamePrefix + "_generated" + "." + ext;
ProductLineTemplateReader reader = new ProductLineTemplateReader(filenamePrefix + "." + ext, "fm");
String outputContent = reader.getContent();
ProductLineGenerator generator = new ProductLineGenerator(outputContent, reader.getVariables());
generator.replaceVariables("/\\*var\\*/");
generator.generateFile(filenamePrefix + "_generated.css");
generator.replaceVariables("/\\*\\s*var\\s*\\*/");
try {
generator.generateFile(generatedFilename);
String readContent = TestHelper.readTextFile(filenamePrefix + "_expected.css");
String readContent = TestHelper.readTextFile(filenamePrefix + "_expected" + "." + ext);
assertEquals(readContent, generator.getContent());
} finally {
// clean up
new File(generatedFilename).delete();
}
}
@Test
public void testStandardCss() throws IOException {
testProductLineGenerator("resource/standard");
testProductLineGenerator("resource/standard", "css");
}
@Test
public void testTemplateCss() throws Exception {
testProductLineGenerator("resource/template");
testProductLineGenerator("resource/template", "css");
}
@Test
public void testTwoProductLines() throws IOException, RecognitionException {
testProductLineGenerator("resource/two-pls");
testProductLineGenerator("resource/two-pls", "css");
}
@Test
public void testMultipleVariables() throws IOException, RecognitionException {
testProductLineGenerator("resource/multiple-vars", "css");
}
@Test
public void testTwoVariables() throws IOException, RecognitionException {
testProductLineGenerator("resource/multiple-vars");
public void testMultipleReplacements() throws IOException, RecognitionException {
testProductLineGenerator("resource/DataSource", "groovy");
}
}
package de.chw.css.script;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import de.chw.util.TestHelper;
public class ProductLineTemplateReaderTest {
@Test
public void testReadingContentCorrectly() throws IOException {
final String filenamePrefix = "DataSource";
final String ext = "groovy";
String filename = "resource/" + filenamePrefix + "." + ext;
String chosenProduct = "fm";
ProductLineTemplateReader reader = new ProductLineTemplateReader(filename, chosenProduct);
String expected = TestHelper.readTextFile("resource/" + filenamePrefix + "_content_expected" + "." + ext);
assertEquals(expected, reader.getContent());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment