001: /*
002: * Copyright 2006 Dan Shellman
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.iscreen.ognl;
017:
018: import java.util.Locale;
019: import junit.framework.TestCase;
020: import org.iscreen.impl.*;
021:
022: /**
023: * Tests the OgnlMessage class.
024: *
025: * @author Shellman, Dan
026: */
027: public class OgnlMessageTest extends TestCase {
028: public OgnlMessageTest(String name) {
029: super (name);
030: }
031:
032: /**
033: * Simple test to ensure the getMessage() method uses the built-in OGNL root.
034: */
035: public void testGetMessage() {
036: TestContextBean root = new TestContextBean();
037: String template = "This is a ${stringValue} day.";
038: OgnlMessage msg = new OgnlMessage(template);
039:
040: msg.setOgnlRoot(root);
041: root.setStringValue("fine");
042: assertEquals("This is a fine day.", msg.getMessage());
043:
044: root.setStringValue("bad");
045: assertEquals("This is a bad day.", msg.getMessage(root, Locale
046: .getDefault()));
047: } //end testGetMessage()
048:
049: /**
050: * Tests when the OGNL root is null (so, the template is left unchanged).
051: */
052: public void testNullRoot() {
053: String template = "This is a ${stringValue} day.";
054: OgnlMessage msg = new OgnlMessage(template);
055:
056: assertEquals("This is a ${stringValue} day.", msg.getMessage());
057: assertEquals("This is a ${stringValue} day.", msg.getMessage(
058: null, Locale.getDefault()));
059: } //end testNullRoot()
060:
061: /**
062: * Tests when the embedded OGNL is the first character.
063: */
064: public void testFirstCharacterEmbed() {
065: TestContextBean root = new TestContextBean();
066: String template = "${stringValue} is my name.";
067: OgnlMessage msg = new OgnlMessage(template);
068:
069: msg.setOgnlRoot(root);
070: root.setStringValue("Dan");
071: assertEquals("Dan is my name.", msg.getMessage());
072: } //end testFirstCharacterEmbed()
073:
074: /**
075: * Tests when the embedded OGNL is the last character.
076: */
077: public void testLastCharacterEmbed() {
078: TestContextBean root = new TestContextBean();
079: String template = "My name is ${stringValue}";
080: OgnlMessage msg = new OgnlMessage(template);
081:
082: msg.setOgnlRoot(root);
083: root.setStringValue("Dan");
084: assertEquals("My name is Dan", msg.getMessage());
085: } //end testLastCharacterEmbed()
086:
087: /**
088: * Tests when the entire template is just one OGNL expression.
089: */
090: public void testFirstAndLastEmbed() {
091: TestContextBean root = new TestContextBean();
092: String template = "${stringValue}";
093: OgnlMessage msg = new OgnlMessage(template);
094:
095: msg.setOgnlRoot(root);
096: root.setStringValue("Frog");
097: assertEquals("Frog", msg.getMessage());
098: } //end testFirstAndLastEmbed()
099:
100: /**
101: * Tests when there are multiple embedded OGNL expressions.
102: */
103: public void testMultipleEmbeddings() {
104: TestContextBean root = new TestContextBean();
105: String template = "${stringValue} is ${value}.";
106: OgnlMessage msg = new OgnlMessage(template);
107:
108: msg.setOgnlRoot(root);
109: root.setStringValue("Programming");
110: root.setValue("fun");
111: assertEquals("Programming is fun.", msg.getMessage());
112: } //end testMultipleEmbeddings()
113:
114: /**
115: * Tests when the template is emtpy.
116: */
117: public void testEmptyTemplate() {
118: TestContextBean root = new TestContextBean();
119: String template = "";
120: OgnlMessage msg = new OgnlMessage(template);
121:
122: msg.setOgnlRoot(root);
123: root.setStringValue("ignored");
124: assertEquals("", msg.getMessage());
125: } //end testEmptyTemplate()
126: } //end OgnlMessageTest
|