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();
    }
}

@@ -1405,16 +1405,53 @@
 context;
+%0A    protected boolean DEBUG = false;
 %0A%0A    pu
@@ -2475,49 +2475,79 @@
     
-try%0A        %7B%0A  
+assertEquals(expected, evaluate(template));%0A
     
+%7D%0A%0A
     
-  String result =
+protected String
  eva
@@ -2544,32 +2544,39 @@
 String evaluate(
+String 
 template);%0A     
@@ -2572,22 +2572,23 @@
 ate)
-;
 %0A    
+%7B%0A
         
 asse
@@ -2587,37 +2587,47 @@
     
-assertEquals(expected, result
+StringWriter writer = new StringWriter(
 );%0A 
@@ -2629,25 +2629,27 @@
 ();%0A        
-%7D
+try
 %0A        cat
@@ -2649,80 +2649,46 @@
     
-catch (Exception e)%0A        %7B%0A            throw new RuntimeException(e);
+%7B%0A            if (DEBUG)%0A            %7B
 %0A   
@@ -2696,95 +2696,73 @@
     
-%7D%0A
     
-%7D%0A%0A
     
-protected String evaluate(String template) throws Exception
+engine.getLog().info(%22Template: %22+template);
 %0A    
-%7B%0A
         
 Stri
@@ -2761,50 +2761,15 @@
     
-StringWriter writer = new StringWriter();%0A
+%7D%0A%0A    
     
@@ -2839,24 +2839,28 @@
 ort%0A        
+    
 engine.evalu
@@ -2901,24 +2901,28 @@
 plate);%0A
+%0A   
         
 return w
@@ -2917,14 +2917,24 @@
     
-return
+ String result =
  wri
@@ -2945,20 +2945,256 @@
 toString();%0A
+            if (DEBUG)%0A            %7B%0A                engine.getLog().info(%22Result: %22+result);%0A            %7D%0A            return result;%0A        %7D%0A        catch (Exception e)%0A        %7B%0A            throw new RuntimeException(e);%0A        %7D%0A
     %7D%0A%7D%0A

make it easier to debug extensions