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  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().getVariable("foo"));
48          assertEquals("name not set", "testing", customer.getName());
49          assertEquals("city not set", "sydney", customer.getCity());
50      }
51  
52      /***
53       * test extension
54       */
55      public void testExtension() throws Exception {
56          setUpScript("testUseBeanTag.jelly");
57          Script script = getJelly().compileScript();
58          getJellyContext().setVariable("test.extension",Boolean.TRUE);
59          script.run(getJellyContext(),getXMLOutput());
60          assertNotNull(getJellyContext().getVariable("foo"));
61          assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
62          Customer customer = (Customer)(getJellyContext().getVariable("foo"));
63          assertNull("name set wrongly", customer.getName());
64          assertEquals("city not set", "sydney", customer.getCity());
65      }
66  
67      /*** Test set a bad property name on a bean, should fail.
68       * @throws Exception
69       */
70      public void testBadProperty() throws Exception {
71          setUpScript("testUseBeanTag.jelly");
72          Script script = getJelly().compileScript();
73          getJellyContext().setVariable("test.badProperty",Boolean.TRUE);
74          script.run(getJellyContext(),getXMLOutput());
75          Exception e = (Exception)getJellyContext().getVariable("ex");
76          assertNotNull("Should have failed to set invalid bean property", e);
77      }
78  
79      /*** Test set a bad property name on a bean, this should be silently ignored.
80       * @throws Exception
81       */
82      public void testIgnoredBadProperty() throws Exception {
83          setUpScript("testUseBeanTag.jelly");
84          Script script = getJelly().compileScript();
85          getJellyContext().setVariable("test.badPropertyIgnored",Boolean.TRUE);
86          script.run(getJellyContext(),getXMLOutput());
87          Customer customer = (Customer)(getJellyContext().getVariable("foo"));
88          assertNotNull("Should have ignored invalid bean property", customer);
89      }
90  }