package com.atlassian.bitbucket.test.runners;

import org.junit.runner.notification.RunNotifier;


public class FuncTestSplitterRunner extends ConditionalRunner {
    private final Integer buckets;
    private final Integer bucket;
    private final String suffix;

    public FuncTestSplitterRunner(Class<?> klass) throws Throwable {
        super(klass);

        buckets = Integer.getInteger("stash.test.func.buckets", 1);
        bucket = Integer.getInteger("stash.test.func.bucket", 0);
        suffix = System.getProperty("stash.test.func.suffix", "");
    }

    @Override
    public void run(RunNotifier notifier) {
        String className = getTestClass().getName();
        int hash = (className + suffix).hashCode();
        if (hash < 0) {
            hash *= -1;
        }
        int thisBucket = hash % buckets;
        if (thisBucket == bucket) {
            super.run(notifier);
        }
    }
}