final StatementContext statementContext = ((StatementException) t.getCause()).getStatementContext();
@Test public void testUsefulExceptionForBackTracing() throws Exception { Handle h = openHandle(); try { h.createStatement("insert-id-name").bind("id", 1).execute(); fail("should have raised an exception"); } catch (StatementException e) { assertTrue(e.getMessage().contains("insert into something(id, name) values (:id, :name)")); assertTrue(e.getMessage().contains("insert into something(id, name) values (?, ?)")); assertTrue(e.getMessage().contains("insert-id-name")); } }
@Test public void testUsefulExceptionForBackTracing() throws Exception { Handle h = openHandle(); try { h.createStatement("insert-id-name").bind("id", 1).execute(); fail("should have raised an exception"); } catch (StatementException e) { assertTrue(e.getMessage().contains("insert into something(id, name) values (:id, :name)")); assertTrue(e.getMessage().contains("insert into something(id, name) values (?, ?)")); assertTrue(e.getMessage().contains("insert-id-name")); } }
final StatementContext statementContext = ((StatementException) t.getCause()).getStatementContext();
@Test public void testUsefulArgumentOutputForDebug() throws Exception { try { h.createStatement("insert into something (id, name) values (:id, :name)") .bind("name", "brian") .bind(7, 8) .bindFromMap(new HandyMapThing<String>().add("one", "two")) .bindFromProperties(new Object()) .execute(); } catch (StatementException e) { assertTrue(e.getMessage() .contains("arguments:{ positional:{7:8}, named:{name:'brian'}, finder:[{one=two},{lazy bean proprty arguments \"java.lang.Object")); } }
final StatementContext statementContext = ((StatementException) t.getCause()).getStatementContext();
@Test public void testUsefulArgumentOutputForDebug() throws Exception { try { h.createStatement("insert into something (id, name) values (:id, :name)") .bind("name", "brian") .bind(7, 8) .bindFromMap(new HandyMapThing<String>().add("one", "two")) .bindFromProperties(new Object()) .execute(); } catch (StatementException e) { assertTrue(e.getMessage() .contains("arguments:{ positional:{7:8}, named:{name:'brian'}, finder:[{one=two},{lazy bean proprty arguments \"java.lang.Object")); } }
@Override public String getMessage() { String base = super.getMessage(); StatementContext ctx = getStatementContext(); if (ctx == null) { return base; } else { return String.format("%s [statement:\"%s\", located:\"%s\", rewritten:\"%s\", arguments:%s]", base, ctx.getRawSql(), ctx.getLocatedSql(), ctx.getRewrittenSql(), ctx.getBinding()); } } }
@Override public String getMessage() { String base = super.getMessage(); StatementContext ctx = getStatementContext(); if (ctx == null) { return base; } else { return String.format("%s [statement:\"%s\", located:\"%s\", rewritten:\"%s\", arguments:%s]", base, ctx.getRawSql(), ctx.getLocatedSql(), ctx.getRewrittenSql(), ctx.getBinding()); } } }
@Test public void testFoo() throws Exception { Handle h = openHandle(); try { h.insert("WOOF", 7, "Tom"); } catch (StatementException e) { assertEquals(e.getStatementContext().getRawSql(), "WOOF"); } } }
@Test public void testScriptAsSetOfSeparateStatements() throws Exception { try { BasicHandle h = openHandle(); Script script = h.createScript("malformed-sql-script"); script.executeAsSeparateStatements(); fail("Should fail because the script is malformed"); } catch (StatementException e) { StatementContext context = e.getStatementContext(); assertEquals(context.getRawSql().trim(), "insert into something(id, name) values (2, eric)"); } } }
@Test public void testFoo() throws Exception { Handle h = openHandle(); try { h.insert("WOOF", 7, "Tom"); } catch (StatementException e) { assertEquals(e.getStatementContext().getRawSql(), "WOOF"); } } }
@Test public void testScriptAsSetOfSeparateStatements() throws Exception { try { BasicHandle h = openHandle(); Script script = h.createScript("malformed-sql-script"); script.executeAsSeparateStatements(); fail("Should fail because the script is malformed"); } catch (StatementException e) { StatementContext context = e.getStatementContext(); assertEquals(context.getRawSql().trim(), "insert into something(id, name) values (2, eric)"); } } }