|
|
@@ -6,6 +6,8 @@
|
|
|
*/
|
|
|
package net.ranides.assira.trace;
|
|
|
|
|
|
+import net.ranides.assira.reflection.*;
|
|
|
+
|
|
|
import java.util.List;
|
|
|
import java.util.Optional;
|
|
|
import java.util.function.Predicate;
|
|
|
@@ -119,4 +121,40 @@ public final class TraceUtils {
|
|
|
return getName(2);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Finds first public StackFrame invoked by JUnit framework.
|
|
|
+ * @return empty if code is not invoked from unit test
|
|
|
+ */
|
|
|
+ public static Optional<StackTraceElement> getTestFrame() {
|
|
|
+ List<StackTraceElement> frames = TraceUtils.getFrames();
|
|
|
+ int junit = -1;
|
|
|
+
|
|
|
+ // find first junit frame
|
|
|
+ for(int i=0, n=frames.size(); i<n; i++) {
|
|
|
+ if(frames.get(i).getClassName().startsWith("org.junit")) {
|
|
|
+ junit = i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // move back to first public frame
|
|
|
+ for(int i=junit-1; i>0; i--) {
|
|
|
+ StackTraceElement frame = frames.get(i);
|
|
|
+ IClass<?> type = IClass.typeinfo(frame.getClassName());
|
|
|
+
|
|
|
+ if(!type.isPublic()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(type.name().startsWith("java.")) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(type.methods().require(IAttribute.PUBLIC).require(frame.getMethodName()).isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ return Optional.of(frame);
|
|
|
+ }
|
|
|
+ return Optional.empty();
|
|
|
+ }
|
|
|
+
|
|
|
}
|