You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2008/08/18 22:43:24 UTC

svn commit: r686864 - in /velocity/engine/trunk/src/test/org/apache/velocity/test: ArrayMethodsTestCase.java BaseEvalTestCase.java ForeachBreakTestCase.java IfNullTestCase.java PropertyMethodPrecedenceTestCase.java VarargMethodsTestCase.java

Author: nbubna
Date: Mon Aug 18 13:43:23 2008
New Revision: 686864

URL: http://svn.apache.org/viewvc?rev=686864&view=rev
Log:
move common test code to a base class

Added:
    velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java   (with props)
Modified:
    velocity/engine/trunk/src/test/org/apache/velocity/test/ArrayMethodsTestCase.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachBreakTestCase.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/IfNullTestCase.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/PropertyMethodPrecedenceTestCase.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/VarargMethodsTestCase.java

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/ArrayMethodsTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/ArrayMethodsTestCase.java?rev=686864&r1=686863&r2=686864&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/ArrayMethodsTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/ArrayMethodsTestCase.java Mon Aug 18 13:43:23 2008
@@ -19,29 +19,18 @@
  * under the License.    
  */
 
-import java.io.StringWriter;
 import java.lang.reflect.Array;
 import java.util.Arrays;
 import java.util.ArrayList;
 import java.util.List;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.runtime.RuntimeConstants;
-import org.apache.velocity.runtime.log.SystemLogChute;
 
 /**
  * Used to check that method calls on Array references work properly
  * and that they produce the same results as the same methods would on
  * a fixed-size {@link List}.
  */
-public class ArrayMethodsTestCase extends TestCase
+public class ArrayMethodsTestCase extends BaseEvalTestCase
 {
-    private VelocityEngine engine;
-    private VelocityContext context;
-
     private final static boolean PRINT_RESULTS = true;
 
     public ArrayMethodsTestCase(final String name)
@@ -49,30 +38,6 @@
         super(name);
     }
 
-    public void setUp() throws Exception
-    {
-        engine = new VelocityEngine();
-
-        // make the engine's log output go to the test-report
-        SystemLogChute log = new SystemLogChute();
-        log.setEnabledLevel(SystemLogChute.INFO_ID);
-        log.setSystemErrLevel(SystemLogChute.WARN_ID);
-        engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, log);
-
-        context = new VelocityContext();
-    }
-
-    public void tearDown()
-    {
-        engine = null;
-        context = null;
-    }
-
-    public static Test suite ()
-    {
-        return new TestSuite(ArrayMethodsTestCase.class);
-    }
-
     /**
      * Runs the test.
      */
@@ -225,14 +190,6 @@
         }
     }
 
-    private String evaluate(String template) throws Exception
-    {
-        StringWriter writer = new StringWriter();
-        // use template as its own name, since our templates are short
-        engine.evaluate(context, writer, template, template);
-        return writer.toString();
-    }
-
 }
 
 

