forgotten files
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/54601f19 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/54601f19 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/54601f19 Branch: refs/heads/cassandra-1.1 Commit: 54601f1927a70ef11bf5707b65cf08f81f070873 Parents: bf8c1ce Author: Brandon Williams <[email protected]> Authored: Wed Mar 6 16:23:35 2013 -0600 Committer: Brandon Williams <[email protected]> Committed: Wed Mar 6 16:23:35 2013 -0600 ---------------------------------------------------------------------- .../org/apache/cassandra/MethodComparator.java | 146 +++++++++++++++ .../apache/cassandra/OrderedJUnit4ClassRunner.java | 34 ++++ 2 files changed, 180 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/54601f19/test/unit/org/apache/cassandra/MethodComparator.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/MethodComparator.java b/test/unit/org/apache/cassandra/MethodComparator.java new file mode 100644 index 0000000..690ae57 --- /dev/null +++ b/test/unit/org/apache/cassandra/MethodComparator.java @@ -0,0 +1,146 @@ +package org.apache.cassandra; + +import org.junit.Ignore; +import org.junit.runners.model.FrameworkMethod; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.LineNumberReader; +import java.lang.reflect.Method; +import java.util.Comparator; + +public class MethodComparator<T> implements Comparator<T> +{ + private static final char[] METHOD_SEPARATORS = {1, 7}; + + private MethodComparator() + { + } + + public static MethodComparator<FrameworkMethod> getFrameworkMethodComparatorForJUnit4() + { + return new MethodComparator<FrameworkMethod>(); + } + + @Override + public int compare(T o1, T o2) + { + final MethodPosition methodPosition1 = this.getIndexOfMethodPosition(o1); + final MethodPosition methodPosition2 = this.getIndexOfMethodPosition(o2); + return methodPosition1.compareTo(methodPosition2); + } + + private MethodPosition getIndexOfMethodPosition(final Object method) + { + if (method instanceof FrameworkMethod) + { + return this.getIndexOfMethodPosition((FrameworkMethod) method); + } + else if (method instanceof Method) + { + return this.getIndexOfMethodPosition((Method) method); + } + else + { + return new NullMethodPosition(); + } + } + + private MethodPosition getIndexOfMethodPosition(final FrameworkMethod frameworkMethod) + { + return getIndexOfMethodPosition(frameworkMethod.getMethod()); + } + + private MethodPosition getIndexOfMethodPosition(final Method method) + { + final Class aClass = method.getDeclaringClass(); + if (method.getAnnotation(Ignore.class) == null) + { + return getIndexOfMethodPosition(aClass, method.getName()); + } + else + { + return new NullMethodPosition(); + } + } + + private MethodPosition getIndexOfMethodPosition(final Class aClass, final String methodName) + { + MethodPosition methodPosition; + for (final char methodSeparator : METHOD_SEPARATORS) + { + methodPosition = getIndexOfMethodPosition(aClass, methodName, methodSeparator); + if (!(methodPosition instanceof NullMethodPosition)) + { + return methodPosition; + } + } + return new NullMethodPosition(); + } + + private MethodPosition getIndexOfMethodPosition(final Class aClass, final String methodName, final char methodSeparator) + { + final InputStream inputStream = aClass.getResourceAsStream(aClass.getSimpleName() + ".class"); + final LineNumberReader lineNumberReader = new LineNumberReader(new InputStreamReader(inputStream)); + final String methodNameWithSeparator = methodName + methodSeparator; + try + { + try + { + String line; + while ((line = lineNumberReader.readLine()) != null) + { + if (line.contains(methodNameWithSeparator)) + { + return new MethodPosition(lineNumberReader.getLineNumber(), line.indexOf(methodNameWithSeparator)); + } + } + } + finally + { + lineNumberReader.close(); + } + } + catch (IOException e) + { + return new NullMethodPosition(); + } + return new NullMethodPosition(); + } + + private static class MethodPosition implements Comparable<MethodPosition> + { + private final Integer lineNumber; + private final Integer indexInLine; + + public MethodPosition(int lineNumber, int indexInLine) + { + this.lineNumber = lineNumber; + this.indexInLine = indexInLine; + } + + @Override + public int compareTo(MethodPosition o) + { + + // If line numbers are equal, then compare by indexes in this line. + if (this.lineNumber.equals(o.lineNumber)) + { + return this.indexInLine.compareTo(o.indexInLine); + } + else + { + return this.lineNumber.compareTo(o.lineNumber); + } + } + } + + private static class NullMethodPosition extends MethodPosition + { + public NullMethodPosition() + { + super(-1, -1); + } + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/54601f19/test/unit/org/apache/cassandra/OrderedJUnit4ClassRunner.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/OrderedJUnit4ClassRunner.java b/test/unit/org/apache/cassandra/OrderedJUnit4ClassRunner.java new file mode 100644 index 0000000..d84aedb --- /dev/null +++ b/test/unit/org/apache/cassandra/OrderedJUnit4ClassRunner.java @@ -0,0 +1,34 @@ +package org.apache.cassandra; + +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.InitializationError; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class OrderedJUnit4ClassRunner extends BlockJUnit4ClassRunner +{ + + public OrderedJUnit4ClassRunner(Class aClass) throws InitializationError + { + super(aClass); + } + + @Override + protected List<FrameworkMethod> computeTestMethods() + { + final List<FrameworkMethod> list = super.computeTestMethods(); + try + { + final List<FrameworkMethod> copy = new ArrayList<FrameworkMethod>(list); + Collections.sort(copy, MethodComparator.getFrameworkMethodComparatorForJUnit4()); + return copy; + } + catch (Throwable throwable) + { + return list; + } + } +} \ No newline at end of file
