Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] Add Repeat annotation into the testing API.
Signed-off-by: Stéphane Galland <galland@arakhne.org>
- Loading branch information
1 parent
d2d3cec
commit 99cc24e
Showing
2 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
tests/io.sarl.tests.api/src/main/java/io/sarl/tests/api/Repeat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
tests/io.sarl.tests.api/src/main/java/io/sarl/tests/api/RepeatRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |