/modules/test/poshi-runner/poshi-runner/src/test/java/com/liferay/poshi/runner/pql/PQLOperatorTest.java

http://github.com/liferay/liferay-portal · Java · 412 lines · 316 code · 80 blank · 16 comment · 13 complexity · 3a736b8302892a2f8ef4cd557a74c081 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.poshi.runner.pql;
  15. import java.util.HashSet;
  16. import java.util.Properties;
  17. import java.util.Set;
  18. import junit.framework.TestCase;
  19. import org.junit.Test;
  20. /**
  21. * @author Michael Hashimoto
  22. */
  23. public class PQLOperatorTest extends TestCase {
  24. @Test
  25. public void testGetOperator() throws Exception {
  26. Set<String> availableOperators = PQLOperator.getAvailableOperators();
  27. for (String operator : availableOperators) {
  28. PQLOperator pqlOperator = PQLOperatorFactory.newPQLOperator(
  29. operator);
  30. _compareString(pqlOperator.getOperator(), operator);
  31. }
  32. }
  33. @Test
  34. public void testGetPQLResultConditionalOperatorAND() throws Exception {
  35. _validateGetPQLResult("true", "AND", "true", Boolean.TRUE);
  36. _validateGetPQLResult("true", "AND", "false", Boolean.FALSE);
  37. _validateGetPQLResult("false", "AND", "false", Boolean.FALSE);
  38. }
  39. @Test
  40. public void testGetPQLResultConditionalOperatorErrors() throws Exception {
  41. Set<String> conditionalOperators = new HashSet<>();
  42. conditionalOperators.add("AND");
  43. conditionalOperators.add("OR");
  44. for (String operator : conditionalOperators) {
  45. String expectedError =
  46. "Operators must be surrounded by 2 boolean values: " + operator;
  47. _validateGetPQLResultError(null, operator, null, expectedError);
  48. _validateGetPQLResultError("test", operator, "test", expectedError);
  49. _validateGetPQLResultError("123", operator, "123", expectedError);
  50. _validateGetPQLResultError("12.3", operator, "12.3", expectedError);
  51. _validateGetPQLResultError("true", operator, null, expectedError);
  52. _validateGetPQLResultError("true", operator, "test", expectedError);
  53. _validateGetPQLResultError("true", operator, "123", expectedError);
  54. _validateGetPQLResultError("true", operator, "12.3", expectedError);
  55. _validateGetPQLResultError(null, operator, "false", expectedError);
  56. _validateGetPQLResultError(
  57. "test", operator, "false", expectedError);
  58. _validateGetPQLResultError("123", operator, "false", expectedError);
  59. _validateGetPQLResultError(
  60. "12.3", operator, "false", expectedError);
  61. }
  62. }
  63. @Test
  64. public void testGetPQLResultConditionalOperatorOR() throws Exception {
  65. _validateGetPQLResult("true", "OR", "true", Boolean.TRUE);
  66. _validateGetPQLResult("true", "OR", "false", Boolean.TRUE);
  67. _validateGetPQLResult("false", "OR", "false", Boolean.FALSE);
  68. }
  69. @Test
  70. public void testGetPQLResultEqualityOperatorEquals() throws Exception {
  71. _validateGetPQLResult("test", "==", "test", Boolean.TRUE);
  72. _validateGetPQLResult(null, "==", "test", Boolean.FALSE);
  73. _validateGetPQLResult("test", "==", null, Boolean.FALSE);
  74. _validateGetPQLResult(null, "==", null, Boolean.TRUE);
  75. _validateGetPQLResult("test1", "==", "test2", Boolean.FALSE);
  76. }
  77. @Test
  78. public void testGetPQLResultEqualityOperatorNotEquals() throws Exception {
  79. _validateGetPQLResult("test", "!=", "test", Boolean.FALSE);
  80. _validateGetPQLResult(null, "!=", "test", Boolean.TRUE);
  81. _validateGetPQLResult("test", "!=", null, Boolean.TRUE);
  82. _validateGetPQLResult(null, "!=", null, Boolean.FALSE);
  83. _validateGetPQLResult("test1", "!=", "test2", Boolean.TRUE);
  84. }
  85. @Test
  86. public void testGetPQLResultRelationalOperatorErrors() throws Exception {
  87. Set<String> conditionalOperators = new HashSet<>();
  88. conditionalOperators.add("<");
  89. conditionalOperators.add("<=");
  90. conditionalOperators.add(">");
  91. conditionalOperators.add(">=");
  92. for (String operator : conditionalOperators) {
  93. String expectedError =
  94. "Operator only works for number values: " + operator;
  95. _validateGetPQLResultError("123", operator, null, expectedError);
  96. _validateGetPQLResultError("12.3", operator, null, expectedError);
  97. _validateGetPQLResultError(null, operator, null, expectedError);
  98. _validateGetPQLResultError(null, operator, "123", expectedError);
  99. _validateGetPQLResultError(null, operator, "12.3", expectedError);
  100. _validateGetPQLResultError("123", operator, "true", expectedError);
  101. _validateGetPQLResultError("12.3", operator, "true", expectedError);
  102. _validateGetPQLResultError(
  103. "false", operator, "true", expectedError);
  104. _validateGetPQLResultError("false", operator, "123", expectedError);
  105. _validateGetPQLResultError(
  106. "false", operator, "12.3", expectedError);
  107. _validateGetPQLResultError("123", operator, "test", expectedError);
  108. _validateGetPQLResultError("12.3", operator, "test", expectedError);
  109. _validateGetPQLResultError("test", operator, "test", expectedError);
  110. _validateGetPQLResultError("test", operator, "123", expectedError);
  111. _validateGetPQLResultError("test", operator, "12.3", expectedError);
  112. }
  113. }
  114. @Test
  115. public void testGetPQLResultRelationalOperatorGreaterThan()
  116. throws Exception {
  117. _validateGetPQLResult("2", ">", "1", Boolean.TRUE);
  118. _validateGetPQLResult("2.1", ">", "1", Boolean.TRUE);
  119. _validateGetPQLResult("2", ">", "1.1", Boolean.TRUE);
  120. _validateGetPQLResult("2.1", ">", "1.1", Boolean.TRUE);
  121. _validateGetPQLResult("2", ">", "2", Boolean.FALSE);
  122. _validateGetPQLResult("2.1", ">", "2.1", Boolean.FALSE);
  123. _validateGetPQLResult("1", ">", "2", Boolean.FALSE);
  124. _validateGetPQLResult("1.1", ">", "2", Boolean.FALSE);
  125. _validateGetPQLResult("1", ">", "2.1", Boolean.FALSE);
  126. _validateGetPQLResult("1.1", ">", "2.1", Boolean.FALSE);
  127. }
  128. @Test
  129. public void testGetPQLResultRelationalOperatorGreaterThanEquals()
  130. throws Exception {
  131. _validateGetPQLResult("2", ">=", "1", Boolean.TRUE);
  132. _validateGetPQLResult("2.1", ">=", "1", Boolean.TRUE);
  133. _validateGetPQLResult("2", ">=", "1.1", Boolean.TRUE);
  134. _validateGetPQLResult("2.1", ">=", "1.1", Boolean.TRUE);
  135. _validateGetPQLResult("2", ">=", "2", Boolean.TRUE);
  136. _validateGetPQLResult("2.1", ">=", "2.1", Boolean.TRUE);
  137. _validateGetPQLResult("1", ">=", "2", Boolean.FALSE);
  138. _validateGetPQLResult("1.1", ">=", "2", Boolean.FALSE);
  139. _validateGetPQLResult("1", ">=", "2.1", Boolean.FALSE);
  140. _validateGetPQLResult("1.1", ">=", "2.1", Boolean.FALSE);
  141. }
  142. @Test
  143. public void testGetPQLResultRelationalOperatorLessThan() throws Exception {
  144. _validateGetPQLResult("2", "<", "1", Boolean.FALSE);
  145. _validateGetPQLResult("2.1", "<", "1", Boolean.FALSE);
  146. _validateGetPQLResult("2", "<", "1.1", Boolean.FALSE);
  147. _validateGetPQLResult("2.1", "<", "1.1", Boolean.FALSE);
  148. _validateGetPQLResult("2", "<", "2", Boolean.FALSE);
  149. _validateGetPQLResult("2.1", "<", "2.1", Boolean.FALSE);
  150. _validateGetPQLResult("1", "<", "2", Boolean.TRUE);
  151. _validateGetPQLResult("1.1", "<", "2", Boolean.TRUE);
  152. _validateGetPQLResult("1", "<", "2.1", Boolean.TRUE);
  153. _validateGetPQLResult("1.1", "<", "2.1", Boolean.TRUE);
  154. }
  155. @Test
  156. public void testGetPQLResultRelationalOperatorLessThanEquals()
  157. throws Exception {
  158. _validateGetPQLResult("2", "<=", "1", Boolean.FALSE);
  159. _validateGetPQLResult("2.1", "<=", "1", Boolean.FALSE);
  160. _validateGetPQLResult("2", "<=", "1.1", Boolean.FALSE);
  161. _validateGetPQLResult("2.1", "<=", "1.1", Boolean.FALSE);
  162. _validateGetPQLResult("2", "<=", "2", Boolean.TRUE);
  163. _validateGetPQLResult("2.1", "<=", "2.1", Boolean.TRUE);
  164. _validateGetPQLResult("1", "<=", "2", Boolean.TRUE);
  165. _validateGetPQLResult("1.1", "<=", "2", Boolean.TRUE);
  166. _validateGetPQLResult("1", "<=", "2.1", Boolean.TRUE);
  167. _validateGetPQLResult("1.1", "<=", "1.1", Boolean.TRUE);
  168. }
  169. @Test
  170. public void testGetPQLResultStringOperatorContains() throws Exception {
  171. _validateGetPQLResult("test", "~", "test", Boolean.TRUE);
  172. _validateGetPQLResult("test1", "~", "test", Boolean.TRUE);
  173. _validateGetPQLResult(null, "~", "test", Boolean.FALSE);
  174. _validateGetPQLResult("test", "~", null, Boolean.FALSE);
  175. _validateGetPQLResult(null, "~", null, Boolean.FALSE);
  176. _validateGetPQLResult("test1", "~", "test2", Boolean.FALSE);
  177. _validateGetPQLResult("fal", "~", "false", Boolean.FALSE);
  178. _validateGetPQLResult("false", "~", "fal", Boolean.TRUE);
  179. _validateGetPQLResult("false", "~", "false", Boolean.TRUE);
  180. _validateGetPQLResult("false", "~", "true", Boolean.FALSE);
  181. _validateGetPQLResult("tru", "~", "true", Boolean.FALSE);
  182. _validateGetPQLResult("true", "~", "false", Boolean.FALSE);
  183. _validateGetPQLResult("true", "~", "tru", Boolean.TRUE);
  184. _validateGetPQLResult("true", "~", "true", Boolean.TRUE);
  185. _validateGetPQLResult("12", "~", "12.3", Boolean.FALSE);
  186. _validateGetPQLResult("12.3", "~", "12", Boolean.TRUE);
  187. _validateGetPQLResult("12.3", "~", "12.3", Boolean.TRUE);
  188. _validateGetPQLResult("12.3", "~", "test", Boolean.FALSE);
  189. _validateGetPQLResult("test", "~", "12.3", Boolean.FALSE);
  190. _validateGetPQLResult("12", "~", "123", Boolean.FALSE);
  191. _validateGetPQLResult("123", "~", "12", Boolean.TRUE);
  192. _validateGetPQLResult("123", "~", "123", Boolean.TRUE);
  193. _validateGetPQLResult("123", "~", "test", Boolean.FALSE);
  194. _validateGetPQLResult("test", "~", "123", Boolean.FALSE);
  195. }
  196. @Test
  197. public void testGetPQLResultStringOperatorNotContains() throws Exception {
  198. _validateGetPQLResult("test", "!~", "test", Boolean.FALSE);
  199. _validateGetPQLResult("test1", "!~", "test", Boolean.FALSE);
  200. _validateGetPQLResult(null, "!~", "test", Boolean.FALSE);
  201. _validateGetPQLResult("test", "!~", null, Boolean.FALSE);
  202. _validateGetPQLResult(null, "!~", null, Boolean.FALSE);
  203. _validateGetPQLResult("test1", "!~", "test2", Boolean.TRUE);
  204. _validateGetPQLResult("fal", "!~", "false", Boolean.TRUE);
  205. _validateGetPQLResult("false", "!~", "fal", Boolean.FALSE);
  206. _validateGetPQLResult("false", "!~", "false", Boolean.FALSE);
  207. _validateGetPQLResult("false", "!~", "true", Boolean.TRUE);
  208. _validateGetPQLResult("tru", "!~", "true", Boolean.TRUE);
  209. _validateGetPQLResult("true", "!~", "false", Boolean.TRUE);
  210. _validateGetPQLResult("true", "!~", "tru", Boolean.FALSE);
  211. _validateGetPQLResult("true", "!~", "true", Boolean.FALSE);
  212. _validateGetPQLResult("12", "!~", "12.3", Boolean.TRUE);
  213. _validateGetPQLResult("12.3", "!~", "12", Boolean.FALSE);
  214. _validateGetPQLResult("12.3", "!~", "12.3", Boolean.FALSE);
  215. _validateGetPQLResult("12.3", "!~", "test", Boolean.TRUE);
  216. _validateGetPQLResult("test", "!~", "12.3", Boolean.TRUE);
  217. _validateGetPQLResult("12", "!~", "123", Boolean.TRUE);
  218. _validateGetPQLResult("123", "!~", "12", Boolean.FALSE);
  219. _validateGetPQLResult("123", "!~", "123", Boolean.FALSE);
  220. _validateGetPQLResult("123", "!~", "test", Boolean.TRUE);
  221. _validateGetPQLResult("test", "!~", "123", Boolean.TRUE);
  222. }
  223. @Test
  224. public void testOperatorValidate() throws Exception {
  225. Set<String> availableOperators = PQLOperator.getAvailableOperators();
  226. for (String operator : availableOperators) {
  227. PQLOperator.validateOperator(operator);
  228. }
  229. }
  230. @Test
  231. public void testOperatorValidateError() throws Exception {
  232. Set<String> operators = new HashSet<>();
  233. operators.add(null);
  234. operators.add("bad");
  235. operators.add("bad value");
  236. for (String operator : operators) {
  237. _validatePQLOperatorError(
  238. operator, "Invalid operator: " + operator);
  239. }
  240. }
  241. private void _compareString(String actualString, String expectedString)
  242. throws Exception {
  243. if (!actualString.equals(expectedString)) {
  244. StringBuilder sb = new StringBuilder();
  245. sb.append("Mismatched string values:");
  246. sb.append("\n* Actual: ");
  247. sb.append(actualString);
  248. sb.append("\n* Expected: ");
  249. sb.append(expectedString);
  250. throw new Exception(sb.toString());
  251. }
  252. }
  253. private void _validateGetPQLResult(
  254. String value1, String operator, String value2,
  255. Object expectedPQLResult)
  256. throws Exception {
  257. String pql = value1 + " " + operator + " " + value2;
  258. PQLEntity pqlEntity1 = PQLEntityFactory.newPQLEntity(value1);
  259. PQLOperator pqlOperator = PQLOperatorFactory.newPQLOperator(operator);
  260. PQLEntity pqlEntity2 = PQLEntityFactory.newPQLEntity(value2);
  261. Object actualPQLResult = pqlOperator.getPQLResult(
  262. pqlEntity1, pqlEntity2, new Properties());
  263. if (!actualPQLResult.equals(expectedPQLResult)) {
  264. StringBuilder sb = new StringBuilder();
  265. sb.append("Mismatched PQL result within the following PQL:\n");
  266. sb.append(pql);
  267. sb.append("\n* Actual: ");
  268. sb.append(actualPQLResult);
  269. sb.append("\n* Expected: ");
  270. sb.append(expectedPQLResult);
  271. throw new Exception(sb.toString());
  272. }
  273. }
  274. private void _validateGetPQLResultError(
  275. String value1, String operator, String value2, String expectedError)
  276. throws Exception {
  277. String pql = value1 + " " + operator + " " + value2;
  278. PQLEntity pqlEntity1 = PQLEntityFactory.newPQLEntity(value1);
  279. PQLOperator pqlOperator = PQLOperatorFactory.newPQLOperator(operator);
  280. PQLEntity pqlEntity2 = PQLEntityFactory.newPQLEntity(value2);
  281. String actualError = null;
  282. try {
  283. pqlOperator.getPQLResult(pqlEntity1, pqlEntity2, new Properties());
  284. }
  285. catch (Exception exception) {
  286. actualError = exception.getMessage();
  287. if (!actualError.equals(expectedError)) {
  288. StringBuilder sb = new StringBuilder();
  289. sb.append("Mismatched error within the following PQL:\n");
  290. sb.append(pql);
  291. sb.append(value2);
  292. sb.append("\n* Actual: ");
  293. sb.append(actualError);
  294. sb.append("\n* Expected: ");
  295. sb.append(expectedError);
  296. throw new Exception(sb.toString(), exception);
  297. }
  298. }
  299. finally {
  300. if (actualError == null) {
  301. throw new Exception(
  302. "No error thrown for the following PQL: " + pql);
  303. }
  304. }
  305. }
  306. private void _validatePQLOperatorError(
  307. String operator, String expectedError)
  308. throws Exception {
  309. String actualError = null;
  310. try {
  311. PQLOperator.validateOperator(operator);
  312. }
  313. catch (Exception exception) {
  314. actualError = exception.getMessage();
  315. if (!actualError.equals(expectedError)) {
  316. StringBuilder sb = new StringBuilder();
  317. sb.append("Mismatched error for PQL operator validation:");
  318. sb.append("\n* Actual: ");
  319. sb.append(actualError);
  320. sb.append("\n* Expected: ");
  321. sb.append(expectedError);
  322. throw new Exception(sb.toString(), exception);
  323. }
  324. }
  325. finally {
  326. if (actualError == null) {
  327. throw new Exception(
  328. "No error thrown for PQL operator validation: " + operator);
  329. }
  330. }
  331. }
  332. }