diff --git a/test/src/com/mchange/v2/c3p0/test/Percent20FailConnectionTester.java b/test/src/com/mchange/v2/c3p0/test/Percent20FailConnectionTester.java index 18d9867f..dfd5d72b 100644 --- a/test/src/com/mchange/v2/c3p0/test/Percent20FailConnectionTester.java +++ b/test/src/com/mchange/v2/c3p0/test/Percent20FailConnectionTester.java @@ -6,41 +6,9 @@ import com.mchange.v2.log.MLog; import com.mchange.v2.log.MLogger; -public final class Percent20FailConnectionTester implements QueryConnectionTester +public final class Percent20FailConnectionTester extends PercentXXFailConnectionTester { - final static MLogger logger = MLog.getLogger( Percent20FailConnectionTester.class ); - - { - //logger.log(MLevel.WARNING, "Instantiated: " + this, new Exception("Instantiation Stack Trace.") ); - } - - private int roulette() - { - if (Math.random() < 0.20d) - return CONNECTION_IS_INVALID; - else - return CONNECTION_IS_OKAY; - } - - public int activeCheckConnection(Connection c) - { - //logger.warning(this + ": activeCheckConnection(Connection c)"); - return roulette(); - } - - public int statusOnException(Connection c, Throwable t) - { - //logger.warning(this + ": statusOnException(Connection c, Throwable t)"); - return roulette(); - } - - public int activeCheckConnection(Connection c, String preferredTestQuery) - { - //logger.warning(this + ": activeCheckConnection(Connection c, String preferredTestQuery)"); - return roulette(); - } - - public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); } - public int hashCode() { return this.getClass().getName().hashCode(); } + public Percent20FailConnectionTester() + { super(20); } } diff --git a/test/src/com/mchange/v2/c3p0/test/Percent80FailConnectionTester.java b/test/src/com/mchange/v2/c3p0/test/Percent80FailConnectionTester.java index 2e973946..aa8bb933 100644 --- a/test/src/com/mchange/v2/c3p0/test/Percent80FailConnectionTester.java +++ b/test/src/com/mchange/v2/c3p0/test/Percent80FailConnectionTester.java @@ -6,41 +6,9 @@ import com.mchange.v2.log.MLog; import com.mchange.v2.log.MLogger; -public final class Percent80FailConnectionTester implements QueryConnectionTester +public final class Percent80FailConnectionTester extends PercentXXFailConnectionTester { - final static MLogger logger = MLog.getLogger( Percent80FailConnectionTester.class ); - - { - //logger.log(MLevel.WARNING, "Instantiated: " + this, new Exception("Instantiation Stack Trace.") ); - } - - private int roulette() - { - if (Math.random() < 0.80d) - return CONNECTION_IS_INVALID; - else - return CONNECTION_IS_OKAY; - } - - public int activeCheckConnection(Connection c) - { - //logger.warning(this + ": activeCheckConnection(Connection c)"); - return roulette(); - } - - public int statusOnException(Connection c, Throwable t) - { - //logger.warning(this + ": statusOnException(Connection c, Throwable t)"); - return roulette(); - } - - public int activeCheckConnection(Connection c, String preferredTestQuery) - { - //logger.warning(this + ": activeCheckConnection(Connection c, String preferredTestQuery)"); - return roulette(); - } - - public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); } - public int hashCode() { return this.getClass().getName().hashCode(); } + public Percent80FailConnectionTester() + { super(80); } } diff --git a/test/src/com/mchange/v2/c3p0/test/PercentXXFailConnectionTester.java b/test/src/com/mchange/v2/c3p0/test/PercentXXFailConnectionTester.java new file mode 100644 index 00000000..3963844f --- /dev/null +++ b/test/src/com/mchange/v2/c3p0/test/PercentXXFailConnectionTester.java @@ -0,0 +1,52 @@ +package com.mchange.v2.c3p0.test; + +import java.sql.Connection; +import com.mchange.v2.c3p0.QueryConnectionTester; +import com.mchange.v2.log.MLevel; +import com.mchange.v2.log.MLog; +import com.mchange.v2.log.MLogger; + +public abstract class PercentXXFailConnectionTester implements QueryConnectionTester +{ + //final static MLogger logger = MLog.getLogger( PercentXXFailConnectionTester.class ); + + double threshold; + + protected PercentXXFailConnectionTester(int pct) + { + if (pct < 0 || pct > 100) + throw new IllegalArgumentException("Percentage must be between 0 and 100, found " + pct + "."); + this.threshold = pct / 100d; + //logger.log(MLevel.WARNING, "Instantiated: " + this, new Exception("Instantiation Stack Trace.") ); + } + + private int roulette() + { + if (Math.random() < threshold) + return CONNECTION_IS_INVALID; + else + return CONNECTION_IS_OKAY; + } + + public int activeCheckConnection(Connection c) + { + //logger.warning(this + ": activeCheckConnection(Connection c)"); + return roulette(); + } + + public int statusOnException(Connection c, Throwable t) + { + //logger.warning(this + ": statusOnException(Connection c, Throwable t)"); + return roulette(); + } + + public int activeCheckConnection(Connection c, String preferredTestQuery) + { + //logger.warning(this + ": activeCheckConnection(Connection c, String preferredTestQuery)"); + return roulette(); + } + + public boolean equals( Object o ) { return this.getClass().equals( o.getClass() ); } + public int hashCode() { return this.getClass().getName().hashCode(); } +} +