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.Script;
022: import org.apache.commons.jelly.test.BaseJellyTest;
023:
024: /**
025: * @author <a href="mailto:robert@bull-enterprises.com">Robert McIntosh</a>
026: * @version $Revision: 155420 $
027: */
028: public class TestInvokeStaticTag extends BaseJellyTest {
029:
030: public TestInvokeStaticTag(String name) {
031: super (name);
032: }
033:
034: public static TestSuite suite() throws Exception {
035: return new TestSuite(TestInvokeStaticTag.class);
036: }
037:
038: public void setUp() throws Exception {
039: super .setUp();
040: }
041:
042: public void tearDown() throws Exception {
043: super .tearDown();
044: }
045:
046: /**
047: * Gets the System property 'java.runtime.version' and compares it with,
048: * well, the same system property
049: */
050: public void testSimpleSystemInvoke() throws Exception {
051: setUpScript("testInvokeStaticTag.jelly");
052: Script script = getJelly().compileScript();
053:
054: getJellyContext().setVariable("test.simpleSystemInvoke",
055: Boolean.TRUE);
056:
057: getJellyContext().setVariable("propertyName",
058: "java.runtime.version");
059: script.run(getJellyContext(), getXMLOutput());
060:
061: assertTrue(System.getProperty("java.runtime.version").equals(
062: getJellyContext().getVariable("propertyName")));
063: }
064:
065: /**
066: * Sets the System property 'TEST PROPERTY' to the value 'Jelly is cool' and compares it with,
067: * well, the same system property
068: */
069: public void testSystemInvoke() throws Exception {
070: setUpScript("testInvokeStaticTag.jelly");
071: Script script = getJelly().compileScript();
072:
073: getJellyContext()
074: .setVariable("test.systemInvoke", Boolean.TRUE);
075:
076: getJellyContext().setVariable("propertyName", "TEST PROPERTY");
077: getJellyContext().setVariable("propertyValue", "Jelly is cool");
078: script.run(getJellyContext(), getXMLOutput());
079:
080: assertTrue(System.getProperty("TEST PROPERTY").equals(
081: "Jelly is cool"));
082:
083: }
084:
085: /**
086: * Uses the java.text.MessageFormat class to format a text message
087: * with 3 arguments.
088: */
089: public void testMessageFormatInvoke() throws Exception {
090: System.out.println(System.getProperties());
091: setUpScript("testInvokeStaticTag.jelly");
092: Script script = getJelly().compileScript();
093:
094: getJellyContext().setVariable("test.messageFormatInvoke",
095: Boolean.TRUE);
096:
097: Object[] args = new Object[3];
098: args[0] = "Jelly";
099: args[1] = "coolest";
100: args[2] = "used";
101:
102: getJellyContext().setVariable("args", args);
103: getJellyContext().setVariable("message",
104: "Is not {0} the {1} thing you have ever {2}?");
105: script.run(getJellyContext(), getXMLOutput());
106:
107: assertNotNull(getJellyContext().getVariable("message"));
108: assertTrue(getJellyContext().getVariable("message").equals(
109: "Is not Jelly the coolest thing you have ever used?"));
110:
111: }
112:
113: public void testInvokeThatThrowsException() throws Exception {
114: setUpScript("testInvokeStaticTag.jelly");
115: Script script = getJelly().compileScript();
116: getJellyContext().setVariable("test.invokeThatThrowsException",
117: Boolean.TRUE);
118: script.run(getJellyContext(), getXMLOutput());
119: String exceptionMessage = (String) getJellyContext()
120: .getVariable("exceptionMessage");
121: assertNotNull(exceptionMessage);
122: Exception jellyException = (Exception) getJellyContext()
123: .getVariable("jellyException");
124: assertNull(jellyException);
125: Exception exception = (Exception) getJellyContext()
126: .getVariable("exceptionThrown");
127: assertNotNull(exception);
128: assertEquals(exceptionMessage, exception.getMessage());
129: }
130:
131: public void testInvokeThatDoesNotHandleException() throws Exception {
132: setUpScript("testInvokeStaticTag.jelly");
133: Script script = getJelly().compileScript();
134: getJellyContext().setVariable(
135: "test.invokeThatDoesNotHandleException", Boolean.TRUE);
136: script.run(getJellyContext(), getXMLOutput());
137: String exceptionMessage = (String) getJellyContext()
138: .getVariable("exceptionMessage");
139: assertNotNull(exceptionMessage);
140: JellyException jellyException = (JellyException) getJellyContext()
141: .getVariable("jellyException");
142: assertNotNull(jellyException);
143: assertTrue("messages are the same", !exceptionMessage
144: .equals(jellyException.getMessage()));
145: assertTrue("exception '" + jellyException.getMessage()
146: + "' does not ends with '" + exceptionMessage + "'",
147: jellyException.getMessage().endsWith(exceptionMessage));
148: assertNotNull(jellyException.getCause());
149: assertEquals(exceptionMessage, jellyException.getCause()
150: .getMessage());
151: }
152:
153: }
|