|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- /*
- Copyright (c) 2018 James Ahlborn
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
-
- package com.healthmarketscience.jackcess;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Locale;
- import javax.script.Bindings;
- import javax.script.SimpleBindings;
-
- import com.healthmarketscience.jackcess.expr.EvalConfig;
- import com.healthmarketscience.jackcess.expr.EvalContext;
- import com.healthmarketscience.jackcess.expr.Function;
- import com.healthmarketscience.jackcess.expr.FunctionLookup;
- import com.healthmarketscience.jackcess.expr.TemporalConfig;
- import com.healthmarketscience.jackcess.expr.Value;
- import com.healthmarketscience.jackcess.impl.expr.DefaultFunctions;
- import com.healthmarketscience.jackcess.impl.expr.FunctionSupport;
- import com.healthmarketscience.jackcess.impl.expr.ValueSupport;
- import junit.framework.TestCase;
-
- import static com.healthmarketscience.jackcess.Database.*;
- import static com.healthmarketscience.jackcess.TestUtil.*;
- import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
-
- /**
- *
- * @author James Ahlborn
- */
- public class PropertyExpressionTest extends TestCase
- {
-
- public PropertyExpressionTest(String name) {
- super(name);
- }
-
- public void testDefaultValue() throws Exception
- {
- for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
- Database db = create(fileFormat);
- db.setEvaluateExpressions(true);
-
- Table t = new TableBuilder("test")
- .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
- .addColumn(new ColumnBuilder("data1", DataType.TEXT)
- .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
- "=\"FOO \" & \"BAR\""))
- .addColumn(new ColumnBuilder("data2", DataType.LONG)
- .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
- "37"))
- .toTable(db);
-
- t.addRow(Column.AUTO_NUMBER, null, 13);
- t.addRow(Column.AUTO_NUMBER, "blah", null);
-
- setProp(t, "data1", PropertyMap.DEFAULT_VALUE_PROP, null);
- setProp(t, "data2", PropertyMap.DEFAULT_VALUE_PROP, "42");
-
- t.addRow(Column.AUTO_NUMBER, null, null);
-
- List<Row> expectedRows =
- createExpectedTable(
- createExpectedRow(
- "id", 1,
- "data1", "FOO BAR",
- "data2", 13),
- createExpectedRow(
- "id", 2,
- "data1", "blah",
- "data2", 37),
- createExpectedRow(
- "id", 3,
- "data1", null,
- "data2", 42));
-
- assertTable(expectedRows, t);
-
- setProp(t, "data2", PropertyMap.REQUIRED_PROP, true);
-
- t.addRow(Column.AUTO_NUMBER, "blah", 13);
- t.addRow(Column.AUTO_NUMBER, "blah", null);
-
- expectedRows = new ArrayList<Row>(expectedRows);
- expectedRows.add(
- createExpectedRow(
- "id", 4,
- "data1", "blah",
- "data2", 13));
- expectedRows.add(
- createExpectedRow(
- "id", 5,
- "data1", "blah",
- "data2", 42));
-
- assertTable(expectedRows, t);
-
-
- db.close();
- }
- }
-
- public void testCalculatedValue() throws Exception
- {
- Database db = create(FileFormat.V2016);
- db.setEvaluateExpressions(true);
-
- Table t = new TableBuilder("test")
- .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
- .addColumn(new ColumnBuilder("c1", DataType.LONG)
- .setCalculatedInfo("[c2]+[c3]"))
- .addColumn(new ColumnBuilder("c2", DataType.LONG)
- .setCalculatedInfo("[c3]*5"))
- .addColumn(new ColumnBuilder("c3", DataType.LONG)
- .setCalculatedInfo("[c4]-6"))
- .addColumn(new ColumnBuilder("c4", DataType.LONG))
- .toTable(db);
-
- t.addRow(Column.AUTO_NUMBER, null, null, null, 16);
-
- setProp(t, "c1", PropertyMap.EXPRESSION_PROP, "[c4]+2");
- setProp(t, "c2", PropertyMap.EXPRESSION_PROP, "[c1]+[c3]");
- setProp(t, "c3", PropertyMap.EXPRESSION_PROP, "[c1]*7");
-
- t.addRow(Column.AUTO_NUMBER, null, null, null, 7);
-
- List<Row> expectedRows =
- createExpectedTable(
- createExpectedRow(
- "id", 1,
- "c1", 60,
- "c2", 50,
- "c3", 10,
- "c4", 16),
- createExpectedRow(
- "id", 2,
- "c1", 9,
- "c2", 72,
- "c3", 63,
- "c4", 7));
-
- assertTable(expectedRows, t);
-
- db.close();
- }
-
- public void testColumnValidator() throws Exception
- {
- for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
- Database db = create(fileFormat);
- db.setEvaluateExpressions(true);
-
- Table t = new TableBuilder("test")
- .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
- .addColumn(new ColumnBuilder("data1", DataType.LONG)
- .putProperty(PropertyMap.VALIDATION_RULE_PROP,
- ">37"))
- .addColumn(new ColumnBuilder("data2", DataType.LONG)
- .putProperty(PropertyMap.VALIDATION_RULE_PROP,
- "between 7 and 10")
- .putProperty(PropertyMap.VALIDATION_TEXT_PROP,
- "You failed"))
- .toTable(db);
-
- t.addRow(Column.AUTO_NUMBER, 42, 8);
-
- try {
- t.addRow(Column.AUTO_NUMBER, 42, 20);
- fail("InvalidValueException should have been thrown");
- } catch(InvalidValueException ive) {
- // success
- assertTrue(ive.getMessage().contains("You failed"));
- }
-
- try {
- t.addRow(Column.AUTO_NUMBER, 3, 8);
- fail("InvalidValueException should have been thrown");
- } catch(InvalidValueException ive) {
- // success
- assertFalse(ive.getMessage().contains("You failed"));
- }
-
- t.addRow(Column.AUTO_NUMBER, 54, 9);
-
- setProp(t, "data1", PropertyMap.VALIDATION_RULE_PROP, null);
- setProp(t, "data2", PropertyMap.VALIDATION_RULE_PROP, "<100");
- setProp(t, "data2", PropertyMap.VALIDATION_TEXT_PROP, "Too big");
-
- try {
- t.addRow(Column.AUTO_NUMBER, 42, 200);
- fail("InvalidValueException should have been thrown");
- } catch(InvalidValueException ive) {
- // success
- assertTrue(ive.getMessage().contains("Too big"));
- }
-
- t.addRow(Column.AUTO_NUMBER, 1, 9);
-
- List<Row> expectedRows =
- createExpectedTable(
- createExpectedRow(
- "id", 1,
- "data1", 42,
- "data2", 8),
- createExpectedRow(
- "id", 2,
- "data1", 54,
- "data2", 9),
- createExpectedRow(
- "id", 3,
- "data1", 1,
- "data2", 9));
-
- assertTable(expectedRows, t);
-
- db.close();
- }
- }
-
- public void testRowValidator() throws Exception
- {
- for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
- Database db = create(fileFormat);
- db.setEvaluateExpressions(true);
-
- Table t = new TableBuilder("test")
- .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
- .addColumn(new ColumnBuilder("data1", DataType.LONG))
- .addColumn(new ColumnBuilder("data2", DataType.LONG))
- .putProperty(PropertyMap.VALIDATION_RULE_PROP,
- "([data1] > 10) and ([data2] < 100)")
- .putProperty(PropertyMap.VALIDATION_TEXT_PROP,
- "You failed")
- .toTable(db);
-
- t.addRow(Column.AUTO_NUMBER, 42, 8);
-
- try {
- t.addRow(Column.AUTO_NUMBER, 1, 20);
- fail("InvalidValueException should have been thrown");
- } catch(InvalidValueException ive) {
- // success
- assertTrue(ive.getMessage().contains("You failed"));
- }
-
- t.addRow(Column.AUTO_NUMBER, 54, 9);
-
- setTableProp(t, PropertyMap.VALIDATION_RULE_PROP, "[data2]<100");
- setTableProp(t, PropertyMap.VALIDATION_TEXT_PROP, "Too big");
-
- try {
- t.addRow(Column.AUTO_NUMBER, 42, 200);
- fail("InvalidValueException should have been thrown");
- } catch(InvalidValueException ive) {
- // success
- assertTrue(ive.getMessage().contains("Too big"));
- }
-
- t.addRow(Column.AUTO_NUMBER, 1, 9);
-
- List<Row> expectedRows =
- createExpectedTable(
- createExpectedRow(
- "id", 1,
- "data1", 42,
- "data2", 8),
- createExpectedRow(
- "id", 2,
- "data1", 54,
- "data2", 9),
- createExpectedRow(
- "id", 3,
- "data1", 1,
- "data2", 9));
-
- assertTable(expectedRows, t);
-
- db.close();
- }
- }
-
- public static void testCustomEvalConfig() throws Exception
- {
- TemporalConfig tempConf = new TemporalConfig("yyyy/M/d", "M/d",
- "yyyy-MMM-d",
- "hh.mm.ss a",
- "HH.mm.ss", '/', '.',
- Locale.US);
-
- FunctionLookup lookup = new FunctionLookup() {
- public Function getFunction(String name) {
- if("FooFunc".equalsIgnoreCase(name)) {
- return FOO;
- }
- return DefaultFunctions.LOOKUP.getFunction(name);
- }
- };
-
- Bindings bindings = new SimpleBindings();
- bindings.put("someKey", "someVal");
-
- for (final FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
- Database db = create(fileFormat);
-
- EvalConfig ec = db.getEvalConfig();
- ec.setTemporalConfig(tempConf);
- ec.setFunctionLookup(lookup);
- ec.setBindings(bindings);
-
- db.setEvaluateExpressions(true);
-
- Table t = new TableBuilder("test")
- .addColumn(new ColumnBuilder("id", DataType.LONG).setAutoNumber(true))
- .addColumn(new ColumnBuilder("data1", DataType.TEXT)
- .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
- "=FooFunc()"))
- .addColumn(new ColumnBuilder("data2", DataType.TEXT)
- .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
- "=Date()"))
- .addColumn(new ColumnBuilder("data3", DataType.TEXT)
- .putProperty(PropertyMap.DEFAULT_VALUE_PROP,
- "=Time()"))
- .toTable(db);
-
- t.addRow(Column.AUTO_NUMBER, null, null);
-
- Row row = t.iterator().next();
-
- assertEquals(1, row.get("id"));
- assertEquals("FOO_someVal", row.get("data1"));
- assertTrue(((String)row.get("data2"))
- .matches("\\d{4}/\\d{1,2}/\\d{1,2}"));
- assertTrue(((String)row.get("data3"))
- .matches("\\d{2}.\\d{2}.\\d{2} (AM|PM)"));
-
- db.close();
- }
- }
-
- private static void setProp(Table t, String colName, String propName,
- Object propVal) throws Exception {
- PropertyMap props = t.getColumn(colName).getProperties();
- if(propVal != null) {
- props.put(propName, propVal);
- } else {
- props.remove(propName);
- }
- props.save();
- }
-
- private static void setTableProp(Table t, String propName,
- String propVal) throws Exception {
- PropertyMap props = t.getProperties();
- if(propVal != null) {
- props.put(propName, propVal);
- } else {
- props.remove(propName);
- }
- props.save();
- }
-
- private static final Function FOO = new FunctionSupport.Func0("FooFunc") {
- @Override
- public boolean isPure() { return false; }
- @Override
- protected Value eval0(EvalContext ctx) {
- Object val = ctx.get("someKey");
- return ValueSupport.toValue("FOO_" + val);
- }
- };
- }
|