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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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.text.SimpleDateFormat;
  15. import java.util.Date;
  16. import com.healthmarketscience.jackcess.DatabaseBuilder;
  17. import com.healthmarketscience.jackcess.TestUtil;
  18. import com.healthmarketscience.jackcess.expr.EvalContext;
  19. import com.healthmarketscience.jackcess.expr.Expression;
  20. import com.healthmarketscience.jackcess.expr.Function;
  21. import com.healthmarketscience.jackcess.expr.TemporalConfig;
  22. import com.healthmarketscience.jackcess.expr.Value;
  23. import junit.framework.TestCase;
  24. /**
  25. *
  26. * @author James Ahlborn
  27. */
  28. public class ExpressionatorTest extends TestCase
  29. {
  30. private static final double[] DBLS = {
  31. -10.3d,-9.0d,-8.234d,-7.11111d,-6.99999d,-5.5d,-4.0d,-3.4159265d,-2.84d,
  32. -1.0000002d,-1.0d,-0.0002013d,0.0d, 0.9234d,1.0d,1.954d,2.200032d,3.001d,
  33. 4.9321d,5.0d,6.66666d,7.396d,8.1d,9.20456200d,10.325d};
  34. public ExpressionatorTest(String name) {
  35. super(name);
  36. }
  37. public void testParseSimpleExprs() throws Exception
  38. {
  39. validateExpr("\"A\"", "<ELiteralValue>{\"A\"}");
  40. validateExpr("13", "<ELiteralValue>{13}");
  41. validateExpr("-42", "<ELiteralValue>{-42}");
  42. doTestSimpleBinOp("EBinaryOp", "+", "-", "*", "/", "\\", "^", "&", "Mod");
  43. doTestSimpleBinOp("ECompOp", "<", "<=", ">", ">=", "=", "<>");
  44. doTestSimpleBinOp("ELogicalOp", "And", "Or", "Eqv", "Xor", "Imp");
  45. for(String constStr : new String[]{"True", "False", "Null"}) {
  46. validateExpr(constStr, "<EConstValue>{" + constStr + "}");
  47. }
  48. validateExpr("[Field1]", "<EObjValue>{[Field1]}");
  49. validateExpr("[Table2].[Field3]", "<EObjValue>{[Table2].[Field3]}");
  50. validateExpr("Not \"A\"", "<EUnaryOp>{Not <ELiteralValue>{\"A\"}}");
  51. validateExpr("-[Field1]", "<EUnaryOp>{- <EObjValue>{[Field1]}}");
  52. validateExpr("\"A\" Is Null", "<ENullOp>{<ELiteralValue>{\"A\"} Is Null}");
  53. validateExpr("\"A\" In (1,2,3)", "<EInOp>{<ELiteralValue>{\"A\"} In (<ELiteralValue>{1},<ELiteralValue>{2},<ELiteralValue>{3})}");
  54. validateExpr("\"A\" Not Between 3 And 7", "<EBetweenOp>{<ELiteralValue>{\"A\"} Not Between <ELiteralValue>{3} And <ELiteralValue>{7}}");
  55. validateExpr("(\"A\" Or \"B\")", "<EParen>{(<ELogicalOp>{<ELiteralValue>{\"A\"} Or <ELiteralValue>{\"B\"}})}");
  56. validateExpr("IIf(\"A\",42,False)", "<EFunc>{IIf(<ELiteralValue>{\"A\"},<ELiteralValue>{42},<EConstValue>{False})}");
  57. validateExpr("\"A\" Like \"a*b\"", "<ELikeOp>{<ELiteralValue>{\"A\"} Like \"a*b\"(a.*b)}");
  58. }
  59. private static void doTestSimpleBinOp(String opName, String... ops) throws Exception
  60. {
  61. for(String op : ops) {
  62. validateExpr("\"A\" " + op + " \"B\"",
  63. "<" + opName + ">{<ELiteralValue>{\"A\"} " + op +
  64. " <ELiteralValue>{\"B\"}}");
  65. }
  66. }
  67. public void testOrderOfOperations() throws Exception
  68. {
  69. validateExpr("\"A\" Eqv \"B\"",
  70. "<ELogicalOp>{<ELiteralValue>{\"A\"} Eqv <ELiteralValue>{\"B\"}}");
  71. validateExpr("\"A\" Eqv \"B\" Xor \"C\"",
  72. "<ELogicalOp>{<ELiteralValue>{\"A\"} Eqv <ELogicalOp>{<ELiteralValue>{\"B\"} Xor <ELiteralValue>{\"C\"}}}");
  73. validateExpr("\"A\" Eqv \"B\" Xor \"C\" Or \"D\"",
  74. "<ELogicalOp>{<ELiteralValue>{\"A\"} Eqv <ELogicalOp>{<ELiteralValue>{\"B\"} Xor <ELogicalOp>{<ELiteralValue>{\"C\"} Or <ELiteralValue>{\"D\"}}}}");
  75. validateExpr("\"A\" Eqv \"B\" Xor \"C\" Or \"D\" And \"E\"",
  76. "<ELogicalOp>{<ELiteralValue>{\"A\"} Eqv <ELogicalOp>{<ELiteralValue>{\"B\"} Xor <ELogicalOp>{<ELiteralValue>{\"C\"} Or <ELogicalOp>{<ELiteralValue>{\"D\"} And <ELiteralValue>{\"E\"}}}}}");
  77. validateExpr("\"A\" Or \"B\" Or \"C\"",
  78. "<ELogicalOp>{<ELogicalOp>{<ELiteralValue>{\"A\"} Or <ELiteralValue>{\"B\"}} Or <ELiteralValue>{\"C\"}}");
  79. validateExpr("\"A\" & \"B\" Is Null",
  80. "<ENullOp>{<EBinaryOp>{<ELiteralValue>{\"A\"} & <ELiteralValue>{\"B\"}} Is Null}");
  81. validateExpr("\"A\" Or \"B\" Is Null",
  82. "<ELogicalOp>{<ELiteralValue>{\"A\"} Or <ENullOp>{<ELiteralValue>{\"B\"} Is Null}}");
  83. validateExpr("Not \"A\" & \"B\"",
  84. "<EUnaryOp>{Not <EBinaryOp>{<ELiteralValue>{\"A\"} & <ELiteralValue>{\"B\"}}}");
  85. validateExpr("Not \"A\" Or \"B\"",
  86. "<ELogicalOp>{<EUnaryOp>{Not <ELiteralValue>{\"A\"}} Or <ELiteralValue>{\"B\"}}");
  87. validateExpr("\"A\" + \"B\" Not Between 37 - 15 And 52 / 4",
  88. "<EBetweenOp>{<EBinaryOp>{<ELiteralValue>{\"A\"} + <ELiteralValue>{\"B\"}} Not Between <EBinaryOp>{<ELiteralValue>{37} - <ELiteralValue>{15}} And <EBinaryOp>{<ELiteralValue>{52} / <ELiteralValue>{4}}}");
  89. validateExpr("\"A\" + (\"B\" Not Between 37 - 15 And 52) / 4",
  90. "<EBinaryOp>{<ELiteralValue>{\"A\"} + <EBinaryOp>{<EParen>{(<EBetweenOp>{<ELiteralValue>{\"B\"} Not Between <EBinaryOp>{<ELiteralValue>{37} - <ELiteralValue>{15}} And <ELiteralValue>{52}})} / <ELiteralValue>{4}}}");
  91. }
  92. public void testSimpleMathExpressions() throws Exception
  93. {
  94. for(long i = -10L; i <= 10L; ++i) {
  95. assertEquals(-i, eval("=-(" + i + ")"));
  96. }
  97. for(double i : DBLS) {
  98. assertEquals(-i, eval("=-(" + i + ")"));
  99. }
  100. for(long i = -10L; i <= 10L; ++i) {
  101. for(long j = -10L; j <= 10L; ++j) {
  102. assertEquals((i + j), eval("=" + i + " + " + j));
  103. }
  104. }
  105. for(double i : DBLS) {
  106. for(double j : DBLS) {
  107. assertEquals((i + j), eval("=" + i + " + " + j));
  108. }
  109. }
  110. for(long i = -10L; i <= 10L; ++i) {
  111. for(long j = -10L; j <= 10L; ++j) {
  112. assertEquals((i - j), eval("=" + i + " - " + j));
  113. }
  114. }
  115. for(double i : DBLS) {
  116. for(double j : DBLS) {
  117. assertEquals((i - j), eval("=" + i + " - " + j));
  118. }
  119. }
  120. for(long i = -10L; i <= 10L; ++i) {
  121. for(long j = -10L; j <= 10L; ++j) {
  122. assertEquals((i * j), eval("=" + i + " * " + j));
  123. }
  124. }
  125. for(double i : DBLS) {
  126. for(double j : DBLS) {
  127. assertEquals((i * j), eval("=" + i + " * " + j));
  128. }
  129. }
  130. for(long i = -10L; i <= 10L; ++i) {
  131. for(long j = -10L; j <= 10L; ++j) {
  132. if(j == 0L) {
  133. evalFail("=" + i + " \\ " + j, ArithmeticException.class);
  134. } else {
  135. assertEquals((i / j), eval("=" + i + " \\ " + j));
  136. }
  137. }
  138. }
  139. for(double i : DBLS) {
  140. for(double j : DBLS) {
  141. if((long)j == 0L) {
  142. evalFail("=" + i + " \\ " + j, ArithmeticException.class);
  143. } else {
  144. assertEquals(((long)i / (long)j), eval("=" + i + " \\ " + j));
  145. }
  146. }
  147. }
  148. for(long i = -10L; i <= 10L; ++i) {
  149. for(long j = -10L; j <= 10L; ++j) {
  150. if(j == 0L) {
  151. evalFail("=" + i + " Mod " + j, ArithmeticException.class);
  152. } else {
  153. assertEquals((i % j), eval("=" + i + " Mod " + j));
  154. }
  155. }
  156. }
  157. for(double i : DBLS) {
  158. for(double j : DBLS) {
  159. if((long)j == 0L) {
  160. evalFail("=" + i + " Mod " + j, ArithmeticException.class);
  161. } else {
  162. assertEquals(((long)i % (long)j), eval("=" + i + " Mod " + j));
  163. }
  164. }
  165. }
  166. for(long i = -10L; i <= 10L; ++i) {
  167. for(long j = -10L; j <= 10L; ++j) {
  168. if(j == 0L) {
  169. evalFail("=" + i + " / " + j, ArithmeticException.class);
  170. } else {
  171. double result = (double)i / (double)j;
  172. if((long)result == result) {
  173. assertEquals((long)result, eval("=" + i + " / " + j));
  174. } else {
  175. assertEquals(result, eval("=" + i + " / " + j));
  176. }
  177. }
  178. }
  179. }
  180. for(double i : DBLS) {
  181. for(double j : DBLS) {
  182. if(j == 0.0d) {
  183. evalFail("=" + i + " / " + j, ArithmeticException.class);
  184. } else {
  185. assertEquals((i / j), eval("=" + i + " / " + j));
  186. }
  187. }
  188. }
  189. for(long i = -10L; i <= 10L; ++i) {
  190. for(long j = -10L; j <= 10L; ++j) {
  191. double result = Math.pow(i, j);
  192. if((long)result == result) {
  193. assertEquals((long)result, eval("=" + i + " ^ " + j));
  194. } else {
  195. assertEquals(result, eval("=" + i + " ^ " + j));
  196. }
  197. }
  198. }
  199. }
  200. public void testTypeCoercion() throws Exception
  201. {
  202. assertEquals("foobar", eval("=\"foo\" + \"bar\""));
  203. assertEquals("12foo", eval("=12 + \"foo\""));
  204. assertEquals("foo12", eval("=\"foo\" + 12"));
  205. assertEquals(37L, eval("=\"25\" + 12"));
  206. assertEquals(37L, eval("=12 + \"25\""));
  207. evalFail(("=12 - \"foo\""), RuntimeException.class);
  208. evalFail(("=\"foo\" - 12"), RuntimeException.class);
  209. assertEquals("foo1225", eval("=\"foo\" + 12 + 25"));
  210. assertEquals("37foo", eval("=12 + 25 + \"foo\""));
  211. assertEquals("foo37", eval("=\"foo\" + (12 + 25)"));
  212. assertEquals("25foo12", eval("=\"25foo\" + 12"));
  213. assertEquals(new Date(1485579600000L), eval("=#1/1/2017# + 27"));
  214. assertEquals(128208L, eval("=#1/1/2017# * 3"));
  215. }
  216. private static void validateExpr(String exprStr, String debugStr) {
  217. validateExpr(exprStr, debugStr, exprStr);
  218. }
  219. private static void validateExpr(String exprStr, String debugStr,
  220. String cleanStr) {
  221. Expression expr = Expressionator.parse(
  222. Expressionator.Type.FIELD_VALIDATOR, exprStr, null);
  223. assertEquals(debugStr, expr.toDebugString());
  224. assertEquals(cleanStr, expr.toString());
  225. }
  226. private static Object eval(String exprStr) {
  227. Expression expr = Expressionator.parse(
  228. Expressionator.Type.DEFAULT_VALUE, exprStr, new TestParseContext());
  229. return expr.eval(new TestEvalContext());
  230. }
  231. private static void evalFail(String exprStr, Class<? extends Exception> failure) {
  232. Expression expr = Expressionator.parse(
  233. Expressionator.Type.DEFAULT_VALUE, exprStr, new TestParseContext());
  234. try {
  235. expr.eval(new TestEvalContext());
  236. fail(failure + " should have been thrown");
  237. } catch(Exception e) {
  238. assertTrue(failure.isInstance(e));
  239. }
  240. }
  241. private static final class TestParseContext implements Expressionator.ParseContext
  242. {
  243. public TemporalConfig getTemporalConfig() {
  244. return TemporalConfig.US_TEMPORAL_CONFIG;
  245. }
  246. public SimpleDateFormat createDateFormat(String formatStr) {
  247. SimpleDateFormat sdf = DatabaseBuilder.createDateFormat(formatStr);
  248. sdf.setTimeZone(TestUtil.TEST_TZ);
  249. return sdf;
  250. }
  251. public Function getExpressionFunction(String name) {
  252. return null;
  253. }
  254. }
  255. private static final class TestEvalContext implements EvalContext
  256. {
  257. public Value.Type getResultType() {
  258. return null;
  259. }
  260. public TemporalConfig getTemporalConfig() {
  261. return TemporalConfig.US_TEMPORAL_CONFIG;
  262. }
  263. public SimpleDateFormat createDateFormat(String formatStr) {
  264. SimpleDateFormat sdf = DatabaseBuilder.createDateFormat(formatStr);
  265. sdf.setTimeZone(TestUtil.TEST_TZ);
  266. return sdf;
  267. }
  268. public Value getThisColumnValue() {
  269. throw new UnsupportedOperationException();
  270. }
  271. public Value getRowValue(String collectionName, String objName,
  272. String colName) {
  273. throw new UnsupportedOperationException();
  274. }
  275. }
  276. }