Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PropertyExpressionTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. Copyright (c) 2018 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;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.Locale;
  17. import javax.script.Bindings;
  18. import javax.script.SimpleBindings;
  19. import com.healthmarketscience.jackcess.expr.EvalConfig;
  20. import com.healthmarketscience.jackcess.expr.EvalContext;
  21. import com.healthmarketscience.jackcess.expr.Function;
  22. import com.healthmarketscience.jackcess.expr.FunctionLookup;
  23. import com.healthmarketscience.jackcess.expr.TemporalConfig;
  24. import com.healthmarketscience.jackcess.expr.Value;
  25. import com.healthmarketscience.jackcess.impl.expr.DefaultFunctions;
  26. import com.healthmarketscience.jackcess.impl.expr.FunctionSupport;
  27. import com.healthmarketscience.jackcess.impl.expr.ValueSupport;
  28. import junit.framework.TestCase;
  29. import static com.healthmarketscience.jackcess.Database.*;
  30. import static com.healthmarketscience.jackcess.TestUtil.*;
  31. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  32. /**
  33. *
  34. * @author James Ahlborn
  35. */
  36. public class PropertyExpressionTest extends TestCase
  37. {
  38. public PropertyExpressionTest(String name) {
  39. super(name);
  40. }
  41. public void testDefaultValue() throws Exception
  42. {
  43. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  44. Database db = create(fileFormat);
  45. db.setEvaluateExpressions(true);
  46. Table t = new TableBuilder("test")
  47. .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
  48. .addColumn(new ColumnBuilder("data1", DataType.TEXT)
  49. .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
  50. "=\"FOO \" & \"BAR\""))
  51. .addColumn(new ColumnBuilder("data2", DataType.LONG)
  52. .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
  53. "37"))
  54. .toTable(db);
  55. t.addRow(Column.AUTO_NUMBER, null, 13);
  56. t.addRow(Column.AUTO_NUMBER, "blah", null);
  57. setProp(t, "data1", PropertyMap.DEFAULT_VALUE_PROP, null);
  58. setProp(t, "data2", PropertyMap.DEFAULT_VALUE_PROP, "42");
  59. t.addRow(Column.AUTO_NUMBER, null, null);
  60. List<Row> expectedRows =
  61. createExpectedTable(
  62. createExpectedRow(
  63. "id", 1,
  64. "data1", "FOO BAR",
  65. "data2", 13),
  66. createExpectedRow(
  67. "id", 2,
  68. "data1", "blah",
  69. "data2", 37),
  70. createExpectedRow(
  71. "id", 3,
  72. "data1", null,
  73. "data2", 42));
  74. assertTable(expectedRows, t);
  75. setProp(t, "data2", PropertyMap.REQUIRED_PROP, true);
  76. t.addRow(Column.AUTO_NUMBER, "blah", 13);
  77. t.addRow(Column.AUTO_NUMBER, "blah", null);
  78. expectedRows = new ArrayList<Row>(expectedRows);
  79. expectedRows.add(
  80. createExpectedRow(
  81. "id", 4,
  82. "data1", "blah",
  83. "data2", 13));
  84. expectedRows.add(
  85. createExpectedRow(
  86. "id", 5,
  87. "data1", "blah",
  88. "data2", 42));
  89. assertTable(expectedRows, t);
  90. db.close();
  91. }
  92. }
  93. public void testCalculatedValue() throws Exception
  94. {
  95. Database db = create(FileFormat.V2016);
  96. db.setEvaluateExpressions(true);
  97. Table t = new TableBuilder("test")
  98. .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
  99. .addColumn(new ColumnBuilder("c1", DataType.LONG)
  100. .setCalculatedInfo("[c2]+[c3]"))
  101. .addColumn(new ColumnBuilder("c2", DataType.LONG)
  102. .setCalculatedInfo("[c3]*5"))
  103. .addColumn(new ColumnBuilder("c3", DataType.LONG)
  104. .setCalculatedInfo("[c4]-6"))
  105. .addColumn(new ColumnBuilder("c4", DataType.LONG))
  106. .toTable(db);
  107. t.addRow(Column.AUTO_NUMBER, null, null, null, 16);
  108. setProp(t, "c1", PropertyMap.EXPRESSION_PROP, "[c4]+2");
  109. setProp(t, "c2", PropertyMap.EXPRESSION_PROP, "[c1]+[c3]");
  110. setProp(t, "c3", PropertyMap.EXPRESSION_PROP, "[c1]*7");
  111. t.addRow(Column.AUTO_NUMBER, null, null, null, 7);
  112. List<Row> expectedRows =
  113. createExpectedTable(
  114. createExpectedRow(
  115. "id", 1,
  116. "c1", 60,
  117. "c2", 50,
  118. "c3", 10,
  119. "c4", 16),
  120. createExpectedRow(
  121. "id", 2,
  122. "c1", 9,
  123. "c2", 72,
  124. "c3", 63,
  125. "c4", 7));
  126. assertTable(expectedRows, t);
  127. db.close();
  128. }
  129. public void testColumnValidator() throws Exception
  130. {
  131. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  132. Database db = create(fileFormat);
  133. db.setEvaluateExpressions(true);
  134. Table t = new TableBuilder("test")
  135. .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
  136. .addColumn(new ColumnBuilder("data1", DataType.LONG)
  137. .putProperty(PropertyMap.VALIDATION_RULE_PROP,
  138. ">37"))
  139. .addColumn(new ColumnBuilder("data2", DataType.LONG)
  140. .putProperty(PropertyMap.VALIDATION_RULE_PROP,
  141. "between 7 and 10")
  142. .putProperty(PropertyMap.VALIDATION_TEXT_PROP,
  143. "You failed"))
  144. .toTable(db);
  145. t.addRow(Column.AUTO_NUMBER, 42, 8);
  146. try {
  147. t.addRow(Column.AUTO_NUMBER, 42, 20);
  148. fail("InvalidValueException should have been thrown");
  149. } catch(InvalidValueException ive) {
  150. // success
  151. assertTrue(ive.getMessage().contains("You failed"));
  152. }
  153. try {
  154. t.addRow(Column.AUTO_NUMBER, 3, 8);
  155. fail("InvalidValueException should have been thrown");
  156. } catch(InvalidValueException ive) {
  157. // success
  158. assertFalse(ive.getMessage().contains("You failed"));
  159. }
  160. t.addRow(Column.AUTO_NUMBER, 54, 9);
  161. setProp(t, "data1", PropertyMap.VALIDATION_RULE_PROP, null);
  162. setProp(t, "data2", PropertyMap.VALIDATION_RULE_PROP, "<100");
  163. setProp(t, "data2", PropertyMap.VALIDATION_TEXT_PROP, "Too big");
  164. try {
  165. t.addRow(Column.AUTO_NUMBER, 42, 200);
  166. fail("InvalidValueException should have been thrown");
  167. } catch(InvalidValueException ive) {
  168. // success
  169. assertTrue(ive.getMessage().contains("Too big"));
  170. }
  171. t.addRow(Column.AUTO_NUMBER, 1, 9);
  172. List<Row> expectedRows =
  173. createExpectedTable(
  174. createExpectedRow(
  175. "id", 1,
  176. "data1", 42,
  177. "data2", 8),
  178. createExpectedRow(
  179. "id", 2,
  180. "data1", 54,
  181. "data2", 9),
  182. createExpectedRow(
  183. "id", 3,
  184. "data1", 1,
  185. "data2", 9));
  186. assertTable(expectedRows, t);
  187. db.close();
  188. }
  189. }
  190. public void testRowValidator() throws Exception
  191. {
  192. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  193. Database db = create(fileFormat);
  194. db.setEvaluateExpressions(true);
  195. Table t = new TableBuilder("test")
  196. .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
  197. .addColumn(new ColumnBuilder("data1", DataType.LONG))
  198. .addColumn(new ColumnBuilder("data2", DataType.LONG))
  199. .putProperty(PropertyMap.VALIDATION_RULE_PROP,
  200. "([data1] > 10) and ([data2] < 100)")
  201. .putProperty(PropertyMap.VALIDATION_TEXT_PROP,
  202. "You failed")
  203. .toTable(db);
  204. t.addRow(Column.AUTO_NUMBER, 42, 8);
  205. try {
  206. t.addRow(Column.AUTO_NUMBER, 1, 20);
  207. fail("InvalidValueException should have been thrown");
  208. } catch(InvalidValueException ive) {
  209. // success
  210. assertTrue(ive.getMessage().contains("You failed"));
  211. }
  212. t.addRow(Column.AUTO_NUMBER, 54, 9);
  213. setTableProp(t, PropertyMap.VALIDATION_RULE_PROP, "[data2]<100");
  214. setTableProp(t, PropertyMap.VALIDATION_TEXT_PROP, "Too big");
  215. try {
  216. t.addRow(Column.AUTO_NUMBER, 42, 200);
  217. fail("InvalidValueException should have been thrown");
  218. } catch(InvalidValueException ive) {
  219. // success
  220. assertTrue(ive.getMessage().contains("Too big"));
  221. }
  222. t.addRow(Column.AUTO_NUMBER, 1, 9);
  223. List<Row> expectedRows =
  224. createExpectedTable(
  225. createExpectedRow(
  226. "id", 1,
  227. "data1", 42,
  228. "data2", 8),
  229. createExpectedRow(
  230. "id", 2,
  231. "data1", 54,
  232. "data2", 9),
  233. createExpectedRow(
  234. "id", 3,
  235. "data1", 1,
  236. "data2", 9));
  237. assertTable(expectedRows, t);
  238. db.close();
  239. }
  240. }
  241. public static void testCustomEvalConfig() throws Exception
  242. {
  243. TemporalConfig tempConf = new TemporalConfig("yyyy/M/d", "M/d",
  244. "yyyy-MMM-d",
  245. "hh.mm.ss a",
  246. "HH.mm.ss", '/', '.',
  247. Locale.US);
  248. FunctionLookup lookup = new FunctionLookup() {
  249. public Function getFunction(String name) {
  250. if("FooFunc".equalsIgnoreCase(name)) {
  251. return FOO;
  252. }
  253. return DefaultFunctions.LOOKUP.getFunction(name);
  254. }
  255. };
  256. Bindings bindings = new SimpleBindings();
  257. bindings.put("someKey", "someVal");
  258. for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  259. Database db = create(fileFormat);
  260. EvalConfig ec = db.getEvalConfig();
  261. ec.setTemporalConfig(tempConf);
  262. ec.setFunctionLookup(lookup);
  263. ec.setBindings(bindings);
  264. db.setEvaluateExpressions(true);
  265. Table t = new TableBuilder("test")
  266. .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
  267. .addColumn(new ColumnBuilder("data1", DataType.TEXT)
  268. .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
  269. "=FooFunc()"))
  270. .addColumn(new ColumnBuilder("data2", DataType.TEXT)
  271. .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
  272. "=Date()"))
  273. .addColumn(new ColumnBuilder("data3", DataType.TEXT)
  274. .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
  275. "=Time()"))
  276. .toTable(db);
  277. t.addRow(Column.AUTO_NUMBER, null, null);
  278. Row row = t.iterator().next();
  279. assertEquals(1, row.get("id"));
  280. assertEquals("FOO_someVal", row.get("data1"));
  281. assertTrue(((String)row.get("data2"))
  282. .matches("\\d{4}/\\d{1,2}/\\d{1,2}"));
  283. assertTrue(((String)row.get("data3"))
  284. .matches("\\d{2}.\\d{2}.\\d{2} (AM|PM)"));
  285. db.close();
  286. }
  287. }
  288. private static void setProp(Table t, String colName, String propName,
  289. Object propVal) throws Exception {
  290. PropertyMap props = t.getColumn(colName).getProperties();
  291. if(propVal != null) {
  292. props.put(propName, propVal);
  293. } else {
  294. props.remove(propName);
  295. }
  296. props.save();
  297. }
  298. private static void setTableProp(Table t, String propName,
  299. String propVal) throws Exception {
  300. PropertyMap props = t.getProperties();
  301. if(propVal != null) {
  302. props.put(propName, propVal);
  303. } else {
  304. props.remove(propName);
  305. }
  306. props.save();
  307. }
  308. private static final Function FOO = new FunctionSupport.Func0("FooFunc") {
  309. @Override
  310. public boolean isPure() { return false; }
  311. @Override
  312. protected Value eval0(EvalContext ctx) {
  313. Object val = ctx.get("someKey");
  314. return ValueSupport.toValue("FOO_" + val);
  315. }
  316. };
  317. }