KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > beanshell > BeanShellFunctionProviderTestCase


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.util.beanshell;
6
7 import com.opensymphony.module.propertyset.PropertySet;
8 import com.opensymphony.module.propertyset.database.JDBCPropertySet;
9
10 import com.opensymphony.workflow.AbstractWorkflow;
11 import com.opensymphony.workflow.Workflow;
12 import com.opensymphony.workflow.basic.BasicWorkflow;
13 import com.opensymphony.workflow.config.Configuration;
14 import com.opensymphony.workflow.config.DefaultConfiguration;
15
16 import junit.framework.TestCase;
17
18 import org.apache.commons.lang.exception.ExceptionUtils;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23
24 /**
25  * Unit test to prove that the BeanShellFunctionProvider eats the key exception
26  * you need. It tells you what line the script fails, but not the underlying
27  * solution.
28  *
29  * @author Eric Pugh (epugh@upstate.com)
30  */

31 public class BeanShellFunctionProviderTestCase extends TestCase {
32     //~ Constructors ///////////////////////////////////////////////////////////
33

34     public BeanShellFunctionProviderTestCase(String JavaDoc s) {
35         super(s);
36     }
37
38     //~ Methods ////////////////////////////////////////////////////////////////
39

40     /**
41      * Test that an exception that is thrown while processing a script is properly
42      * dealt with.
43      *
44      * In this example, by not starting up a JDBC DataSource, the BeanShell script fails.
45      * It should record the underlying lcoation of the NullPointerError JDBC error, but instead you get a null pointer
46      * exception location of the script instead.
47      *
48      * What I want somewhere:
49      * java.lang.NullPointerException
50     at com.opensymphony.module.propertyset.database.JDBCPropertySet.setImpl(JDBCPropertySet.java:267)
51     at com.opensymphony.module.propertyset.AbstractPropertySet.set(AbstractPropertySet.java:565)
52     at com.opensymphony.module.propertyset.AbstractPropertySet.setString(AbstractPropertySet.java:363)
53     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
54     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
55     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
56     at java.lang.reflect.Method.invoke(Method.java:324)
57     at bsh.Reflect.invokeMethod(Unknown Source)
58     at bsh.Reflect.invokeObjectMethod(Unknown Source)
59     at bsh.Name.invokeMethod(Unknown Source)
60     at bsh.BSHMethodInvocation.eval(Unknown Source)
61      *
62      */

63     public void testThrowingException() throws Exception JavaDoc {
64         Configuration config = new DefaultConfiguration();
65         config.load(getClass().getResource("/osworkflow-jdbc.xml"));
66
67         Workflow workflow = new BasicWorkflow("test");
68         workflow.setConfiguration(config);
69
70         // long workflowId = workflow.initialize(getClass().getResource("/example.xml").toString(), 1, new HashMap());
71
BeanShellFunctionProvider function = new BeanShellFunctionProvider();
72         PropertySet ps = new JDBCPropertySet();
73         Map JavaDoc transientVars = new HashMap JavaDoc();
74         Map JavaDoc args = new HashMap JavaDoc();
75         args.put(AbstractWorkflow.BSH_SCRIPT, "String caller = \"testcaller\"; propertySet.setString(\"caller\", caller);");
76
77         try {
78             function.execute(transientVars, args, ps);
79         } catch (Exception JavaDoc e) {
80             String JavaDoc fullStackTrace = ExceptionUtils.getFullStackTrace(e);
81             assertTrue("Make sure our stack trace records the error in JDBC:" + fullStackTrace, fullStackTrace.indexOf("at com.opensymphony.module.propertyset.database.JDBCPropertySet.setImpl") > -1);
82         }
83     }
84 }
85
Popular Tags