Remove obsolete helper class for unit testing.

Retries are handled by the "surefire" plugin.
This commit is contained in:
Gilles Sadowski 2021-08-27 19:16:03 +02:00
parent dd0acaa9ff
commit bf1c2c214c
3 changed files with 0 additions and 140 deletions

View File

@ -1,81 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.legacy;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
/**
* A test runner that retries tests when assertions fail.
*/
public class RetryRunner extends BlockJUnit4ClassRunner {
/**
* Simple constructor.
*
* @param testClass Class to test.
* @throws InitializationError if default runner cannot be built.
*/
public RetryRunner(final Class<?> testClass)
throws InitializationError {
super(testClass);
}
@Override
public Statement methodInvoker(final FrameworkMethod method,
Object test) {
final Statement singleTryStatement = super.methodInvoker(method, test);
return new Statement() {
/**
* Evaluate the statement.
* We attempt several runs for the test, at most MAX_ATTEMPTS.
* if one attempt succeeds, we succeed, if all attempts fail, we
* fail with the reason corresponding to the last attempt
*/
@Override
public void evaluate() throws Throwable {
Throwable failureReason = null;
final Retry retry = method.getAnnotation(Retry.class);
if (retry == null) {
// Do a single test run attempt.
singleTryStatement.evaluate();
} else {
final int numRetries = retry.value();
for (int i = 0; i < numRetries; ++i) {
try {
// Do a single test run attempt.
singleTryStatement.evaluate();
// Attempt succeeded, stop evaluation here.
return;
} catch (Throwable t) {
// Attempt failed, store the reason.
failureReason = t;
}
}
// All attempts failed.
throw failureReason;
}
}
};
}
}

View File

@ -1,54 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math4.legacy;
import java.util.Random;
import org.apache.commons.math4.legacy.exception.MathIllegalStateException;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Test for the "Retry" functionality (retrying Junit test methods).
*/
@RunWith(RetryRunner.class)
public class RetryRunnerTest {
private final Random rng = new Random();
/**
* Shows that an always failing test will fail even if it is retried.
*/
@Test(expected=MathIllegalStateException.class)
@Retry
public void testRetryFailAlways() {
throw new MathIllegalStateException();
}
/**
* Shows that a test that sometimes fail might succeed if it is retried.
* In this case the high number of retries makes it quite unlikely that
* the exception will be thrown by all of the calls.
*/
@Test
@Retry(100)
public void testRetryFailSometimes() {
if (rng.nextBoolean()) {
throw new MathIllegalStateException();
}
}
}

View File

@ -16,8 +16,6 @@
*/
package org.apache.commons.math4.legacy.optim.nonlinear.scalar.noderiv;
import org.apache.commons.math4.legacy.Retry;
import org.apache.commons.math4.legacy.RetryRunner;
import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
import org.apache.commons.math4.legacy.exception.NotPositiveException;
@ -41,7 +39,6 @@ import org.junit.runner.RunWith;
/**
* Test for {@link CMAESOptimizer}.
*/
@RunWith(RetryRunner.class)
public class CMAESOptimizerTest {
static final int DIM = 13;
@ -125,7 +122,6 @@ public class CMAESOptimizerTest {
}
@Test
@Retry(3)
public void testRosen() {
final int dim = 12;
double[] startPoint = OptimTestUtils.point(dim, 0.1);
@ -142,7 +138,6 @@ public class CMAESOptimizerTest {
}
@Test
@Retry(3)
public void testMaximize() {
double[] startPoint = OptimTestUtils.point(DIM,1.0);
double[] insigma = OptimTestUtils.point(DIM,0.1);