001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.core;
017:
018: import junit.framework.TestSuite;
019:
020: import org.apache.commons.jelly.JellyException;
021: import org.apache.commons.jelly.Script;
022: import org.apache.commons.jelly.core.Customer;
023: import org.apache.commons.jelly.test.BaseJellyTest;
024:
025: /**
026: * @author Rodney Waldhoff
027: * @version $Revision: 155420 $ $Date: 2005-02-27 00:06:03 +1100 (Sun, 27 Feb 2005) $
028: */
029: public class TestInvokeTag extends BaseJellyTest {
030:
031: public TestInvokeTag(String name) {
032: super (name);
033: }
034:
035: public static TestSuite suite() throws Exception {
036: return new TestSuite(TestInvokeTag.class);
037: }
038:
039: public void setUp() throws Exception {
040: super .setUp();
041: }
042:
043: public void tearDown() throws Exception {
044: super .tearDown();
045: }
046:
047: public void testSimpleInvoke() throws Exception {
048: setUpScript("testInvokeTag.jelly");
049: Script script = getJelly().compileScript();
050: getJellyContext()
051: .setVariable("test.simpleInvoke", Boolean.TRUE);
052: script.run(getJellyContext(), getXMLOutput());
053: assertNotNull(getJellyContext().getVariable("foo"));
054: assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
055: Customer customer = (Customer) (getJellyContext()
056: .getVariable("foo"));
057: assertEquals("Jane Doe", customer.getName());
058: assertEquals("Chicago", customer.getCity());
059: assertNotNull(customer.getOrders());
060: assertEquals(1, customer.getOrders().size());
061: assertNotNull(customer.getOrders().get(0));
062: }
063:
064: public void testInvokeWithVar() throws Exception {
065: setUpScript("testInvokeTag.jelly");
066: Script script = getJelly().compileScript();
067: getJellyContext().setVariable("test.invokeWithVar",
068: Boolean.TRUE);
069: script.run(getJellyContext(), getXMLOutput());
070: assertNotNull(getJellyContext().getVariable("size"));
071: assertTrue(getJellyContext().getVariable("size") instanceof Integer);
072: Integer size = (Integer) (getJellyContext().getVariable("size"));
073: assertEquals(3, size.intValue());
074: }
075:
076: public void testInvokeWithReturnedValueAsArg() throws Exception {
077: setUpScript("testInvokeTag.jelly");
078: Script script = getJelly().compileScript();
079: getJellyContext().setVariable(
080: "test.invokeWithReturnedValueAsArg", Boolean.TRUE);
081: script.run(getJellyContext(), getXMLOutput());
082: assertNotNull(getJellyContext().getVariable("customer"));
083: assertTrue(getJellyContext().getVariable("customer") instanceof Customer);
084: Customer customer = (Customer) (getJellyContext()
085: .getVariable("customer"));
086: assertEquals("Jane Doe", customer.getName());
087: assertEquals("Chicago", customer.getCity());
088: }
089:
090: public void testInvokeWithReturnedValueAsArgAndVar()
091: throws Exception {
092: setUpScript("testInvokeTag.jelly");
093: Script script = getJelly().compileScript();
094: getJellyContext()
095: .setVariable("test.invokeWithReturnedValueAsArgAndVar",
096: Boolean.TRUE);
097: script.run(getJellyContext(), getXMLOutput());
098: assertNotNull(getJellyContext().getVariable("customer"));
099: assertTrue(getJellyContext().getVariable("customer") instanceof Customer);
100: Customer customer = (Customer) (getJellyContext()
101: .getVariable("customer"));
102: assertEquals("Jane Doe", customer.getName());
103: assertEquals("Chicago", customer.getCity());
104: assertNotNull(getJellyContext().getVariable("argtwo"));
105: assertEquals("Chicago", getJellyContext().getVariable("argtwo"));
106: }
107:
108: public void testInvokeThatThrowsException() throws Exception {
109: setUpScript("testInvokeTag.jelly");
110: Script script = getJelly().compileScript();
111: getJellyContext().setVariable("test.invokeThatThrowsException",
112: Boolean.TRUE);
113: script.run(getJellyContext(), getXMLOutput());
114: String exceptionMessage = (String) getJellyContext()
115: .getVariable("exceptionMessage");
116: assertNotNull(exceptionMessage);
117: assertNotNull(getJellyContext().getVariable("exceptionBean"));
118: Exception jellyException = (Exception) getJellyContext()
119: .getVariable("jellyException");
120: assertNull(jellyException);
121: Exception exception = (Exception) getJellyContext()
122: .getVariable("exceptionThrown");
123: assertNotNull(exception);
124: assertEquals(exceptionMessage, exception.getMessage());
125: }
126:
127: public void testInvokeThatDoesNotHandleException() throws Exception {
128: setUpScript("testInvokeTag.jelly");
129: Script script = getJelly().compileScript();
130: getJellyContext().setVariable(
131: "test.invokeThatDoesNotHandleException", Boolean.TRUE);
132: script.run(getJellyContext(), getXMLOutput());
133: String exceptionMessage = (String) getJellyContext()
134: .getVariable("exceptionMessage");
135: assertNotNull(exceptionMessage);
136: assertNotNull(getJellyContext().getVariable("exceptionBean"));
137: JellyException jellyException = (JellyException) getJellyContext()
138: .getVariable("jellyException");
139: assertNotNull(jellyException);
140: assertTrue("messages are the same", !exceptionMessage
141: .equals(jellyException.getMessage()));
142: assertTrue("exception '" + jellyException.getMessage()
143: + "' does not ends with '" + exceptionMessage + "'",
144: jellyException.getMessage().endsWith(exceptionMessage));
145: assertNotNull(jellyException.getCause());
146: assertEquals(exceptionMessage, jellyException.getCause()
147: .getMessage());
148: }
149:
150: }
|