diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2018-07-26 03:08:22 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2018-07-26 03:08:22 +0000 |
commit | dd7b26018c0eacddc7681e48aee1c3cd1cf5c2a6 (patch) | |
tree | 3fe31a123d3ad7215daf739a8e17401d54920f29 /src | |
parent | b2aba90126da7421be9d30656354dda71d0de301 (diff) | |
download | jackcess-dd7b26018c0eacddc7681e48aee1c3cd1cf5c2a6.tar.gz jackcess-dd7b26018c0eacddc7681e48aee1c3cd1cf5c2a6.zip |
test custom EvalConfig; fix time format
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1186 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src')
3 files changed, 87 insertions, 8 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/expr/TemporalConfig.java b/src/main/java/com/healthmarketscience/jackcess/expr/TemporalConfig.java index 1c5bd74..8059538 100644 --- a/src/main/java/com/healthmarketscience/jackcess/expr/TemporalConfig.java +++ b/src/main/java/com/healthmarketscience/jackcess/expr/TemporalConfig.java @@ -20,11 +20,11 @@ package com.healthmarketscience.jackcess.expr; * * @author James Ahlborn */ -public class TemporalConfig +public class TemporalConfig { public static final String US_DATE_FORMAT = "M/d/yyyy"; - public static final String US_TIME_FORMAT_12 = "hh:mm:ss a"; - public static final String US_TIME_FORMAT_24 = "HH:mm:ss"; + public static final String US_TIME_FORMAT_12 = "h:mm:ss a"; + public static final String US_TIME_FORMAT_24 = "H:mm:ss"; public static final TemporalConfig US_TEMPORAL_CONFIG = new TemporalConfig( US_DATE_FORMAT, US_TIME_FORMAT_12, US_TIME_FORMAT_24, '/', ':'); @@ -37,8 +37,8 @@ public class TemporalConfig private final String _dateTimeFormat12; private final String _dateTimeFormat24; - public TemporalConfig(String dateFormat, String timeFormat12, - String timeFormat24, char dateSeparator, + public TemporalConfig(String dateFormat, String timeFormat12, + String timeFormat24, char dateSeparator, char timeSeparator) { _dateFormat = dateFormat; diff --git a/src/test/java/com/healthmarketscience/jackcess/PropertyExpressionTest.java b/src/test/java/com/healthmarketscience/jackcess/PropertyExpressionTest.java index 5d3fb44..6295477 100644 --- a/src/test/java/com/healthmarketscience/jackcess/PropertyExpressionTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/PropertyExpressionTest.java @@ -17,7 +17,17 @@ 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.BuiltinOperators; +import com.healthmarketscience.jackcess.impl.expr.DefaultFunctions; import junit.framework.TestCase; import static com.healthmarketscience.jackcess.Database.*; @@ -259,6 +269,62 @@ public class PropertyExpressionTest extends TestCase } } + 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(); @@ -280,4 +346,14 @@ public class PropertyExpressionTest extends TestCase } props.save(); } + + private static final Function FOO = new DefaultFunctions.Func0("FooFunc") { + @Override + public boolean isPure() { return false; } + @Override + protected Value eval0(EvalContext ctx) { + Object val = ctx.get("someKey"); + return BuiltinOperators.toValue("FOO_" + val); + } + }; } diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java index 461f153..8248c08 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java @@ -216,7 +216,7 @@ public class DefaultFunctionsTest extends TestCase public void testDateFuncs() throws Exception { assertEquals("1/2/2003", eval("=CStr(DateValue(#01/02/2003 7:00:00 AM#))")); - assertEquals("07:00:00 AM", eval("=CStr(TimeValue(#01/02/2003 7:00:00 AM#))")); + assertEquals("7:00:00 AM", eval("=CStr(TimeValue(#01/02/2003 7:00:00 AM#))")); assertEquals(2003, eval("=Year(#01/02/2003 7:00:00 AM#)")); assertEquals(1, eval("=Month(#01/02/2003 7:00:00 AM#)")); @@ -233,7 +233,10 @@ public class DefaultFunctionsTest extends TestCase assertTrue(((String)eval("=CStr(Date())")) .matches("\\d{1,2}/\\d{1,2}/\\d{4}")); assertTrue(((String)eval("=CStr(Time())")) - .matches("\\d{1,2}:\\d{1,2}:\\d{2} (AM|PM)")); + .matches("\\d{1,2}:\\d{2}:\\d{2} (AM|PM)")); + + assertEquals("3:57:34 AM", eval("=CStr(TimeSerial(3,57,34))")); + assertEquals("3:57:34 PM", eval("=CStr(TimeSerial(15,57,34))")); } public void testFinancialFuncs() throws Exception |