From 99cc24eb786836591bb5e11e1dcc9ea7e8145d92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Sat, 6 Oct 2018 13:39:07 +0200 Subject: [PATCH] [tests] Add Repeat annotation into the testing API. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Galland --- .../main/java/io/sarl/tests/api/Repeat.java | 48 ++++++++++++ .../java/io/sarl/tests/api/RepeatRule.java | 75 +++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 tests/io.sarl.tests.api/src/main/java/io/sarl/tests/api/Repeat.java create mode 100644 tests/io.sarl.tests.api/src/main/java/io/sarl/tests/api/RepeatRule.java diff --git a/tests/io.sarl.tests.api/src/main/java/io/sarl/tests/api/Repeat.java b/tests/io.sarl.tests.api/src/main/java/io/sarl/tests/api/Repeat.java new file mode 100644 index 0000000000..b840e90334 --- /dev/null +++ b/tests/io.sarl.tests.api/src/main/java/io/sarl/tests/api/Repeat.java @@ -0,0 +1,48 @@ +/* + * $Id$ + * + * Janus platform is an open-source multiagent platform. + * More details on http://www.janusproject.io + * + * Copyright (C) 2014-2015 Sebastian RODRIGUEZ, Nicolas GAUD, Stéphane GALLAND. + * + * Licensed 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 io.sarl.tests.api; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Repeat an unit test. + * + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + * @since 0.9 + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) +public @interface Repeat { + + /** Replies the number of repeats. + * + * @return the number of repeats. + */ + int value() default 1; + +} diff --git a/tests/io.sarl.tests.api/src/main/java/io/sarl/tests/api/RepeatRule.java b/tests/io.sarl.tests.api/src/main/java/io/sarl/tests/api/RepeatRule.java new file mode 100644 index 0000000000..df427287ed --- /dev/null +++ b/tests/io.sarl.tests.api/src/main/java/io/sarl/tests/api/RepeatRule.java @@ -0,0 +1,75 @@ +/* + * $Id$ + * + * Janus platform is an open-source multiagent platform. + * More details on http://www.janusproject.io + * + * Copyright (C) 2014-2015 Sebastian RODRIGUEZ, Nicolas GAUD, Stéphane GALLAND. + * + * Licensed 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 io.sarl.tests.api; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * Rule for repeating unit tests. + * + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + * @since 0.9 + */ +public class RepeatRule implements TestRule { + + @Override + public Statement apply(Statement statement, Description description) { + Statement result = statement; + Repeat repeat = description.getAnnotation(Repeat.class); + if (repeat != null) { + int times = repeat.value(); + result = new RepeatStatement(statement, times); + } + return result; + } + + /** + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + * @since 0.9 + */ + private static class RepeatStatement extends Statement { + + private final Statement statement; + + private final int repeat; + + RepeatStatement(Statement statement, int repeat) { + this.statement = statement; + this.repeat = repeat; + } + + @Override + public void evaluate() throws Throwable { + for (int i = 0; i < this.repeat; i++) { + this.statement.evaluate(); + } + } + + } + +}