aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/healthmarketscience/jackcess/PropertyExpressionTest.java
blob: e5d72954706c97a691a78256178581afe1ce49c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
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.List;
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);

      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",
                                                 "hh.mm.ss a",
                                                 "HH.mm.ss", '/', '.');

    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,
                              String 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);
    }
  };
}