    1   /*
    2    * $Id: CacheBSFTest.java 4166 2006-10-25 07:07:33Z paulk $
    3    *
    4    * Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
    5    *
    6    * Redistribution and use of this software and associated documentation
    7    * ("Software"), with or without modification, are permitted provided that the
    8    * following conditions are met: 1. Redistributions of source code must retain
    9    * copyright statements and notices. Redistributions must also contain a copy
    10   * of this document. 2. Redistributions in binary form must reproduce the above
    11   * copyright notice, this list of conditions and the following disclaimer in
    12   * the documentation and/or other materials provided with the distribution. 3.
    13   * The name "groovy" must not be used to endorse or promote products derived
    14   * from this Software without prior written permission of The Codehaus. For
    15   * written permission, please contact info@codehaus.org. 4. Products derived
    16   * from this Software may not be called "groovy" nor may "groovy" appear in
    17   * their names without prior written permission of The Codehaus. "groovy" is a
    18   * registered trademark of The Codehaus. 5. Due credit should be given to The
    19   * Codehaus - http://groovy.codehaus.org/
    20   *
    21   * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY
    22   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    23   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    24   * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR
    25   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    27   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    28   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    29   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    30   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
    31   * DAMAGE.
    32   *
    33   */
    34  package org.codehaus.groovy.bsf;
    35
    36  import java.util.List;
    37  import java.util.Vector;
    38
    39  import junit.framework.TestCase;
    40
    41  import org.apache.bsf.BSFManager;
    42  import org.apache.bsf.BSFEngine;
    43  import org.apache.bsf.BSFException;
    44  import groovy.lang.GroovyShell;
    45
    46  /***
    47   * Tests the Caching BSF integration
    48   *
    49   * @author James Birchfield
    50   * @version $Revision: 4166 $
    51   */
    52  public class CacheBSFTest extends TestCase {
    53
    54      protected BSFManager manager;
    55      private static final Class CACHING_ENGINE = CachingGroovyEngine.class;
    56
    57      protected void setUp() throws Exception {
    58          // override standard engine with caching one
    59          BSFManager.registerScriptingEngine("groovy", CACHING_ENGINE.getName(), new String[] { "groovy", "gy" });
    60          manager = new BSFManager();
    61      }
    62
    63      public void testVersion() throws Exception {
    64          //System.out.println("BSFManager.getVersion() = " + BSFManager.getVersion());
    65          BSFEngine bsfEngine = manager.loadScriptingEngine("groovy");
    66          assertEquals(CACHING_ENGINE, bsfEngine.getClass());
    67      }
    68
    69      public void testExec() throws Exception {
    70          manager.exec("groovy", "Test1.groovy", 0, 0, "println('testing Exec')");
    71          //nothing to really test here...just looking for debug that says it
    72          // used cache version
    73          manager.exec("groovy", "Test1.groovy", 0, 0, "println('testing Exec')");
    74      }
    75
    76      public void testCompileErrorWithExec() throws Exception {
    77          try {
    78              manager.exec("groovy", "dummy", 0, 0, "assert assert");
    79              fail("Should have caught compile exception");
    80          } catch (BSFException e) {
    81              assertTrue("e.getMessage() should contain CompilationError: " + e.getMessage(),
    82                      e.getMessage().indexOf("CompilationError") != -1);
    83          }
    84      }
    85
    86      public void testEval() throws Exception {
    87          Object dontcare = manager.eval("groovy", "Test1.groovy", 0, 0, "return [1, 2, 3]");
    88          // nothing to really test here...just looking for debug that says it
    89          // used cache version
    90          Object answer = manager.eval("groovy", "Test.groovy", 0, 0, "return [1, 2, 3]");
    91          assertTrue("Should return a list: " + answer, answer instanceof List);
    92          List list = (List) answer;
    93          assertEquals("List should be of right size", 3, list.size());
    94      }
    95
    96      public void testCompileErrorWithEval() throws Exception {
    97          try {
    98              manager.eval("groovy", "dummy", 0, 0, "assert assert");
    99              fail("Should have caught compile exception");
    100         } catch (BSFException e) {
    101             assertTrue("e.getMessage() should contain CompilationError: " + e.getMessage(),
    102                     e.getMessage().indexOf("CompilationError") != -1);
    103         }
    104     }
    105
    106     public void testBuiltInVariable() throws Exception {
    107         Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
    108                 "assert this.bsf != null\n  return this.bsf");
    109         assertTrue("Should have an answer", answer != null);
    110     }
    111
    112     public void testVariables() throws Exception {
    113         manager.registerBean("x", new Integer(4));
    114         Object dontcare = manager.eval("groovy", "Test1.groovy", 0, 0,
    115                 "valueOfX = this.bsf.lookupBean('x'); assert valueOfX == 4; valueOfX + 1");
    116         // nothing to really test here...just looking for debug that says it
    117         // used cache version
    118         Object answer = manager.eval("groovy", "Test2.groovy", 0, 0,
    119                 "valueOfX = this.bsf.lookupBean('x'); assert valueOfX == 4; valueOfX + 1");
    120         assertEquals("Incorrect return", new Integer(5), answer);
    121     }
    122
    123     public void testClassLoaderSet() throws BSFException {
    124         CachingGroovyEngine cachingGroovyEngine = new CachingGroovyEngine();
    125         manager.setClassLoader(null);
    126         cachingGroovyEngine.initialize(manager, "dummy", new Vector());
    127         // still working implies classloader set, coverage confirms this
    128         assertEquals("hi", manager.eval("groovy", "dummy", 0, 0, "'hi'"));
    129     }
    130
    131     public void testDeclaredVariables() throws Exception {
    132         manager.declareBean("foo", new Integer(5), Integer.class);
    133         Object answer = manager.eval("groovy", "Test1.groovy", 0, 0, "valueOfFoo = foo; return valueOfFoo");
    134         assertEquals(new Integer(5), answer);
    135         manager.declareBean("foo", new Integer(6), Integer.class);
    136         answer = manager.eval("groovy", "Test2.groovy", 0, 0, "valueOfFoo = foo; return valueOfFoo");
    137         assertEquals(new Integer(6), answer);
    138     }
    139 }