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.Script;
21  
22  /***
23   * Tests for UseBean tag
24   */
25  public class TestUseBeanTag extends BaseJellyTest {
26  
27      public TestUseBeanTag(String name) {
28          super(name);
29      }
30  
31      public static TestSuite suite() throws Exception {
32          return new TestSuite(TestUseBeanTag.class);
33      }
34  
35      /***
36       * Test a simple useBean tag works ok
37       * @throws Exception
38       */
39      public void testSimple() throws Exception{
40          setUpScript("testUseBeanTag.jelly");
41          Script script = getJelly().compileScript();
42          getJellyContext().setVariable("test.simple",Boolean.TRUE);
43          script.run(getJellyContext(),getXMLOutput());
44          assertNotNull(getJellyContext().getVariable("foo"));
45          assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
46          Customer customer = (Customer)(getJellyContext().getVariable("foo"));
47          assertEquals("name not set", "testing", customer.getName());
48          assertEquals("city not set", "sydney", customer.getCity());
49      }
50  
51      /***
52       * test extension
53       */
54      public void testExtension() throws Exception {
55          setUpScript("testUseBeanTag.jelly");
56          Script script = getJelly().compileScript();
57          getJellyContext().setVariable("test.extension",Boolean.TRUE);
58          script.run(getJellyContext(),getXMLOutput());
59          assertNotNull(getJellyContext().getVariable("foo"));
60          assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
61          Customer customer = (Customer)(getJellyContext().getVariable("foo"));
62          assertNull("name set wrongly", customer.getName());
63          assertEquals("city not set", "sydney", customer.getCity());
64      }
65  
66      /*** Test set a bad property name on a bean, should fail.
67       * @throws Exception
68       */
69      public void testBadProperty() throws Exception {
70          setUpScript("testUseBeanTag.jelly");
71          Script script = getJelly().compileScript();
72          getJellyContext().setVariable("test.badProperty",Boolean.TRUE);
73          script.run(getJellyContext(),getXMLOutput());
74          Exception e = (Exception)getJellyContext().getVariable("ex");
75          assertNotNull("Should have failed to set invalid bean property", e);
76      }
77  
78      /*** Test set a bad property name on a bean, this should be silently ignored.
79       * @throws Exception
80       */
81      public void testIgnoredBadProperty() throws Exception {
82          setUpScript("testUseBeanTag.jelly");
83          Script script = getJelly().compileScript();
84          getJellyContext().setVariable("test.badPropertyIgnored",Boolean.TRUE);
85          script.run(getJellyContext(),getXMLOutput());
86          Customer customer = (Customer)(getJellyContext().getVariable("foo"));
87          assertNotNull("Should have ignored invalid bean property", customer);
88      }
89  }