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