    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  import org.apache.commons.jelly.test.BaseJellyTest;
    24
    25  /***
    26   * @author Rodney Waldhoff
    27   * @version $Revision: 155420 $ $Date: 2005-02-27 00:06:03 +1100 (Sun, 27 Feb 2005) $
    28   */
    29  public class TestInvokeTag extends BaseJellyTest {
    30
    31      public TestInvokeTag(String name) {
    32          super(name);
    33      }
    34
    35      public static TestSuite suite() throws Exception {
    36          return new TestSuite(TestInvokeTag.class);
    37      }
    38
    39      public void setUp() throws Exception {
    40          super.setUp();
    41      }
    42
    43      public void tearDown() throws Exception {
    44          super.tearDown();
    45      }
    46
    47      public void testSimpleInvoke() throws Exception {
    48          setUpScript("testInvokeTag.jelly");
    49          Script script = getJelly().compileScript();
    50          getJellyContext().setVariable("test.simpleInvoke",Boolean.TRUE);
    51          script.run(getJellyContext(),getXMLOutput());
    52          assertNotNull(getJellyContext().getVariable("foo"));
    53          assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
    54          Customer customer = (Customer)(getJellyContext().getVariable("foo"));
    55          assertEquals("Jane Doe",customer.getName());
    56          assertEquals("Chicago",customer.getCity());
    57          assertNotNull(customer.getOrders());
    58          assertEquals(1,customer.getOrders().size());
    59          assertNotNull(customer.getOrders().get(0));
    60      }
    61
    62      public void testInvokeWithVar() throws Exception {
    63          setUpScript("testInvokeTag.jelly");
    64          Script script = getJelly().compileScript();
    65          getJellyContext().setVariable("test.invokeWithVar",Boolean.TRUE);
    66          script.run(getJellyContext(),getXMLOutput());
    67          assertNotNull(getJellyContext().getVariable("size"));
    68          assertTrue(getJellyContext().getVariable("size") instanceof Integer);
    69          Integer size = (Integer)(getJellyContext().getVariable("size"));
    70          assertEquals(3,size.intValue());
    71      }
    72
    73      public void testInvokeWithReturnedValueAsArg() throws Exception {
    74          setUpScript("testInvokeTag.jelly");
    75          Script script = getJelly().compileScript();
    76          getJellyContext().setVariable("test.invokeWithReturnedValueAsArg",Boolean.TRUE);
    77          script.run(getJellyContext(),getXMLOutput());
    78          assertNotNull(getJellyContext().getVariable("customer"));
    79          assertTrue(getJellyContext().getVariable("customer") instanceof Customer);
    80          Customer customer = (Customer)(getJellyContext().getVariable("customer"));
    81          assertEquals("Jane Doe",customer.getName());
    82          assertEquals("Chicago",customer.getCity());
    83      }
    84
    85      public void testInvokeWithReturnedValueAsArgAndVar() throws Exception {
    86          setUpScript("testInvokeTag.jelly");
    87          Script script = getJelly().compileScript();
    88          getJellyContext().setVariable("test.invokeWithReturnedValueAsArgAndVar",Boolean.TRUE);
    89          script.run(getJellyContext(),getXMLOutput());
    90          assertNotNull(getJellyContext().getVariable("customer"));
    91          assertTrue(getJellyContext().getVariable("customer") instanceof Customer);
    92          Customer customer = (Customer)(getJellyContext().getVariable("customer"));
    93          assertEquals("Jane Doe",customer.getName());
    94          assertEquals("Chicago",customer.getCity());
    95          assertNotNull(getJellyContext().getVariable("argtwo"));
    96          assertEquals("Chicago",getJellyContext().getVariable("argtwo"));
    97      }
    98
    99      public void testInvokeThatThrowsException() throws Exception {
    100         setUpScript("testInvokeTag.jelly");
    101         Script script = getJelly().compileScript();
    102         getJellyContext().setVariable("test.invokeThatThrowsException",Boolean.TRUE);
    103         script.run(getJellyContext(),getXMLOutput());
    104         String exceptionMessage = (String) getJellyContext().getVariable("exceptionMessage");
    105         assertNotNull( exceptionMessage );
    106         assertNotNull( getJellyContext().getVariable("exceptionBean"));
    107         Exception jellyException = (Exception) getJellyContext().getVariable("jellyException");
    108         assertNull( jellyException );
    109         Exception exception = (Exception) getJellyContext().getVariable("exceptionThrown");
    110         assertNotNull( exception );
    111         assertEquals( exceptionMessage, exception.getMessage() );
    112     }
    113
    114     public void testInvokeThatDoesNotHandleException() throws Exception {
    115         setUpScript("testInvokeTag.jelly");
    116         Script script = getJelly().compileScript();
    117         getJellyContext().setVariable("test.invokeThatDoesNotHandleException",Boolean.TRUE);
    118         script.run(getJellyContext(),getXMLOutput());
    119         String exceptionMessage = (String) getJellyContext().getVariable("exceptionMessage");
    120         assertNotNull( exceptionMessage );
    121         assertNotNull( getJellyContext().getVariable("exceptionBean"));
    122         JellyException jellyException = (JellyException) getJellyContext().getVariable("jellyException");
    123         assertNotNull( jellyException );
    124         assertTrue( "messages are the same", ! exceptionMessage.equals(jellyException.getMessage()) );
    125         assertTrue( "exception '" + jellyException.getMessage() + "' does not ends with '" +
    126                 exceptionMessage+"'", jellyException.getMessage().endsWith(exceptionMessage) );
    127         assertNotNull( jellyException.getCause() );
    128         assertEquals( exceptionMessage, jellyException.getCause().getMessage() );
    129     }
    130
    131
    132 }