01: /*
02: * Copyright 2002,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.Script;
21: import org.apache.commons.jelly.test.BaseJellyTest;
22:
23: /**
24: * Tests for UseBean tag
25: */
26: public class TestUseBeanTag extends BaseJellyTest {
27:
28: public TestUseBeanTag(String name) {
29: super (name);
30: }
31:
32: public static TestSuite suite() throws Exception {
33: return new TestSuite(TestUseBeanTag.class);
34: }
35:
36: /**
37: * Test a simple useBean tag works ok
38: * @throws Exception
39: */
40: public void testSimple() throws Exception {
41: setUpScript("testUseBeanTag.jelly");
42: Script script = getJelly().compileScript();
43: getJellyContext().setVariable("test.simple", Boolean.TRUE);
44: script.run(getJellyContext(), getXMLOutput());
45: assertNotNull(getJellyContext().getVariable("foo"));
46: assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
47: Customer customer = (Customer) (getJellyContext()
48: .getVariable("foo"));
49: assertEquals("name not set", "testing", customer.getName());
50: assertEquals("city not set", "sydney", customer.getCity());
51: }
52:
53: /**
54: * test extension
55: */
56: public void testExtension() throws Exception {
57: setUpScript("testUseBeanTag.jelly");
58: Script script = getJelly().compileScript();
59: getJellyContext().setVariable("test.extension", Boolean.TRUE);
60: script.run(getJellyContext(), getXMLOutput());
61: assertNotNull(getJellyContext().getVariable("foo"));
62: assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
63: Customer customer = (Customer) (getJellyContext()
64: .getVariable("foo"));
65: assertNull("name set wrongly", customer.getName());
66: assertEquals("city not set", "sydney", customer.getCity());
67: }
68:
69: /** Test set a bad property name on a bean, should fail.
70: * @throws Exception
71: */
72: public void testBadProperty() throws Exception {
73: setUpScript("testUseBeanTag.jelly");
74: Script script = getJelly().compileScript();
75: getJellyContext().setVariable("test.badProperty", Boolean.TRUE);
76: script.run(getJellyContext(), getXMLOutput());
77: Exception e = (Exception) getJellyContext().getVariable("ex");
78: assertNotNull(
79: "Should have failed to set invalid bean property", e);
80: }
81:
82: /** Test set a bad property name on a bean, this should be silently ignored.
83: * @throws Exception
84: */
85: public void testIgnoredBadProperty() throws Exception {
86: setUpScript("testUseBeanTag.jelly");
87: Script script = getJelly().compileScript();
88: getJellyContext().setVariable("test.badPropertyIgnored",
89: Boolean.TRUE);
90: script.run(getJellyContext(), getXMLOutput());
91: Customer customer = (Customer) (getJellyContext()
92: .getVariable("foo"));
93: assertNotNull("Should have ignored invalid bean property",
94: customer);
95: }
96: }
|