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 junit.framework.TestSuite;
019:
020: import org.apache.commons.jelly.JellyException;
021: import org.apache.commons.jelly.MissingAttributeException;
022: import org.apache.commons.jelly.Script;
023: import org.apache.commons.jelly.test.BaseJellyTest;
024:
025: /**
026: * @author Rodney Waldhoff
027: * @version $Revision: 155420 $ $Date: 2005-02-27 00:06:03 +1100 (Sun, 27 Feb 2005) $
028: */
029: public class TestSwitchTag extends BaseJellyTest {
030:
031: public TestSwitchTag(String name) {
032: super (name);
033: }
034:
035: public static TestSuite suite() throws Exception {
036: return new TestSuite(TestSwitchTag.class);
037: }
038:
039: public void setUp() throws Exception {
040: super .setUp();
041: }
042:
043: public void tearDown() throws Exception {
044: super .tearDown();
045: }
046:
047: public void testSimpleSwitch() throws Exception {
048: setUpScript("testSwitchTag.jelly");
049: Script script = getJelly().compileScript();
050: getJellyContext().setVariable("switch.on.a", "two");
051: script.run(getJellyContext(), getXMLOutput());
052: assertNull("should not have 'a.one' variable set",
053: getJellyContext().getVariable("a.one"));
054: assertTrue("should have set 'a.two' variable to 'true'",
055: getJellyContext().getVariable("a.two").equals("true"));
056: assertNull("should not have 'a.three' variable set",
057: getJellyContext().getVariable("a.three"));
058: assertNull("should not have 'a.null' variable set",
059: getJellyContext().getVariable("a.null"));
060: assertNull("should not have 'a.default' variable set",
061: getJellyContext().getVariable("a.default"));
062: }
063:
064: public void testFallThru() throws Exception {
065: setUpScript("testSwitchTag.jelly");
066: Script script = getJelly().compileScript();
067: getJellyContext().setVariable("switch.on.a", "one");
068: script.run(getJellyContext(), getXMLOutput());
069: assertTrue("should have set 'a.one' variable to 'true'",
070: getJellyContext().getVariable("a.one").equals("true"));
071: assertTrue("should have set 'a.two' variable to 'true'",
072: getJellyContext().getVariable("a.two").equals("true"));
073: assertNull("should not have 'a.three' variable set",
074: getJellyContext().getVariable("a.three"));
075: assertNull("should not have 'a.null' variable set",
076: getJellyContext().getVariable("a.null"));
077: assertNull("should not have 'a.default' variable set",
078: getJellyContext().getVariable("a.default"));
079: }
080:
081: public void testDefault() throws Exception {
082: setUpScript("testSwitchTag.jelly");
083: Script script = getJelly().compileScript();
084: getJellyContext().setVariable("switch.on.a", "negative one");
085: script.run(getJellyContext(), getXMLOutput());
086: assertNull("should not have 'a.one' variable set",
087: getJellyContext().getVariable("a.one"));
088: assertNull("should not have 'a.two' variable set",
089: getJellyContext().getVariable("a.two"));
090: assertNull("should not have 'a.three' variable set",
091: getJellyContext().getVariable("a.three"));
092: assertNull("should not have 'a.null' variable set",
093: getJellyContext().getVariable("a.null"));
094: assertTrue("should have set 'a.default' variable to 'true'",
095: getJellyContext().getVariable("a.default").equals(
096: "true"));
097: }
098:
099: public void testNullCase() throws Exception {
100: setUpScript("testSwitchTag.jelly");
101: Script script = getJelly().compileScript();
102: getJellyContext().setVariable("switch.on.a", null);
103: script.run(getJellyContext(), getXMLOutput());
104: assertNull("should not have 'a.one' variable set",
105: getJellyContext().getVariable("a.one"));
106: assertNull("should not have 'a.two' variable set",
107: getJellyContext().getVariable("a.two"));
108: assertNull("should not have 'a.three' variable set",
109: getJellyContext().getVariable("a.three"));
110: assertTrue("should have set 'a.null' variable to 'true'",
111: getJellyContext().getVariable("a.null").equals("true"));
112: assertNull("should not have 'a.default' variable set",
113: getJellyContext().getVariable("a.default"));
114: }
115:
116: public void testSwitchWithoutOn() throws Exception {
117: setUpScript("testSwitchTag.jelly");
118: Script script = getJelly().compileScript();
119: getJellyContext().setVariable("switch.without.on",
120: new Boolean(true));
121: try {
122: script.run(getJellyContext(), getXMLOutput());
123: fail("Expected MissingAttributeException");
124: } catch (MissingAttributeException e) {
125: // expected
126: }
127: }
128:
129: public void testCaseWithoutSwitch() throws Exception {
130: setUpScript("testSwitchTag.jelly");
131: Script script = getJelly().compileScript();
132: getJellyContext().setVariable("case.without.switch",
133: new Boolean(true));
134: try {
135: script.run(getJellyContext(), getXMLOutput());
136: fail("Expected JellyException");
137: } catch (JellyException e) {
138: // expected
139: }
140: }
141:
142: public void testDefaultWithoutSwitch() throws Exception {
143: setUpScript("testSwitchTag.jelly");
144: Script script = getJelly().compileScript();
145: getJellyContext().setVariable("default.without.switch",
146: new Boolean(true));
147: try {
148: script.run(getJellyContext(), getXMLOutput());
149: fail("Expected JellyException");
150: } catch (JellyException e) {
151: // expected
152: }
153: }
154:
155: public void testCaseWithoutValue() throws Exception {
156: setUpScript("testSwitchTag.jelly");
157: Script script = getJelly().compileScript();
158: getJellyContext().setVariable("case.without.value",
159: new Boolean(true));
160: try {
161: script.run(getJellyContext(), getXMLOutput());
162: fail("Expected MissingAttributeException");
163: } catch (MissingAttributeException e) {
164: // expected
165: }
166: }
167:
168: public void testMultipleDefaults() throws Exception {
169: setUpScript("testSwitchTag.jelly");
170: Script script = getJelly().compileScript();
171: getJellyContext().setVariable("multiple.defaults",
172: new Boolean(true));
173: try {
174: script.run(getJellyContext(), getXMLOutput());
175: fail("Expected JellyException");
176: } catch (JellyException e) {
177: // expected
178: }
179: }
180:
181: public void testCaseAfterDefault() throws Exception {
182: setUpScript("testSwitchTag.jelly");
183: Script script = getJelly().compileScript();
184: getJellyContext().setVariable("case.after.default",
185: new Boolean(true));
186: try {
187: script.run(getJellyContext(), getXMLOutput());
188: fail("Expected JellyException");
189: } catch (JellyException e) {
190: // expected
191: }
192: }
193:
194: public void testSeveralCall() throws Exception {
195: setUpScript("testSeveralSwitchCall.jelly");
196: Script script = getJelly().compileScript();
197: getJellyContext().setVariable("var", "foo");
198: script.run(getJellyContext(), getXMLOutput());
199: assertEquals("defaultdefault", getJellyContext().getVariable(
200: "res"));
201: }
202:
203: }
|