001: /*
002: * Copyright 2006, 2007 Odysseus Software GmbH
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 de.odysseus.el.tree.impl.ast;
017:
018: import javax.el.ELException;
019:
020: import de.odysseus.el.TestCase;
021:
022: public class AstBinaryTest extends TestCase {
023: AstBinary parseNode(String expression) {
024: return (AstBinary) parse(expression).getRoot().getChild(0);
025: }
026:
027: public void testEval() {
028: assertEquals(6l, parseNode("${4+2}").eval(null, null));
029: assertEquals(8l, parseNode("${4*2}").eval(null, null));
030: assertEquals(2d, parseNode("${4/2}").eval(null, null));
031: assertEquals(0l, parseNode("${4%2}").eval(null, null));
032:
033: assertEquals(false, parseNode("${true && false}").eval(null,
034: null));
035:
036: assertEquals(true, parseNode("${true || false}").eval(null,
037: null));
038:
039: assertEquals(true, parseNode("${1 == 1}").eval(null, null));
040: assertEquals(false, parseNode("${1 == 2}").eval(null, null));
041: assertEquals(false, parseNode("${2 == 1}").eval(null, null));
042:
043: assertEquals(false, parseNode("${1 != 1}").eval(null, null));
044: assertEquals(true, parseNode("${1 != 2}").eval(null, null));
045: assertEquals(false, parseNode("${2 == 1}").eval(null, null));
046:
047: assertEquals(false, parseNode("${1 < 1}").eval(null, null));
048: assertEquals(true, parseNode("${1 < 2}").eval(null, null));
049: assertEquals(false, parseNode("${2 < 1}").eval(null, null));
050:
051: assertEquals(false, parseNode("${1 > 1}").eval(null, null));
052: assertEquals(false, parseNode("${1 > 2}").eval(null, null));
053: assertEquals(true, parseNode("${2 > 1}").eval(null, null));
054:
055: assertEquals(true, parseNode("${1 <= 1}").eval(null, null));
056: assertEquals(true, parseNode("${1 <= 2}").eval(null, null));
057: assertEquals(false, parseNode("${2 <= 1}").eval(null, null));
058:
059: assertEquals(true, parseNode("${1 >= 1}").eval(null, null));
060: assertEquals(false, parseNode("${1 >= 2}").eval(null, null));
061: assertEquals(true, parseNode("${2 >= 1}").eval(null, null));
062: }
063:
064: public void testAppendStructure() {
065: StringBuilder s = null;
066: s = new StringBuilder();
067: parseNode("${1+1}").appendStructure(s, null);
068: parseNode("${1*1}").appendStructure(s, null);
069: parseNode("${1/1}").appendStructure(s, null);
070: parseNode("${1%1}").appendStructure(s, null);
071: assertEquals("1 + 11 * 11 / 11 % 1", s.toString());
072:
073: s = new StringBuilder();
074: parseNode("${1<1}").appendStructure(s, null);
075: parseNode("${1>1}").appendStructure(s, null);
076: parseNode("${1<=1}").appendStructure(s, null);
077: parseNode("${1>=1}").appendStructure(s, null);
078: assertEquals("1 < 11 > 11 <= 11 >= 1", s.toString());
079:
080: s = new StringBuilder();
081: parseNode("${1==1}").appendStructure(s, null);
082: parseNode("${1!=1}").appendStructure(s, null);
083: assertEquals("1 == 11 != 1", s.toString());
084:
085: s = new StringBuilder();
086: parseNode("${1&&1}").appendStructure(s, null);
087: parseNode("${1||1}").appendStructure(s, null);
088: assertEquals("1 && 11 || 1", s.toString());
089: }
090:
091: public void testIsLiteralText() {
092: assertFalse(parseNode("${1+1}").isLiteralText());
093: }
094:
095: public void testIsLeftValue() {
096: assertFalse(parseNode("${1+1}").isLeftValue());
097: }
098:
099: public void testGetType() {
100: assertNull(parseNode("${1+1}").getType(null, null));
101: }
102:
103: public void testIsReadOnly() {
104: assertTrue(parseNode("${1+1}").isReadOnly(null, null));
105: }
106:
107: public void testSetValue() {
108: try {
109: parseNode("${1+1}").setValue(null, null, null);
110: fail();
111: } catch (ELException e) {
112: }
113: }
114:
115: public void testGetValue() {
116: assertEquals(Long.valueOf(2l), parseNode("${1+1}").getValue(
117: null, null, null));
118: assertEquals("2", parseNode("${1+1}").getValue(null, null,
119: String.class));
120: }
121: }
|