Hi Christopher,

I only called the basicdatasource.getconnection() in the main test class.

but i added TestDriver.addconnection() beforehand.

this is my driver class

public class TestDriver implements Driver {

public static final String TESTDRIVER_PREFIX = "jdbc:not_a_db:";

static {
try {
DriverManager.registerDriver(new TestDriver());
} catch (SQLException e) {
e.printStackTrace();
}
}

private static List<Connection> connections = new ArrayList<Connection>();

public static void reset() {
connections.clear();
}

public static void addConnection(Connection c) {
connections.add(c);
}

private static Connection getNextConnectionFromList() {
if (connections.isEmpty()) {
throw new IllegalStateException("No more connections available.");
}
return connections.remove(0);
}


@Override
public Connection connect(String url, Properties info) throws SQLException {
return getNextConnectionFromList();
}

@Override
public boolean acceptsURL(String url) throws SQLException {
return url.contains(TESTDRIVER_PREFIX);
}

@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
throws SQLException {
return new DriverPropertyInfo[0];
}

@Override
public int getMajorVersion() {
return 3;
}

@Override
public int getMinorVersion() {
return 42;
}

@Override
public boolean jdbcCompliant() {
return true;
}

// for JDK 7
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}

as you can see, there is no resultset returned.

regarding the validation query, i only use it during the test with Tomcat
8, as it complains about it.

I agree with the mocking connection, the biggest PITA is : it does not
prove anything, that is why i am using Derby to speed up the process.

What John Huss wrote in the email chain is also interesting and need to
keep in note, though.

Roy


On Thu, Aug 11, 2016 at 6:02 PM, Christopher Schultz <
ch...@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> Roy,
>
> On 8/11/16 3:43 AM, Roy Leonardus wrote:
> > Hello Christopher,
> >
> > maybe i'm a little bit slow here, but from what i see, we should
> > mock the connection method
> >
> > Connection connection= ds.getConnection();
> >
> > something like
> >
> > Connection connection =(Connection) EasyMock.expect(ds.
> > isvalid()).andReturn(true).anyTimes();
> >
> > Problem is, the error happened during the validation of the
> > PoolableConnection, below is the build error from the surefire
> >
> > Caused by: java.sql.SQLException: isValid() returned false at
> > org.apache.tomcat.dbcp.dbcp2.PoolableConnection.validate(PoolableConne
> ction.java:283)
> >
> >
> at
> > org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory.validateConnect
> ion(PoolableConnectionFactory.java:357)
> >
> >
> at
> > org.apache.tomcat.dbcp.dbcp2.BasicDataSource.validateConnectionFactory
> (BasicDataSource.java:2307)
> >
> >
> at
> > org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createPoolableConnectionF
> actory(BasicDataSource.java:2290)
>
>
> It's
> >
> been a while since I've bothered to mock the JDBC interfaces
> because they are such a PITA to do. But it's possible that your mock
> Connection isn't being used by the BasicDataSource. Perhaps you need
> to mock the BasicDataSource (or maybe just DataSource) too.
>
> > so i think we should either override the validate() so the
> > validation is always returning true( which i am reluctant to do, as
> > the point is to test the whole thing)
> >
> > or enhancing the fake driver connection, so it always responding
> > with a result set.
>
> If you have set a validation query, that query should be executed. If
> you don't specify a query, I'd expect Connection.isValid to be used
> instead. I see from the exception that the isValid is failing, but you
> also mentioned that you tried to set a validation query and got a
> different error.
>
> I think you should keep going without a validation query because it
> should simplify things a little bit. Can you post more of your code?
> It's hard to tell how you are setting-up your test-case. If you have
> mock objects, what are you mocking and how are you injecting those
> mock objects into the code you are testing?
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iQIcBAEBCAAGBQJXrKGFAAoJEBzwKT+lPKRYhfwP/AvTbY+aDWduuHChdoRAoPR2
> bjcA6u6QjCOxmpFT2qzMnp9PYNnUbqx6GpGyY0poSN6eX9lG5jc27r0SN1Hvrbwk
> Rp/+dFySnwMupnLccvhfZnqMZvoyVhPegdEwsbZ4Ep9zzE+wVK0hxAc/8V5nAZpY
> 5SLTYACt63i5pWyYTwxCHpVVYYyeSm+S9OH2nogD4QugDoV93g6K+YSLpM35SVHp
> ul55OBUrWazDQIay6FcFmIf1aQmeNlGejHIPjwdwCsF4QXEwY179s3VFMlKeKkUa
> ur/fnZpJ2ZTGFe9q7UzZW+X0HaZHNDHlu/FJAzjquNIxzHxZBh1vsOHncn0D44F5
> 0yWLRJkuDDT/2e9gwZPQ2civfrAC5c6bKudB03KIZfx5tmCrJMRuneUTt5viIHet
> yCF5F30jeOEbBeag1u6A5MH1f96kwJ6h995ohitSzwH4K9vUqaDhL+UFnkVdjusR
> vjanlOQcLUYu99fce7QtCMJTThlZy00REmrB0kgnM/VKPOU7ovBFQC95vYknsYyx
> 8zt2vwKdcz5DYYBhwEJ5D1/SdLNXTeIMJnH9rsIEbXP3DSQVEjeyLTMusVKUP6ER
> fN23NOTIyiiceVZdNdXeDh1nhuhsadkGdzFdzK7zdlygcQ+e3yUfhl/T5dfNZyJ+
> jnfF+jGQ+CODibpRYFqh
> =QV9g
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>

Reply via email to