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.test.misc.TestLogChute;

/**
 * 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;
    protected boolean DEBUG = false;
    protected TestLogChute log;

    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
        log = new TestLogChute(false, false);
        log.setEnabledLevel(TestLogChute.INFO_ID);
        log.setSystemErrLevel(TestLogChute.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)
    {
        if (DEBUG)
        {
            engine.getLog().info("Expectation: "+expected);
        }
        assertEquals(expected, evaluate(template));
    }

    protected void assertEvalException(String evil)
    {
        assertEvalException(evil, null);
    }

    protected void assertEvalException(String evil, Class exceptionType)
    {
        try
        {
            if (!DEBUG)
            {
                log.off();
            }
            evaluate(evil);
            fail("Template '"+evil+"' should have thrown an exception.");
        }
        catch (Exception e)
        {
            if (exceptionType != null && !exceptionType.isAssignableFrom(e.getClass()))
            {
                fail("Was expecting template '"+evil+"' to throw "+exceptionType+" not "+e);
            }
        }
        finally
        {
            if (!DEBUG)
            {
                log.on();
            }
        }
    }

    protected String evaluate(String template)
    {
        StringWriter writer = new StringWriter();
        try
        {
            if (DEBUG)
            {
                engine.getLog().info("Template: "+template);
            }

            // use template as its own name, since our templates are short
            engine.evaluate(context, writer, template, template);

            String result = writer.toString();
            if (DEBUG)
            {
                engine.getLog().info("Result: "+result);
            }
            return result;
        }
        catch (RuntimeException re)
        {
            if (DEBUG)
            {
                engine.getLog().info("RuntimeException!", re);
            }
            throw re;
        }
        catch (Exception e)
        {
            if (DEBUG)
            {
                engine.getLog().info("Exception!", e);
            }
            throw new RuntimeException(e);
        }
    }
}

@@ -2646,36 +2646,41 @@
 %0A%0A    protected 
-void
+Exception
  assertEvalExcep
@@ -2703,32 +2703,39 @@
 )%0A    %7B%0A        
+return 
 assertEvalExcept
@@ -2764,36 +2764,41 @@
 %0A%0A    protected 
-void
+Exception
  assertEvalExcep
@@ -3294,32 +3294,54 @@
 ;%0A            %7D%0A
+            return e;%0A
         %7D%0A      
@@ -3444,24 +3444,45 @@
 %7D%0A        %7D%0A
+        return null;%0A
     %7D%0A%0A    p

make assertEvalException more useful