1 16 package org.apache.commons.jxpath.ri.compiler; 17 18 import java.text.DecimalFormatSymbols ; 19 20 import org.apache.commons.jxpath.IdentityManager; 21 import org.apache.commons.jxpath.JXPathContext; 22 import org.apache.commons.jxpath.JXPathTestCase; 23 import org.apache.commons.jxpath.KeyManager; 24 import org.apache.commons.jxpath.Pointer; 25 import org.apache.commons.jxpath.TestMixedModelBean; 26 import org.apache.commons.jxpath.Variables; 27 import org.apache.commons.jxpath.ri.model.NodePointer; 28 29 35 36 public class CoreFunctionTest extends JXPathTestCase { 37 private JXPathContext context; 38 39 44 public CoreFunctionTest(String name) { 45 super(name); 46 } 47 48 public void setUp() { 49 if (context == null) { 50 context = JXPathContext.newContext(new TestMixedModelBean()); 51 Variables vars = context.getVariables(); 52 vars.declareVariable("nan", new Double (Double.NaN)); 53 vars.declareVariable("bool_true", new Boolean ("true")); 54 vars.declareVariable("bool_false", new Boolean ("false")); 55 } 56 } 57 58 public void testCoreFunctions() { 59 assertXPathValue(context, "string(2)", "2"); 60 assertXPathValue(context, "string($nan)", "NaN"); 61 assertXPathValue(context, "string(-$nan)", "NaN"); 62 assertXPathValue(context, "string(-2 div 0)", "-Infinity"); 63 assertXPathValue(context, "string(2 div 0)", "Infinity"); 64 assertXPathValue(context, "concat('a', 'b', 'c')", "abc"); 65 assertXPathValue(context, "starts-with('abc', 'ab')", Boolean.TRUE); 66 assertXPathValue(context, "starts-with('xabc', 'ab')", Boolean.FALSE); 67 assertXPathValue(context, "contains('xabc', 'ab')", Boolean.TRUE); 68 assertXPathValue(context, "contains('xabc', 'ba')", Boolean.FALSE); 69 assertXPathValue( 70 context, 71 "substring-before('1999/04/01', '/')", 72 "1999"); 73 assertXPathValue( 74 context, 75 "substring-after('1999/04/01', '/')", 76 "04/01"); 77 assertXPathValue(context, "substring('12345', 2, 3)", "234"); 78 assertXPathValue(context, "substring('12345', 2)", "2345"); 79 assertXPathValue(context, "substring('12345', 1.5, 2.6)", "234"); 80 assertXPathValue(context, "substring('12345', 0, 3)", "12"); 81 assertXPathValue(context, "substring('12345', 0 div 0, 3)", ""); 82 assertXPathValue(context, "substring('12345', 1, 0 div 0)", ""); 83 assertXPathValue(context, "substring('12345', -42, 1 div 0)", "12345"); 84 assertXPathValue(context, "substring('12345', -1 div 0, 1 div 0)", ""); 85 assertXPathValue(context, "string-length('12345')", new Double (5)); 86 assertXPathValue(context, "normalize-space(' abc def ')", "abc def"); 87 assertXPathValue(context, "normalize-space('abc def')", "abc def"); 88 assertXPathValue(context, "normalize-space(' ')", ""); 89 assertXPathValue(context, "translate('--aaa--', 'abc-', 'ABC')", "AAA"); 90 assertXPathValue(context, "boolean(1)", Boolean.TRUE); 91 assertXPathValue(context, "boolean(0)", Boolean.FALSE); 92 assertXPathValue(context, "boolean('x')", Boolean.TRUE); 93 assertXPathValue(context, "boolean('')", Boolean.FALSE); 94 95 assertXPathValue(context, "true()", Boolean.TRUE); 96 assertXPathValue(context, "false()", Boolean.FALSE); 97 assertXPathValue(context, "not(false())", Boolean.TRUE); 98 assertXPathValue(context, "not(true())", Boolean.FALSE); 99 assertXPathValue(context, "number('1')", new Double (1)); 100 assertXPathValue(context, "number($bool_true)", new Double (1)); 101 assertXPathValue(context, "number($bool_false)", new Double (0)); 102 assertXPathValue(context, "floor(1.5)", new Double (1)); 103 assertXPathValue(context, "floor(-1.5)", new Double (-2)); 104 assertXPathValue(context, "ceiling(1.5)", new Double (2)); 105 assertXPathValue(context, "ceiling(-1.5)", new Double (-1)); 106 assertXPathValue(context, "round(1.5)", new Double (2)); 107 assertXPathValue(context, "round(-1.5)", new Double (-1)); 108 assertXPathValue(context, "null()", null); 109 } 110 111 public void testIDFunction() { 112 context.setIdentityManager(new IdentityManager() { 113 public Pointer getPointerByID(JXPathContext context, String id) { 114 NodePointer ptr = (NodePointer) context.getPointer("/document"); 115 ptr = ptr.getValuePointer(); 116 return ptr.getPointerByID(context, id); 117 } 118 }); 119 120 assertXPathValueAndPointer( 121 context, 122 "id(101)//street", 123 "Tangerine Drive", 124 "id('101')/address[1]/street[1]"); 125 126 assertXPathPointerLenient( 127 context, 128 "id(105)/address/street", 129 "id(105)/address/street"); 130 } 131 132 public void testKeyFunction() { 133 context.setKeyManager(new KeyManager() { 134 public Pointer getPointerByKey( 135 JXPathContext context, 136 String key, 137 String value) 138 { 139 return NodePointer.newNodePointer(null, "42", null); 140 } 141 }); 142 143 assertEquals("Test key", "42", context.getValue("key('a', 'b')")); 144 } 145 146 public void testFormatNumberFunction() { 147 148 DecimalFormatSymbols symbols = new DecimalFormatSymbols (); 149 symbols.setDigit('D'); 150 151 context.setDecimalFormatSymbols("test", symbols); 152 153 assertXPathValue( 154 context, 155 "format-number(123456789, '#.000000000')", 156 "123456789.000000000"); 157 158 assertXPathValue( 159 context, 160 "format-number(123456789, '#.0')", 161 "123456789.0"); 162 163 assertXPathValue( 164 context, 165 "format-number(0.123456789, '##%')", 166 "12%"); 167 168 assertXPathValue( 169 context, 170 "format-number(123456789, '################')", 171 "123456789"); 172 173 assertXPathValue( 174 context, 175 "format-number(123456789, 'D.0', 'test')", 176 "123456789.0"); 177 178 assertXPathValue( 179 context, 180 "format-number(123456789, '$DDD,DDD,DDD.DD', 'test')", 181 "$123,456,789"); 182 } 183 } | Popular Tags |