    1   /*
    2    $Id: BSFTest.java 4254 2006-11-23 20:38:19Z blackdrag $
    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
    8    that the following conditions are met:
    9
    10   1. Redistributions of source code must retain copyright
    11      statements and notices.  Redistributions must also contain a
    12      copy of this document.
    13
    14   2. Redistributions in binary form must reproduce the
    15      above copyright notice, this list of conditions and the
    16      following disclaimer in the documentation and/or other
    17      materials provided with the distribution.
    18
    19   3. The name "groovy" must not be used to endorse or promote
    20      products derived from this Software without prior written
    21      permission of The Codehaus.  For written permission,
    22      please contact info@codehaus.org.
    23
    24   4. Products derived from this Software may not be called "groovy"
    25      nor may "groovy" appear in their names without prior written
    26      permission of The Codehaus. "groovy" is a registered
    27      trademark of The Codehaus.
    28
    29   5. Due credit should be given to The Codehaus -
    30      http://groovy.codehaus.org/
    31
    32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
    33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
    34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    43   OF THE POSSIBILITY OF SUCH DAMAGE.
    44
    45   */
    46  package org.codehaus.groovy.bsf;
    47
    48  import java.io.File;
    49  import java.util.List;
    50  import java.util.Vector;
    51
    52  import junit.framework.TestCase;
    53
    54  import org.apache.bsf.BSFManager;
    55  import org.apache.bsf.BSFException;
    56  import org.apache.bsf.BSFEngine;
    57  import org.codehaus.groovy.runtime.DefaultGroovyMethods;
    58
    59  /***
    60   * Tests the BSF integration
    61   *
    62   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
    63   * @author Paul King
    64   * @version $Revision: 4254 $
    65   */
    66  public class BSFTest extends TestCase {
    67
    68      protected BSFManager manager;
    69
    70      protected void setUp() throws Exception {
    71          manager = new BSFManager();
    72      }
    73
    74      public void testInvalidName() throws Exception {
    75          manager.exec("groovy", null, 0, 0, "assert bsf != null");
    76          manager.exec("groovy", "", 0, 0, "assert bsf != null");
    77          manager.exec("groovy", "-", 0, 0, "assert bsf != null");
    78      }
    79
    80      public void testCompileErrorWithExec() throws Exception {
    81          try {
    82              manager.exec("groovy", "dummy", 0, 0, "assert assert");
    83              fail("Should have caught compile exception");
    84          } catch (BSFException e) {
    85              assertTrue("e.getMessage() should contain CompilationError: " + e.getMessage(),
    86                      e.getMessage().indexOf("CompilationError") != -1);
    87          }
    88      }
    89
    90      public void testCompileErrorWithEval() throws Exception {
    91          try {
    92              manager.eval("groovy", "dummy", 0, 0, "assert assert");
    93              fail("Should have caught compile exception");
    94          } catch (BSFException e) {
    95              assertTrue("e.getMessage() should contain CompilationError: " + e.getMessage(),
    96                      e.getMessage().indexOf("CompilationError") != -1);
    97          }
    98      }
    99
    100     public void testExec() throws Exception {
    101         manager.exec("groovy", "Test1.groovy", 0, 0, "assert bsf != null , 'should have a bsf variable'");
    102     }
    103
    104     public void testApplyWithClosure() throws Exception {
    105         Vector ignoreParamNames = null;
    106         Vector ignoreArgs = null;
    107         Integer actual = (Integer) manager.apply("groovy", "applyTest", 0, 0,
    108                 "251", ignoreParamNames, ignoreArgs);
    109         assertEquals(251, actual.intValue());
    110     }
    111
    112     public void testApply() throws Exception {
    113         Vector ignoreParamNames = null;
    114         Vector args = new Vector();
    115         args.add(new Integer(2));
    116         args.add(new Integer(5));
    117         args.add(new Integer(1));
    118         Integer actual = (Integer) manager.apply("groovy", "applyTest", 0, 0,
    119                 "def summer = { a, b, c -> a * 100 + b * 10 + c }", ignoreParamNames, args);
    120         assertEquals(251, actual.intValue());
    121     }
    122
    123     public void testBracketName() throws Exception {
    124         manager.exec("groovy", "Test1<groovy>", 0, 0, "assert bsf != null , 'should have a bsf variable'");
    125     }
    126
    127     public void testEval() throws Exception {
    128         Object answer = manager.eval("groovy", "Test1.groovy", 0, 0, "return [1, 2, 3]");
    129         assertTrue("Should return a list: " + answer, answer instanceof List);
    130         List list = (List) answer;
    131         assertEquals("List should be of right size", 3, list.size());
    132     }
    133
    134     public void testTwoEvalsWithSameName() throws Exception {
    135         Object answer = manager.eval("groovy", "Test1.groovy", 0, 0, "return 'cheese'");
    136         assertEquals("cheese", answer);
    137         answer = manager.eval("groovy", "Test1.groovy", 0, 0, "return 'gromit'");
    138         assertEquals("gromit", answer);
    139     }
    140
    141     public void testExecBug() throws Exception {
    142         for (int i = 0; i < 10; i++) {
    143             manager.exec("groovy", "Test1.groovy", 0, 0, "assert true");
    144             manager.exec("groovy", "Test1.groovy", 0, 0, "assert true");
    145         }
    146     }
    147
    148     public void testBsfVariables() throws Exception {
    149         Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
    150                 "assert this.bsf != null\n  return this.bsf");
    151         assertTrue("Should have an answer", answer != null);
    152     }
    153
    154     public void testNotFoundVariables() throws Exception {
    155         manager.registerBean("x", new Integer(4));
    156         Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
    157                 "def valueOfX = this.bsf.lookupBean('y'); assert valueOfX == null");
    158         assertNull("Undeclared beans should yield null", answer);
    159     }
    160
    161     public void testRegisteredVariables() throws Exception {
    162         manager.registerBean("x", new Integer(4));
    163         Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
    164                 "def valueOfX = this.bsf.lookupBean('x'); assert valueOfX == 4; valueOfX + 1");
    165         assertEquals("Incorrect return", new Integer(5), answer);
    166     }
    167
    168     public void testUnregisteredVariables() throws Exception {
    169         manager.registerBean("x", new Integer(4));
    170         Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
    171                 "def valueOfX = this.bsf.lookupBean('x'); assert valueOfX == 4; valueOfX + 1");
    172         assertEquals("Incorrect return", new Integer(5), answer);
    173         manager.unregisterBean("x");
    174         // have to lookup registered beans
    175         answer = manager.eval("groovy", "Test1.groovy", 0, 0,
    176                 "def valueOfX = this.bsf.lookupBean('x'); assert valueOfX == null");
    177         assertNull("Unregistered beans should yield null", answer);
    178     }
    179
    180     public void testDeclaredVariables() throws Exception {
    181         manager.declareBean("xyz", new Integer(4), Integer.class);
    182         Object answer = manager.eval("groovy", "Test1.groovy", 0, 0, "xyz + 1");
    183         assertEquals("Incorrect return", new Integer(5), answer);
    184     }
    185
    186     public void testUndeclaredVariables() throws Exception {
    187         manager.declareBean("abc", new Integer(4), Integer.class);
    188         // declared beans should just be available
    189         Object answer = manager.eval("groovy", "Test1.groovy", 0, 0, "abc + 1");
    190         assertEquals("Incorrect return", new Integer(5), answer);
    191         manager.undeclareBean("abc");
    192         answer = manager.eval("groovy", "Test1.groovy", 0, 0, "abc");
    193         assertNull("Undeclared beans should yield null", answer);
    194     }
    195
    196     public void testCall() throws Exception {
    197         BSFEngine bsfEngine = manager.loadScriptingEngine("groovy");
    198         manager.declareBean("myvar", "hello", String.class);
    199         Object myvar = manager.lookupBean("myvar");
    200         String result = (String) bsfEngine.call(myvar, "reverse", new Object[]{});
    201         assertEquals("olleh", result);
    202     }
    203
    204     public void testExecFile() throws Exception {
    205         execScript("src/test/groovy/script/MapFromList.groovy");
    206         execScript("src/test/groovy/script/AtomTestScript.groovy");
    207     }
    208
    209     protected void execScript(String fileName) throws Exception {
    210         manager.exec("groovy", fileName, 0, 0, DefaultGroovyMethods.getText(new File(fileName)));
    211     }
    212 }