Update of /cvsroot/webmacro/webmacro/test/unit/org/webmacro/template In directory sc8-pr-cvs1:/tmp/cvs-serv6675/test/unit/org/webmacro/template Modified Files: AbstractVariableTestCase.java EncodingTestCase.java PerformanceOperations.java PrivateValue.java PublicValue.java TemplateTestCase.java TestAbuse.java TestAlternate.java TestArithmetic.java TestBlocks.java TestCountDirective.java TestCrankyEEH.java TestDefaultEEH.java TestDirectiveParser.java TestFunction.java TestGetSet.java TestISO88591Encoding.java TestIf.java TestLineNumbers.java TestList.java TestMacro.java TestNastySebastianTemplate.java TestParseInclude.java TestPerformanceAreas.java TestQuote.java TestSetblock.java TestShortCircuit.java TestSyntheticTemplate.java TestUndefined.java TestWhitespace.java TestWindows1251Encoding.java Log Message: If this doesn't drive our SF "activity" stats through the roof, I don't know what will. Mass re-formatting of all code, following (to the best of my interpertation) the Sun (w/ jakarta tweaks) coding style guidelines defined here: http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html and here: http://jakarta.apache.org/turbine/common/code-standards.html I did this reformatting with IDEA 3.0.5, and I am going to commit the style configuration for IDEA (if I can find it). Index: AbstractVariableTestCase.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/test/unit/org/webmacro/template/AbstractVariableTestCase.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** AbstractVariableTestCase.java 23 Mar 2003 02:00:37 -0000 1.5 --- AbstractVariableTestCase.java 12 Jun 2003 00:47:49 -0000 1.6 *************** *** 1,10 **** package org.webmacro.template; - import java.io.*; - import org.webmacro.*; - import org.webmacro.engine.StringTemplate; - - import junit.framework.*; /** abstract variable test case for ROGUE variables/properties --- 1,5 ---- *************** *** 12,150 **** It is expected that atleast 3 subclasses will exist: TestDefaultEEH, TestCrankyEEH, TestComplacentEEH ! */ ! public abstract class AbstractVariableTestCase extends TemplateTestCase { ! public static class ObjectWithNullMethod { ! public Object getNull () { ! return null; ! } } - public AbstractVariableTestCase (String name) { - super (name); - } ! public void testGoodVariable () throws Exception { ! assertTrue(false); ! } - public void testGoodMethod () throws Exception { - assertTrue(false); - } ! public void testGoodProperty () throws Exception { ! assertTrue(false); ! } - public void testNoSuchVariable () throws Exception { - assertTrue(false); - } ! public void testNoSuchMethod () throws Exception { ! assertTrue(false); ! } - public void testNoSuchProperty () throws Exception { - assertTrue(false); - } ! public void testVoidMethod () throws Exception { ! assertTrue(false); ! } - public void testNullMethod () throws Exception { - assertTrue(false); - } ! public void testThrowsMethod() throws Exception { ! assertTrue(false); ! } - /** A variable in the context who's .toString() method returns null */ - public void testNullVariable () throws Exception { - assertTrue(false); - } - public void testEvalGoodVariable () throws Exception { - assertTrue(false); - } ! public void testEvalGoodMethod () throws Exception { ! assertTrue(false); ! } - public void testEvalGoodProperty () throws Exception { - assertTrue(false); - } ! public void testEvalNoSuchVariable () throws Exception { ! assertTrue(false); ! } - public void testEvalNoSuchMethod () throws Exception { - assertTrue(false); - } - - public void testNoSuchMethodWithArguments () throws Exception { - assertTrue(false); - } ! public void testEvalNoSuchMethodWithArguments () throws Exception { ! assertTrue(false); ! } - public void testEvalNoSuchProperty () throws Exception { - assertTrue(false); - } ! public void testEvalVoidMethod () throws Exception { ! assertTrue(false); ! } - public void testEvalNullMethod () throws Exception { - assertTrue(false); - } ! public void testEvalThrowsMethod() throws Exception { ! assertTrue(false); ! } - public void testEvalNullVariable () throws Exception { - assertTrue(false); - } ! /** ! * This is the old "... --possibly null?" error message. ! * It has always thrown a PropertyExcpetion, and probably still should. ! */ ! public void testEvaluationOfNullReturnValue() throws Exception { ! assertTrue(false); ! } ! /** Below are a few objects that should be used to ! do the actual testing */ - public static class TestObject { ! public String property = "Property"; ! ! public String toString () { return "TestObject"; } ! public String getString () { return "String"; } ! public void voidMethod () { ; } - public Object nullMethod () { return null; } ! public void throwException() throws Exception { ! throw new Exception ("GoodTestObject.throwException() was called"); ! } ! } ! public static class NullTestObject extends TestObject { ! public String toString () { return null; } ! } } --- 7,218 ---- It is expected that atleast 3 subclasses will exist: TestDefaultEEH, TestCrankyEEH, TestComplacentEEH ! */ ! public abstract class AbstractVariableTestCase extends TemplateTestCase ! { ! public static class ObjectWithNullMethod ! { ! public Object getNull () ! { ! return null; ! } } ! public AbstractVariableTestCase (String name) ! { ! super(name); ! } ! public void testGoodVariable () throws Exception ! { ! assertTrue(false); ! } ! public void testGoodMethod () throws Exception ! { ! assertTrue(false); ! } ! public void testGoodProperty () throws Exception ! { ! assertTrue(false); ! } ! public void testNoSuchVariable () throws Exception ! { ! assertTrue(false); ! } + public void testNoSuchMethod () throws Exception + { + assertTrue(false); + } ! public void testNoSuchProperty () throws Exception ! { ! assertTrue(false); ! } ! public void testVoidMethod () throws Exception ! { ! assertTrue(false); ! } ! public void testNullMethod () throws Exception ! { ! assertTrue(false); ! } ! public void testThrowsMethod () throws Exception ! { ! assertTrue(false); ! } ! /** A variable in the context who's .toString() method returns null */ ! public void testNullVariable () throws Exception ! { ! assertTrue(false); ! } ! public void testEvalGoodVariable () throws Exception ! { ! assertTrue(false); ! } ! public void testEvalGoodMethod () throws Exception ! { ! assertTrue(false); ! } ! ! ! public void testEvalGoodProperty () throws Exception ! { ! assertTrue(false); ! } ! ! ! public void testEvalNoSuchVariable () throws Exception ! { ! assertTrue(false); ! } ! ! ! public void testEvalNoSuchMethod () throws Exception ! { ! assertTrue(false); ! } ! ! ! public void testNoSuchMethodWithArguments () throws Exception ! { ! assertTrue(false); ! } ! ! ! public void testEvalNoSuchMethodWithArguments () throws Exception ! { ! assertTrue(false); ! } ! ! ! public void testEvalNoSuchProperty () throws Exception ! { ! assertTrue(false); ! } ! ! ! public void testEvalVoidMethod () throws Exception ! { ! assertTrue(false); ! } ! ! ! public void testEvalNullMethod () throws Exception ! { ! assertTrue(false); ! } ! ! ! public void testEvalThrowsMethod () throws Exception ! { ! assertTrue(false); ! } ! ! ! public void testEvalNullVariable () throws Exception ! { ! assertTrue(false); ! } ! ! ! /** ! * This is the old "... --possibly null?" error message. ! * It has always thrown a PropertyExcpetion, and probably still should. */ + public void testEvaluationOfNullReturnValue () throws Exception + { + assertTrue(false); + } ! /** Below are a few objects that should be used to ! do the actual testing ! */ ! public static class TestObject ! { ! public String property = "Property"; ! public String toString () ! { ! return "TestObject"; ! } ! ! public String getString () ! { ! return "String"; ! } ! ! ! public void voidMethod () ! { ! ; ! } ! ! ! public Object nullMethod () ! { ! return null; ! } ! ! ! public void throwException () throws Exception ! { ! throw new Exception("GoodTestObject.throwException() was called"); ! } ! } ! ! public static class NullTestObject extends TestObject ! { ! public String toString () ! { ! return null; ! } ! } } Index: EncodingTestCase.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/test/unit/org/webmacro/template/EncodingTestCase.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** EncodingTestCase.java 23 Mar 2003 02:00:37 -0000 1.3 --- EncodingTestCase.java 12 Jun 2003 00:47:49 -0000 1.4 *************** *** 1,9 **** package org.webmacro.template; ! import java.io.*; ! import org.webmacro.*; ! import junit.framework.*; /** --- 1,8 ---- package org.webmacro.template; ! import junit.framework.TestCase; import org.webmacro.*; ! import java.io.*; /** *************** *** 22,26 **** * @author Sebastian Kanthak */ ! public abstract class EncodingTestCase extends TestCase { public static final String TEMPLATE_FILENAME = "org.webmacro.template.TestEncoding.wm"; public static final String TEMPLATE_FILENAME_UTF8 = "org.webmacro.templateTestEncoding-utf8.wm"; --- 21,26 ---- * @author Sebastian Kanthak */ ! public abstract class EncodingTestCase extends TestCase ! { public static final String TEMPLATE_FILENAME = "org.webmacro.template.TestEncoding.wm"; public static final String TEMPLATE_FILENAME_UTF8 = "org.webmacro.templateTestEncoding-utf8.wm"; *************** *** 32,41 **** protected String expected; protected String encoding; ! ! public EncodingTestCase(String name) { super(name); } ! protected void setUp() throws Exception { this.template = getTemplate(); this.expected = getExpected(); --- 32,45 ---- protected String expected; protected String encoding; ! ! ! public EncodingTestCase (String name) ! { super(name); } ! ! protected void setUp () throws Exception ! { this.template = getTemplate(); this.expected = getExpected(); *************** *** 45,66 **** } ! protected void tearDown() throws Exception { deleteFiles(); } /** * Return the template source to test with */ ! protected abstract String getTemplate(); /** * Return the encoding to test with */ ! protected abstract String getEncoding(); /** * Return the expected result */ ! protected abstract String getExpected(); /** --- 49,76 ---- } ! ! protected void tearDown () throws Exception ! { deleteFiles(); } + /** * Return the template source to test with */ ! protected abstract String getTemplate (); ! /** * Return the encoding to test with */ ! protected abstract String getEncoding (); ! /** * Return the expected result */ ! protected abstract String getExpected (); ! /** *************** *** 68,84 **** * in your template */ ! protected abstract void stuffContext(Context context); ! ! protected void createWebMacro() throws Exception { File f = new File("WebMacro.properties-".concat(encoding)); PrintWriter w = new PrintWriter(new FileWriter(f)); w.println("TemplateEncoding: ".concat(encoding)); w.close(); ! f = new File("WebMacro.properties-UTF8"); w = new PrintWriter(new FileWriter(f)); w.println("TemplateEncoding: UTF8"); w.close(); ! if (System.getProperties().getProperty("org.webmacro.LogLevel") == null) System.getProperties().setProperty("org.webmacro.LogLevel", "ERROR"); --- 78,96 ---- * in your template */ ! protected abstract void stuffContext (Context context); ! ! ! protected void createWebMacro () throws Exception ! { File f = new File("WebMacro.properties-".concat(encoding)); PrintWriter w = new PrintWriter(new FileWriter(f)); w.println("TemplateEncoding: ".concat(encoding)); w.close(); ! f = new File("WebMacro.properties-UTF8"); w = new PrintWriter(new FileWriter(f)); w.println("TemplateEncoding: UTF8"); w.close(); ! if (System.getProperties().getProperty("org.webmacro.LogLevel") == null) System.getProperties().setProperty("org.webmacro.LogLevel", "ERROR"); *************** *** 88,105 **** } ! protected void createFiles() throws IOException { File f = new File(TEMPLATE_FILENAME); ! Writer writer = new OutputStreamWriter(new FileOutputStream(f),encoding); writer.write(template); writer.close(); ! f = new File(TEMPLATE_FILENAME_UTF8); ! writer = new OutputStreamWriter(new FileOutputStream(f),"UTF8"); writer.write(template); writer.close(); ! } ! protected void deleteFile(String filename) throws IOException { File f = new File(filename); if (f.exists()) --- 100,121 ---- } ! ! protected void createFiles () throws IOException ! { File f = new File(TEMPLATE_FILENAME); ! Writer writer = new OutputStreamWriter(new FileOutputStream(f), encoding); writer.write(template); writer.close(); ! f = new File(TEMPLATE_FILENAME_UTF8); ! writer = new OutputStreamWriter(new FileOutputStream(f), "UTF8"); writer.write(template); writer.close(); ! } ! ! protected void deleteFile (String filename) throws IOException ! { File f = new File(filename); if (f.exists()) *************** *** 107,111 **** } ! protected void deleteFiles() throws IOException { deleteFile(TEMPLATE_FILENAME); deleteFile(TEMPLATE_FILENAME_UTF8); --- 123,129 ---- } ! ! protected void deleteFiles () throws IOException ! { deleteFile(TEMPLATE_FILENAME); deleteFile(TEMPLATE_FILENAME_UTF8); *************** *** 113,118 **** deleteFile("WebMacro.properties-UTF8"); } ! ! private Context getContext(WebMacro wm) { Context context = wm.getContext(); stuffContext(context); --- 131,138 ---- deleteFile("WebMacro.properties-UTF8"); } ! ! ! private Context getContext (WebMacro wm) ! { Context context = wm.getContext(); stuffContext(context); *************** *** 120,174 **** } ! public void testInputEncoding() throws Exception { ! assertEquals("Input Encoding is not "+encoding, ! wm.getConfig("TemplateEncoding"),encoding); Template t = wm.getTemplate(TEMPLATE_FILENAME); String evaluated = t.evaluate(getContext(wm)).toString(); ! assertEquals("Template evaluated to \""+evaluated+"\" instead of \""+expected+"\"", ! evaluated,expected); } ! public void testOutputEncoding() throws Exception { ! assertEquals("InputEncoding is not "+encoding, ! wm.getConfig("TemplateEncoding"),encoding); Template t = wm.getTemplate(TEMPLATE_FILENAME); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ! FastWriter fw = wm.getFastWriter(bos,encoding); ! t.write(fw,getContext(wm)); fw.flush(); fw.close(); ! assertByteArrayEquals(bos.toByteArray(),expected.getBytes(encoding)); } ! public void testUTF8OutputEncoding() throws Exception { ! assertEquals("InputEncoding is not "+encoding, ! wm.getConfig("TemplateEncoding"),encoding); Template t = wm.getTemplate(TEMPLATE_FILENAME); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ! FastWriter fw = wm.getFastWriter(bos,"UTF8"); ! t.write(fw,getContext(wm)); fw.flush(); fw.close(); ! assertByteArrayEquals(bos.toByteArray(),expected.getBytes("UTF8")); } ! public void testUTF8InputEncoding() throws Exception { assertEquals("InputEncoding is not UFT8", ! wmUTF8.getConfig("TemplateEncoding"),"UTF8"); Template t = wmUTF8.getTemplate(TEMPLATE_FILENAME_UTF8); String evaluated = t.evaluate(getContext(wmUTF8)).toString(); ! assertEquals("Template evaluated to \""+evaluated+"\" instead of \""+expected+"\"", ! expected,evaluated); } ! protected void assertByteArrayEquals(byte[] a,byte[] b) throws Exception { if (a == b) return; assertTrue("One byte array is null", ! ((a != null) && (b != null))); assertTrue("Size of binary output differs", ! a.length == b.length); ! for (int i=0; i < a.length; i++) { assertTrue("Binary output differs", ! a[i] == b[i]); } } --- 140,205 ---- } ! ! public void testInputEncoding () throws Exception ! { ! assertEquals("Input Encoding is not " + encoding, ! wm.getConfig("TemplateEncoding"), encoding); Template t = wm.getTemplate(TEMPLATE_FILENAME); String evaluated = t.evaluate(getContext(wm)).toString(); ! assertEquals("Template evaluated to \"" + evaluated + "\" instead of \"" + expected + "\"", ! evaluated, expected); } ! ! public void testOutputEncoding () throws Exception ! { ! assertEquals("InputEncoding is not " + encoding, ! wm.getConfig("TemplateEncoding"), encoding); Template t = wm.getTemplate(TEMPLATE_FILENAME); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ! FastWriter fw = wm.getFastWriter(bos, encoding); ! t.write(fw, getContext(wm)); fw.flush(); fw.close(); ! assertByteArrayEquals(bos.toByteArray(), expected.getBytes(encoding)); } ! ! public void testUTF8OutputEncoding () throws Exception ! { ! assertEquals("InputEncoding is not " + encoding, ! wm.getConfig("TemplateEncoding"), encoding); Template t = wm.getTemplate(TEMPLATE_FILENAME); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ! FastWriter fw = wm.getFastWriter(bos, "UTF8"); ! t.write(fw, getContext(wm)); fw.flush(); fw.close(); ! assertByteArrayEquals(bos.toByteArray(), expected.getBytes("UTF8")); } ! ! public void testUTF8InputEncoding () throws Exception ! { assertEquals("InputEncoding is not UFT8", ! wmUTF8.getConfig("TemplateEncoding"), "UTF8"); Template t = wmUTF8.getTemplate(TEMPLATE_FILENAME_UTF8); String evaluated = t.evaluate(getContext(wmUTF8)).toString(); ! assertEquals("Template evaluated to \"" + evaluated + "\" instead of \"" + expected + "\"", ! expected, evaluated); } ! ! protected void assertByteArrayEquals (byte[] a, byte[] b) throws Exception ! { if (a == b) return; assertTrue("One byte array is null", ! ((a != null) && (b != null))); assertTrue("Size of binary output differs", ! a.length == b.length); ! for (int i = 0; i < a.length; i++) ! { assertTrue("Binary output differs", ! a[i] == b[i]); } } Index: PerformanceOperations.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/test/unit/org/webmacro/template/PerformanceOperations.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PerformanceOperations.java 12 Oct 2001 21:31:48 -0000 1.1 --- PerformanceOperations.java 12 Jun 2003 00:47:49 -0000 1.2 *************** *** 1,7 **** package org.webmacro.template; ! import java.io.*; ! import java.util.*; ! import org.webmacro.*; /** --- 1,10 ---- package org.webmacro.template; ! import org.webmacro.Context; ! import org.webmacro.WM; ! import org.webmacro.WebMacro; ! ! import java.util.ArrayList; ! import java.util.List; /** *************** *** 20,116 **** * required. */ ! public class PerformanceOperations implements Runnable { ! ! // the classes which are to be run: ! Runnable[] testCases = { ! new PublicVersusPrivate() ! }; ! ! // The WebMacro instance which can be shared for each test case. ! private WebMacro wm; ! ! // the public variables available for introspection ! /** The iteration count used in the test. Defaults to 100K.*/ ! public int iterationCount = 100000; ! ! /** The total elapsed time for public access. */ ! public long tetPublicAccess; ! ! /** The total elapsed time for private access. */ ! public long tetPrivateAccess; ! ! public PerformanceOperations() {} ! ! public PerformanceOperations(int iterationCount) { ! this.iterationCount = iterationCount; ! } ! public void run() { ! try { ! wm = new WM(); ! } ! catch (Exception e) { ! throw new IllegalStateException(e.toString()); } ! for (int index = 0; index < testCases.length; index++) ! testCases[index].run(); ! } ! ! /** ! * The private versus public test case. ! */ ! class PublicVersusPrivate implements Runnable { ! static final String publicTemplate = ! "org/webmacro/template/Public.wm"; ! static final String privateTemplate = ! "org/webmacro/template/Private.wm"; ! ! ! private Context setupPublic() { ! // the public values: ! PublicValue pub = new PublicValue(); ! pub.object = this; ! // the lists for iteration: ! List pubList = new ArrayList(iterationCount); ! // populate: ! for (int index = 0; index < iterationCount; index++){ ! pubList.add(pub); ! } ! Context context = wm.getContext(); ! context.put("PublicList", pubList); ! return context; } ! ! private Context setupPrivate() { ! // the public values: ! PrivateValue priv = new PrivateValue(); ! priv.setObject(this); ! // the lists for iteration: ! List privList = new ArrayList(iterationCount); ! // populate: ! for (int index = 0; index < iterationCount; index++){ ! privList.add(priv); ! } ! Context context = wm.getContext(); ! context.put("PrivateList", privList); ! return context; } ! ! public void run(){ ! try { ! Context c = setupPublic(); ! long start = System.currentTimeMillis(); ! wm.writeTemplate(publicTemplate, System.out, c); ! tetPublicAccess = System.currentTimeMillis() - start; ! ! c = setupPrivate(); ! start = System.currentTimeMillis(); ! wm.writeTemplate(privateTemplate, System.out, c); ! tetPrivateAccess = System.currentTimeMillis() - start; ! } ! catch (Exception e) { ! throw new IllegalStateException(e.toString()); ! } } - } } --- 23,140 ---- * required. */ ! public class PerformanceOperations implements Runnable ! { ! ! // the classes which are to be run: ! Runnable[] testCases = { ! new PublicVersusPrivate() ! }; ! ! // The WebMacro instance which can be shared for each test case. ! private WebMacro wm; ! // the public variables available for introspection ! /** The iteration count used in the test. Defaults to 100K.*/ ! public int iterationCount = 100000; ! ! /** The total elapsed time for public access. */ ! public long tetPublicAccess; ! ! /** The total elapsed time for private access. */ ! public long tetPrivateAccess; ! ! ! public PerformanceOperations () ! { } ! ! ! public PerformanceOperations (int iterationCount) ! { ! this.iterationCount = iterationCount; } ! ! ! public void run () ! { ! try ! { ! wm = new WM(); ! } ! catch (Exception e) ! { ! throw new IllegalStateException(e.toString()); ! } ! for (int index = 0; index < testCases.length; index++) ! testCases[index].run(); } ! ! ! /** ! * The private versus public test case. ! */ ! class PublicVersusPrivate implements Runnable ! { ! static final String publicTemplate = ! "org/webmacro/template/Public.wm"; ! static final String privateTemplate = ! "org/webmacro/template/Private.wm"; ! ! ! private Context setupPublic () ! { ! // the public values: ! PublicValue pub = new PublicValue(); ! pub.object = this; ! // the lists for iteration: ! List pubList = new ArrayList(iterationCount); ! // populate: ! for (int index = 0; index < iterationCount; index++) ! { ! pubList.add(pub); ! } ! Context context = wm.getContext(); ! context.put("PublicList", pubList); ! return context; ! } ! ! ! private Context setupPrivate () ! { ! // the public values: ! PrivateValue priv = new PrivateValue(); ! priv.setObject(this); ! // the lists for iteration: ! List privList = new ArrayList(iterationCount); ! // populate: ! for (int index = 0; index < iterationCount; index++) ! { ! privList.add(priv); ! } ! Context context = wm.getContext(); ! context.put("PrivateList", privList); ! return context; ! } ! ! ! public void run () ! { ! try ! { ! Context c = setupPublic(); ! long start = System.currentTimeMillis(); ! wm.writeTemplate(publicTemplate, System.out, c); ! tetPublicAccess = System.currentTimeMillis() - start; ! ! c = setupPrivate(); ! start = System.currentTimeMillis(); ! wm.writeTemplate(privateTemplate, System.out, c); ! tetPrivateAccess = System.currentTimeMillis() - start; ! } ! catch (Exception e) ! { ! throw new IllegalStateException(e.toString()); ! } ! } } } Index: PrivateValue.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/test/unit/org/webmacro/template/PrivateValue.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PrivateValue.java 12 Oct 2001 21:31:48 -0000 1.1 --- PrivateValue.java 12 Jun 2003 00:47:49 -0000 1.2 *************** *** 1,9 **** package org.webmacro.template; ! public class PrivateValue { private long value; private Object object; ! public long getValue() { return value; } ! public void setValue(long value) { this.value = value; } ! public Object getObject() { return object; } ! public void setObject(Object object) { this.object = object; } } --- 1,31 ---- package org.webmacro.template; ! ! public class PrivateValue ! { private long value; private Object object; ! ! ! public long getValue () ! { ! return value; ! } ! ! ! public void setValue (long value) ! { ! this.value = value; ! } ! ! ! public Object getObject () ! { ! return object; ! } ! ! ! public void setObject (Object object) ! { ! this.object = object; ! } } Index: PublicValue.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/test/unit/org/webmacro/template/PublicValue.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PublicValue.java 12 Oct 2001 21:31:48 -0000 1.1 --- PublicValue.java 12 Jun 2003 00:47:49 -0000 1.2 *************** *** 1,4 **** package org.webmacro.template; ! public class PublicValue { public long value; public Object object; --- 1,6 ---- package org.webmacro.template; ! ! public class PublicValue ! { public long value; public Object object; Index: TemplateTestCase.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/test/unit/org/webmacro/template/TemplateTestCase.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** TemplateTestCase.java 23 Mar 2003 02:00:38 -0000 1.14 --- TemplateTestCase.java 12 Jun 2003 00:47:49 -0000 1.15 *************** *** 1,305 **** package org.webmacro.template; ! import java.io.*; ! import java.util.*; ! import org.webmacro.*; import org.webmacro.engine.StringTemplate; ! import junit.framework.*; - import org.apache.regexp.RE; ! public abstract class TemplateTestCase extends TestCase { ! /** our WebMacro instance */ ! protected WebMacro _wm; - /** context to use for each template */ - protected Context _context; ! public TemplateTestCase(String name) { ! super(name); ! } - protected void setUp() throws Exception { - init(); - } ! /** ! * create a default-configured instance of WebMacro. Subclasses may ! * override if WM needs to be created/configured differently. ! */ ! protected WebMacro createWebMacro () throws Exception ! { ! return new WM (); ! } ! /** ! * initialize this TemplateTester by creating a WebMacro instance ! * and a default Context. ! */ ! public void init () throws Exception ! { ! if (System.getProperties().getProperty("org.webmacro.LogLevel") == null) ! System.getProperties().setProperty("org.webmacro.LogLevel", "NONE"); ! _wm = createWebMacro (); ! _context = _wm.getContext (); - // let subclasses stuff the context with custom data - stuffContext (_context); - } ! /** ! * stuff the provided context with custom variables and objects<p> ! * ! * @throws Exception if something goes wrong while stuffing context ! */ ! protected abstract void stuffContext (Context context) throws Exception; ! /** ! * Utility method to convert a template file to a string for use in ! * the test suite. ! * Note: if the file is not found as an absolute arg, then the ! * broker is called to use its get resource method. */ ! public String templateFileToString(String fileReference) throws Exception { ! InputStream in = null; ! try { ! in = new FileInputStream(fileReference); ! } ! catch (FileNotFoundException e) { ! in = _wm.getBroker().getResourceAsStream(fileReference); ! if (in == null) throw new Exception(fileReference + " not found"); ! } ! byte[] value = new byte[in.available()]; ! in.read(value); ! in.close(); ! String string = new String(value); ! return string; ! } - /* - * Evaluates a template and returns its value. */ - public String executeTemplate(Template template) throws Exception { - return template.evaluate(_context).toString(); - } ! /** ! * Executes a file template. ! */ ! public String executeFileTemplate(String fileReference) throws Exception { ! return executeStringTemplate(templateFileToString(fileReference)); ! } - /** Execute a string as a template against the current context, - * and return the result. */ - public String executeStringTemplate(String templateText) throws Exception { - Template template = new StringTemplate(_wm.getBroker(), templateText); - template.parse(); - FastWriter fw = FastWriter.getInstance(_wm.getBroker(), null, "UTF8"); - template.write (fw, _context); - String output = fw.toString(); - fw.close(); - return output; - } - - public void store(String fileName, String value) throws Exception { - Writer out = new FileWriter(fileName); - out.write(value); - out.close(); - } ! public void assertEvalutionEquals (String eval, Object result) throws Exception { String template = "#set $assertEvalutionEquals = " + eval; executeStringTemplate(template); if (result == null) ! assertTrue (_context.get("assertEvalutionEquals") == null); else ! assertTrue (result==null ? "null" : result.toString(), result.equals(_context.get("assertEvalutionEquals"))); } - /** - * asserts that the specified template file (loaded via classpath) evaluates - * to the given result text when evaluated against the current context - */ - public void assertTemplateEquals (String templateName, String resultText) throws Exception { - String result = null; ! try { ! result = executeStringTemplate (templateFileToString(templateName)); ! } catch (Exception e) { ! System.err.println ("Execution of /" + templateName + "/ threw " + e.getClass() ! + ", expecting /" + resultText + "/"); ! } ! if (result == null || !result.equals(resultText)) { ! System.err.println ("/" + templateName + "/ does not " ! + "evaluate to /" + resultText + "/ " ! + "result=/" + result + "/"); ! assertTrue (false); ! } ! } ! /** Asserts that the given template text evalutes to the given result text ! * when evaluated against the current context */ - public void assertStringTemplateEquals(String templateText, - String resultText) { - String result = null; ! try { ! result = executeStringTemplate(templateText); ! } ! catch (Exception e) { ! System.err.println("Execution of /" + templateText + "/" ! + " threw " + e.getClass() ! + "(" + e.getMessage() + ")" ! + ", expecting /" ! + resultText + "/"); ! e.printStackTrace(); ! assertTrue(false); ! } ! if (result == null) ! return; ! if (!result.equals(resultText)) { ! System.err.println("Execution of /" + templateText + "/" ! + " yielded /" + result + "/, expecting /" ! + resultText + "/"); ! assertTrue(false); } - } - /** Asserts that the given template text throws the given exception - * when evaluated against the current context, and the message text - * matches the specified RE */ ! public void assertStringTemplateThrows(String templateText, ! Class exceptionClass, ! String messageMatchText) ! throws Exception { ! String result = null; ! Exception caught = null; ! try { ! result = executeStringTemplate(templateText); ! } ! catch (Exception e) { ! caught = e; ! } ! if (caught == null) { ! System.err.println("Execution of /" + templateText + "/" ! + " yielded /" + result + "/, expecting throw " ! + exceptionClass); ! assertTrue(false); ! } ! else if (!exceptionClass.isAssignableFrom(caught.getClass())) { ! System.err.println("Execution of /" + templateText + "/" ! + " threw " + caught.getClass() + ", expecting " ! + exceptionClass); ! assertTrue(false); } ! else if (messageMatchText != null) { ! RE re = new RE(messageMatchText); ! if (!re.match(caught.getMessage())) { ! System.err.println("Exception " + caught.getMessage() ! + " does not match /" ! + messageMatchText + "/"); ! assertTrue(false); ! } } - } - /** Asserts that the given template text throws the given exception - * when evaluated against the current context */ - public void assertStringTemplateThrows(String templateText, - Class exceptionClass) - throws Exception { - assertStringTemplateThrows(templateText, exceptionClass, null); - } ! /** Asserts that the given template text matches the given regular ! * expression when evaluated against the current context */ ! public void assertStringTemplateMatches(String templateText, ! String resultPattern) ! throws Exception { ! String result = null; ! try { ! result = executeStringTemplate(templateText); ! } ! catch (Exception e) { ! System.err.println("Execution of /" + templateText + "/" ! + " threw " + e.getClass() + "/, expecting match /" ! + resultPattern + "/"); ! e.printStackTrace(System.err); ! assertTrue(false); ! } ! if (result == null) ! return; ! RE re = new RE(resultPattern); ! if (!re.match(result)) { ! System.err.println("Execution of /" + templateText + "/" ! + " yielded /" + result + "/, expecting match /" ! + resultPattern + "/"); ! assertTrue(false); } - } ! public void assertStringTemplateThrowsWithCaught(String templateText, ! Class exceptionClass) { ! String result = null; ! Throwable caught = null; ! try { ! result = executeStringTemplate(templateText); ! } ! catch (Exception e) { ! caught = e; ! if (!(caught instanceof PropertyException)) { ! System.err.println("Execution of /" + templateText + "/" ! + " yielded /" + caught.getClass() ! + "/, expecting throw PropertyException"); ! assertTrue(false); ! } else { ! caught = ((PropertyException)e).getCaught(); ! } ! } ! if (caught == null) { ! System.err.println("Execution of /" + templateText + "/" ! + " yielded /" + result + "/, expecting throw " ! + "PropertyException with caught exception " ! + exceptionClass); ! assertTrue(false); } ! else if (!exceptionClass.isAssignableFrom(caught.getClass())) { ! System.err.println("Execution of /" + templateText + "/" ! + " threw " + caught.getClass() + ", expecting throw " ! + "PropertyException with caught exception " ! + exceptionClass); ! assertTrue(false); } - } - /** Asserts that the specified expression is considered true or false - * depending on the value of 'yesno' */ - public void assertExpr(String expr, boolean yesno) { - assertStringTemplateEquals("#if (" + expr +") {Yes} #else {No}", - yesno? "Yes" : "No"); - } ! /** Asserts that the specified expression evaluates to the desired ! * boolean result */ ! public void assertBooleanExpr(String expr, boolean result) { ! assertStringTemplateEquals("#set $result=(" + expr + ") $result", ! result? "true" : "false"); ! } } --- 1,358 ---- package org.webmacro.template; ! import junit.framework.TestCase; ! import org.apache.regexp.RE; import org.webmacro.*; import org.webmacro.engine.StringTemplate; ! import java.io.*; + public abstract class TemplateTestCase extends TestCase + { ! /** our WebMacro instance */ ! protected WebMacro _wm; ! /** context to use for each template */ ! protected Context _context; ! public TemplateTestCase (String name) ! { ! super(name); ! } ! protected void setUp () throws Exception ! { ! init(); ! } ! /** ! * create a default-configured instance of WebMacro. Subclasses may ! * override if WM needs to be created/configured differently. ! */ ! protected WebMacro createWebMacro () throws Exception ! { ! return new WM(); ! } ! /** ! * initialize this TemplateTester by creating a WebMacro instance ! * and a default Context. ! */ ! public void init () throws Exception ! { ! if (System.getProperties().getProperty("org.webmacro.LogLevel") == null) ! System.getProperties().setProperty("org.webmacro.LogLevel", "NONE"); ! _wm = createWebMacro(); ! _context = _wm.getContext(); ! // let subclasses stuff the context with custom data ! stuffContext(_context); ! } + /** + * stuff the provided context with custom variables and objects<p> + * + * @throws Exception if something goes wrong while stuffing context + */ + protected abstract void stuffContext (Context context) throws Exception; ! /** ! * Utility method to convert a template file to a string for use in ! * the test suite. ! * Note: if the file is not found as an absolute arg, then the ! * broker is called to use its get resource method. */ ! public String templateFileToString (String fileReference) throws Exception ! { ! InputStream in = null; ! try ! { ! in = new FileInputStream(fileReference); ! } ! catch (FileNotFoundException e) ! { ! in = _wm.getBroker().getResourceAsStream(fileReference); ! if (in == null) throw new Exception(fileReference + " not found"); ! } ! byte[] value = new byte[in.available()]; ! in.read(value); ! in.close(); ! String string = new String(value); ! return string; ! } + /* + * Evaluates a template and returns its value. */ + public String executeTemplate (Template template) throws Exception + { + return template.evaluate(_context).toString(); + } ! ! /** ! * Executes a file template. ! */ ! public String executeFileTemplate (String fileReference) throws Exception ! { ! return executeStringTemplate(templateFileToString(fileReference)); ! } ! ! ! /** Execute a string as a template against the current context, ! * and return the result. */ ! public String executeStringTemplate (String templateText) throws Exception ! { ! Template template = new StringTemplate(_wm.getBroker(), templateText); ! template.parse(); ! FastWriter fw = FastWriter.getInstance(_wm.getBroker(), null, "UTF8"); ! template.write(fw, _context); ! String output = fw.toString(); ! fw.close(); ! return output; ! } ! ! ! public void store (String fileName, String value) throws Exception ! { ! Writer out = new FileWriter(fileName); ! out.write(value); ! out.close(); ! } ! ! ! public void assertEvalutionEquals (String eval, Object result) throws Exception ! { String template = "#set $assertEvalutionEquals = " + eval; executeStringTemplate(template); if (result == null) ! assertTrue(_context.get("assertEvalutionEquals") == null); else ! assertTrue(result == null ? "null" : result.toString(), result.equals(_context.get("assertEvalutionEquals"))); } ! /** ! * asserts that the specified template file (loaded via classpath) evaluates ! * to the given result text when evaluated against the current context ! */ ! public void assertTemplateEquals (String templateName, String resultText) throws Exception ! { ! String result = null; ! try ! { ! result = executeStringTemplate(templateFileToString(templateName)); ! } ! catch (Exception e) ! { ! System.err.println("Execution of /" + templateName + "/ threw " + e.getClass() ! + ", expecting /" + resultText + "/"); ! } ! if (result == null || !result.equals(resultText)) ! { ! System.err.println("/" + templateName + "/ does not " ! + "evaluate to /" + resultText + "/ " ! + "result=/" + result + "/"); ! assertTrue(false); ! } ! } ! /** Asserts that the given template text evalutes to the given result text ! * when evaluated against the current context */ ! public void assertStringTemplateEquals (String templateText, ! String resultText) ! { ! String result = null; ! ! try ! { ! result = executeStringTemplate(templateText); ! } ! catch (Exception e) ! { ! System.err.println("Execution of /" + templateText + "/" ! + " threw " + e.getClass() ! + "(" + e.getMessage() + ")" ! + ", expecting /" ! + resultText + "/"); ! e.printStackTrace(); ! assertTrue(false); ! } ! if (result == null) ! return; ! ! if (!result.equals(resultText)) ! { ! System.err.println("Execution of /" + templateText + "/" ! + " yielded /" + result + "/, expecting /" ! + resultText + "/"); ! assertTrue(false); ! } } ! /** Asserts that the given template text throws the given exception ! * when evaluated against the current context, and the message text ! * matches the specified RE */ ! public void assertStringTemplateThrows (String templateText, ! Class exceptionClass, ! String messageMatchText) ! throws Exception ! { ! String result = null; ! Exception caught = null; ! ! try ! { ! result = executeStringTemplate(templateText); ! } ! catch (Exception e) ! { ! caught = e; ! } ! if (caught == null) ! { ! System.err.println("Execution of /" + templateText + "/" ! + " yielded /" + result + "/, expecting throw " ! + exceptionClass); ! assertTrue(false); ! } ! else if (!exceptionClass.isAssignableFrom(caught.getClass())) ! { ! System.err.println("Execution of /" + templateText + "/" ! + " threw " + caught.getClass() + ", expecting " ! + exceptionClass); ! assertTrue(false); ! } ! else if (messageMatchText != null) ! { ! RE re = new RE(messageMatchText); ! if (!re.match(caught.getMessage())) ! { ! System.err.println("Exception " + caught.getMessage() ! + " does not match /" ! + messageMatchText + "/"); ! assertTrue(false); ! } ! } } ! ! ! /** Asserts that the given template text throws the given exception ! * when evaluated against the current context */ ! public void assertStringTemplateThrows (String templateText, ! Class exceptionClass) ! throws Exception ! { ! assertStringTemplateThrows(templateText, exceptionClass, null); } ! /** Asserts that the given template text matches the given regular ! * expression when evaluated against the current context */ ! public void assertStringTemplateMatches (String templateText, ! String resultPattern) ! throws Exception ! { ! String result = null; ! try ! { ! result = executeStringTemplate(templateText); ! } ! catch (Exception e) ! { ! System.err.println("Execution of /" + templateText + "/" ! + " threw " + e.getClass() + "/, expecting match /" ! + resultPattern + "/"); ! e.printStackTrace(System.err); ! assertTrue(false); ! } ! if (result == null) ! return; ! RE re = new RE(resultPattern); ! if (!re.match(result)) ! { ! System.err.println("Execution of /" + templateText + "/" ! + " yielded /" + result + "/, expecting match /" ! + resultPattern + "/"); ! assertTrue(false); ! } } ! public void assertStringTemplateThrowsWithCaught (String templateText, ! Class exceptionClass) ! { ! String result = null; ! Throwable caught = null; ! try ! { ! result = executeStringTemplate(templateText); ! } ! catch (Exception e) ! { ! caught = e; ! if (!(caught instanceof PropertyException)) ! { ! System.err.println("Execution of /" + templateText + "/" ! + " yielded /" + caught.getClass() ! + "/, expecting throw PropertyException"); ! assertTrue(false); ! } ! else ! { ! caught = ((PropertyException) e).getCaught(); ! } ! } ! if (caught == null) ! { ! System.err.println("Execution of /" + templateText + "/" ! + " yielded /" + result + "/, expecting throw " ! + "PropertyException with caught exception " ! + exceptionClass); ! assertTrue(false); ! } ! else if (!exceptionClass.isAssignableFrom(caught.getClass())) ! { ! System.err.println("Execution of /" + templateText + "/" ! + " threw " + caught.getClass() + ", expecting throw " ! + "PropertyException with caught exception " ! + exceptionClass); ! assertTrue(false); ! } } ! ! ! /** Asserts that the specified expression is considered true or false ! * depending on the value of 'yesno' */ ! public void assertExpr (String expr, boolean yesno) ! { ! assertStringTemplateEquals("#if (" + expr + ") {Yes} #else {No}", ! yesno ? "Yes" : "No"); } ! /** Asserts that the specified expression evaluates to the desired ! * boolean result */ ! public void assertBooleanExpr (String expr, boolean result) ! { ! assertStringTemplateEquals("#set $result=(" + expr + ") $result", ! result ? "true" : "false"); ! } } Index: TestAbuse.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/test/unit/org/webmacro/template/TestAbuse.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** TestAbuse.java 23 Mar 2003 02:00:38 -0000 1.8 --- TestAbuse.java 12 Jun 2003 00:47:49 -0000 1.9 *************** *** 1,13 **** package org.webmacro.template; ! import java.io.*; ! import org.webmacro.*; ! import org.webmacro.engine.StringTemplate; import org.webmacro.engine.DefaultEvaluationExceptionHandler; - import junit.framework.*; - - import org.apache.regexp.RE; - /** --- 1,7 ---- package org.webmacro.template; ! import org.webmacro.Context; import org.webmacro.engine.DefaultEvaluationExceptionHandler; /** *************** *** 16,139 **** * correctly. */ ! public class TestAbuse extends TemplateTestCase { ! public TestAbuse (String name) { ! super (name); ! } - public void stuffContext (Context context) throws Exception { - context.setEvaluationExceptionHandler ( - new DefaultEvaluationExceptionHandler ()); - context.put ("User", "Eric"); - } ! /** We'll start easy */ ! public void testEOLComment () throws Exception { ! assertStringTemplateEquals ("## a comment", ""); ! } - /** this is easy too */ - public void testBlockComment () throws Exception { - assertStringTemplateEquals ("#comment { this is a comment }", ""); - } ! /** test a variable reference like: Welcome to the site, $User. */ ! public void testTrailingDot () throws Exception { ! assertStringTemplateEquals ("Hello, $User.", ! "Hello, Eric."); ! } - /** EOL comments at end of directive blocks. */ - public void testEOLCommentWithDirective () throws Exception { - String tmpl = "#if (true) { ## this is a comment\n" - + "pass\n" - + "} else { ## this is another comment\n" - + "fail\n" - + "}"; ! assertStringTemplateMatches (tmpl, ".*pass.*"); ! } ! /** WMScript that would fail if it weren't commented out */ ! public void testBlockCommentWithBadWMScript () throws Exception { ! String tmpl = "#comment { #if ($true) }"; ! assertStringTemplateMatches (tmpl, ""); ! } ! ! /** split an #if across multiple lines. ! currently produces a big parse error ! */ ! public void testMultiLineIf () throws Exception { ! System.err.println ("NEED TO REVISIT: TestAbuse.testMultiLineIf()"); ! assertTrue (true); // String tmpl = "#if (true\n&& true && true\n) {pass} #else {fail}"; // assertStringTemplateEquals (tmpl, "pass"); ! } - /** - * The error message this produces is: - * Attempt to dereference null value $Object - * but $Object <b>isn't</b> null. Only its - * toString() method returns null. The message - * implies that $Object is null. - * - * Can this be detected and say something like: - * $Object's toString() method returned null. - * - * Probably requires another Exception type for EEH - * and friends. - */ - public void testToStringIsNull () throws Exception { - Object obj = new Object () { - public String toString () { return null; } - }; ! _context.put ("Object", obj); ! assertStringTemplateMatches("$Object", ! "<!--.*returns null.*-->"); ! } ! /** the infamous "but I want to put Javascript in an #if block!!" - Currently fails when using { and }, but passes with - #begin and #end. Any reason why? - */ - public void testBlockDirectiveWithJavascript () throws Exception { - String jscript = "<script>\n" - + " if (true) {\n" - + " alert (\"Hello, World\");\n" - + " } else {\n" - + " alert (\"Goodbye, World\");\n" - + " }\n" - + "</script>"; - String jscriptnl = "<script>\n" - + " if (true) {\n" - + " alert (\"Hello, World\");\n" - + " } else {\n" - + " alert (\"Goodbye, World\");\n" - + " }\n" - + "</script>\n"; ! // check it with #begin and #end ! String tmpl = "#if (true) #begin" ! + jscript ! + "#end"; ! assertStringTemplateEquals (tmpl, jscript); ! } ! public void testSemi() throws Exception { ! String assn = "#set $foo=\"blah\" "; ! assertStringTemplateEquals(assn + "$(foo);", "blah;"); ! assertStringTemplateEquals(assn + "$(foo)", "blah"); ! assertStringTemplateEquals(assn + "$foo", "blah"); ! assertStringTemplateEquals(assn + "$foo;", "blah"); ! assertStringTemplateEquals(assn + "$foo.substring(1)", "lah"); ! assertStringTemplateEquals(assn + "$foo.substring(1);", "lah;"); ! } ! public void testNastyEndreTemplate() throws Exception { String tmpl = templateFileToString("org/webmacro/template/nasty_endre.wm"); assertStringTemplateThrows(tmpl, org.webmacro.engine.BuildException.class); --- 10,161 ---- * correctly. */ ! public class TestAbuse extends TemplateTestCase ! { ! public TestAbuse (String name) ! { ! super(name); ! } ! public void stuffContext (Context context) throws Exception ! { ! context.setEvaluationExceptionHandler( ! new DefaultEvaluationExceptionHandler()); ! context.put("User", "Eric"); ! } ! /** We'll start easy */ ! public void testEOLComment () throws Exception ! { ! assertStringTemplateEquals("## a comment", ""); ! } ! /** this is easy too */ ! public void testBlockComment () throws Exception ! { ! assertStringTemplateEquals("#comment { this is a comment }", ""); ! } ! ! /** test a variable reference like: Welcome to the site, $User. */ ! public void testTrailingDot () throws Exception ! { ! assertStringTemplateEquals("Hello, $User.", ! "Hello, Eric."); ! } ! ! ! /** EOL comments at end of directive blocks. */ ! public void testEOLCommentWithDirective () throws Exception ! { ! String tmpl = "#if (true) { ## this is a comment\n" ! + "pass\n" ! + "} else { ## this is another comment\n" ! + "fail\n" ! + "}"; ! ! assertStringTemplateMatches(tmpl, ".*pass.*"); ! } ! ! ! /** WMScript that would fail if it weren't commented out */ ! public void testBlockCommentWithBadWMScript () throws Exception ! { ! String tmpl = "#comment { #if ($true) }"; ! assertStringTemplateMatches(tmpl, ""); ! } ! ! ! /** split an #if across multiple lines. ! currently produces a big parse error ! */ ! public void testMultiLineIf () throws Exception ! { ! System.err.println("NEED TO REVISIT: TestAbuse.testMultiLineIf()"); ! assertTrue(true); // String tmpl = "#if (true\n&& true && true\n) {pass} #else {fail}"; // assertStringTemplateEquals (tmpl, "pass"); ! } ! /** ! * The error message this produces is: ! * Attempt to dereference null value $Obje... [truncated message content] |