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

tested generator

parent 43f6902e
No related branches found
No related tags found
No related merge requests found
Showing
with 450 additions and 8 deletions
...@@ -9,5 +9,6 @@ ...@@ -9,5 +9,6 @@
<classpathentry kind="lib" path="lib/log4j-api-2.0-beta3.jar"/> <classpathentry kind="lib" path="lib/log4j-api-2.0-beta3.jar"/>
<classpathentry kind="lib" path="lib/log4j-core-2.0-beta3.jar"/> <classpathentry kind="lib" path="lib/log4j-core-2.0-beta3.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>
bin bin
/log.log /log.log
*_generated.*
\ No newline at end of file
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
</buildCommand> </buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.eclipse.jdt.groovy.core.groovyNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.deved.antlride.core.nature</nature> <nature>org.deved.antlride.core.nature</nature>
</natures> </natures>
......
This diff is collapsed.
eclipse.preferences.version=1
groovy.compiler.level=18
eclipse.preferences.version=1
formatter_profile=_chw
formatter_settings_version=12
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
</appenders> </appenders>
<loggers> <loggers>
<logger name="de.chw.css.script" level="debug" additivity="false"> <logger name="de.chw.css.script" level="off" additivity="false">
<appender-ref ref="Console" /> <appender-ref ref="Console" />
<!-- <appender-ref ref="File" /> --> <!-- <appender-ref ref="File" /> -->
</logger> </logger>
......
...@@ -5,6 +5,8 @@ TEMPLATE ...@@ -5,6 +5,8 @@ TEMPLATE
#fafafa #fafafa
VARIABLE bordercolor VARIABLE bordercolor
#2884C4 #2884C4
VARIABLE text-align
center
PRODUCTLINE bm PRODUCTLINE bm
VARIABLE bgcolor VARIABLE bgcolor
#ffffff #ffffff
...@@ -15,7 +17,7 @@ TEMPLATE END ...@@ -15,7 +17,7 @@ TEMPLATE END
background: /*bgcolor*/; background: /*bgcolor*/;
border-color: #2884C4; border-color: #2884C4;
padding-left: 10px; padding-left: 10px;
text-align: center; text-align: /*text-align*/;
} }
.ui-widget-content { .ui-widget-content {
......
.ui-widget-header {
background: #fafafa;
border-color: #2884C4;
padding-left: 10px;
text-align: center;
}
.ui-widget-content {
background: #ffffff;
}
\ No newline at end of file
.ui-widget-header {
background: #2884C4;
border-color: #2884C4;
padding-left: 10px;
text-align: center;
}
.ui-widget-content {
background: #ffffff;
}
\ No newline at end of file
.ui-widget-header {
background: #fafafa;
border-color: #2884C4;
padding-left: 10px;
text-align: center;
}
.ui-widget-content {
background: #ffffff;
}
\ No newline at end of file
.ui-widget-header {
background: #fafafa;
border-color: #2884C4;
padding-left: 10px;
text-align: center;
}
.ui-widget-content {
background: #ffffff;
}
\ No newline at end of file
package de.chw.css.script;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
public class ProductLineGenerator {
private String content;
private Map<String, String> variables;
public ProductLineGenerator(String content, Map<String, String> variables) {
this.content = content;
this.variables = variables;
}
public void replaceVariables() {
for (String key : variables.keySet()) {
content = content.replaceAll("/\\*" + key + "\\*/", variables.get(key));
}
}
public void generateFile(String filename) throws IOException {
FileWriter fw = new FileWriter(filename);
try {
fw.write(content);
} finally {
fw.close();
}
}
public String getContent() {
return content;
}
}
...@@ -42,15 +42,16 @@ public class CssTemplateEngineParserTest { ...@@ -42,15 +42,16 @@ public class CssTemplateEngineParserTest {
} }
@Test @Test
public void testTwoVariables() throws IOException, RecognitionException { public void testMultipleVariables() throws IOException, RecognitionException {
String filename = "resource/two-vars.css"; String filename = "resource/multiple-vars.css";
ProductLineTemplateReader engine = new ProductLineTemplateReader(filename, "fm"); ProductLineTemplateReader engine = new ProductLineTemplateReader(filename, "fm");
assertEquals(2, engine.getVariables().size()); assertEquals(3, engine.getVariables().size());
assertEquals("#fafafa", engine.getVariables().get("bgcolor")); assertEquals("#fafafa", engine.getVariables().get("bgcolor"));
assertEquals("#2884C4", engine.getVariables().get("bordercolor")); assertEquals("#2884C4", engine.getVariables().get("bordercolor"));
assertEquals("center", engine.getVariables().get("text-align"));
assertEquals(170, engine.getContent().length()); assertEquals(178, engine.getContent().length());
} }
} }
...@@ -21,7 +21,7 @@ public class CssTemplateEngineTest { ...@@ -21,7 +21,7 @@ public class CssTemplateEngineTest {
CommonTokenStream tokenStream = new CommonTokenStream(lexer); CommonTokenStream tokenStream = new CommonTokenStream(lexer);
tokenStream.fill(); tokenStream.fill();
List tokens = tokenStream.getTokens(); List<?> tokens = tokenStream.getTokens();
System.out.println("tokens: " + tokens); System.out.println("tokens: " + tokens);
// assertEquals(2, tokens.size()); // assertEquals(2, tokens.size());
assertEquals(156, tokens.size()); assertEquals(156, tokens.size());
...@@ -36,7 +36,7 @@ public class CssTemplateEngineTest { ...@@ -36,7 +36,7 @@ public class CssTemplateEngineTest {
CommonTokenStream tokenStream = new CommonTokenStream(lexer); CommonTokenStream tokenStream = new CommonTokenStream(lexer);
tokenStream.fill(); tokenStream.fill();
List tokens = tokenStream.getTokens(); List<?> tokens = tokenStream.getTokens();
System.out.println("tokens: " + tokens); System.out.println("tokens: " + tokens);
// assertEquals(3, tokens.size()); // assertEquals(3, tokens.size());
assertEquals(189, tokens.size()); assertEquals(189, tokens.size());
......
package de.chw.css.script;
import static org.junit.Assert.*;
import java.io.IOException;
import org.antlr.runtime.RecognitionException;
import org.junit.Test;
import de.chw.util.TestHelper;
public class ProductLineGeneratorTest {
private void testProductLineGenerator(String filenamePrefix) throws IOException {
ProductLineTemplateReader reader = new ProductLineTemplateReader(filenamePrefix + ".css", "fm");
String outputContent = reader.getContent();
ProductLineGenerator generator = new ProductLineGenerator(outputContent, reader.getVariables());
generator.replaceVariables();
generator.generateFile(filenamePrefix + "_generated.css");
String readContent = TestHelper.readTextFile(filenamePrefix + "_expected.css");
assertEquals(readContent, generator.getContent());
}
@Test
public void testStandardCss() throws IOException {
testProductLineGenerator("resource/standard");
}
@Test
public void testTemplateCss() throws Exception {
testProductLineGenerator("resource/template");
}
@Test
public void testTwoProductLines() throws IOException, RecognitionException {
testProductLineGenerator("resource/two-pls");
}
@Test
public void testTwoVariables() throws IOException, RecognitionException {
testProductLineGenerator("resource/multiple-vars");
}
}
package de.chw.util;
import java.io.FileReader;
import java.io.IOException;
public class TestHelper {
protected TestHelper() {
// utility class
}
public static String readTextFile(String filename) throws IOException {
char[] cbuf = new char[1024];
int len;
StringBuilder builder = new StringBuilder();
FileReader reader = new FileReader(filename);
try {
while ((len = reader.read(cbuf)) != -1) {
builder.append(cbuf, 0, len);
}
} finally {
reader.close();
}
return builder.toString();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment