]> source.dussan.org Git - jackcess.git/commitdiff
test custom EvalConfig; fix time format
authorJames Ahlborn <jtahlborn@yahoo.com>
Thu, 26 Jul 2018 03:08:22 +0000 (03:08 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Thu, 26 Jul 2018 03:08:22 +0000 (03:08 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1186 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/expr/TemporalConfig.java
src/test/java/com/healthmarketscience/jackcess/PropertyExpressionTest.java
src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java

index 1c5bd740cd7a1a65600b23aadb7942e823534957..8059538335da906cff9c262d73fc476b7027e3a3 100644 (file)
@@ -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;
index 5d3fb44bf39900c293bf0e0f9c94d6c49a41c2a8..62954775b0aaf620a62e4da9388653680a50e505 100644 (file)
@@ -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);
+    }
+  };
 }
index 461f1537687aaeba2a4a6f72d25da22e9de3728c..8248c080d2da6f1d1d192f0280a79476c06ddd25 100644 (file)
@@ -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