Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PropertyExpressionTest.java 12KB

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