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 java.util.Date;
19  
20  import junit.framework.TestSuite;
21  
22  import org.apache.commons.jelly.Script;
23  import org.apache.commons.jelly.core.Customer;
24  
25  /***
26   * @author Rodney Waldhoff
27   * @version $Revision: 1.7 $ $Date: 2004/09/09 12:29:35 $
28   */
29  public class TestNewTag extends BaseJellyTest {
30  
31      public TestNewTag(String name) {
32          super(name);
33      }
34  
35      public static TestSuite suite() throws Exception {
36          return new TestSuite(TestNewTag.class);
37      }
38  
39      public void testSimpleNew() throws Exception {
40          setUpScript("testNewTag.jelly");
41          Script script = getJelly().compileScript();
42          getJellyContext().setVariable("test.simpleNew",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          assertNull(customer.getName());
48      }
49  
50      public void testNewThenOverwrite() throws Exception {
51          setUpScript("testNewTag.jelly");
52          Script script = getJelly().compileScript();
53          getJellyContext().setVariable("test.newThenOverwrite",Boolean.TRUE);
54          script.run(getJellyContext(),getXMLOutput());
55          assertNotNull(getJellyContext().getVariable("foo"));
56          assertTrue(getJellyContext().getVariable("foo") instanceof Date);
57      }
58  
59      public void testNewWithLiteralArg() throws Exception {
60          setUpScript("testNewTag.jelly");
61          Script script = getJelly().compileScript();
62          getJellyContext().setVariable("test.newWithLiteralArg",Boolean.TRUE);
63          script.run(getJellyContext(),getXMLOutput());
64          assertNotNull(getJellyContext().getVariable("foo"));
65          assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
66          Customer customer = (Customer)(getJellyContext().getVariable("foo"));
67          assertNotNull(customer.getName());
68          assertEquals("Jane Doe",customer.getName());
69      }
70  
71      public void testNewWithTwoArgs() throws Exception {
72          setUpScript("testNewTag.jelly");
73          Script script = getJelly().compileScript();
74          getJellyContext().setVariable("test.newWithTwoArgs",Boolean.TRUE);
75          script.run(getJellyContext(),getXMLOutput());
76          assertNotNull(getJellyContext().getVariable("foo"));
77          assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
78          Customer customer = (Customer)(getJellyContext().getVariable("foo"));
79          assertNotNull(customer.getName());
80          assertEquals("Jane Doe",customer.getName());
81          assertNotNull(customer.getCity());
82          assertEquals("Chicago",customer.getCity());
83      }
84  
85      public void testNewWithExpressionArg() throws Exception {
86          setUpScript("testNewTag.jelly");
87          Script script = getJelly().compileScript();
88          getJellyContext().setVariable("test.newWithExpressionArg",Boolean.TRUE);
89          script.run(getJellyContext(),getXMLOutput());
90          assertNotNull(getJellyContext().getVariable("foo"));
91          assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
92          Customer customer = (Customer)(getJellyContext().getVariable("foo"));
93          assertNotNull(customer.getName());
94          assertEquals("Jane Doe",customer.getName());
95      }
96  
97      public void testNewWithNullArg() throws Exception {
98          setUpScript("testNewTag.jelly");
99          Script script = getJelly().compileScript();
100         getJellyContext().setVariable("test.newWithNullArg",Boolean.TRUE);
101         script.run(getJellyContext(),getXMLOutput());
102         assertNotNull(getJellyContext().getVariable("foo"));
103         assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
104         Customer customer = (Customer)(getJellyContext().getVariable("foo"));
105         assertNull(customer.getName());
106     }
107 
108     public void testNewWithNewArg() throws Exception {
109         setUpScript("testNewTag.jelly");
110         Script script = getJelly().compileScript();
111         getJellyContext().setVariable("test.newWithNewArg",Boolean.TRUE);
112         script.run(getJellyContext(),getXMLOutput());
113         {
114             assertNotNull(getJellyContext().getVariable("foo"));
115             assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
116             Customer customer = (Customer)(getJellyContext().getVariable("foo"));
117             assertNotNull(customer.getName());
118             assertEquals("",customer.getName());
119         }
120         {
121             assertNotNull(getJellyContext().getVariable("bar"));
122             assertTrue(getJellyContext().getVariable("bar") instanceof Customer);
123             Customer customer = (Customer)(getJellyContext().getVariable("bar"));
124             assertEquals("Jane Doe",customer.getName());
125             assertEquals("Chicago",customer.getCity());
126             assertNotNull(customer.getOrders());
127             assertEquals(1,customer.getOrders().size());
128             assertNotNull(customer.getOrders().get(0));
129         }
130         {
131             assertNotNull(getJellyContext().getVariable("qux"));
132             assertTrue(getJellyContext().getVariable("qux") instanceof Customer);
133             Customer customer = (Customer)(getJellyContext().getVariable("qux"));
134             assertEquals("Jane Doe",customer.getName());
135             assertEquals("Chicago",customer.getCity());
136             assertNotNull(customer.getOrders());
137             assertEquals(1,customer.getOrders().size());
138             assertNotNull(customer.getOrders().get(0));
139         }
140     }
141 
142     public void testNewWithUseBeanArg() throws Exception {
143         setUpScript("testNewTag.jelly");
144         Script script = getJelly().compileScript();
145         getJellyContext().setVariable("test.newWithUseBeanArg",Boolean.TRUE);
146         script.run(getJellyContext(),getXMLOutput());
147         assertNotNull(getJellyContext().getVariable("foo"));
148         assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
149         Customer customer = (Customer)(getJellyContext().getVariable("foo"));
150         assertEquals("Jane Doe",customer.getName());
151         assertEquals("Chicago",customer.getCity());
152         assertEquals("Location",customer.getLocation());
153     }
154 }