Added: velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java?rev=686864&view=auto
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java (added)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java Mon Aug 18 13:43:23 2008
@@ -0,0 +1,102 @@
+package org.apache.velocity.test;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import junit.framework.TestCase;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.RuntimeConstants;
+import org.apache.velocity.runtime.log.SystemLogChute;
+
+/**
+ * Base for test cases that use evaluate, instead of going
+ * through the resource loaders.
+ */
+public class BaseEvalTestCase extends TestCase
+{
+    protected VelocityEngine engine;
+    protected VelocityContext context;
+
+    public BaseEvalTestCase(String name)
+    {
+        super(name);
+    }
+
+    public void setUp() throws Exception
+    {
+        engine = new VelocityEngine();
+
+        //by default, make the engine's log output go to the test-report
+        SystemLogChute log = new SystemLogChute();
+        log.setEnabledLevel(SystemLogChute.INFO_ID);
+        log.setSystemErrLevel(SystemLogChute.WARN_ID);
+        engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, log);
+
+        context = new VelocityContext();
+        setContext(context);
+    }
+
+    public void tearDown()
+    {
+        engine = null;
+        context = null;
+    }
+
+    public void testBase()
+    {
+        assertEvalEquals("","");
+        assertEvalEquals("abc\n123","abc\n123");
+    }
+
+    protected void setProperties(VelocityEngine engine)
+    {
+        // extension hook
+    }
+
+    protected void setContext(VelocityContext context)
+    {
+        // extension hook
+    }
+
+    protected void assertEvalEquals(String expected, String template)
+    {
+        try
+        {
+            String result = evaluate(template);
+            assertEquals(expected, result);
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    protected String evaluate(String template) throws Exception
+    {
+        StringWriter writer = new StringWriter();
+        // use template as its own name, since our templates are short
+        engine.evaluate(context, writer, template, template);
+        return writer.toString();
+    }
+}

Propchange: velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Revision

Propchange: velocity/engine/trunk/src/test/org/apache/velocity/test/BaseEvalTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachBreakTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachBreakTestCase.java?rev=686864&r1=686863&r2=686864&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachBreakTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachBreakTestCase.java Mon Aug 18 13:43:23 2008
@@ -19,42 +19,15 @@
  * under the License.    
  */
 
-import java.io.StringWriter;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import junit.framework.TestCase;
-import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.runtime.RuntimeConstants;
-import org.apache.velocity.runtime.log.SystemLogChute;
-
 /**
  * This class tests the break directive within Foreach loop.
  */
-public class ForeachBreakTestCase extends TestCase
+public class ForeachBreakTestCase extends BaseEvalTestCase
 {
-    private VelocityEngine engine;
-    private VelocityContext context;
-
     public ForeachBreakTestCase(String name)
     {
         super(name);
     }
-
-    public void setUp()
-        throws Exception
-    {
-        engine = new VelocityEngine();
-
-        // make the engine's log output go to the test-report
-        SystemLogChute log = new SystemLogChute();
-        log.setEnabledLevel(SystemLogChute.INFO_ID);
-        log.setSystemErrLevel(SystemLogChute.WARN_ID);
-        engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, log);
-
-        context = new VelocityContext();
-    }
     
     /**
      * Tests break directive with a couple of iterations.
@@ -78,25 +51,4 @@
         assertEvalEquals("~~~, ~~, ~, ",
             "#foreach($i in [1..3])#foreach($j in [2..4])#if($i*$j >= 8)#break#end~#end, #end");
     }
-
-    protected void assertEvalEquals(String expected, String template)
-    {
-        try
-        {
-            String result = evaluate(template);
-            assertEquals(expected, result);
-        }
-        catch (Exception e)
-        {
-            throw new RuntimeException(e);
-        }
-    }
-
-    private String evaluate(String template) throws Exception
-    {
-        StringWriter writer = new StringWriter();
-        // use template as its own name, since our templates are short
-        engine.evaluate(context, writer, template, template);
-        return writer.toString();
-    }
 }

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/IfNullTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/IfNullTestCase.java?rev=686864&r1=686863&r2=686864&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/IfNullTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/IfNullTestCase.java Mon Aug 18 13:43:23 2008
@@ -19,58 +19,24 @@
  * under the License.    
  */
 
-import java.io.StringWriter;
-import java.lang.reflect.Array;
-import java.util.Arrays;
-import java.util.ArrayList;
-import java.util.List;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
 import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.runtime.RuntimeConstants;
-import org.apache.velocity.runtime.log.SystemLogChute;
 
 /**
  * Used to check that nulls are properly handled in #if statements
  */
-public class IfNullTestCase extends TestCase
+public class IfNullTestCase extends BaseEvalTestCase
 {
-    private VelocityEngine engine;
-    private VelocityContext context;
-
     public IfNullTestCase(final String name)
     {
         super(name);
     }
 
-    public static Test suite ()
+    public void setContext(VelocityContext context)
     {
-        return new TestSuite(IfNullTestCase.class);
-    }
-
-    public void setUp() throws Exception
-    {
-        engine = new VelocityEngine();
-
-        // make the engine's log output go to the test-report
-        SystemLogChute log = new SystemLogChute();
-        log.setEnabledLevel(SystemLogChute.INFO_ID);
-        log.setSystemErrLevel(SystemLogChute.WARN_ID);
-        engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, log);
-
-        context = new VelocityContext();
         context.put("nullToString", new NullToString());
         context.put("notnull", new Object());
     }
 
-    public void tearDown()
-    {
-        engine = null;
-        context = null;
-    }
-
     public void testIfEquals()
     {
         // both null
@@ -107,27 +73,6 @@
         assertEvalEquals("foo", "#if( !$nullToString )foo#{else}bar#end");
     }
 
-    protected void assertEvalEquals(String expected, String template)
-    {
-        try
-        {
-            String result = evaluate(template);
-            assertEquals(expected, result);
-        }
-        catch (Exception e)
-        {
-            throw new RuntimeException(e);
-        }
-    }
-
-    private String evaluate(String template) throws Exception
-    {
-        StringWriter writer = new StringWriter();
-        // use template as its own name, since our templates are short
-        engine.evaluate(context, writer, template, template);
-        return writer.toString();
-    }
-
     public static class NullToString
     {
         public String toString()

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/PropertyMethodPrecedenceTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/PropertyMethodPrecedenceTestCase.java?rev=686864&r1=686863&r2=686864&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/PropertyMethodPrecedenceTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/PropertyMethodPrecedenceTestCase.java Mon Aug 18 13:43:23 2008
@@ -19,48 +19,20 @@
  * under the License.    
  */
 
-import java.io.StringWriter;
-import java.lang.reflect.Array;
-import java.util.Arrays;
-import java.util.ArrayList;
-import java.util.List;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
 import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.runtime.RuntimeConstants;
-import org.apache.velocity.runtime.log.SystemLogChute;
 
 /**
  * Used to check that vararg method calls on references work properly
  */
-public class PropertyMethodPrecedenceTestCase extends TestCase
+public class PropertyMethodPrecedenceTestCase extends BaseEvalTestCase
 {
-    private VelocityEngine engine;
-    private VelocityContext context;
-
     public PropertyMethodPrecedenceTestCase(final String name)
     {
         super(name);
     }
 
-    public static Test suite ()
+    public void setContext(VelocityContext context)
     {
-        return new TestSuite(PropertyMethodPrecedenceTestCase.class);
-    }
-
-    public void setUp() throws Exception
-    {
-        engine = new VelocityEngine();
-
-        // make the engine's log output go to the test-report
-        SystemLogChute log = new SystemLogChute();
-        log.setEnabledLevel(SystemLogChute.INFO_ID);
-        log.setSystemErrLevel(SystemLogChute.WARN_ID);
-        engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, log);
-
-        context = new VelocityContext();
         context.put("geta", new getGetgetisTool());
         context.put("getA", new GetgetisTool());
         context.put("geta2", new get2getisTool());
@@ -68,12 +40,6 @@
         context.put("isA", new isTool());
     }
 
-    public void tearDown()
-    {
-        engine = null;
-        context = null;
-    }
-
     public void testLowercasePropertyMethods()
     {
         assertEvalEquals("getfoo", "$geta.foo");
@@ -92,28 +58,6 @@
     }
 
 
-    protected void assertEvalEquals(String expected, String template)
-    {
-        try
-        {
-            String result = evaluate(template);
-            assertEquals(expected, result);
-        }
-        catch (Exception e)
-        {
-            throw new RuntimeException(e);
-        }
-    }
-
-    private String evaluate(String template) throws Exception
-    {
-        StringWriter writer = new StringWriter();
-        // use template as its own name, since our templates are short
-        engine.evaluate(context, writer, template, template);
-        return writer.toString();
-    }
-
-
     public static class isTool
     {
         public boolean isFoo()

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/VarargMethodsTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/VarargMethodsTestCase.java?rev=686864&r1=686863&r2=686864&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/VarargMethodsTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/VarargMethodsTestCase.java Mon Aug 18 13:43:23 2008
@@ -19,63 +19,29 @@
  * under the License.    
  */
 
-import java.io.StringWriter;
-import java.lang.reflect.Array;
-import java.util.Arrays;
-import java.util.ArrayList;
-import java.util.List;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
 import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.VelocityEngine;
-import org.apache.velocity.runtime.RuntimeConstants;
-import org.apache.velocity.runtime.log.SystemLogChute;
 
 /**
  * Used to check that vararg method calls on references work properly
  */
-public class VarargMethodsTestCase extends TestCase
+public class VarargMethodsTestCase extends BaseEvalTestCase
 {
-    private VelocityEngine engine;
-    private VelocityContext context;
-
     public VarargMethodsTestCase(final String name)
     {
         super(name);
     }
 
-    public static Test suite ()
+    public void setContext(VelocityContext context)
     {
-        return new TestSuite(VarargMethodsTestCase.class);
-    }
-
-    public void setUp() throws Exception
-    {
-        engine = new VelocityEngine();
-
-        // make the engine's log output go to the test-report
-        SystemLogChute log = new SystemLogChute();
-        log.setEnabledLevel(SystemLogChute.INFO_ID);
-        log.setSystemErrLevel(SystemLogChute.WARN_ID);
-        engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, log);
-
-        context = new VelocityContext();
         context.put("nice", new NiceTool());
         context.put("nasty", new NastyTool());
-        context.put("objects", new Object[] { this, Test.class });
+        context.put("objects", new Object[] { this, VelocityContext.class });
         context.put("strings", new String[] { "one", "two" });
         context.put("doubles", new double[] { 1.5, 2.5 });
         context.put("float", new Float(1f));
         context.put("ints", new int[] { 1, 2 });
     }
 
-    public void tearDown()
-    {
-        engine = null;
-        context = null;
-    }
-
     public void testStrings()
     {
         assertEvalEquals("onetwo", "$nice.var($strings)");
@@ -144,27 +110,6 @@
     }
 
 
-    protected void assertEvalEquals(String expected, String template)
-    {
-        try
-        {
-            assertEquals(expected, evaluate(template));
-        }
-        catch (Exception e)
-        {
-            throw new RuntimeException(e);
-        }
-    }
-
-    private String evaluate(String template) throws Exception
-    {
-        StringWriter writer = new StringWriter();
-        // use template as its own name, since our templates are short
-        engine.evaluate(context, writer, template, template);
-        return writer.toString();
-    }
-
-
 
     public static class NiceTool
     {