Repository: jena Updated Branches: refs/heads/master 338d2bb4a -> 4b5cd2677
Test function for Sprintf that is Timezone aware. Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/5676aeb1 Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/5676aeb1 Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/5676aeb1 Branch: refs/heads/master Commit: 5676aeb180619a0f7cb48bf820fd8932999d8250 Parents: 0df832a Author: ales004 <[email protected]> Authored: Tue May 10 23:23:11 2016 +0200 Committer: ales004 <[email protected]> Committed: Tue May 10 23:23:11 2016 +0200 ---------------------------------------------------------------------- .../apache/jena/sparql/expr/TestFunctions.java | 37 ++++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/5676aeb1/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestFunctions.java ---------------------------------------------------------------------- diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestFunctions.java b/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestFunctions.java index 8aa63a2..5b4c5fc 100644 --- a/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestFunctions.java +++ b/jena-arq/src/test/java/org/apache/jena/sparql/expr/TestFunctions.java @@ -30,6 +30,14 @@ import org.apache.jena.sparql.function.FunctionEnvBase ; import org.apache.jena.sparql.util.ExprUtils ; import org.junit.Test ; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + public class TestFunctions { private static final NodeValue INT_ZERO = NodeValue.makeInteger(0) ; @@ -81,28 +89,35 @@ public class TestFunctions // test("afn:sprintf('%1$tm %1$te,%1$tY', "+nodeStr+")",NodeValue.makeString("10 14,2005")) ; // } - private static void test_exprSprintf_05(String nodeStr, String... possible) { - String exprStr = "afn:sprintf('%1$tm %1$te,%1$tY', "+NodeValue.makeDateTime("2005-10-14T13:09:43Z").toString()+")" ; + private static void test_exprSprintf_05(String nodeStr) { + String exprStr = "afn:sprintf('%1$tm %1$te,%1$tY', "+NodeValue.makeDateTime(nodeStr).toString()+")" ; Expr expr = ExprUtils.parse(exprStr) ; NodeValue r = expr.eval(null, FunctionEnvBase.createTest()) ; assertTrue(r.isString()) ; String s = r.getString() ; - // Timezones! The locale data can be -1, 0, +1 from the Z day. - boolean b = false ; - for (String poss : possible ) { - if ( poss.equals(s) ) - b = true ; + // Parse the date + String dtFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"; + SimpleDateFormat sdtFormat = new SimpleDateFormat(dtFormat); + Date dtDate = null; + try { + dtDate = sdtFormat.parse(nodeStr); + } catch (ParseException e) { + assertFalse("Cannot parse the input date string. Message:"+e.getMessage(),false); } - assertTrue(b) ; + // print the date based on the current timeZone. + SimpleDateFormat stdFormatOut = new SimpleDateFormat("MM dd,yyyy"); + stdFormatOut.setTimeZone(TimeZone.getDefault()); + String outDate = stdFormatOut.format(dtDate); + assertEquals(s,outDate); } // Temporary fix for JENA-1175 // Timezone -11:00 to any timezone can be a day ahead - @Test public void exprSprintf_05a() { test_exprSprintf_05("2005-10-14T14:09:43-11:00", "10 14,2005", "10 15,2005") ; } + @Test public void exprSprintf_05a() { test_exprSprintf_05("2005-10-14T14:09:43-11:00") ; } // Timezone Z to any timezone can be a day behind or a day ahead - @Test public void exprSprintf_05b() { test_exprSprintf_05("2005-10-14T12:09:43Z", "10 13,2005", "10 14,2005", "10 15,2005") ; } + @Test public void exprSprintf_05b() { test_exprSprintf_05("2005-10-14T12:09:43+00:00") ; } // Timezone +11:00 can be a day behind - @Test public void exprSprintf_05c() { test_exprSprintf_05("2005-10-14T10:09:43+11:00", "10 13,2005", "10 14,2005") ; } + @Test public void exprSprintf_05d() { test_exprSprintf_05("2005-10-14T10:09:43+11:00") ; } @Test public void exprSprintf_06() { test("afn:sprintf('this is %s', 'false'^^xsd:boolean)",NodeValue.makeString("this is false")) ; } @Test public void exprSprintf_07() { test("afn:sprintf('this number is equal to %.2f', '11.22'^^xsd:decimal)",NodeValue.makeString("this number is equal to "+String.format("%.2f",11.22))) ; }
