1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.core;
17  
18  import junit.framework.TestSuite;
19  
20  import org.apache.commons.jelly.JellyException;
21  import org.apache.commons.jelly.Script;
22  import org.apache.commons.jelly.core.Customer;
23  
24  /***
25   * @author Rodney Waldhoff
26   * @version $Revision: 1.8 $ $Date: 2004/09/08 04:36:52 $
27   */
28  public class TestInvokeTag extends BaseJellyTest {
29  
30      public TestInvokeTag(String name) {
31          super(name);
32      }
33  
34      public static TestSuite suite() throws Exception {
35          return new TestSuite(TestInvokeTag.class);
36      }
37  
38      public void setUp() throws Exception {
39          super.setUp();
40      }
41  
42      public void tearDown() throws Exception {
43          super.tearDown();
44      }
45  
46      public void testSimpleInvoke() throws Exception {
47          setUpScript("testInvokeTag.jelly");
48          Script script = getJelly().compileScript();
49          getJellyContext().setVariable("test.simpleInvoke",Boolean.TRUE);
50          script.run(getJellyContext(),getXMLOutput());
51          assertNotNull(getJellyContext().getVariable("foo"));
52          assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
53          Customer customer = (Customer)(getJellyContext().getVariable("foo"));
54          assertEquals("Jane Doe",customer.getName());
55          assertEquals("Chicago",customer.getCity());
56          assertNotNull(customer.getOrders());
57          assertEquals(1,customer.getOrders().size());
58          assertNotNull(customer.getOrders().get(0));
59      }
60  
61      public void testInvokeWithVar() throws Exception {
62          setUpScript("testInvokeTag.jelly");
63          Script script = getJelly().compileScript();
64          getJellyContext().setVariable("test.invokeWithVar",Boolean.TRUE);
65          script.run(getJellyContext(),getXMLOutput());
66          assertNotNull(getJellyContext().getVariable("size"));
67          assertTrue(getJellyContext().getVariable("size") instanceof Integer);
68          Integer size = (Integer)(getJellyContext().getVariable("size"));
69          assertEquals(3,size.intValue());
70      }
71  
72      public void testInvokeWithReturnedValueAsArg() throws Exception {
73          setUpScript("testInvokeTag.jelly");
74          Script script = getJelly().compileScript();
75          getJellyContext().setVariable("test.invokeWithReturnedValueAsArg",Boolean.TRUE);
76          script.run(getJellyContext(),getXMLOutput());
77          assertNotNull(getJellyContext().getVariable("customer"));
78          assertTrue(getJellyContext().getVariable("customer") instanceof Customer);
79          Customer customer = (Customer)(getJellyContext().getVariable("customer"));
80          assertEquals("Jane Doe",customer.getName());
81          assertEquals("Chicago",customer.getCity());
82      }
83  
84      public void testInvokeWithReturnedValueAsArgAndVar() throws Exception {
85          setUpScript("testInvokeTag.jelly");
86          Script script = getJelly().compileScript();
87          getJellyContext().setVariable("test.invokeWithReturnedValueAsArgAndVar",Boolean.TRUE);
88          script.run(getJellyContext(),getXMLOutput());
89          assertNotNull(getJellyContext().getVariable("customer"));
90          assertTrue(getJellyContext().getVariable("customer") instanceof Customer);
91          Customer customer = (Customer)(getJellyContext().getVariable("customer"));
92          assertEquals("Jane Doe",customer.getName());
93          assertEquals("Chicago",customer.getCity());
94          assertNotNull(getJellyContext().getVariable("argtwo"));
95          assertEquals("Chicago",getJellyContext().getVariable("argtwo"));
96      }
97  
98      public void testInvokeThatThrowsException() throws Exception {
99          setUpScript("testInvokeTag.jelly");
100         Script script = getJelly().compileScript();
101         getJellyContext().setVariable("test.invokeThatThrowsException",Boolean.TRUE);
102         script.run(getJellyContext(),getXMLOutput());
103         String exceptionMessage = (String) getJellyContext().getVariable("exceptionMessage");
104         assertNotNull( exceptionMessage );
105         assertNotNull( getJellyContext().getVariable("exceptionBean"));
106         Exception jellyException = (Exception) getJellyContext().getVariable("jellyException");
107         assertNull( jellyException );
108         Exception exception = (Exception) getJellyContext().getVariable("exceptionThrown");
109         assertNotNull( exception );
110         assertEquals( exceptionMessage, exception.getMessage() );
111     }
112 
113     public void testInvokeThatDoesNotHandleException() throws Exception {
114         setUpScript("testInvokeTag.jelly");
115         Script script = getJelly().compileScript();
116         getJellyContext().setVariable("test.invokeThatDoesNotHandleException",Boolean.TRUE);
117         script.run(getJellyContext(),getXMLOutput());
118         String exceptionMessage = (String) getJellyContext().getVariable("exceptionMessage");
119         assertNotNull( exceptionMessage );
120         assertNotNull( getJellyContext().getVariable("exceptionBean"));
121         JellyException jellyException = (JellyException) getJellyContext().getVariable("jellyException");
122         assertNotNull( jellyException );
123         assertTrue( "messages are the same", ! exceptionMessage.equals(jellyException.getMessage()) );
124         assertTrue( "exception '" + jellyException.getMessage() + "' does not ends with '" +
125                 exceptionMessage+"'", jellyException.getMessage().endsWith(exceptionMessage) );
126         assertNotNull( jellyException.getCause() );
127         assertEquals( exceptionMessage, jellyException.getCause().getMessage() );
128     }
129 
130 
131 }