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.JellyException;
21  import org.apache.commons.jelly.MissingAttributeException;
22  import org.apache.commons.jelly.Script;
23  import org.apache.commons.jelly.test.BaseJellyTest;
24  
25  /***
26   * @author Rodney Waldhoff
27   * @version $Revision: 155420 $ $Date: 2005-02-26 14:06:03 +0100 (Sat, 26 Feb 2005) $
28   */
29  public class TestSwitchTag extends BaseJellyTest {
30  
31      public TestSwitchTag(String name) {
32          super(name);
33      }
34  
35      public static TestSuite suite() throws Exception {
36          return new TestSuite(TestSwitchTag.class);
37      }
38  
39      public void setUp() throws Exception {
40          super.setUp();
41      }
42  
43      public void tearDown() throws Exception {
44          super.tearDown();
45      }
46  
47      public void testSimpleSwitch() throws Exception {
48          setUpScript("testSwitchTag.jelly");
49          Script script = getJelly().compileScript();
50          getJellyContext().setVariable("switch.on.a","two");
51          script.run(getJellyContext(),getXMLOutput());
52          assertNull("should not have 'a.one' variable set",
53                     getJellyContext().getVariable("a.one"));
54          assertTrue("should have set 'a.two' variable to 'true'",
55                     getJellyContext().getVariable("a.two").equals("true"));
56          assertNull("should not have 'a.three' variable set",
57                     getJellyContext().getVariable("a.three"));
58          assertNull("should not have 'a.null' variable set",
59                     getJellyContext().getVariable("a.null"));
60          assertNull("should not have 'a.default' variable set",
61                     getJellyContext().getVariable("a.default"));
62      }
63  
64      public void testFallThru() throws Exception {
65          setUpScript("testSwitchTag.jelly");
66          Script script = getJelly().compileScript();
67          getJellyContext().setVariable("switch.on.a","one");
68          script.run(getJellyContext(),getXMLOutput());
69          assertTrue("should have set 'a.one' variable to 'true'",
70                     getJellyContext().getVariable("a.one").equals("true"));
71          assertTrue("should have set 'a.two' variable to 'true'",
72                     getJellyContext().getVariable("a.two").equals("true"));
73          assertNull("should not have 'a.three' variable set",
74                     getJellyContext().getVariable("a.three"));
75          assertNull("should not have 'a.null' variable set",
76                     getJellyContext().getVariable("a.null"));
77          assertNull("should not have 'a.default' variable set",
78                     getJellyContext().getVariable("a.default"));
79      }
80  
81      public void testDefault() throws Exception {
82          setUpScript("testSwitchTag.jelly");
83          Script script = getJelly().compileScript();
84          getJellyContext().setVariable("switch.on.a","negative one");
85          script.run(getJellyContext(),getXMLOutput());
86          assertNull("should not have 'a.one' variable set",
87                     getJellyContext().getVariable("a.one"));
88          assertNull("should not have 'a.two' variable set",
89                     getJellyContext().getVariable("a.two"));
90          assertNull("should not have 'a.three' variable set",
91                     getJellyContext().getVariable("a.three"));
92          assertNull("should not have 'a.null' variable set",
93                     getJellyContext().getVariable("a.null"));
94          assertTrue("should have set 'a.default' variable to 'true'",
95                     getJellyContext().getVariable("a.default").equals("true"));
96      }
97  
98      public void testNullCase() throws Exception {
99          setUpScript("testSwitchTag.jelly");
100         Script script = getJelly().compileScript();
101         getJellyContext().setVariable("switch.on.a",null);
102         script.run(getJellyContext(),getXMLOutput());
103         assertNull("should not have 'a.one' variable set",
104                    getJellyContext().getVariable("a.one"));
105         assertNull("should not have 'a.two' variable set",
106                    getJellyContext().getVariable("a.two"));
107         assertNull("should not have 'a.three' variable set",
108                    getJellyContext().getVariable("a.three"));
109         assertTrue("should have set 'a.null' variable to 'true'",
110                    getJellyContext().getVariable("a.null").equals("true"));
111         assertNull("should not have 'a.default' variable set",
112                    getJellyContext().getVariable("a.default"));
113     }
114 
115     public void testSwitchWithoutOn() throws Exception {
116         setUpScript("testSwitchTag.jelly");
117         Script script = getJelly().compileScript();
118         getJellyContext().setVariable("switch.without.on",new Boolean(true));
119         try {
120             script.run(getJellyContext(),getXMLOutput());
121             fail("Expected MissingAttributeException");
122         } catch(MissingAttributeException e) {
123             // expected
124         }
125     }
126 
127     public void testCaseWithoutSwitch() throws Exception {
128         setUpScript("testSwitchTag.jelly");
129         Script script = getJelly().compileScript();
130         getJellyContext().setVariable("case.without.switch",new Boolean(true));
131         try {
132             script.run(getJellyContext(),getXMLOutput());
133             fail("Expected JellyException");
134         } catch(JellyException e) {
135             // expected
136         }
137     }
138 
139     public void testDefaultWithoutSwitch() throws Exception {
140         setUpScript("testSwitchTag.jelly");
141         Script script = getJelly().compileScript();
142         getJellyContext().setVariable("default.without.switch",new Boolean(true));
143         try {
144             script.run(getJellyContext(),getXMLOutput());
145             fail("Expected JellyException");
146         } catch(JellyException e) {
147             // expected
148         }
149     }
150 
151     public void testCaseWithoutValue() throws Exception {
152         setUpScript("testSwitchTag.jelly");
153         Script script = getJelly().compileScript();
154         getJellyContext().setVariable("case.without.value",new Boolean(true));
155         try {
156             script.run(getJellyContext(),getXMLOutput());
157             fail("Expected MissingAttributeException");
158         } catch(MissingAttributeException e) {
159             // expected
160         }
161     }
162 
163     public void testMultipleDefaults() throws Exception {
164         setUpScript("testSwitchTag.jelly");
165         Script script = getJelly().compileScript();
166         getJellyContext().setVariable("multiple.defaults",new Boolean(true));
167         try {
168             script.run(getJellyContext(),getXMLOutput());
169             fail("Expected JellyException");
170         } catch(JellyException e) {
171             // expected
172         }
173     }
174 
175     public void testCaseAfterDefault() throws Exception {
176         setUpScript("testSwitchTag.jelly");
177         Script script = getJelly().compileScript();
178         getJellyContext().setVariable("case.after.default",new Boolean(true));
179         try {
180             script.run(getJellyContext(),getXMLOutput());
181             fail("Expected JellyException");
182         } catch(JellyException e) {
183             // expected
184         }
185     }
186 
187     public void testSeveralCall() throws Exception {
188         setUpScript("testSeveralSwitchCall.jelly");
189         Script script = getJelly().compileScript();
190         getJellyContext().setVariable("var","foo");
191         script.run(getJellyContext(),getXMLOutput());        
192         assertEquals("defaultdefault",getJellyContext().getVariable("res"));
193     }
194 
195 }