private GroovyShell createEngine(AbstractBuild<?, ?> build, TaskListener listener) {

    ClassLoader cl = Jenkins.getInstance().getPluginManager().uberClassLoader;
    ScriptSandbox sandbox = null;
    CompilerConfiguration cc = new CompilerConfiguration();
    cc.addCompilationCustomizers(
        new ImportCustomizer()
            .addStarImports("jenkins", "jenkins.model", "hudson", "hudson.model"));

    ExtendedEmailPublisher publisher =
        build.getProject().getPublishersList().get(ExtendedEmailPublisher.class);
    if (publisher.getDescriptor().isSecurityEnabled()) {
      cc.addCompilationCustomizers(new SandboxTransformer());
      sandbox = new ScriptSandbox();
    }

    Binding binding = new Binding();
    binding.setVariable("build", build);
    binding.setVariable("project", build.getParent());
    binding.setVariable("rooturl", publisher.getDescriptor().getHudsonUrl());
    binding.setVariable("out", listener.getLogger());

    GroovyShell shell = new GroovyShell(cl, binding, cc);

    if (sandbox != null) {
      sandbox.register();
    }

    return shell;
  }
  @Override
  public Run<?, ?> getFirstIncludedRun(Run<?, ?> run, TaskListener listener) {
    Run<?, ?> firstIncludedBuild = run;

    Run<?, ?> prev = ExtendedEmailPublisher.getPreviousRun(firstIncludedBuild, listener);
    while (prev != null && prev.getResult().isWorseThan(Result.UNSTABLE)) {
      firstIncludedBuild = prev;
      prev = ExtendedEmailPublisher.getPreviousRun(firstIncludedBuild, listener);
    }

    return firstIncludedBuild;
  }
        @Override
        protected void before() throws Throwable {
          super.before();
          publisher = new ExtendedEmailPublisher();
          publisher.defaultSubject = "%DEFAULT_SUBJECT";
          publisher.defaultContent = "%DEFAULT_CONTENT";
          publisher.attachmentsPattern = "";
          publisher.recipientList = "%DEFAULT_RECIPIENTS";
          publisher.presendScript = "";

          project = createFreeStyleProject();
          project.getPublishersList().add(publisher);
        }
  @Test
  public void testTriggerName() throws Exception {
    List<RecipientProvider> recProviders = Collections.emptyList();
    PreBuildTrigger trigger =
        new PreBuildTrigger(
            recProviders,
            "$DEFAULT_RECIPIENTS",
            "$DEFAULT_REPLYTO",
            "$DEFAULT_SUBJECT",
            "$DEFAULT_CONTENT",
            "",
            0,
            "project");
    addEmailType(trigger);
    publisher.getConfiguredTriggers().add(trigger);

    FreeStyleBuild build = project.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(build);

    assertThat(
        "Email should have been triggered, so we should see it in the logs.",
        build.getLog(100),
        hasItems("Email was triggered for: " + PreBuildTrigger.TRIGGER_NAME));
    assertEquals(1, Mailbox.get("*****@*****.**").size());
    Message message = Mailbox.get("*****@*****.**").get(0);
    assertEquals(PreBuildTrigger.TRIGGER_NAME, message.getSubject());
  }
  @Override
  public boolean trigger(AbstractBuild<?, ?> build, TaskListener listener) {
    AbstractBuild<?, ?> previousBuild = ExtendedEmailPublisher.getPreviousBuild(build, listener);
    if (previousBuild == null) return build.getResult() == Result.FAILURE;

    if (build.getAction(AbstractTestResultAction.class) == null) return false;

    // if previous run didn't have test results and this one does (with failures)
    if (previousBuild.getAction(AbstractTestResultAction.class) == null)
      return build.getAction(AbstractTestResultAction.class).getFailCount() > 0;

    // if more tests failed during this run
    if (build.getAction(AbstractTestResultAction.class).getFailCount()
        > previousBuild.getAction(AbstractTestResultAction.class).getFailCount()) return true;

    // if any test failed this time, but not last time
    for (Object result : build.getAction(AbstractTestResultAction.class).getFailedTests()) {
      CaseResult res = (CaseResult) result;
      if (res.getAge() == 1) return true;
    }

    return false;
  }