package jetbrains.mps.testbench.junit;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import java.util.Comparator;
import org.junit.runner.Description;
import java.lang.reflect.Method;

/**
 * fyodor, Sep 6, 2010
 */
@GeneratedClass(node = "r:7eb6d2ae-f266-42d4-885f-016951b158e4(jetbrains.mps.testbench.junit)/5294483648489412039", model = "r:7eb6d2ae-f266-42d4-885f-016951b158e4(jetbrains.mps.testbench.junit)")
public class OrderComparator implements Comparator<Description> {
  public OrderComparator() {
  }
  @Override
  public int compare(Description a, Description b) {
    if (a.getTestClass() != b.getTestClass()) {
      return a.getTestClass().getName().compareTo(b.getTestClass().getName());
    }
    if (a.getMethodName().equals(b.getMethodName())) {
      return 0;
    }
    Order oaa = a.getAnnotation(Order.class);
    int orderA = (oaa != null ? oaa.value() : -1);
    Order oab = b.getAnnotation(Order.class);
    int orderB = (oab != null ? oab.value() : -1);
    if (orderA >= 0 || orderB >= 0) {
      return orderA - orderB;
    }
    // default order
    for (Method m : a.getTestClass().getMethods()) {
      if (m.getName().equals(a.getMethodName())) {
        return -1;
      } else
      if (m.getName().equals(b.getMethodName())) {
        return 1;
      }
    }

    throw new IllegalArgumentException("Method(s) not found : either " + a + " or " + b);
  }
}
