You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExpressionatorTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. Copyright (c) 2016 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl.expr;
  14. import java.math.BigDecimal;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Date;
  17. import javax.script.Bindings;
  18. import javax.script.SimpleBindings;
  19. import com.healthmarketscience.jackcess.DatabaseBuilder;
  20. import com.healthmarketscience.jackcess.TestUtil;
  21. import com.healthmarketscience.jackcess.expr.EvalContext;
  22. import com.healthmarketscience.jackcess.expr.Expression;
  23. import com.healthmarketscience.jackcess.expr.Function;
  24. import com.healthmarketscience.jackcess.expr.Identifier;
  25. import com.healthmarketscience.jackcess.expr.TemporalConfig;
  26. import com.healthmarketscience.jackcess.expr.Value;
  27. import junit.framework.TestCase;
  28. /**
  29. *
  30. * @author James Ahlborn
  31. */
  32. public class ExpressionatorTest extends TestCase
  33. {
  34. private static final double[] DBLS = {
  35. -10.3d,-9.0d,-8.234d,-7.11111d,-6.99999d,-5.5d,-4.0d,-3.4159265d,-2.84d,
  36. -1.0000002d,-1.0d,-0.0002013d,0.0d, 0.9234d,1.0d,1.954d,2.200032d,3.001d,
  37. 4.9321d,5.0d,6.66666d,7.396d,8.1d,9.20456200d,10.325d};
  38. public ExpressionatorTest(String name) {
  39. super(name);
  40. }
  41. public void testParseSimpleExprs() throws Exception
  42. {
  43. validateExpr("\"A\"", "<ELiteralValue>{\"A\"}");
  44. validateExpr("13", "<ELiteralValue>{13}");
  45. validateExpr("-42", "<EUnaryOp>{- <ELiteralValue>{42}}");
  46. validateExpr("(+37)", "<EParen>{(<EUnaryOp>{+ <ELiteralValue>{37}})}");
  47. doTestSimpleBinOp("EBinaryOp", "+", "-", "*", "/", "\\", "^", "&", "Mod");
  48. doTestSimpleBinOp("ECompOp", "<", "<=", ">", ">=", "=", "<>");
  49. doTestSimpleBinOp("ELogicalOp", "And", "Or", "Eqv", "Xor", "Imp");
  50. for(String constStr : new String[]{"True", "False", "Null"}) {
  51. validateExpr(constStr, "<EConstValue>{" + constStr + "}");
  52. }
  53. validateExpr("[Field1]", "<EObjValue>{[Field1]}");
  54. validateExpr("[Table2].[Field3]", "<EObjValue>{[Table2].[Field3]}");
  55. validateExpr("Not \"A\"", "<EUnaryOp>{Not <ELiteralValue>{\"A\"}}");
  56. validateExpr("-[Field1]", "<EUnaryOp>{- <EObjValue>{[Field1]}}");
  57. validateExpr("\"A\" Is Null", "<ENullOp>{<ELiteralValue>{\"A\"} Is Null}");
  58. validateExpr("\"A\" In (1,2,3)", "<EInOp>{<ELiteralValue>{\"A\"} In (<ELiteralValue>{1},<ELiteralValue>{2},<ELiteralValue>{3})}");
  59. validateExpr("\"A\" Not Between 3 And 7", "<EBetweenOp>{<ELiteralValue>{\"A\"} Not Between <ELiteralValue>{3} And <ELiteralValue>{7}}");
  60. validateExpr("(\"A\" Or \"B\")", "<EParen>{(<ELogicalOp>{<ELiteralValue>{\"A\"} Or <ELiteralValue>{\"B\"}})}");
  61. validateExpr("IIf(\"A\",42,False)", "<EFunc>{IIf(<ELiteralValue>{\"A\"},<ELiteralValue>{42},<EConstValue>{False})}");
  62. validateExpr("\"A\" Like \"a*b\"", "<ELikeOp>{<ELiteralValue>{\"A\"} Like \"a*b\"(a.*b)}");
  63. validateExpr("' \"A\" '", "<ELiteralValue>{\" \"\"A\"\" \"}",
  64. "\" \"\"A\"\" \"");
  65. }
  66. private static void doTestSimpleBinOp(String opName, String... ops) throws Exception
  67. {
  68. for(String op : ops) {
  69. validateExpr("\"A\" " + op + " \"B\"",
  70. "<" + opName + ">{<ELiteralValue>{\"A\"} " + op +
  71. " <ELiteralValue>{\"B\"}}");
  72. }
  73. }
  74. public void testOrderOfOperations() throws Exception
  75. {
  76. validateExpr("\"A\" Eqv \"B\"",
  77. "<ELogicalOp>{<ELiteralValue>{\"A\"} Eqv <ELiteralValue>{\"B\"}}");
  78. validateExpr("\"A\" Eqv \"B\" Xor \"C\"",
  79. "<ELogicalOp>{<ELiteralValue>{\"A\"} Eqv <ELogicalOp>{<ELiteralValue>{\"B\"} Xor <ELiteralValue>{\"C\"}}}");
  80. validateExpr("\"A\" Eqv \"B\" Xor \"C\" Or \"D\"",
  81. "<ELogicalOp>{<ELiteralValue>{\"A\"} Eqv <ELogicalOp>{<ELiteralValue>{\"B\"} Xor <ELogicalOp>{<ELiteralValue>{\"C\"} Or <ELiteralValue>{\"D\"}}}}");
  82. validateExpr("\"A\" Eqv \"B\" Xor \"C\" Or \"D\" And \"E\"",
  83. "<ELogicalOp>{<ELiteralValue>{\"A\"} Eqv <ELogicalOp>{<ELiteralValue>{\"B\"} Xor <ELogicalOp>{<ELiteralValue>{\"C\"} Or <ELogicalOp>{<ELiteralValue>{\"D\"} And <ELiteralValue>{\"E\"}}}}}");
  84. validateExpr("\"A\" Or \"B\" Or \"C\"",
  85. "<ELogicalOp>{<ELogicalOp>{<ELiteralValue>{\"A\"} Or <ELiteralValue>{\"B\"}} Or <ELiteralValue>{\"C\"}}");
  86. validateExpr("\"A\" & \"B\" Is Null",
  87. "<ENullOp>{<EBinaryOp>{<ELiteralValue>{\"A\"} & <ELiteralValue>{\"B\"}} Is Null}");
  88. validateExpr("\"A\" Or \"B\" Is Null",
  89. "<ELogicalOp>{<ELiteralValue>{\"A\"} Or <ENullOp>{<ELiteralValue>{\"B\"} Is Null}}");
  90. validateExpr("Not \"A\" & \"B\"",
  91. "<EUnaryOp>{Not <EBinaryOp>{<ELiteralValue>{\"A\"} & <ELiteralValue>{\"B\"}}}");
  92. validateExpr("Not \"A\" Or \"B\"",
  93. "<ELogicalOp>{<EUnaryOp>{Not <ELiteralValue>{\"A\"}} Or <ELiteralValue>{\"B\"}}");
  94. validateExpr("\"A\" + \"B\" Not Between 37 - 15 And 52 / 4",
  95. "<EBetweenOp>{<EBinaryOp>{<ELiteralValue>{\"A\"} + <ELiteralValue>{\"B\"}} Not Between <EBinaryOp>{<ELiteralValue>{37} - <ELiteralValue>{15}} And <EBinaryOp>{<ELiteralValue>{52} / <ELiteralValue>{4}}}");
  96. validateExpr("\"A\" + (\"B\" Not Between 37 - 15 And 52) / 4",
  97. "<EBinaryOp>{<ELiteralValue>{\"A\"} + <EBinaryOp>{<EParen>{(<EBetweenOp>{<ELiteralValue>{\"B\"} Not Between <EBinaryOp>{<ELiteralValue>{37} - <ELiteralValue>{15}} And <ELiteralValue>{52}})} / <ELiteralValue>{4}}}");
  98. }
  99. public void testSimpleMathExpressions() throws Exception
  100. {
  101. for(int i = -10; i <= 10; ++i) {
  102. assertEquals(-i, eval("=-(" + i + ")"));
  103. }
  104. for(int i = -10; i <= 10; ++i) {
  105. assertEquals(i, eval("=+(" + i + ")"));
  106. }
  107. for(double i : DBLS) {
  108. assertEquals(-i, eval("=-(" + i + ")"));
  109. }
  110. for(double i : DBLS) {
  111. assertEquals(i, eval("=+(" + i + ")"));
  112. }
  113. for(int i = -10; i <= 10; ++i) {
  114. for(int j = -10; j <= 10; ++j) {
  115. assertEquals((i + j), eval("=" + i + " + " + j));
  116. }
  117. }
  118. for(double i : DBLS) {
  119. for(double j : DBLS) {
  120. assertEquals((i + j), eval("=" + i + " + " + j));
  121. }
  122. }
  123. for(int i = -10; i <= 10; ++i) {
  124. for(int j = -10; j <= 10; ++j) {
  125. assertEquals((i - j), eval("=" + i + " - " + j));
  126. }
  127. }
  128. for(double i : DBLS) {
  129. for(double j : DBLS) {
  130. assertEquals((i - j), eval("=" + i + " - " + j));
  131. }
  132. }
  133. for(int i = -10; i <= 10; ++i) {
  134. for(int j = -10; j <= 10; ++j) {
  135. assertEquals((i * j), eval("=" + i + " * " + j));
  136. }
  137. }
  138. for(double i : DBLS) {
  139. for(double j : DBLS) {
  140. assertEquals((i * j), eval("=" + i + " * " + j));
  141. }
  142. }
  143. for(int i = -10; i <= 10; ++i) {
  144. for(int j = -10; j <= 10; ++j) {
  145. if(j == 0L) {
  146. evalFail("=" + i + " \\ " + j, ArithmeticException.class);
  147. } else {
  148. assertEquals((i / j), eval("=" + i + " \\ " + j));
  149. }
  150. }
  151. }
  152. for(double i : DBLS) {
  153. for(double j : DBLS) {
  154. if(roundToLongInt(j) == 0) {
  155. evalFail("=" + i + " \\ " + j, ArithmeticException.class);
  156. } else {
  157. assertEquals((roundToLongInt(i) / roundToLongInt(j)),
  158. eval("=" + i + " \\ " + j));
  159. }
  160. }
  161. }
  162. for(int i = -10; i <= 10; ++i) {
  163. for(int j = -10; j <= 10; ++j) {
  164. if(j == 0) {
  165. evalFail("=" + i + " Mod " + j, ArithmeticException.class);
  166. } else {
  167. assertEquals((i % j), eval("=" + i + " Mod " + j));
  168. }
  169. }
  170. }
  171. for(double i : DBLS) {
  172. for(double j : DBLS) {
  173. if(roundToLongInt(j) == 0) {
  174. evalFail("=" + i + " Mod " + j, ArithmeticException.class);
  175. } else {
  176. assertEquals((roundToLongInt(i) % roundToLongInt(j)),
  177. eval("=" + i + " Mod " + j));
  178. }
  179. }
  180. }
  181. for(int i = -10; i <= 10; ++i) {
  182. for(int j = -10; j <= 10; ++j) {
  183. if(j == 0) {
  184. evalFail("=" + i + " / " + j, ArithmeticException.class);
  185. } else {
  186. double result = (double)i / (double)j;
  187. if((int)result == result) {
  188. assertEquals((int)result, eval("=" + i + " / " + j));
  189. } else {
  190. assertEquals(result, eval("=" + i + " / " + j));
  191. }
  192. }
  193. }
  194. }
  195. for(double i : DBLS) {
  196. for(double j : DBLS) {
  197. if(j == 0.0d) {
  198. evalFail("=" + i + " / " + j, ArithmeticException.class);
  199. } else {
  200. assertEquals((i / j), eval("=" + i + " / " + j));
  201. }
  202. }
  203. }
  204. for(int i = -10; i <= 10; ++i) {
  205. for(int j = -10; j <= 10; ++j) {
  206. double result = Math.pow(i, j);
  207. if((int)result == result) {
  208. assertEquals((int)result, eval("=" + i + " ^ " + j));
  209. } else {
  210. assertEquals(result, eval("=" + i + " ^ " + j));
  211. }
  212. }
  213. }
  214. }
  215. public void testTrickyMathExpressions() throws Exception
  216. {
  217. assertEquals(37, eval("=30+7"));
  218. assertEquals(23, eval("=30+-7"));
  219. assertEquals(23, eval("=30-+7"));
  220. assertEquals(37, eval("=30--7"));
  221. assertEquals(23, eval("=30-7"));
  222. assertEquals(100, eval("=-10^2"));
  223. assertEquals(-100, eval("=-(10)^2"));
  224. assertEquals(-100, eval("=-\"10\"^2"));
  225. assertEquals(99d, eval("=-10E-1+10e+1"));
  226. assertEquals(-101d, eval("=-10E-1-10e+1"));
  227. }
  228. public void testTypeCoercion() throws Exception
  229. {
  230. assertEquals("foobar", eval("=\"foo\" + \"bar\""));
  231. assertEquals("12foo", eval("=12 + \"foo\""));
  232. assertEquals("foo12", eval("=\"foo\" + 12"));
  233. assertEquals(37, eval("=\"25\" + 12"));
  234. assertEquals(37, eval("=12 + \"25\""));
  235. evalFail(("=12 - \"foo\""), RuntimeException.class);
  236. evalFail(("=\"foo\" - 12"), RuntimeException.class);
  237. assertEquals("foo1225", eval("=\"foo\" + 12 + 25"));
  238. assertEquals("37foo", eval("=12 + 25 + \"foo\""));
  239. assertEquals("foo37", eval("=\"foo\" + (12 + 25)"));
  240. assertEquals("25foo12", eval("=\"25foo\" + 12"));
  241. assertEquals(new Date(1485579600000L), eval("=#1/1/2017# + 27"));
  242. assertEquals(128208, eval("=#1/1/2017# * 3"));
  243. }
  244. public void testLikeExpression() throws Exception
  245. {
  246. validateExpr("Like \"[abc]*\"", "<ELikeOp>{<EThisValue>{<THIS_COL>} Like \"[abc]*\"([abc].*)}",
  247. "<THIS_COL> Like \"[abc]*\"");
  248. assertTrue(evalCondition("Like \"[abc]*\"", "afcd"));
  249. assertFalse(evalCondition("Like \"[abc]*\"", "fcd"));
  250. validateExpr("Like \"[abc*\"", "<ELikeOp>{<EThisValue>{<THIS_COL>} Like \"[abc*\"((?!))}",
  251. "<THIS_COL> Like \"[abc*\"");
  252. assertFalse(evalCondition("Like \"[abc*\"", "afcd"));
  253. assertFalse(evalCondition("Like \"[abc*\"", "fcd"));
  254. assertFalse(evalCondition("Like \"[abc*\"", ""));
  255. }
  256. private static void validateExpr(String exprStr, String debugStr) {
  257. validateExpr(exprStr, debugStr, exprStr);
  258. }
  259. private static void validateExpr(String exprStr, String debugStr,
  260. String cleanStr) {
  261. Expression expr = Expressionator.parse(
  262. Expressionator.Type.FIELD_VALIDATOR, exprStr, null);
  263. String foundDebugStr = expr.toDebugString();
  264. if(foundDebugStr.startsWith("<EImplicitCompOp>")) {
  265. assertEquals("<EImplicitCompOp>{<EThisValue>{<THIS_COL>} = " +
  266. debugStr + "}", foundDebugStr);
  267. } else {
  268. assertEquals(debugStr, foundDebugStr);
  269. }
  270. assertEquals(cleanStr, expr.toString());
  271. }
  272. static Object eval(String exprStr) {
  273. Expression expr = Expressionator.parse(
  274. Expressionator.Type.DEFAULT_VALUE, exprStr, new TestParseContext());
  275. return expr.eval(new TestEvalContext(null));
  276. }
  277. private static void evalFail(String exprStr, Class<? extends Exception> failure) {
  278. Expression expr = Expressionator.parse(
  279. Expressionator.Type.DEFAULT_VALUE, exprStr, new TestParseContext());
  280. try {
  281. expr.eval(new TestEvalContext(null));
  282. fail(failure + " should have been thrown");
  283. } catch(Exception e) {
  284. assertTrue(failure.isInstance(e));
  285. }
  286. }
  287. private static Boolean evalCondition(String exprStr, String thisVal) {
  288. Expression expr = Expressionator.parse(
  289. Expressionator.Type.FIELD_VALIDATOR, exprStr, new TestParseContext());
  290. return (Boolean)expr.eval(new TestEvalContext(BuiltinOperators.toValue(thisVal)));
  291. }
  292. static int roundToLongInt(double d) {
  293. return new BigDecimal(d).setScale(0, BuiltinOperators.ROUND_MODE)
  294. .intValueExact();
  295. }
  296. private static final class TestParseContext implements Expressionator.ParseContext
  297. {
  298. public TemporalConfig getTemporalConfig() {
  299. return TemporalConfig.US_TEMPORAL_CONFIG;
  300. }
  301. public SimpleDateFormat createDateFormat(String formatStr) {
  302. SimpleDateFormat sdf = DatabaseBuilder.createDateFormat(formatStr);
  303. sdf.setTimeZone(TestUtil.TEST_TZ);
  304. return sdf;
  305. }
  306. public Function getExpressionFunction(String name) {
  307. return DefaultFunctions.getFunction(name);
  308. }
  309. }
  310. private static final class TestEvalContext implements EvalContext
  311. {
  312. private final Value _thisVal;
  313. private final RandomContext _rndCtx = new RandomContext();
  314. private final Bindings _bindings = new SimpleBindings();
  315. private TestEvalContext(Value thisVal) {
  316. _thisVal = thisVal;
  317. }
  318. public Value.Type getResultType() {
  319. return null;
  320. }
  321. public TemporalConfig getTemporalConfig() {
  322. return TemporalConfig.US_TEMPORAL_CONFIG;
  323. }
  324. public SimpleDateFormat createDateFormat(String formatStr) {
  325. SimpleDateFormat sdf = DatabaseBuilder.createDateFormat(formatStr);
  326. sdf.setTimeZone(TestUtil.TEST_TZ);
  327. return sdf;
  328. }
  329. public Value getThisColumnValue() {
  330. if(_thisVal == null) {
  331. throw new UnsupportedOperationException();
  332. }
  333. return _thisVal;
  334. }
  335. public Value getIdentifierValue(Identifier identifier) {
  336. throw new UnsupportedOperationException();
  337. }
  338. public float getRandom(Integer seed) {
  339. return _rndCtx.getRandom(seed);
  340. }
  341. public Bindings getBindings() {
  342. return _bindings;
  343. }
  344. public Object get(String key) {
  345. return _bindings.get(key);
  346. }
  347. public void put(String key, Object value) {
  348. _bindings.put(key, value);
  349. }
  350. }
  351. }