Last active
August 29, 2015 13:56
-
-
Save tauty/8947516 to your computer and use it in GitHub Desktop.
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
package com.github.tauty.rufa.rules; | |
import java.io.PrintStream; | |
import java.io.PrintWriter; | |
import java.lang.reflect.Array; | |
import java.lang.reflect.Field; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import org.junit.rules.TestRule; | |
import org.junit.runner.Description; | |
import org.junit.runners.model.Statement; | |
public class Theories implements TestRule { | |
private final Errors errors = new Errors("There are failure patterns:"); | |
private final Object testInstance; | |
public Theories(Object testInstance){ | |
this.testInstance = testInstance; | |
} | |
private static final String FIELD_SUFFIX_OF_DATAPOINT = "DataPoint"; | |
private static final class FieldPair{ | |
private final Field value; | |
private final Field dataPoint; | |
private FieldPair(Field value, Field dataPoint){ | |
this.value = value; | |
this.dataPoint = dataPoint; | |
} | |
} | |
public Statement apply(final Statement stmt, final Description desc) { | |
Map<String, Field> map = new HashMap<String, Field>(); | |
final List<FieldPair> fl = new ArrayList<FieldPair>(); | |
for (Field f : testInstance.getClass().getDeclaredFields()) { | |
String name = f.getName(); | |
if(name.endsWith(FIELD_SUFFIX_OF_DATAPOINT)){ | |
Field field = map.get(name.substring(0, name.length() - FIELD_SUFFIX_OF_DATAPOINT.length())); | |
if(field != null) | |
fl.add(new FieldPair(field,f)); | |
else | |
map.put(f.getName(), f); | |
} else { | |
Field field = map.get(name + FIELD_SUFFIX_OF_DATAPOINT); | |
if(field != null) | |
fl.add(new FieldPair(f, field)); | |
else | |
map.put(f.getName(), f); | |
} | |
} | |
final Theories self = this; | |
return new Statement() { | |
@Override | |
public void evaluate() throws Throwable { | |
Map<String, Field> map = new HashMap<String, Field>(); | |
for (Field f : testInstance.getClass().getDeclaredFields()) { | |
final boolean currentAccesible = f.isAccessible(); | |
f.setAccessible(true); | |
try { | |
Object o = f.get(self); | |
if (o == null) | |
continue; | |
if (o.getClass().isArray()) { | |
for (int i = 0; i < Array.getLength(o); i++) | |
eachEvaluate(self, Array.get(o, i)); | |
} else if (Iterable.class.isInstance(o)) { | |
for (Object ele : (Iterable<?>) o) | |
eachEvaluate(self, ele); | |
} else { | |
eachEvaluate(self, o); | |
} | |
} finally { | |
f.setAccessible(currentAccesible); | |
} | |
} | |
if (errors.isFailure()) | |
throw errors; | |
} | |
private void eachEvaluate(final Theories self, Object ele) | |
throws Throwable { | |
try { | |
self.current = cast(ele); | |
} catch (ClassCastException ignore) { | |
System.out.println("ignored"); | |
return; | |
} | |
try { | |
stmt.evaluate(); | |
} catch (Throwable t) { | |
self.errors.addErrors(t); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
private <R> R cast(Object o) { | |
return (R) o; | |
} | |
}; | |
} | |
public static class Errors extends AssertionError { | |
/***/ | |
private static final long serialVersionUID = 1L; | |
private List<Throwable> errs = new ArrayList<Throwable>(); | |
public Errors(String msg) { | |
super(msg); | |
} | |
public void addErrors(Throwable t) { | |
errs.add(t); | |
} | |
public boolean isFailure() { | |
return !errs.isEmpty(); | |
} | |
@Override | |
public void printStackTrace(PrintStream s) { | |
s.println(this.getMessage()); | |
s.println("======================================================="); | |
for (Throwable t : errs) { | |
t.printStackTrace(s); | |
s.println("======================================================="); | |
} | |
} | |
@Override | |
public void printStackTrace(PrintWriter s) { | |
s.println(this.getMessage()); | |
s.println("======================================================="); | |
for (Throwable t : errs) { | |
t.printStackTrace(s); | |
s.println("======================================================="); | |
} | |
} | |
} | |
} | |
--------- | |
package com.github.tauty.rufa.rules; | |
import static org.junit.Assert.*; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.experimental.runners.Enclosed; | |
import org.junit.runner.RunWith; | |
@RunWith(Enclosed.class) | |
public class TheoriesTest { | |
public static class ManyString { | |
@Rule | |
Theories rufa = new Theories(this); | |
@Test | |
public void test() { | |
// System.out.println(rufa.get()); | |
fail("Not yet implemented"); | |
} | |
// @Rule | |
// @SuppressWarnings({ "unused", "unchecked" }) | |
// public Theories<String> rufa = new Theories<String>() { | |
// int dummy = 100; | |
// | |
// Object value = "The list of sea foods"; | |
// | |
// String[] values = { "tako", "ika", "namako", "sakana" }; | |
// | |
// List<?> list = Arrays.asList("octopus", 8, "squid", 10, | |
// "sea cucumber", 0, "fish", 0); | |
// }; | |
// | |
} | |
} | |
----------- | |
package com.github.tauty.rufa; | |
public class Rufa { | |
// public abstract static class FR<R>{ | |
// private final Class<R> clazz; | |
// | |
// public FR(Class<R> clazz){ | |
// this.clazz = clazz; | |
// } | |
// | |
// public Class<R> getClazz() { | |
// return clazz; | |
// } | |
// | |
// public abstract R invoke(); | |
// } | |
public interface F<R> { | |
R invoke(); | |
} | |
// public static <R> R loadOrInvoke(FR<R> f){ | |
// String callerName = getCaller(); | |
// System.out.println(callerName); | |
// return f.invoke(); | |
// } | |
public static <R> R loadOrInvoke(Class<R> clazz, F<R> f) { | |
String callerName = getCaller(); | |
System.out.println(callerName); | |
return f.invoke(); | |
} | |
private static String getCaller() { | |
StackTraceElement[] elements = new Throwable().getStackTrace(); | |
return elements[2].getClassName() + "." + elements[2].getMethodName(); | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
String s = loadOrInvoke(String.class, new F<String>() { | |
public String invoke() { | |
return "tako"; | |
} | |
}); | |
System.out.println(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment