All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.split.client.testing.runner.RunWithSplits Maven / Gradle / Ivy

There is a newer version: 4.13.0
Show newest version
package io.split.client.testing.runner;

import io.split.client.testing.SplitClientForTest;
import io.split.client.testing.annotations.SplitTestClient;
import org.junit.runners.model.Statement;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class RunWithSplits extends Statement {
    private final Object target;
    private final Scenario scenario;
    private final Statement next;

    public RunWithSplits(Scenario scenario, Object target, Statement next) {
        this.scenario = scenario;
        this.target = target;
        this.next = next;
    }

    @Override
    public void evaluate() throws Throwable {
        SplitClientForTest splitClient = findFirstSplitClient(target, target.getClass());

        // Preserve the Split state between Test runs
        Map priorTests = new HashMap<>(splitClient.tests());

        // Apply the Active Scenario for this
        if (scenario != null) {
            scenario.apply(splitClient);
        }

        try {
            next.evaluate();
        } finally {
            // Clear any Scenario specific changes and re-apply existing splits
            splitClient.clearTreatments();
            splitClient.registerTreatments(priorTests);
        }
    }

    private static SplitClientForTest findFirstSplitClient(Object target, Class type) throws IllegalAccessException {
        for (Field field : type.getDeclaredFields()) {
            if (field.isAnnotationPresent(SplitTestClient.class)
                    && field.getType().isAssignableFrom(SplitClientForTest.class)) {
                field.setAccessible(true); // should work on private fields
                if (field.get(target) instanceof SplitClientForTest) {
                    return (SplitClientForTest) field.get(target);
                }
            }
        }

        if (type.getSuperclass() != null) {
            return findFirstSplitClient(target, type.getSuperclass());
        } else {
            throw new IllegalArgumentException("No SplitTestClient found in hierarchy");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy