Skip to content

Instantly share code, notes, and snippets.

@rapodaca
Created February 29, 2012 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rapodaca/1943633 to your computer and use it in GitHub Desktop.
Save rapodaca/1943633 to your computer and use it in GitHub Desktop.
Failing template render from STRawGroupDir and StringTemplate 4.0.5
package com.kemga.framework;
import static org.junit.Assert.assertEquals;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STRawGroupDir;
public class TestTest {
public static final String tmpdir = System.getProperty("java.io.tmpdir");
/**
* This passes with ST-4.0.5
*/
@Test
public void testTemplateFromMemory() {
ST template = new ST("$values:{foo|[$foo$]}$", '$', '$');
List<String> values = new ArrayList<String>();
values.add("one");
values.add("two");
values.add("three");
template.add("values", values);
assertEquals("[one][two][three]", template.render());
}
/**
* This fails with exception ST-4.0.5:
* context [template] 1:1 no such template: /_sub1
* context [template] 1:8 passed 1 arg(s) to template null with 0 declared arg(s)
* context [template] 1:1 passed 1 arg(s) to template null with 0 declared arg(s)
* context [template] 1:1 passed 1 arg(s) to template null with 0 declared arg(s)
* context [template] 1:1 passed 1 arg(s) to template null with 0 declared arg(s)
*/
@Test public void testSTRawGroupDir() {
String dir = getRandomDir();
writeFile(dir, "template.st", "$values:{foo|[$foo$]}$");
STGroup group = new STRawGroupDir(dir, '$', '$');
ST template = group.getInstanceOf("template");
List<String> values = new ArrayList<String>();
values.add("one");
values.add("two");
values.add("three");
template.add("values", values);
assertEquals("[one][two][three]", template.render());
}
public static String getRandomDir() {
String randomDir = tmpdir+"dir"+String.valueOf((int)(Math.random()*100000));
File f = new File(randomDir);
f.mkdirs();
return randomDir;
}
public static void writeFile(String dir, String fileName, String content) {
try {
File f = new File(dir, fileName);
if ( !f.getParentFile().exists() ) f.getParentFile().mkdirs();
FileWriter w = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(w);
bw.write(content);
bw.close();
w.close();
}
catch (IOException ioe) {
System.err.println("can't write file");
ioe.printStackTrace(System.err);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment