com.crosstreelabs.junited.timing.TimingRule Maven / Gradle / Ivy
/*
* 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 com.crosstreelabs.junited.timing;
import com.crosstreelabs.junited.core.WrappingRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class TimingRule extends WrappingRule {
private long start = -1;
@Override
protected void before(final Statement statement,
final Description description) throws Throwable {
start = System.currentTimeMillis();
}
@Override
protected void after(Statement statement, Description description) {
long stop = System.currentTimeMillis();
if (description.getAnnotation(Timed.class) == null && description.getTestClass().isAnnotationPresent(Timed.class)) {
return;
}
long diff = stop - start;
String testCaseIdentifier = description.getTestClass().getSimpleName()+"."+description.getMethodName();
String time;
if (diff < 1000) {
time = diff+"ns";
} else if (diff < 1000000) {
time = (diff/1000)+"us";
} else if (diff < 1000000000) {
time = (diff/1000000)+"ms";
} else {
time = (diff/1000000000)+"s";
}
String dots = new String(new char[78-testCaseIdentifier.length()-time.length()]).replace('\0', '.');
System.out.println(testCaseIdentifier+' '+dots+' '+time);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy