geirm       01/04/13 19:54:05

  Modified:    src/java/org/apache/velocity/test/misc Test.java
  Added:       src/java/org/apache/velocity/test EncodingTestCase.java
  Log:
  New testcase to test non 8859-1 encodings, specifically UTF-8.
  
  Thanks to Kent Johnson for the example template
  
  Revision  Changes    Path
  1.1                  
jakarta-velocity/src/java/org/apache/velocity/test/EncodingTestCase.java
  
  Index: EncodingTestCase.java
  ===================================================================
  package org.apache.velocity.test;
  
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.BufferedWriter;
  import java.io.FileOutputStream;
  import java.io.OutputStreamWriter;
  import java.io.Writer;
  
  import java.util.Vector;
  
  import org.apache.velocity.VelocityContext;
  
  import org.apache.velocity.Template;
  import org.apache.velocity.app.Velocity;
  import org.apache.velocity.runtime.Runtime;
  import org.apache.velocity.test.provider.TestProvider;
  import org.apache.velocity.util.StringUtils;
  
  import junit.framework.TestCase;
  
  /**
   * Tests input encoding handling.  The input target is UTF-8, having
   * chinese and and a spanish enyay (n-twiddle)
   *
   *  Thanks to Kent Johnson for the example input file.
   *
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
   * @version $Id: EncodingTestCase.java,v 1.1 2001/04/14 02:54:04 geirm Exp $
   */
  public class EncodingTestCase extends BaseTestCase implements TemplateTestBase
  {
      public EncodingTestCase()
      {
          super("ContextSafetyTestCase");
          
          try
          {
                Velocity.setProperty(
                    Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
                
              Velocity.setProperty( Velocity.INPUT_ENCODING, "UTF-8" );
  
              Velocity.init();
            }
            catch (Exception e)
            {
              System.err.println("Cannot setup EncodingTestCase!");
              e.printStackTrace();
              System.exit(1);
            }
      }
  
      public static junit.framework.Test suite()
      {
          return new EncodingTestCase();
      }
  
      /**
       * Runs the test.
       */
      public void runTest ()
      {
          
          VelocityContext context = new VelocityContext();
         
          try
          {
              assureResultsDirectoryExists(RESULT_DIR);
              
              /*
               *  get the template and the output
               */
  
              Template template = Runtime.getTemplate(
                  getFileName(null, "encodingtest", TMPL_FILE_EXT));
  
              FileOutputStream fos = 
                  new FileOutputStream (
                      getFileName(RESULT_DIR, "encodingtest", RESULT_FILE_EXT));
  
              Writer writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
             
              template.merge(context, writer);
              writer.flush();
              writer.close();
              
              if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest",
                      RESULT_FILE_EXT,CMP_FILE_EXT) )
              {
                  fail("Output incorrect.");
              }
          }
          catch (Exception e)
          {
              fail(e.getMessage());
          }
      }
  }
  
  
  
  1.24      +39 -2     
jakarta-velocity/src/java/org/apache/velocity/test/misc/Test.java
  
  Index: Test.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/test/misc/Test.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- Test.java 2001/03/20 19:13:28     1.23
  +++ Test.java 2001/04/14 02:54:04     1.24
  @@ -81,6 +81,12 @@
   import org.apache.velocity.runtime.Runtime;
   import org.apache.velocity.test.provider.TestProvider;
   
  +import org.apache.velocity.context.EventCartridge;
  +import org.apache.velocity.context.ReferenceInsertionEventHandler;
  +import org.apache.velocity.context.NullSetEventHandler;
  +import org.apache.velocity.context.NullReferenceEventHandler;
  +import org.apache.velocity.context.Context;
  +
   //import org.apache.velocity.runtime.log.SimpleLogSystem;
   
   /**
  @@ -89,9 +95,9 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  - * @version $Id: Test.java,v 1.23 2001/03/20 19:13:28 geirm Exp $
  + * @version $Id: Test.java,v 1.24 2001/04/14 02:54:04 geirm Exp $
    */
  -public class Test
  +public class Test implements ReferenceInsertionEventHandler, 
NullSetEventHandler,NullReferenceEventHandler
   {
       /**
        * Cache of writers
  @@ -254,6 +260,11 @@
               //Velocity.invokeVelocimacro( "floog", "test", new String[2],  context, 
 w );
               //System.out.println("Invoke = " + w );
   
  +            EventCartridge ec = new EventCartridge();
  +
  +            ec.addEventHandler(this);
  +
  +            ec.attachToContext( context );
    
               /*
                *  make a writer, and merge the template 'against' the context
  @@ -271,7 +282,33 @@
           catch( Exception e )
           {
               Runtime.error(e);
  +
  +            e.printStackTrace( System.out );
           }
  +    }
  +
  +    public String nullReferenceRender( String reference )
  +    {
  +        if( reference.equals("$gloppy"))
  +            return "";
  +
  +        return reference;
  +    }
  +       
  +    public Object referenceInsert( String reference, Object value  )
  +    {
  +        System.out.println("Woo! referenceInsert : " + reference + " = " + 
value.toString() );
  +        return value;
  +    }
  +
  +    public boolean nullSetLogMessage( String reference )
  +    {
  +        System.out.println("Woo2! nullSetLogMessage : " + reference);
  +
  +        if (reference.equals("$woogie"))
  +            return false;
  +        
  +        return true;
       }
   
       public static void main(String[] args)
  
  
  

Reply via email to