jakarta-bsf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From orlikow...@apache.org
Subject cvs commit: jakarta-bsf/src/org/apache/bsf/test/engineTests jaclTest.java javascriptTest.java jythonTest.java
Date Mon, 26 Apr 2004 17:46:17 GMT
orlikowski    2004/04/26 10:46:17

  Modified:    .        build.xml
  Added:       src/org/apache/bsf/test BSFEngineTestTmpl.java BSFTest.java
                        fakeEngine.java
               src/org/apache/bsf/test/engineTests jaclTest.java
                        javascriptTest.java jythonTest.java
  Log:
  Add a test hierarchy to BSF; this is only a very basic set of
  tests; others may choose to make them more extensive.
  This commit will add a JUnit requirement to the build, if testing is
  desired.
  
  Revision  Changes    Path
  1.7       +23 -0     jakarta-bsf/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-bsf/build.xml,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- build.xml	25 Jun 2003 09:44:40 -0000	1.6
  +++ build.xml	26 Apr 2004 17:46:17 -0000	1.7
  @@ -140,6 +140,9 @@
       <available property="xalan.present"
                  classname="org.apache.xalan.xslt.EnvironmentCheck"/>
   
  +    <available property="junit.present"
  +               classname="junit.framework.TestCase"/>
  +
       <uptodate property="javac.notRequired"
                 targetfile="${build.lib}/${project.name}.jar">
         <srcfiles dir="${src.dir}">
  @@ -202,6 +205,9 @@
             Documentation will not be generated.
       </echo>
     </target>
  +  <target name="checkJUnit" unless="junit.present">
  +    <echo message="Warning: JUnit dependencies were not resolved."/>
  +  </target>
   
     <target name="checkDependencies" unless="javac.notRequired">
       <antcall target="checkJacl"/>
  @@ -210,6 +216,7 @@
       <antcall target="checkNetRexx"/>
       <antcall target="checkRhino"/>
       <antcall target="checkXalan"/>
  +    <antcall target="checkJUnit"/>
     </target>
   
   
  @@ -228,6 +235,7 @@
       <exclude name="**/jython/**" unless="jython.present"/>
       <exclude name="**/netrexx/**" unless="netrexx.present"/>
       <exclude name="**/xslt/**" unless="xalan.present"/>
  +    <exclude name="**/test/**" unless="junit.present"/>
     </patternset>
   
     <!-- =================================================================== -->
  @@ -304,6 +312,21 @@
              debug="${project.debug}"
              deprecation="${project.deprecation}"/>
     </target>
  +
  +   <!-- =================================================================== -->
  +   <!-- Performs unit tests                                                 -->
  +   <!-- =================================================================== -->
  +   <target name="test" if="junit.present"
  +           description="Performs unit tests on BSF."
  +           depends="compile">
  +     <junit>
  +        <classpath>
  +         <pathelement location="${build.lib}/${name}.jar" />
  +        </classpath>
  +        <formatter type="brief" usefile="false" />
  +        <test name="org.apache.bsf.test.BSFTest" />
  +     </junit>
  +   </target>
   
     <!-- =================================================================== -->
     <!-- Creates the API documentation                                       -->
  
  
  
  1.1                  jakarta-bsf/src/org/apache/bsf/test/BSFEngineTestTmpl.java
  
  Index: BSFEngineTestTmpl.java
  ===================================================================
  package org.apache.bsf.test;
  
  import java.io.ByteArrayOutputStream;
  import java.io.PrintStream;
  
  import junit.framework.TestCase;
  
  import org.apache.bsf.BSFManager;
  
  /**
   * Superclass for language engine tests.
   * @author   Victor J. Orlikowski <vjo@us.ibm.com>
   */
  public abstract class BSFEngineTestTmpl extends TestCase {
      protected BSFManager bsfManager;
      protected PrintStream sysOut;
  
      private PrintStream tmpOut;
      private ByteArrayOutputStream tmpBaos;
  
      public BSFEngineTestTmpl(String name) {
          super(name);
  
          sysOut = System.out;
          tmpBaos = new ByteArrayOutputStream();
          tmpOut = new PrintStream(tmpBaos);
      }
  
      public void setUp() {
          bsfManager = new BSFManager();
          System.setOut(tmpOut);
      }
  
      public void tearDown() {
          System.setOut(sysOut);
          resetTmpOut();
      }
      
      protected String getTmpOutStr() {
          return tmpBaos.toString();
      }
  
      protected void resetTmpOut() {
          tmpBaos.reset();
      }
      
      protected String failMessage(String failure, Exception e) {
          String message = failure;
          message += "\nReason:\n";
          message += e.getMessage();
          message += "\n";
          return message;
      }
  }
  
  
  
  1.1                  jakarta-bsf/src/org/apache/bsf/test/BSFTest.java
  
  Index: BSFTest.java
  ===================================================================
  package org.apache.bsf.test;
  
  import junit.framework.Test;
  import junit.framework.TestResult;
  import junit.framework.TestSuite;
  import junit.textui.TestRunner;
  
  import org.apache.bsf.BSFManager;
  
  import org.apache.bsf.test.engineTests.*;
  
  /**
   * Primary test class and testing front end for BSF.
   * @author   Victor J. Orlikowski <vjo@us.ibm.com>
   */
  public class BSFTest extends BSFEngineTestTmpl {
      public static String[] testNames;
      
      public BSFTest(String name) {
          super(name);
      }
  
      public static void main(String args[]) {
          TestRunner runner = new TestRunner();
          TestSuite suite = (TestSuite) suite();
          TestResult results;
  
          for (int i = 0; i < suite.testCount(); i++) {
              System.out.print(testNames[i]);
              results = runner.doRun(suite.testAt(i), false);
              System.out.println("Results: " + results.runCount() + 
                                 " tests run, " + results.failureCount() +
                                 " failures, " + results.errorCount() +
                                 " errors.");
              System.out.print("\n----------------------------------------\n");
          }
      }
  
      public static Test suite() { 
          /*
           * Please add testcases here as needed.
           */
          TestSuite suite = new TestSuite(); 
          testNames = new String [4];
  
          suite.addTestSuite(BSFTest.class);
          testNames[0] = "BSFManager Base Tests";
          suite.addTestSuite(jaclTest.class);
          testNames[1] = "Jacl Engine Tests";
          suite.addTestSuite(javascriptTest.class);
          testNames[2] = "Rhino Engine Tests";
          suite.addTestSuite(jythonTest.class);
          testNames[3] = "Jython Engine Tests";
          
          return suite;
      }
  
      public void setUp() {
          super.setUp();
          BSFManager.registerScriptingEngine("fakeEngine", 
                                             fakeEngine.class.getName(), 
                                             new String[] { "fakeEng", "fE" });
      }
  
      public void testRegisterEngine() {
          assertTrue(bsfManager.isLanguageRegistered("fakeEngine"));
      }
  
      public void testGetLangFromFileName() {
          try {
              assertEquals("fakeEngine", 
                           BSFManager.getLangFromFilename("Test.fE"));
          }
          catch (Exception e) {
              fail(failMessage("getLangFromFilename() test failed", e));
          }
      }
  
      public void testExec() {
          try {
              bsfManager.exec("fakeEngine", "Test.fE", 0, 0, "Fake syntax");
          }
          catch (Exception e) {
              fail(failMessage("exec() test failed", e));
          }
  
          assertEquals("PASSED", getTmpOutStr());
      }
  
      public void testEval() {
          Boolean retval = Boolean.FALSE;
  
          try {
              retval = (Boolean) bsfManager.eval("fakeEngine", 
                                                 "Test.fE", 0, 0,
                                                 "Fake Syntax");
          }
          catch (Exception e) {
              fail(failMessage("eval() test failed", e));
          }
          
          assertTrue(retval.booleanValue());
      }
  
      public void testIexec() {
          try {
              bsfManager.iexec("fakeEngine", "Test.fE", 0, 0, "Fake syntax");
          }
          catch (Exception e) {
              fail(failMessage("iexec() test failed", e));
          }
  
          assertEquals("PASSED", getTmpOutStr());
      }
  
      public void testDeclareBean() {
          try {
              bsfManager.declareBean("foo", new Integer(1), Integer.class);
          }
          catch (Exception e) {
              fail(failMessage("declareBean() test failed", e));
          }
  
          assertEquals(new Integer(1), (Integer) bsfManager.lookupBean("foo"));
      }
  
      public void testUndeclareBean() {
          try {
              bsfManager.declareBean("foo", new Integer(1), Integer.class);
              bsfManager.undeclareBean("foo");
          }
          catch (Exception e) {
              fail(failMessage("undeclareBean() test failed", e));
          }
  
          assertNull(bsfManager.lookupBean("foo"));
      }
  
      public void testTerminate() throws Exception {
          try {
              bsfManager.loadScriptingEngine("fakeEngine");
              bsfManager.terminate();
          }
          catch (Exception e) {
              fail(failMessage("terminate() test failed", e));
          }
  
          assertEquals("PASSED", getTmpOutStr());
      }
  }
  
  
  
  1.1                  jakarta-bsf/src/org/apache/bsf/test/fakeEngine.java
  
  Index: fakeEngine.java
  ===================================================================
  package org.apache.bsf.test;
  
  import org.apache.bsf.BSFException;
  import org.apache.bsf.util.BSFEngineImpl;
  
  public class fakeEngine extends BSFEngineImpl {
  
      public Object call(Object object, String method, Object[] args)
          throws BSFException
      {
          return Boolean.TRUE;
      }
  
      public Object eval(String source, int lineNo, int columnNo, Object expr)
          throws BSFException
      {
          return Boolean.TRUE;
      }
  
      public void iexec(String source, int lineNo, int columnNo, Object script)
          throws BSFException
      {
          System.out.print("PASSED");
      }
  
      public void exec(String source, int lineNo, int columnNo, Object script)
          throws BSFException
      {
          System.out.print("PASSED");
      }
  
      public void terminate() {
          super.terminate();
          System.out.print("PASSED");
      }
  }
  
  
  
  1.1                  jakarta-bsf/src/org/apache/bsf/test/engineTests/jaclTest.java
  
  Index: jaclTest.java
  ===================================================================
  package org.apache.bsf.test.engineTests;
  
  import org.apache.bsf.BSFEngine;
  import org.apache.bsf.BSFException;
  import org.apache.bsf.test.BSFEngineTestTmpl;
  
  /**
   * Test class for the jacl language engine.
   * @author   Victor J. Orlikowski <vjo@us.ibm.com>
   */
  public class jaclTest extends BSFEngineTestTmpl {
      private BSFEngine jaclEngine;
  
      public jaclTest(String name) {
          super(name);
      }
  
      public void setUp() {
          super.setUp();
  
          try {
              jaclEngine = bsfManager.loadScriptingEngine("jacl");
          }
          catch (Exception e) {
              fail(failMessage("Failure attempting to load jacl", e));
          }
      }
  
      public void testExec() {
          try {
              jaclEngine.exec("Test.jacl", 0, 0,
                              "puts -nonewline \"PASSED\"");
          }
          catch (Exception e) {
              fail(failMessage("exec() test failed", e));
          }
  
          assertEquals("PASSED", getTmpOutStr());
      }
      
      public void testEval() {
          Integer retval = null;
  
          try {
              retval =  (Integer) jaclEngine.eval("Test.jacl", 0, 0,
                                                  "expr 1 + 1");
          }
          catch (Exception e) {
              fail(failMessage("eval() test failed", e));
          }
  
          assertEquals(new Integer(2), retval);
      }
  
      public void testCall() {
          Object[] args = { new Integer(1) };
          Integer retval = null;
  
          try {
              jaclEngine.exec("Test.jacl", 0, 0,
                              "proc addOne {f} {\n return [expr $f + 1]\n}");
              retval = (Integer) jaclEngine.call(null, "addOne", args);
          }
          catch (Exception e) {
              fail(failMessage("call() test failed", e));
          }
  
          assertEquals(new Integer(2), retval);
      }
  
      public void testIexec() {
          try {
              jaclEngine.iexec("Test.jacl", 0, 0,
                               "puts -nonewline \"PASSED\"");
          }
          catch (Exception e) {
              fail(failMessage("iexec() test failed", e));
          }
  
          assertEquals("PASSED", getTmpOutStr());
      }
  
      public void testBSFManagerEval() {
          Integer retval = null;
  
          try {
              retval = (Integer) bsfManager.eval("jacl", "Test.jacl", 0, 0,
                                                 "expr 1 + 1");
          }
          catch (Exception e) {
              fail(failMessage("BSFManager eval() test failed", e));
          }
  
          assertEquals(new Integer(2), retval);
      }
  
      public void testRegisterBean() {
          Integer foo = new Integer(1);
          Integer bar = null;
  
          try {
              bsfManager.registerBean("foo", foo);
              bar = (Integer) jaclEngine.eval("Test.jacl", 0, 0,
                                              "bsf lookupBean \"foo\"");
          }
          catch (Exception e) {
              fail(failMessage("registerBean() test failed", e));
          }
  
          assertEquals(foo, bar);
      }
  
      public void testUnregisterBean() {
          Integer foo = new Integer(1);
          Integer bar = null;
  
          try {
              bsfManager.registerBean("foo", foo);
              bsfManager.unregisterBean("foo");
              bar = (Integer) jaclEngine.eval("Test.jacl", 0, 0,
                                              "bsf lookupBean \"foo\"");
          }
          catch (BSFException bsfE) {
              // Do nothing. This is the expected case.
          }
          catch (Exception e) {
              fail(failMessage("unregisterBean() test failed", e));
          }
  
          assertNull(bar);
      }
      
      public void testDeclareBean() {
          Integer foo = new Integer(1);
          Integer bar = null;
  
          try {
              bsfManager.declareBean("foo", foo, Integer.class);
              bar = (Integer)
                  jaclEngine.eval("Test.jacl", 0, 0,
                                  "proc ret {} {\n upvar 1 foo lfoo\n " +
                                  "return $lfoo\n }\n ret");
          }
          catch (Exception e) {
              fail(failMessage("declareBean() test failed", e));
          }
  
          assertEquals(foo, bar);
      }
  
      public void testUndeclareBean() {
          Integer foo = new Integer(1);
          Integer bar = null;
  
          try {
              bsfManager.declareBean("foo", foo, Integer.class);
              bsfManager.undeclareBean("foo");
              bar = (Integer)
                  jaclEngine.eval("Test.jacl", 0, 0,
                                  "expr $foo + 1");
          }
          catch (Exception e) {
              fail(failMessage("undeclareBean() test failed", e));
          }
  
          assertEquals(foo, bar);
      }
  }
  
  
  
  1.1                  jakarta-bsf/src/org/apache/bsf/test/engineTests/javascriptTest.java
  
  Index: javascriptTest.java
  ===================================================================
  package org.apache.bsf.test.engineTests;
  
  import org.apache.bsf.BSFEngine;
  import org.apache.bsf.BSFException;
  import org.apache.bsf.test.BSFEngineTestTmpl;
  
  /**
   * Test class for the Rhino language engine.
   * @author   Victor J. Orlikowski <vjo@us.ibm.com>
   */
  public class javascriptTest extends BSFEngineTestTmpl {
      private BSFEngine javascriptEngine;
  
      public javascriptTest(String name) {
          super(name);
      }
  
      public void setUp() {
          super.setUp();
  
          try {
              javascriptEngine = bsfManager.loadScriptingEngine("javascript");
          }
          catch (Exception e) {
              fail(failMessage("Failure attempting to load Rhino", e));
          }
      }
  
      public void testExec() {
          try {
              javascriptEngine.exec("Test.js", 0, 0,
                                    "java.lang.System.out.print " + 
                                    "(\"PASSED\");");
          }
          catch (Exception e) {
              fail(failMessage("exec() test failed", e));
          }
  
          assertEquals("PASSED", getTmpOutStr());
      }
      
      public void testEval() {
          Double retval = null;
  
          try {
              retval = new Double((javascriptEngine.eval("Test.js", 0, 0,
                                                         "1 + 1;").toString()));
          }
          catch (Exception e) {
              fail(failMessage("eval() test failed", e));
          }
  
          assertEquals(new Double(2), retval);
      }
  
      public void testCall() {
          Object[] args = { new Double(1) };
          Double retval = null;
  
          try {
              javascriptEngine.exec("Test.js", 0, 0,
                                    "function addOne (f) {\n return f + 1;\n}");
              retval = 
                  new Double((javascriptEngine.call(null, "addOne",
                                                    args).toString()));
          }
          catch (Exception e) {
              fail(failMessage("call() test failed", e));
          }
  
          assertEquals(new Double(2), retval);
      }
  
      public void testIexec() {
          try {
              javascriptEngine.iexec("Test.js", 0, 0,
                                     "java.lang.System.out.print " + 
                                     "(\"PASSED\");");
          }
          catch (Exception e) {
              fail(failMessage("iexec() test failed", e));
          }
  
          assertEquals("PASSED", getTmpOutStr());
      }
  
      public void testBSFManagerEval() {
          Double retval = null;
  
          try {
              retval = new Double((bsfManager.eval("javascript", "Test.js", 0,
                                                   0, "1 + 1;")).toString());
          }
          catch (Exception e) {
              fail(failMessage("BSFManager eval() test failed", e));
          }
  
          assertEquals(new Double(2), retval);
      }
      
      public void testBSFManagerAvailability() {
          Object retval = null;
  
          try {
              retval = javascriptEngine.eval("Test.js", 0, 0,
                                             "bsf.lookupBean(\"foo\");");
          }
          catch (Exception e) {
              fail(failMessage("Test of BSFManager availability failed", e));
          }
  
          assertNull(retval);
      }
  
      public void testRegisterBean() {
          Double foo = new Double(1);
          Double bar = null;
  
          try {
              bsfManager.registerBean("foo", foo);
              bar = (Double)
                  javascriptEngine.eval("Test.js", 0, 0,
                                        "bsf.lookupBean(\"foo\");");
          }
          catch (Exception e) {
              fail(failMessage("registerBean() test failed", e));
          }
  
          assertEquals(foo, bar);
      }
  
      public void testUnregisterBean() {
          Double foo = new Double(1);
          Double bar = null;
  
          try {
              bsfManager.registerBean("foo", foo);
              bsfManager.unregisterBean("foo");
              bar = (Double) 
                  javascriptEngine.eval("Test.js", 0, 0,
                                        "bsf.lookupBean(\"foo\");");
          }
          catch (Exception e) {
              fail(failMessage("unregisterBean() test failed", e));
          }
  
          assertNull(bar);
      }
      
      public void testDeclareBean() {
          Double foo = new Double(1);
          Double bar = null;
  
          try {
              bsfManager.declareBean("foo", foo, Double.class);
              bar = (Double) javascriptEngine.eval("Test.js", 0, 0, "foo + 1;");
          }
          catch (Exception e) {
              fail(failMessage("declareBean() test failed", e));
          }
  
          assertEquals(new Double(2), bar);
      }
  
      public void testUndeclareBean() {
          Double foo = new Double(1);
          Double bar = null;
  
          try {
              bsfManager.declareBean("foo", foo, Double.class);
              bsfManager.undeclareBean("foo");
              bar = (Double) javascriptEngine.eval("Test.js", 0, 0,
                                                   "foo + 1");
          }
          catch (BSFException bsfE) {
              // Do nothing. This is the expected case.
          }
          catch (Exception e) {
              fail(failMessage("undeclareBean() test failed", e));
          }
  
          assertNull(bar);
      }
  }
  
  
  
  1.1                  jakarta-bsf/src/org/apache/bsf/test/engineTests/jythonTest.java
  
  Index: jythonTest.java
  ===================================================================
  package org.apache.bsf.test.engineTests;
  
  import org.apache.bsf.BSFEngine;
  import org.apache.bsf.BSFException;
  import org.apache.bsf.test.BSFEngineTestTmpl;
  
  /**
   * Test class for the jython language engine.
   * @author   Victor J. Orlikowski <vjo@us.ibm.com>
   */
  public class jythonTest extends BSFEngineTestTmpl {
      private BSFEngine jythonEngine;
  
      public jythonTest(String name) {
          super(name);
      }
  
      public void setUp() {
          super.setUp();
          
          try {
              jythonEngine = bsfManager.loadScriptingEngine("jython");
          }
          catch (Exception e) {
              fail(failMessage("Failure attempting to load jython", e));
          }
      }
  
      public void testExec() {
          try {
              jythonEngine.exec("Test.py", 0, 0,
                                "print \"PASSED\",");
          }
          catch (Exception e) {
              fail(failMessage("exec() test failed", e));
          }
  
          assertEquals("PASSED", getTmpOutStr());
      }
  
      public void testEval() {
          Integer retval = null;
  
          try {
              retval = new Integer((jythonEngine.eval("Test.py", 0, 0,
                                                      "1 + 1")).toString());
          }
          catch (Exception e) {
              fail(failMessage("eval() test failed", e));
          }
  
          assertEquals(new Integer(2), retval);
      }
  
      public void testCall() {
          Object[] args = { new Integer(1) };
          Integer retval = null;
  
          try {
              jythonEngine.exec("Test.py", 0, 0,
                                "def addOne(f):\n\t return f + 1\n");
              retval = 
                  new Integer((jythonEngine.call(null, "addOne",
                                                 args).toString()));
          }
          catch (Exception e) {
              fail(failMessage("call() test failed", e));
          }
  
          assertEquals(new Integer(2), retval);
      }
  
      public void testIexec() {
          // iexec() differs from exec() in this engine, primarily
          // in that it only executes up to the first newline.
          try {
              jythonEngine.iexec("Test.py", 0, 0,
                                 "print \"PASSED\"," + "\n" + "print \"FAILED\",");
          }
          catch (Exception e) {
              fail(failMessage("iexec() test failed", e));
          }
          
          assertEquals("PASSED", getTmpOutStr());
      } 
  
      public void testBSFManagerEval() {
          Integer retval = null;
  
          try {
              retval = new Integer((bsfManager.eval("jython", "Test.py", 0, 0,
                                                    "1 + 1")).toString());
          }
          catch (Exception e) {
              fail(failMessage("BSFManager eval() test failed", e));
          }
  
          assertEquals(new Integer(2), retval);
      }
  
      public void testBSFManagerAvailability() {
          Object retval = null;
  
          try {
              retval = jythonEngine.eval("Test.py", 0, 0,
                                         "bsf.lookupBean(\"foo\")");
          }
          catch (Exception e) {
              fail(failMessage("Test of BSFManager availability failed", e));
          }
  
          assertEquals("None", retval.toString());
      }
  
      public void testRegisterBean() {
          Integer foo = new Integer(1);
          Integer bar = null;
  
          try {
              bsfManager.registerBean("foo", foo);
              bar = new Integer((jythonEngine.eval("Test.py", 0, 0,
                                                   "bsf.lookupBean(\"foo\")"))
                                .toString());
          }
          catch (Exception e) {
              fail(failMessage("registerBean() test failed", e));
          }
  
          assertEquals(foo, bar);
      }
  
      public void testUnregisterBean() {
          Integer foo = new Integer(1);
          Object bar = null;
  
          try {
              bsfManager.registerBean("foo", foo);
              bsfManager.unregisterBean("foo");
              bar = jythonEngine.eval("Test.py", 0, 0,
                                      "bsf.lookupBean(\"foo\")");
          }
          catch (Exception e) {
              fail(failMessage("unregisterBean() test failed", e));
          }
  
          assertEquals("None", bar.toString());
      }
  
      public void testDeclareBean() {
          Integer foo = new Integer(1);
          Integer bar = null;
  
          try {
              bsfManager.declareBean("foo", foo, Integer.class);
              bar = new Integer((jythonEngine.eval("Test.py", 0, 0,
                                                   "foo + 1")).toString());
          }
          catch (Exception e) {
              fail(failMessage("declareBean() test failed", e));
          }
  
          assertEquals(new Integer(2), bar);
      }
  
      public void testUndeclareBean() {
          Integer foo = new Integer(1);
          Integer bar = null;
  
          try {
              bsfManager.declareBean("foo", foo, Integer.class);
              bsfManager.undeclareBean("foo");
              bar = new Integer((jythonEngine.eval("Test.py", 0, 0,
                                                   "foo + 1")).toString());
          }
          catch (BSFException bsfE) {
              // Do nothing. This is the expected case.
          }
          catch (Exception e) {
              fail(failMessage("undeclareBean() test failed", e));
          }
  
          assertNull(bar);
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-dev-help@jakarta.apache.org


Mime
View raw message