
org.jnario.runner.internal.ExtensionClass Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.jnario.standalone Show documentation
Show all versions of org.jnario.standalone Show documentation
The required libraries to execute Jnario specifications without Eclipse.
The newest version!
/*******************************************************************************
* Copyright (c) 2012 BMW Car IT and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.jnario.runner.internal;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.FrameworkMethod;
/**
* @author Sebastian Benz - Initial contribution and API
*/
public class ExtensionClass {
private Map, List> methodsForAnnotations= new HashMap, List>();
private FrameworkField extensionField;
public ExtensionClass(FrameworkField extensionField) {
this.extensionField = extensionField;
for (Class> eachClass : getSuperClasses(extensionField.getField().getType())) {
for (Method eachMethod : eachClass.getDeclaredMethods()){
addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations);
}
}
}
private void addToAnnotationLists(FrameworkMethod member, Map, List> map) {
for (Annotation each : member.getAnnotations()) {
Class extends Annotation> type= each.annotationType();
List members= getAnnotatedMembers(map, type);
for (FrameworkMethod otherMethod : members) {
if(member.isShadowedBy(otherMethod)){
return;
}
}
if (runsTopToBottom(type))
members.add(0, member);
else
members.add(member);
}
}
public List getAnnotatedMethods(
Class extends Annotation> annotationClass) {
return getAnnotatedMembers(methodsForAnnotations, annotationClass);
}
private List getAnnotatedMembers(Map, List> map,
Class extends Annotation> type) {
if (!map.containsKey(type))
map.put(type, new ArrayList());
return map.get(type);
}
private boolean runsTopToBottom(Class extends Annotation> annotation) {
return annotation.equals(Before.class)
|| annotation.equals(BeforeClass.class);
}
private List> getSuperClasses(Class> testClass) {
ArrayList> results= new ArrayList>();
Class> current= testClass;
while (current != null) {
results.add(current);
current= current.getSuperclass();
}
return results;
}
public Object get(Object target) throws IllegalArgumentException, IllegalAccessException {
return extensionField.get(target);
}
public List allMethodsWithAnnotation(
Class extends Annotation> annotationType) {
List result = methodsForAnnotations.get(annotationType);
return result == null ? Collections.emptyList() : result;
}
public String getName() {
Field field = extensionField.getField();
return field.getType().getName() + "#" + field.getName();
}
public boolean isStatic() {
return extensionField.isStatic();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy