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