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.Script;
22  
23  /***
24   * @author <a href="mailto:robert@bull-enterprises.com">Robert McIntosh</a>
25   * @version $Revision: 1.8 $
26   */
27  public class TestInvokeStaticTag extends BaseJellyTest {
28  
29      public TestInvokeStaticTag(String name) {
30          super(name);
31      }
32  
33      public static TestSuite suite() throws Exception {
34          return new TestSuite(TestInvokeStaticTag.class);
35      }
36  
37      public void setUp() throws Exception {
38          super.setUp();
39      }
40  
41      public void tearDown() throws Exception {
42          super.tearDown();
43      }
44  
45      /***
46       *  Gets the System property 'java.runtime.version' and compares it with,
47       *  well, the same system property
48       */
49       public void testSimpleSystemInvoke() throws Exception {
50          setUpScript( "testInvokeStaticTag.jelly" );
51          Script script = getJelly().compileScript();
52  
53          getJellyContext().setVariable( "test.simpleSystemInvoke",Boolean.TRUE );
54  
55          getJellyContext().setVariable( "propertyName", "java.runtime.version" );
56          script.run( getJellyContext(),getXMLOutput() );
57  
58          assertTrue( System.getProperty( "java.runtime.version" ).equals( getJellyContext().getVariable("propertyName" ) ) );
59      }
60  
61       /***
62       *  Sets the System property 'TEST PROPERTY' to the value 'Jelly is cool' and compares it with,
63       *  well, the same system property
64       */
65      public void testSystemInvoke() throws Exception {
66          setUpScript( "testInvokeStaticTag.jelly" );
67          Script script = getJelly().compileScript();
68  
69          getJellyContext().setVariable( "test.systemInvoke",Boolean.TRUE );
70  
71          getJellyContext().setVariable( "propertyName", "TEST PROPERTY" );
72          getJellyContext().setVariable( "propertyValue", "Jelly is cool" );
73          script.run( getJellyContext(),getXMLOutput() );
74  
75          assertTrue( System.getProperty( "TEST PROPERTY" ).equals( "Jelly is cool" ) );
76  
77      }
78  
79       /***
80       *  Uses the java.text.MessageFormat class to format a text message
81       *  with 3 arguments.
82       */
83      public void testMessageFormatInvoke() throws Exception {
84          System.out.println( System.getProperties() );
85          setUpScript( "testInvokeStaticTag.jelly" );
86          Script script = getJelly().compileScript();
87  
88          getJellyContext().setVariable( "test.messageFormatInvoke", Boolean.TRUE );
89  
90          Object[] args = new Object[3];
91          args[0] = "Jelly";
92          args[1] = "coolest";
93          args[2] = "used";
94  
95          getJellyContext().setVariable( "args", args );
96          getJellyContext().setVariable( "message", "Is not {0} the {1} thing you have ever {2}?" );
97          script.run( getJellyContext(),getXMLOutput() );
98  
99          assertNotNull( getJellyContext().getVariable("message") );
100         assertTrue( getJellyContext().getVariable("message").equals("Is not Jelly the coolest thing you have ever used?") );
101 
102     }
103 
104     public void testInvokeThatThrowsException() throws Exception {
105         setUpScript( "testInvokeStaticTag.jelly" );
106         Script script = getJelly().compileScript();
107         getJellyContext().setVariable("test.invokeThatThrowsException",Boolean.TRUE);
108         script.run(getJellyContext(),getXMLOutput());
109         String exceptionMessage = (String) getJellyContext().getVariable("exceptionMessage");
110         assertNotNull( exceptionMessage );
111         Exception jellyException = (Exception) getJellyContext().getVariable("jellyException");
112         assertNull( jellyException );
113         Exception exception = (Exception) getJellyContext().getVariable("exceptionThrown");
114         assertNotNull( exception );
115         assertEquals( exceptionMessage, exception.getMessage() );
116     }
117 
118     public void testInvokeThatDoesNotHandleException() throws Exception {
119         setUpScript( "testInvokeStaticTag.jelly" );
120         Script script = getJelly().compileScript();
121         getJellyContext().setVariable("test.invokeThatDoesNotHandleException",Boolean.TRUE);
122         script.run(getJellyContext(),getXMLOutput());
123         String exceptionMessage = (String) getJellyContext().getVariable("exceptionMessage");
124         assertNotNull( exceptionMessage );
125         JellyException jellyException = (JellyException) getJellyContext().getVariable("jellyException");
126         assertNotNull( jellyException );
127         assertTrue( "messages are the same", ! exceptionMessage.equals(jellyException.getMessage()) );
128         assertTrue( "exception '" + jellyException.getMessage() + "' does not ends with '" +
129                 exceptionMessage+"'", jellyException.getMessage().endsWith(exceptionMessage) );
130         assertNotNull( jellyException.getCause() );
131         assertEquals( exceptionMessage, jellyException.getCause().getMessage() );
132     }
133 
134 }