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

org.ehoffman.test.aspects.BrokenAdvice Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package org.ehoffman.test.aspects;

import java.io.Closeable;
import java.io.IOException;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.ehoffman.test.Broken;

/**
 * 
 * @author rex
 */
public class BrokenAdvice implements MethodInterceptor, Closeable {

    private final Validator validator;
    private final boolean dontRun;
    private final boolean markFailedAnnotatedSkip;

    public BrokenAdvice(final boolean dontRun, final boolean markFailedAnnotatedSkip, final Validator validator) {
        this.dontRun = dontRun;
        this.markFailedAnnotatedSkip = markFailedAnnotatedSkip;
        this.validator = validator;
    }

    public boolean validated(final Broken broken) {
       return broken != null
            && validator.getError(broken.developer(), broken.issueInTracker()) == null;
    }

    @Override
    public Object invoke(final MethodInvocation invocation) throws Throwable {
        final Broken annotation = invocation.getMethod().getAnnotation(Broken.class);
        Object output = null;
        if (validated(annotation) && dontRun) {
            throw new SkipException(this);
        }
        try {
            output = invocation.proceed();
        } catch (final Throwable t) {
            if (validated(annotation) && markFailedAnnotatedSkip) {
                throw new SkipException(t, this);
            }
            if (annotation != null && !validated(annotation)) {
                throw new IllegalArgumentException(validator.getError(annotation.developer(), annotation.issueInTracker()), t);
            }
            throw t;
        }
        if (annotation != null && !validated(annotation)) {
            throw new IllegalArgumentException(validator.getError(annotation.developer(), annotation.issueInTracker()));
        }
        return output;
    }

    @Override
    public void close() throws IOException {
        validator.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy