001: /*
002: * Copyright 1999-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.jxpath.ri.compiler;
017:
018: import java.text.DecimalFormatSymbols;
019:
020: import org.apache.commons.jxpath.IdentityManager;
021: import org.apache.commons.jxpath.JXPathContext;
022: import org.apache.commons.jxpath.JXPathTestCase;
023: import org.apache.commons.jxpath.KeyManager;
024: import org.apache.commons.jxpath.Pointer;
025: import org.apache.commons.jxpath.TestMixedModelBean;
026: import org.apache.commons.jxpath.Variables;
027: import org.apache.commons.jxpath.ri.model.NodePointer;
028:
029: /**
030: * Test basic functionality of JXPath - core functions.
031: *
032: * @author Dmitri Plotnikov
033: * @version $Revision: 1.10 $ $Date: 2004/05/08 20:07:09 $
034: */
035:
036: public class CoreFunctionTest extends JXPathTestCase {
037: private JXPathContext context;
038:
039: /**
040: * Construct a new instance of this test case.
041: *
042: * @param name Name of the test case
043: */
044: public CoreFunctionTest(String name) {
045: super (name);
046: }
047:
048: public void setUp() {
049: if (context == null) {
050: context = JXPathContext
051: .newContext(new TestMixedModelBean());
052: Variables vars = context.getVariables();
053: vars.declareVariable("nan", new Double(Double.NaN));
054: vars.declareVariable("bool_true", new Boolean("true"));
055: vars.declareVariable("bool_false", new Boolean("false"));
056: }
057: }
058:
059: public void testCoreFunctions() {
060: assertXPathValue(context, "string(2)", "2");
061: assertXPathValue(context, "string($nan)", "NaN");
062: assertXPathValue(context, "string(-$nan)", "NaN");
063: assertXPathValue(context, "string(-2 div 0)", "-Infinity");
064: assertXPathValue(context, "string(2 div 0)", "Infinity");
065: assertXPathValue(context, "concat('a', 'b', 'c')", "abc");
066: assertXPathValue(context, "starts-with('abc', 'ab')",
067: Boolean.TRUE);
068: assertXPathValue(context, "starts-with('xabc', 'ab')",
069: Boolean.FALSE);
070: assertXPathValue(context, "contains('xabc', 'ab')",
071: Boolean.TRUE);
072: assertXPathValue(context, "contains('xabc', 'ba')",
073: Boolean.FALSE);
074: assertXPathValue(context,
075: "substring-before('1999/04/01', '/')", "1999");
076: assertXPathValue(context, "substring-after('1999/04/01', '/')",
077: "04/01");
078: assertXPathValue(context, "substring('12345', 2, 3)", "234");
079: assertXPathValue(context, "substring('12345', 2)", "2345");
080: assertXPathValue(context, "substring('12345', 1.5, 2.6)", "234");
081: assertXPathValue(context, "substring('12345', 0, 3)", "12");
082: assertXPathValue(context, "substring('12345', 0 div 0, 3)", "");
083: assertXPathValue(context, "substring('12345', 1, 0 div 0)", "");
084: assertXPathValue(context, "substring('12345', -42, 1 div 0)",
085: "12345");
086: assertXPathValue(context,
087: "substring('12345', -1 div 0, 1 div 0)", "");
088: assertXPathValue(context, "string-length('12345')", new Double(
089: 5));
090: assertXPathValue(context, "normalize-space(' abc def ')",
091: "abc def");
092: assertXPathValue(context, "normalize-space('abc def')",
093: "abc def");
094: assertXPathValue(context, "normalize-space(' ')", "");
095: assertXPathValue(context,
096: "translate('--aaa--', 'abc-', 'ABC')", "AAA");
097: assertXPathValue(context, "boolean(1)", Boolean.TRUE);
098: assertXPathValue(context, "boolean(0)", Boolean.FALSE);
099: assertXPathValue(context, "boolean('x')", Boolean.TRUE);
100: assertXPathValue(context, "boolean('')", Boolean.FALSE);
101:
102: assertXPathValue(context, "true()", Boolean.TRUE);
103: assertXPathValue(context, "false()", Boolean.FALSE);
104: assertXPathValue(context, "not(false())", Boolean.TRUE);
105: assertXPathValue(context, "not(true())", Boolean.FALSE);
106: assertXPathValue(context, "number('1')", new Double(1));
107: assertXPathValue(context, "number($bool_true)", new Double(1));
108: assertXPathValue(context, "number($bool_false)", new Double(0));
109: assertXPathValue(context, "floor(1.5)", new Double(1));
110: assertXPathValue(context, "floor(-1.5)", new Double(-2));
111: assertXPathValue(context, "ceiling(1.5)", new Double(2));
112: assertXPathValue(context, "ceiling(-1.5)", new Double(-1));
113: assertXPathValue(context, "round(1.5)", new Double(2));
114: assertXPathValue(context, "round(-1.5)", new Double(-1));
115: assertXPathValue(context, "null()", null);
116: }
117:
118: public void testIDFunction() {
119: context.setIdentityManager(new IdentityManager() {
120: public Pointer getPointerByID(JXPathContext context,
121: String id) {
122: NodePointer ptr = (NodePointer) context
123: .getPointer("/document");
124: ptr = ptr.getValuePointer();
125: return ptr.getPointerByID(context, id);
126: }
127: });
128:
129: assertXPathValueAndPointer(context, "id(101)//street",
130: "Tangerine Drive", "id('101')/address[1]/street[1]");
131:
132: assertXPathPointerLenient(context, "id(105)/address/street",
133: "id(105)/address/street");
134: }
135:
136: public void testKeyFunction() {
137: context.setKeyManager(new KeyManager() {
138: public Pointer getPointerByKey(JXPathContext context,
139: String key, String value) {
140: return NodePointer.newNodePointer(null, "42", null);
141: }
142: });
143:
144: assertEquals("Test key", "42", context
145: .getValue("key('a', 'b')"));
146: }
147:
148: public void testFormatNumberFunction() {
149:
150: DecimalFormatSymbols symbols = new DecimalFormatSymbols();
151: symbols.setDigit('D');
152:
153: context.setDecimalFormatSymbols("test", symbols);
154:
155: assertXPathValue(context,
156: "format-number(123456789, '#.000000000')",
157: "123456789.000000000");
158:
159: assertXPathValue(context, "format-number(123456789, '#.0')",
160: "123456789.0");
161:
162: assertXPathValue(context, "format-number(0.123456789, '##%')",
163: "12%");
164:
165: assertXPathValue(context,
166: "format-number(123456789, '################')",
167: "123456789");
168:
169: assertXPathValue(context,
170: "format-number(123456789, 'D.0', 'test')",
171: "123456789.0");
172:
173: assertXPathValue(context,
174: "format-number(123456789, '$DDD,DDD,DDD.DD', 'test')",
175: "$123,456,789");
176: }
177: }
|