geirm       01/03/19 09:10:03

  Modified:    src/java/org/apache/velocity/test/misc Test.java
  Added:       src/java/org/apache/velocity/test
                        MethodInvocationExceptionTest.java
  Log:
  New test case to test the MethodInvocationException mechanism, so you can
  tell if a method of an object in the context throws an exception at
  render time.
  
  Revision  Changes    Path
  1.1                  
jakarta-velocity/src/java/org/apache/velocity/test/MethodInvocationExceptionTest.java
  
  Index: MethodInvocationExceptionTest.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.StringWriter;
  
  import org.apache.velocity.app.Velocity;
  import org.apache.velocity.VelocityContext;
  import org.apache.velocity.runtime.Runtime;
  import org.apache.velocity.runtime.log.LogSystem;
  
  import org.apache.velocity.exception.MethodInvocationException;
  
  import junit.framework.TestCase;
  
  /**
   * Tests if we can hand Velocity an arbitrary class for logging.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
   * @version $Id: MethodInvocationExceptionTest.java,v 1.1 2001/03/19 17:10:01 geirm 
Exp $
   */
  public class MethodInvocationExceptionTest extends TestCase 
  {
     /**
       * Default constructor.
       */
      public MethodInvocationExceptionTest()
      {
          super("MethodInvocationExceptionTest");
  
          try
          {
              /*
               *  init() Runtime with defaults
               */
              Velocity.init();
  
          }
          catch (Exception e)
          {
              System.err.println("Cannot setup MethodInvocationExceptionTest : " + e);
              System.exit(1);
          }            
      }
  
      public static junit.framework.Test suite ()
      {
          return new MethodInvocationExceptionTest();
      }
  
      /**
       * Runs the test :
       *
       *  uses the Velocity class to eval a string
       *  which accesses a method that throws an 
       *  exception.
       */
      public void runTest ()
      {
          String template = "$woogie.doException() boing!";
  
          VelocityContext vc = new VelocityContext();
          
          vc.put("woogie", this );
  
          StringWriter w = new StringWriter();
  
          try
          {
              Velocity. evaluate( vc,  w, "test", template );
              fail("No exception thrown");
          }
          catch( MethodInvocationException mie )
          {
              System.out.println("Caught MIE (good!) :" );
              System.out.println("  reference = " + mie.getReferenceName() );
              System.out.println("  method    = " + mie.getMethodName() );
  
              Throwable t = mie.getWrappedThrowable();
              System.out.println("  throwable = " + t );
  
              if( t instanceof Exception)
              {
                  System.out.println("  exception = " + ( (Exception) t).getMessage() 
);
              }
          }
          catch( Exception e)
          {
              fail("Wrong exception thrown");
          }
  
      }
  
      public void doException()
          throws Exception
      {
          System.out.println("DooException");
          throw new NullPointerException();
      }
    
      /**
       * Performs cleanup activities for this test case.
       */
      protected void tearDown () throws Exception
      {
          // No op.
      }
  }
  
  
  
  1.21      +52 -4     
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.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- Test.java 2001/03/14 22:05:33     1.20
  +++ Test.java 2001/03/19 17:10:02     1.21
  @@ -67,6 +67,7 @@
   import java.util.Properties;
   import java.util.Stack;
   import java.util.Vector;
  +import java.util.Enumeration;
   
   import org.apache.velocity.VelocityContext;
   import org.apache.velocity.Template;
  @@ -80,13 +81,15 @@
   import org.apache.velocity.runtime.Runtime;
   import org.apache.velocity.test.provider.TestProvider;
   
  +import org.apache.velocity.runtime.log.SimpleLogSystem;
  +
   /**
    * This class the testbed for Velocity. It is used to
    * test all the directives support by Velocity.
    *
    * @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.20 2001/03/14 22:05:33 jvanzyl Exp $
  + * @version $Id: Test.java,v 1.21 2001/03/19 17:10:02 geirm Exp $
    */
   public class Test
   {
  @@ -121,13 +124,57 @@
           try
           {
               /*
  +             *  this is another way to do properties when initializing Runtime.
  +             *  make a Properties 
  +             */
  +
  +            Properties p = new Properties();
  +
  +            /*
  +             *  now, if you want to, load it from a file (or whatever)
  +             */
  +            
  +            try
  +            {
  +                FileInputStream fis =  new FileInputStream( 
  +                    new File("velocity.properties" ));
  +            
  +                if( fis != null)
  +                    p.load( fis );
  +            }
  +            catch (Exception ex)
  +            {
  +                /* no worries. no file... */
  +            }
  +
  +            /*
  +             *  iterate out the properties
  +             */
  +
  +            for( Enumeration e = p.propertyNames(); e.hasMoreElements(); )
  +            {
  +                String el = (String) e.nextElement();
  +
  +                Velocity.setProperty( el, p.getProperty( el ) );
  +            }
  +
  +            /*
                *  add some individual properties if you wish
                */
   
  +
               Velocity.setProperty(Velocity.RUNTIME_LOG_ERROR_STACKTRACE, "true");
               Velocity.setProperty(Velocity.RUNTIME_LOG_WARN_STACKTRACE, "true");
               Velocity.setProperty(Velocity.RUNTIME_LOG_INFO_STACKTRACE, "true");
  +
  +            /*
  +             *  use an alternative logger.  Set it up here and pass it in.
  +             */
  +            
  +            SimpleLogSystem sls = new SimpleLogSystem("velocity_simple.log");
               
  +            Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, sls );
  +          
               /*
                *  and now call init
                */
  @@ -143,6 +190,7 @@
                   templateFile = "examples/example.vm";
               }                
            
  +
               Template template = null;
   
               try 
  @@ -196,14 +244,14 @@
               String stest = " My name is $name -> $Floog";
               StringWriter w = new StringWriter();
               Velocity.evaluate( context, w, "evaltest",stest );
  -            //System.out.println("Eval = " + w );
  +            System.out.println("Eval = " + w );
   
               w = new StringWriter();
  -            Velocity.mergeTemplate( "mergethis.vm",  context, w );
  +            //Velocity.mergeTemplate( "mergethis.vm",  context, w );
               //System.out.println("Merge = " + w );
   
               w = new StringWriter();
  -            Velocity.invokeVelocimacro( "floog", "test", new String[2],  context,  
w );
  +            //Velocity.invokeVelocimacro( "floog", "test", new String[2],  context, 
 w );
               //System.out.println("Invoke = " + w );
   
    
  
  
  

Reply via email to