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