Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 80756 invoked from network); 8 Mar 2009 08:45:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 8 Mar 2009 08:45:24 -0000 Received: (qmail 17614 invoked by uid 500); 8 Mar 2009 08:45:23 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 17556 invoked by uid 500); 8 Mar 2009 08:45:23 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 17547 invoked by uid 99); 8 Mar 2009 08:45:23 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 08 Mar 2009 00:45:23 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 08 Mar 2009 08:45:22 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C7B3623888F4; Sun, 8 Mar 2009 08:45:01 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r751384 - /commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/QueryRunnerTest.java Date: Sun, 08 Mar 2009 08:45:01 -0000 To: commits@commons.apache.org From: dfabulich@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090308084501.C7B3623888F4@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: dfabulich Date: Sun Mar 8 08:45:01 2009 New Revision: 751384 URL: http://svn.apache.org/viewvc?rev=751384&view=rev Log: Added additional test for fillStatement and nulls Modified: commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/QueryRunnerTest.java Modified: commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/QueryRunnerTest.java URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/QueryRunnerTest.java?rev=751384&r1=751383&r2=751384&view=diff ============================================================================== --- commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/QueryRunnerTest.java (original) +++ commons/proper/dbutils/trunk/src/test/org/apache/commons/dbutils/QueryRunnerTest.java Sun Mar 8 08:45:01 2009 @@ -20,8 +20,11 @@ import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.sql.ParameterMetaData; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.sql.Types; import java.util.Arrays; import junit.framework.TestCase; @@ -30,11 +33,69 @@ QueryRunner runner; PreparedStatement stmt; + static final Method getParameterCount, getParameterType, getParameterMetaData; + static { + try { + getParameterCount = ParameterMetaData.class.getMethod("getParameterCount", new Class[0]); + getParameterType = ParameterMetaData.class.getMethod("getParameterType", new Class[]{int.class}); + getParameterMetaData = PreparedStatement.class.getMethod("getParameterMetaData", new Class[0]); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + public void setUp() { runner = new QueryRunner(); stmt = fakePreparedStatement(); } + public void testFillStatementWithNull() throws Exception { + stmt = fakeFillablePreparedStatement(false, new int[] {Types.VARCHAR, Types.BIGINT}); + runner.fillStatement(stmt, new Object[] { null, null }); + } + + public void testFillStatementWithNullOracle() throws Exception { + stmt = fakeFillablePreparedStatement(true, new int[] {Types.VARCHAR, Types.BIGINT}); + runner.fillStatement(stmt, new Object[] { null, null }); + } + + private PreparedStatement fakeFillablePreparedStatement(final boolean simulateOracle, final int[] types) throws NoSuchMethodException { + // prepare a mock ParameterMetaData and a mock PreparedStatement to return the PMD + final ParameterMetaData pmd = mockParameterMetaData(simulateOracle,types); + InvocationHandler stmtHandler = new InvocationHandler() { + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + if (getParameterMetaData.equals(method)) { + return pmd; + } + return null; + } + }; + return ProxyFactory.instance().createPreparedStatement(stmtHandler); + } + + private ParameterMetaData mockParameterMetaData(final boolean simulateOracle, final int[] types) { + InvocationHandler pmdHandler = new InvocationHandler() { + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + if (getParameterCount.equals(method)) { + return new Integer(types.length); + } + if (getParameterType.equals(method)) { + if (simulateOracle) throw new SQLException("Oracle fails when you call getParameterType"); + int arg = ((Integer)args[0]).intValue(); + return new Integer(types[arg-1]); + } + return null; + } + }; + + return (ParameterMetaData) Proxy.newProxyInstance( + pmdHandler.getClass().getClassLoader(), + new Class[] {ParameterMetaData.class}, + pmdHandler); + } + public void testFillStatementWithBean() throws SQLException { TestBean tb = new TestBean(); tb.setOne("uno"); @@ -137,4 +198,5 @@ this.params = params; } } + }