You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2011/11/10 18:05:52 UTC
svn commit: r1200430 - in
/commons/proper/math/trunk/src/test/java/org/apache/commons/math:
analysis/solvers/ complex/ distribution/ exception/util/ fraction/
Author: sebb
Date: Thu Nov 10 17:05:51 2011
New Revision: 1200430
URL: http://svn.apache.org/viewvc?rev=1200430&view=rev
Log:
Simpify exception checking:
- use expected @Test attribute
- don't catch and rethrow
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtilsTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexUtilsTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/NormalDistributionTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/PoissonDistributionTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ZipfDistributionTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/exception/util/ExceptionContextTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionFormatTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/FractionFormatTest.java
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtilsTest.java?rev=1200430&r1=1200429&r2=1200430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/solvers/UnivariateRealSolverUtilsTest.java Thu Nov 10 17:05:51 2011
@@ -32,25 +32,15 @@ public class UnivariateRealSolverUtilsTe
protected UnivariateRealFunction sin = new SinFunction();
- @Test
+ @Test(expected=MathIllegalArgumentException.class)
public void testSolveNull() {
- try {
- UnivariateRealSolverUtils.solve(null, 0.0, 4.0);
- Assert.fail();
- } catch(MathIllegalArgumentException ex){
- // success
- }
+ UnivariateRealSolverUtils.solve(null, 0.0, 4.0);
}
- @Test
+ @Test(expected=MathIllegalArgumentException.class)
public void testSolveBadEndpoints() {
- try { // bad endpoints
- double root = UnivariateRealSolverUtils.solve(sin, 4.0, -0.1, 1e-6);
- System.out.println("root=" + root);
- Assert.fail("Expecting MathIllegalArgumentException");
- } catch (MathIllegalArgumentException ex) {
- // expected
- }
+ double root = UnivariateRealSolverUtils.solve(sin, 4.0, -0.1, 1e-6);
+ System.out.println("root=" + root);
}
@Test
@@ -69,15 +59,10 @@ public class UnivariateRealSolverUtilsTe
Assert.assertEquals(FastMath.PI, x, 1.0e-4);
}
- @Test
+ @Test(expected=MathIllegalArgumentException.class)
public void testSolveAccuracyNull() {
- try {
- double accuracy = 1.0e-6;
- UnivariateRealSolverUtils.solve(null, 0.0, 4.0, accuracy);
- Assert.fail();
- } catch(MathIllegalArgumentException ex){
- // success
- }
+ double accuracy = 1.0e-6;
+ UnivariateRealSolverUtils.solve(null, 0.0, 4.0, accuracy);
}
@Test
@@ -88,14 +73,9 @@ public class UnivariateRealSolverUtilsTe
Assert.assertEquals(FastMath.PI, x, accuracy);
}
- @Test
+ @Test(expected=MathIllegalArgumentException.class)
public void testSolveNoRoot() {
- try {
- UnivariateRealSolverUtils.solve(sin, 1.0, 1.5);
- Assert.fail("Expecting MathIllegalArgumentException ");
- } catch (MathIllegalArgumentException ex) {
- // expected
- }
+ UnivariateRealSolverUtils.solve(sin, 1.0, 1.5);
}
@Test
@@ -113,44 +93,26 @@ public class UnivariateRealSolverUtilsTe
Assert.assertTrue(sin.value(result[1]) > 0);
}
- @Test
+ @Test(expected=MathIllegalArgumentException.class)
public void testNullFunction() {
- try { // null function
- UnivariateRealSolverUtils.bracket(null, 1.5, 0, 2.0);
- Assert.fail("Expecting MathIllegalArgumentException");
- } catch (MathIllegalArgumentException ex) {
- // expected
- }
+ UnivariateRealSolverUtils.bracket(null, 1.5, 0, 2.0);
}
- @Test
+ @Test(expected=MathIllegalArgumentException.class)
public void testBadInitial() {
- try { // initial not between endpoints
- UnivariateRealSolverUtils.bracket(sin, 2.5, 0, 2.0);
- Assert.fail("Expecting MathIllegalArgumentException");
- } catch (MathIllegalArgumentException ex) {
- // expected
- }
+ UnivariateRealSolverUtils.bracket(sin, 2.5, 0, 2.0);
}
- @Test
+ @Test(expected=MathIllegalArgumentException.class)
public void testBadEndpoints() {
- try { // endpoints not valid
- UnivariateRealSolverUtils.bracket(sin, 1.5, 2.0, 1.0);
- Assert.fail("Expecting MathIllegalArgumentException");
- } catch (MathIllegalArgumentException ex) {
- // expected
- }
+ // endpoints not valid
+ UnivariateRealSolverUtils.bracket(sin, 1.5, 2.0, 1.0);
}
- @Test
+ @Test(expected=MathIllegalArgumentException.class)
public void testBadMaximumIterations() {
- try { // bad maximum iterations
- UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0, 0);
- Assert.fail("Expecting MathIllegalArgumentException");
- } catch (MathIllegalArgumentException ex) {
- // expected
- }
+ // bad maximum iterations
+ UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0, 0);
}
@Test
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexUtilsTest.java?rev=1200430&r1=1200429&r2=1200430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexUtilsTest.java Thu Nov 10 17:05:51 2011
@@ -19,7 +19,6 @@ package org.apache.commons.math.complex;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.util.FastMath;
-import org.junit.Assert;
import org.junit.Test;
@@ -73,14 +72,9 @@ public class ComplexUtilsTest {
return Complex.I.multiply(new Complex(theta, 0)).exp().multiply(new Complex(r, 0));
}
- @Test
+ @Test(expected=IllegalArgumentException.class)
public void testPolar2ComplexIllegalModulus() {
- try {
- ComplexUtils.polar2Complex(-1, 0);
- Assert.fail("Expecting IllegalArgumentException");
- } catch (IllegalArgumentException ex) {
- // expected
- }
+ ComplexUtils.polar2Complex(-1, 0);
}
@Test
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java?rev=1200430&r1=1200429&r2=1200430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java Thu Nov 10 17:05:51 2011
@@ -114,14 +114,9 @@ public class ExponentialDistributionTest
Assert.assertEquals(5d, distribution.getMean(), Double.MIN_VALUE);
}
- @Test
+ @Test(expected=NotStrictlyPositiveException.class)
public void testPreconditions() {
- try {
- new ExponentialDistributionImpl(0);
- Assert.fail("Should have generated NotStrictlyPositiveException");
- } catch (NotStrictlyPositiveException e) {
- // Expected.
- }
+ new ExponentialDistributionImpl(0);
}
@Test
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/NormalDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/NormalDistributionTest.java?rev=1200430&r1=1200429&r2=1200430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/NormalDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/NormalDistributionTest.java Thu Nov 10 17:05:51 2011
@@ -123,14 +123,9 @@ public class NormalDistributionTest exte
Assert.assertEquals(1.4, distribution.getStandardDeviation(), 0);
}
- @Test
+ @Test(expected=NotStrictlyPositiveException.class)
public void testPreconditions() {
- try {
- new NormalDistributionImpl(1, 0);
- Assert.fail("Should have generated NotStrictlyPositiveException");
- } catch (NotStrictlyPositiveException e) {
- // Expected.
- }
+ new NormalDistributionImpl(1, 0);
}
@Test
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/PoissonDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/PoissonDistributionTest.java?rev=1200430&r1=1200429&r2=1200430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/PoissonDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/PoissonDistributionTest.java Thu Nov 10 17:05:51 2011
@@ -135,17 +135,14 @@ public class PoissonDistributionTest ext
Assert.assertEquals(-1, dist.inverseCumulativeProbability(0d));
}
+ @Test(expected=NotStrictlyPositiveException.class)
+ public void testNegativeMean() {
+ new PoissonDistributionImpl(-1);
+ }
+
@Test
public void testMean() {
- PoissonDistribution dist;
- try {
- dist = new PoissonDistributionImpl(-1);
- Assert.fail("negative mean: NotStrictlyPositiveException expected");
- } catch(NotStrictlyPositiveException ex) {
- // Expected.
- }
-
- dist = new PoissonDistributionImpl(10.0);
+ PoissonDistribution dist = new PoissonDistributionImpl(10.0);
Assert.assertEquals(10.0, dist.getMean(), 0.0);
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java?rev=1200430&r1=1200429&r2=1200430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/TDistributionTest.java Thu Nov 10 17:05:51 2011
@@ -108,14 +108,9 @@ public class TDistributionTest extends C
Assert.assertEquals(5d, dist.getDegreesOfFreedom(), Double.MIN_VALUE);
}
- @Test
+ @Test(expected=NotStrictlyPositiveException.class)
public void testPreconditions() {
- try {
- new TDistributionImpl(0);
- Assert.fail("Expecting NotStrictlyPositiveException for df = 0");
- } catch (NotStrictlyPositiveException ex) {
- // expected
- }
+ new TDistributionImpl(0);
}
@Test
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ZipfDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ZipfDistributionTest.java?rev=1200430&r1=1200429&r2=1200430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ZipfDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ZipfDistributionTest.java Thu Nov 10 17:05:51 2011
@@ -32,20 +32,14 @@ import org.junit.Test;
*/
public class ZipfDistributionTest extends IntegerDistributionAbstractTest {
- @Test
- public void testPreconditions() {
- try {
- new ZipfDistributionImpl(0, 1);
- Assert.fail("NotStrictlyPositiveException expected");
- } catch (NotStrictlyPositiveException e) {
- // Expected.
- }
- try {
- new ZipfDistributionImpl(1, 0);
- Assert.fail("NotStrictlyPositiveException expected");
- } catch (NotStrictlyPositiveException e) {
- // Expected.
- }
+ @Test(expected=NotStrictlyPositiveException.class)
+ public void testPreconditions1() {
+ new ZipfDistributionImpl(0, 1);
+ }
+
+ @Test(expected=NotStrictlyPositiveException.class)
+ public void testPreconditions2() {
+ new ZipfDistributionImpl(1, 0);
}
//-------------- Implementations for abstract methods -----------------------
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/exception/util/ExceptionContextTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/exception/util/ExceptionContextTest.java?rev=1200430&r1=1200429&r2=1200430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/exception/util/ExceptionContextTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/exception/util/ExceptionContextTest.java Thu Nov 10 17:05:51 2011
@@ -101,14 +101,14 @@ public class ExceptionContextTest {
}
@Test
- public void testSerializeUnserializable() {
+ public void testSerializeUnserializable() throws Exception {
final ExceptionContext cOut = new ExceptionContext(new Exception("Apache Commons Math"));
cOut.addMessage(LocalizedFormats.SIMPLE_MESSAGE, "OK");
cOut.addMessage(LocalizedFormats.SIMPLE_MESSAGE, new Unserializable());
String key = "Key 1";
cOut.setValue(key, new Unserializable());
- try {
+ {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(cOut);
@@ -119,8 +119,6 @@ public class ExceptionContextTest {
String nsObjStr = (String) cIn.getValue(key);
Assert.assertTrue(nsObjStr.matches(".*could not be serialized.*"));
- } catch (Exception e) {
- Assert.fail(e.toString());
}
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionFormatTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionFormatTest.java?rev=1200430&r1=1200429&r2=1200430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionFormatTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionFormatTest.java Thu Nov 10 17:05:51 2011
@@ -106,7 +106,7 @@ public class BigFractionFormatTest {
public void testParse() {
String source = "1 / 2";
- try {
+ {
BigFraction c = properFormat.parse(source);
Assert.assertNotNull(c);
Assert.assertEquals(BigInteger.ONE, c.getNumerator());
@@ -116,29 +116,23 @@ public class BigFractionFormatTest {
Assert.assertNotNull(c);
Assert.assertEquals(BigInteger.ONE, c.getNumerator());
Assert.assertEquals(BigInteger.valueOf(2l), c.getDenominator());
- } catch (MathParseException ex) {
- Assert.fail(ex.getMessage());
}
}
@Test
public void testParseInteger() {
String source = "10";
- try {
+ {
BigFraction c = properFormat.parse(source);
Assert.assertNotNull(c);
Assert.assertEquals(BigInteger.TEN, c.getNumerator());
Assert.assertEquals(BigInteger.ONE, c.getDenominator());
- } catch (MathParseException ex) {
- Assert.fail(ex.getMessage());
}
- try {
+ {
BigFraction c = improperFormat.parse(source);
Assert.assertNotNull(c);
Assert.assertEquals(BigInteger.TEN, c.getNumerator());
Assert.assertEquals(BigInteger.ONE, c.getDenominator());
- } catch (MathParseException ex) {
- Assert.fail(ex.getMessage());
}
}
@@ -181,7 +175,7 @@ public class BigFractionFormatTest {
@Test
public void testParseNegative() {
- try {
+ {
String source = "-1 / 2";
BigFraction c = properFormat.parse(source);
Assert.assertNotNull(c);
@@ -203,8 +197,6 @@ public class BigFractionFormatTest {
Assert.assertNotNull(c);
Assert.assertEquals(-1, c.getNumeratorAsInt());
Assert.assertEquals(2, c.getDenominatorAsInt());
- } catch (MathParseException ex) {
- Assert.fail(ex.getMessage());
}
}
@@ -212,13 +204,11 @@ public class BigFractionFormatTest {
public void testParseProper() {
String source = "1 2 / 3";
- try {
+ {
BigFraction c = properFormat.parse(source);
Assert.assertNotNull(c);
Assert.assertEquals(5, c.getNumeratorAsInt());
Assert.assertEquals(3, c.getDenominatorAsInt());
- } catch (MathParseException ex) {
- Assert.fail(ex.getMessage());
}
try {
@@ -232,13 +222,11 @@ public class BigFractionFormatTest {
@Test
public void testParseProperNegative() {
String source = "-1 2 / 3";
- try {
+ {
BigFraction c = properFormat.parse(source);
Assert.assertNotNull(c);
Assert.assertEquals(-5, c.getNumeratorAsInt());
Assert.assertEquals(3, c.getDenominatorAsInt());
- } catch (MathParseException ex) {
- Assert.fail(ex.getMessage());
}
try {
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionTest.java?rev=1200430&r1=1200429&r2=1200430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/BigFractionTest.java Thu Nov 10 17:05:51 2011
@@ -90,16 +90,10 @@ public class BigFractionTest {
}
}
- @Test
+ @Test(expected=ConvergenceException.class)
public void testGoldenRatio() {
- try {
- // the golden ratio is notoriously a difficult number for continuous
- // fraction
- new BigFraction((1 + FastMath.sqrt(5)) / 2, 1.0e-12, 25);
- Assert.fail("an exception should have been thrown");
- } catch (ConvergenceException ce) {
- // expected behavior
- }
+ // the golden ratio is notoriously a difficult number for continuous fraction
+ new BigFraction((1 + FastMath.sqrt(5)) / 2, 1.0e-12, 25);
}
// MATH-179
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/FractionFormatTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/FractionFormatTest.java?rev=1200430&r1=1200429&r2=1200430&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/FractionFormatTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/fraction/FractionFormatTest.java Thu Nov 10 17:05:51 2011
@@ -122,21 +122,17 @@ public class FractionFormatTest {
@Test
public void testParseInteger() {
String source = "10";
- try {
+ {
Fraction c = properFormat.parse(source);
Assert.assertNotNull(c);
Assert.assertEquals(10, c.getNumerator());
Assert.assertEquals(1, c.getDenominator());
- } catch (MathParseException ex) {
- Assert.fail(ex.getMessage());
}
- try {
+ {
Fraction c = improperFormat.parse(source);
Assert.assertNotNull(c);
Assert.assertEquals(10, c.getNumerator());
Assert.assertEquals(1, c.getDenominator());
- } catch (MathParseException ex) {
- Assert.fail(ex.getMessage());
}
}
@@ -179,7 +175,7 @@ public class FractionFormatTest {
@Test
public void testParseNegative() {
- try {
+ {
String source = "-1 / 2";
Fraction c = properFormat.parse(source);
Assert.assertNotNull(c);
@@ -201,8 +197,6 @@ public class FractionFormatTest {
Assert.assertNotNull(c);
Assert.assertEquals(-1, c.getNumerator());
Assert.assertEquals(2, c.getDenominator());
- } catch (MathParseException ex) {
- Assert.fail(ex.getMessage());
}
}
@@ -210,13 +204,11 @@ public class FractionFormatTest {
public void testParseProper() {
String source = "1 2 / 3";
- try {
+ {
Fraction c = properFormat.parse(source);
Assert.assertNotNull(c);
Assert.assertEquals(5, c.getNumerator());
Assert.assertEquals(3, c.getDenominator());
- } catch (MathParseException ex) {
- Assert.fail(ex.getMessage());
}
try {
@@ -230,13 +222,11 @@ public class FractionFormatTest {
@Test
public void testParseProperNegative() {
String source = "-1 2 / 3";
- try {
+ {
Fraction c = properFormat.parse(source);
Assert.assertNotNull(c);
Assert.assertEquals(-5, c.getNumerator());
Assert.assertEquals(3, c.getDenominator());
- } catch (MathParseException ex) {
- Assert.fail(ex.getMessage());
}
try {