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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
/*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2018 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
package testsuite;
import java.sql.*;
import virtuoso.jdbc3.*;
public class TestDateTime
{
public static final String URL = "jdbc:virtuoso://localhost:1111";
static int g_PASSED = 0;
static int g_FAILED = 0;
static int g_testCounter = 0;
static int PASSED = 0;
static int FAILED = 0;
static int testCounter = 0;
public static void resetTests()
{
PASSED = 0;
FAILED = 0;
testCounter = 0;
}
public static void startTest(String id)
{
testCounter++;
System.out.println("== TEST " + id + ": " + " : Start");
}
public static void endTest(String id, boolean OK)
{
System.out.println("== TEST " + id + ": " + " : End");
System.out.println((OK ? "PASSED:" : "***FAILED:") + " TEST " + id + "\n");
if (OK) PASSED++;
else FAILED++;
}
public static void getTestTotal()
{
System.out.println("============================");
System.out.println("PASSED:" + PASSED + " FAILED:" + FAILED);
g_PASSED += PASSED;
g_FAILED += FAILED;
g_testCounter += testCounter;
resetTests();
}
public static void getTotal()
{
System.out.println("\n=======TOTAL===================");
System.out.println("PASSED:" + g_PASSED + " FAILED:" + g_FAILED);
if (g_FAILED > 0)
System.exit(-1);
}
public static void log(String mess)
{
System.out.println(" " + mess);
}
public static void assertEquals(Object v1, Object v2) throws Exception
{
if (!v1.equals(v2))
throw new Exception("Equals failed: Received=["+v1+"] Expected=["+v2+"]");
}
public static void main(String[] args)
{
String url;
if(args.length == 0)
url = URL;
else
url = args[0];
Test_SQL(url);
Test_SPARQL_1(url);
Test_SPARQL_2(url);
getTotal();
}
public static void Test_SQL(String url)
{
log("\n TEST SQL");
Connection c = null;
Statement stmt;
PreparedStatement ps;
ResultSet rs;
try
{
Class.forName("virtuoso.jdbc3.Driver");
System.out.println("---------------------");
System.out.print("Establish connection at " + url);
c = DriverManager.getConnection (url, "dba", "dba");
System.out.println(" PASSED");
boolean ok = true;
String query = null;
String s_d = "2010-07-29";
String s_t = "15:30:20";
String s_dt = "2010-07-29 15:30:20.0";
String q_create = "create table EX..TSTDT(id integer, f_d date, f_t time, f_dt datetime)";
String q_drop = "drop table EX..TSTDT";
startTest("1"); ok = true;
try
{
log("Insert data via subst to query");
stmt = c.createStatement();
try {
stmt.executeUpdate(q_drop);
} catch(Exception e){}
stmt.executeUpdate(q_create);
stmt.executeUpdate("insert into EX..TSTDT values(1, {d '"+s_d+"'}, {t '"+s_t+"'}, {ts '"+s_dt+"'})");
rs = stmt.executeQuery("select * from EX..TSTDT where id=1");
rs.next();
assertEquals(rs.getDate(2).toString(), s_d);
assertEquals(rs.getTime(3).toString(), s_t);
assertEquals(rs.getTimestamp(4).toString(), s_dt);
rs.close();
stmt.executeUpdate(q_drop);
} catch (Exception e) {
log("***FAILED Test " + e);
e.printStackTrace();
ok = false;
}
endTest("1", ok);
startTest("2"); ok = true;
try
{
log("Insert data via parameter binding");
stmt = c.createStatement();
try {
stmt.executeUpdate(q_drop);
} catch(Exception e){}
stmt.executeUpdate(q_create);
ps = c.prepareStatement("insert into EX..TSTDT values(1, ?, ?, ?)");
ps.setDate(1, Date.valueOf(s_d));
ps.setTime(2, Time.valueOf(s_t));
ps.setTimestamp(3, Timestamp.valueOf(s_dt));
ps.executeUpdate();
ps.close();
rs = stmt.executeQuery("select * from EX..TSTDT where id=1");
rs.next();
assertEquals(rs.getDate(2).toString(), s_d);
assertEquals(rs.getTime(3).toString(), s_t);
assertEquals(rs.getTimestamp(4).toString(), s_dt);
rs.close();
stmt.executeUpdate(q_drop);
} catch (Exception e) {
log("***FAILED Test " + e);
e.printStackTrace();
ok = false;
}
endTest("2", ok);
getTestTotal();
}catch (Exception e){
System.out.println("ERROR Test Failed.");
e.printStackTrace();
}
finally {
try {
c.close();
}
catch (Exception e) {}
}
}
public static String createRdfType(String val, String v_type)
{
return "\""+val+"\"^^<"+v_type+">";
}
public static Object execSparqlSelect1(Statement stmt, String q) throws SQLException
{
ResultSet rs = stmt.executeQuery(q);
rs.next();
Object o = rs.getObject(1);
rs.close();
return o;
}
public static void Test_SPARQL_1(String url)
{
log("\n TEST SPARQL_1");
Connection c = null;
Statement stmt;
PreparedStatement ps;
ResultSet rs;
try
{
Class.forName("virtuoso.jdbc3.Driver");
System.out.println("---------------------");
System.out.print("Establish connection at " + url);
c = DriverManager.getConnection (url, "dba", "dba");
System.out.println(" PASSED");
boolean ok = true;
String query = null;
String s_d1 = "1999-05-31";
String s_d2 = "1938-01-01";
String s_d3 = "2101-01-01";
String s_t1 = "13:24:00.000Z";
String s_t2 = "13:21:00.000+04:30";
String s_t3 = "13:22:00.000+04:00";
String s_t4 = "13:23:00.000-05:00";
String s_dt1 = "1999-05-31T13:25:00-05:00";
String s_dt2 = "1999-05-31T13:24:00Z";
String s_dt3 = "1999-05-31T13:25:00.001-05:00";
String s_dt4 = "1999-05-31T13:24:00.002Z";
String s_y1 = "2005";
String t_d = "http://www.w3.org/2001/XMLSchema#date";
String t_t = "http://www.w3.org/2001/XMLSchema#time";
String t_dt = "http://www.w3.org/2001/XMLSchema#dateTime";
String t_y = "http://www.w3.org/2001/XMLSchema#gYear";
startTest("1"); ok = true;
try
{
Object o;
stmt = c.createStatement();
stmt.executeUpdate("sparql clear graph <ex_dt>");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <d1> "+createRdfType(s_d1, t_d)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <d2> "+createRdfType(s_d2, t_d)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <d3> "+createRdfType(s_d3, t_d)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <t1> "+createRdfType(s_t1, t_t)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <t2> "+createRdfType(s_t1, t_t)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <t3> "+createRdfType(s_t1, t_t)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <t4> "+createRdfType(s_t1, t_t)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <dt1> "+createRdfType(s_dt1, t_dt)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <dt2> "+createRdfType(s_dt2, t_dt)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <y1> "+createRdfType(s_y1, t_y)+" }");
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <d1> ?o}");
assertEquals(o.getClass(), VirtuosoDate.class);
assertEquals(((VirtuosoDate)o).toXSD_String(), s_d1);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <d2> ?o}");
assertEquals(o.getClass(), VirtuosoDate.class);
assertEquals(((VirtuosoDate)o).toXSD_String(), s_d2);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <d3> ?o}");
assertEquals(o.getClass(), VirtuosoDate.class);
assertEquals(((VirtuosoDate)o).toXSD_String(), s_d3);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <t1> ?o}");
assertEquals(o.getClass(), VirtuosoTime.class);
assertEquals(((VirtuosoTime)o).toXSD_String(), s_t1);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <dt1> ?o}");
assertEquals(o.getClass(), VirtuosoTimestamp.class);
assertEquals(((VirtuosoTimestamp)o).toXSD_String(), s_dt1);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <dt2> ?o}");
assertEquals(o.getClass(), VirtuosoTimestamp.class);
assertEquals(((VirtuosoTimestamp)o).toXSD_String(), s_dt2);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <y1> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_y1);
assertEquals(((VirtuosoRdfBox)o).getType(), t_y);
} catch (Exception e) {
log("***FAILED Test " + e);
e.printStackTrace();
ok = false;
}
endTest("1", ok);
getTestTotal();
}catch (Exception e){
System.out.println("ERROR Test Failed.");
e.printStackTrace();
}
finally {
try {
c.close();
}
catch (Exception e) {}
}
}
public static void Test_SPARQL_2(String url)
{
log("\n TEST SPARQL_2");
Connection c = null;
Statement stmt;
PreparedStatement ps;
ResultSet rs;
try
{
Class.forName("virtuoso.jdbc3.Driver");
System.out.println("---------------------");
System.out.print("Establish connection at " + url);
c = DriverManager.getConnection (url, "dba", "dba");
System.out.println(" PASSED");
boolean ok = true;
String query = null;
String s_y1 = "2005";
String s_y2 = "1954";
String s_gm1 = "--12"; // --MM
String s_gm2 = "--02"; // --MM
String s_gd1 = "---31"; // ---DD
String s_gd2 = "---01"; // ---DD
String s_gym1 = "2005-12"; // CCYY-MM
String s_gym2 = "2005-01"; // CCYY-MM
String s_gmd1 = "--12-31"; // --MM-DD
String s_gmd2 = "--01-01"; // --MM-DD
String s_dts1 = "1999-05-31T13:25:00-05:00";
String s_dts2 = "1999-05-31T13:24:00Z";
String s_dts3 = "1999-05-31T13:25:00.001-05:00";
String s_dts4 = "1999-05-31T13:24:00.002Z";
String t_y = "http://www.w3.org/2001/XMLSchema#gYear";
String t_gm = "http://www.w3.org/2001/XMLSchema#gMonth";
String t_gd = "http://www.w3.org/2001/XMLSchema#gDay";
String t_gym = "http://www.w3.org/2001/XMLSchema#gYearMonth";
String t_gmd = "http://www.w3.org/2001/XMLSchema#gMonthDay";
String t_dts = "http://www.w3.org/2001/XMLSchema#dateTimeStamp";
// String t_dtd = "http://www.w3.org/2001/XMLSchema#dateTimeDuration";
startTest("1"); ok = true;
try
{
Object o;
stmt = c.createStatement();
stmt.executeUpdate("sparql clear graph <ex_dt>");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <y1> "+createRdfType(s_y1, t_y)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <y2> "+createRdfType(s_y2, t_y)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <gm1> "+createRdfType(s_gm1, t_gm)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <gm2> "+createRdfType(s_gm2, t_gm)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <gd1> "+createRdfType(s_gd1, t_gd)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <gd2> "+createRdfType(s_gd2, t_gd)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <gym1> "+createRdfType(s_gym1, t_gym)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <gym2> "+createRdfType(s_gym2, t_gym)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <gmd1> "+createRdfType(s_gmd1, t_gmd)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <gmd2> "+createRdfType(s_gmd2, t_gmd)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <dts1> "+createRdfType(s_dts1, t_dts)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <dts2> "+createRdfType(s_dts2, t_dts)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <dts3> "+createRdfType(s_dts3, t_dts)+" }");
stmt.executeUpdate("sparql insert into graph <ex_dt> {<a> <dts4> "+createRdfType(s_dts4, t_dts)+" }");
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <y1> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_y1);
assertEquals(((VirtuosoRdfBox)o).getType(), t_y);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <y2> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_y2);
assertEquals(((VirtuosoRdfBox)o).getType(), t_y);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <gm1> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_gm1);
assertEquals(((VirtuosoRdfBox)o).getType(), t_gm);
assertEquals((((VirtuosoRdfBox)o).rb_box).getClass(), String.class);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <gm2> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_gm2);
assertEquals(((VirtuosoRdfBox)o).getType(), t_gm);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <gd1> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_gd1);
assertEquals(((VirtuosoRdfBox)o).getType(), t_gd);
assertEquals((((VirtuosoRdfBox)o).rb_box).getClass(), String.class);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <gd2> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_gd2);
assertEquals(((VirtuosoRdfBox)o).getType(), t_gd);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <gym1> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_gym1);
assertEquals(((VirtuosoRdfBox)o).getType(), t_gym);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <gym2> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_gym2);
assertEquals(((VirtuosoRdfBox)o).getType(), t_gym);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <gmd1> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_gmd1);
assertEquals(((VirtuosoRdfBox)o).getType(), t_gmd);
assertEquals((((VirtuosoRdfBox)o).rb_box).getClass(), String.class);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <gmd2> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_gmd2);
assertEquals(((VirtuosoRdfBox)o).getType(), t_gmd);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <dts1> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_dts1);
assertEquals(((VirtuosoRdfBox)o).getType(), t_dts);
assertEquals((((VirtuosoRdfBox)o).rb_box).getClass(), String.class);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <dts2> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_dts2);
assertEquals(((VirtuosoRdfBox)o).getType(), t_dts);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <dts3> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_dts3);
assertEquals(((VirtuosoRdfBox)o).getType(), t_dts);
o = execSparqlSelect1(stmt, "sparql select ?o from <ex_dt> where {<a> <dts4> ?o}");
assertEquals(o.getClass(), VirtuosoRdfBox.class);
assertEquals(((VirtuosoRdfBox)o).toString(), s_dts4);
assertEquals(((VirtuosoRdfBox)o).getType(), t_dts);
} catch (Exception e) {
log("***FAILED Test " + e);
e.printStackTrace();
ok = false;
}
endTest("1", ok);
getTestTotal();
}catch (Exception e){
System.out.println("ERROR Test Failed.");
e.printStackTrace();
}
finally {
try {
c.close();
}
catch (Exception e) {}
}
}
}
|