001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.core;
017:
018: import java.util.Date;
019:
020: import junit.framework.TestSuite;
021:
022: import org.apache.commons.jelly.Script;
023: import org.apache.commons.jelly.core.Customer;
024: import org.apache.commons.jelly.test.BaseJellyTest;
025:
026: /**
027: * @author Rodney Waldhoff
028: * @version $Revision: 155420 $ $Date: 2005-02-27 00:06:03 +1100 (Sun, 27 Feb 2005) $
029: */
030: public class TestNewTag extends BaseJellyTest {
031:
032: public TestNewTag(String name) {
033: super (name);
034: }
035:
036: public static TestSuite suite() throws Exception {
037: return new TestSuite(TestNewTag.class);
038: }
039:
040: public void testSimpleNew() throws Exception {
041: setUpScript("testNewTag.jelly");
042: Script script = getJelly().compileScript();
043: getJellyContext().setVariable("test.simpleNew", Boolean.TRUE);
044: script.run(getJellyContext(), getXMLOutput());
045: assertNotNull(getJellyContext().getVariable("foo"));
046: assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
047: Customer customer = (Customer) (getJellyContext()
048: .getVariable("foo"));
049: assertNull(customer.getName());
050: }
051:
052: public void testNewThenOverwrite() throws Exception {
053: setUpScript("testNewTag.jelly");
054: Script script = getJelly().compileScript();
055: getJellyContext().setVariable("test.newThenOverwrite",
056: Boolean.TRUE);
057: script.run(getJellyContext(), getXMLOutput());
058: assertNotNull(getJellyContext().getVariable("foo"));
059: assertTrue(getJellyContext().getVariable("foo") instanceof Date);
060: }
061:
062: public void testNewWithLiteralArg() throws Exception {
063: setUpScript("testNewTag.jelly");
064: Script script = getJelly().compileScript();
065: getJellyContext().setVariable("test.newWithLiteralArg",
066: Boolean.TRUE);
067: script.run(getJellyContext(), getXMLOutput());
068: assertNotNull(getJellyContext().getVariable("foo"));
069: assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
070: Customer customer = (Customer) (getJellyContext()
071: .getVariable("foo"));
072: assertNotNull(customer.getName());
073: assertEquals("Jane Doe", customer.getName());
074: }
075:
076: public void testNewWithTwoArgs() throws Exception {
077: setUpScript("testNewTag.jelly");
078: Script script = getJelly().compileScript();
079: getJellyContext().setVariable("test.newWithTwoArgs",
080: Boolean.TRUE);
081: script.run(getJellyContext(), getXMLOutput());
082: assertNotNull(getJellyContext().getVariable("foo"));
083: assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
084: Customer customer = (Customer) (getJellyContext()
085: .getVariable("foo"));
086: assertNotNull(customer.getName());
087: assertEquals("Jane Doe", customer.getName());
088: assertNotNull(customer.getCity());
089: assertEquals("Chicago", customer.getCity());
090: }
091:
092: public void testNewWithExpressionArg() throws Exception {
093: setUpScript("testNewTag.jelly");
094: Script script = getJelly().compileScript();
095: getJellyContext().setVariable("test.newWithExpressionArg",
096: Boolean.TRUE);
097: script.run(getJellyContext(), getXMLOutput());
098: assertNotNull(getJellyContext().getVariable("foo"));
099: assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
100: Customer customer = (Customer) (getJellyContext()
101: .getVariable("foo"));
102: assertNotNull(customer.getName());
103: assertEquals("Jane Doe", customer.getName());
104: }
105:
106: public void testNewWithNullArg() throws Exception {
107: setUpScript("testNewTag.jelly");
108: Script script = getJelly().compileScript();
109: getJellyContext().setVariable("test.newWithNullArg",
110: Boolean.TRUE);
111: script.run(getJellyContext(), getXMLOutput());
112: assertNotNull(getJellyContext().getVariable("foo"));
113: assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
114: Customer customer = (Customer) (getJellyContext()
115: .getVariable("foo"));
116: assertNull(customer.getName());
117: }
118:
119: public void testNewWithNewArg() throws Exception {
120: setUpScript("testNewTag.jelly");
121: Script script = getJelly().compileScript();
122: getJellyContext().setVariable("test.newWithNewArg",
123: Boolean.TRUE);
124: script.run(getJellyContext(), getXMLOutput());
125: {
126: assertNotNull(getJellyContext().getVariable("foo"));
127: assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
128: Customer customer = (Customer) (getJellyContext()
129: .getVariable("foo"));
130: assertNotNull(customer.getName());
131: assertEquals("", customer.getName());
132: }
133: {
134: assertNotNull(getJellyContext().getVariable("bar"));
135: assertTrue(getJellyContext().getVariable("bar") instanceof Customer);
136: Customer customer = (Customer) (getJellyContext()
137: .getVariable("bar"));
138: assertEquals("Jane Doe", customer.getName());
139: assertEquals("Chicago", customer.getCity());
140: assertNotNull(customer.getOrders());
141: assertEquals(1, customer.getOrders().size());
142: assertNotNull(customer.getOrders().get(0));
143: }
144: {
145: assertNotNull(getJellyContext().getVariable("qux"));
146: assertTrue(getJellyContext().getVariable("qux") instanceof Customer);
147: Customer customer = (Customer) (getJellyContext()
148: .getVariable("qux"));
149: assertEquals("Jane Doe", customer.getName());
150: assertEquals("Chicago", customer.getCity());
151: assertNotNull(customer.getOrders());
152: assertEquals(1, customer.getOrders().size());
153: assertNotNull(customer.getOrders().get(0));
154: }
155: }
156:
157: public void testNewWithUseBeanArg() throws Exception {
158: setUpScript("testNewTag.jelly");
159: Script script = getJelly().compileScript();
160: getJellyContext().setVariable("test.newWithUseBeanArg",
161: Boolean.TRUE);
162: script.run(getJellyContext(), getXMLOutput());
163: assertNotNull(getJellyContext().getVariable("foo"));
164: assertTrue(getJellyContext().getVariable("foo") instanceof Customer);
165: Customer customer = (Customer) (getJellyContext()
166: .getVariable("foo"));
167: assertEquals("Jane Doe", customer.getName());
168: assertEquals("Chicago", customer.getCity());
169: assertEquals("Location", customer.getLocation());
170: }
171: }
|