Package net.jangaroo.hudson

Source Code of net.jangaroo.hudson.JooTestArchiver$FactoryImpl

/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jason Chaffee, Maciek Starzyk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.jangaroo.hudson;

import hudson.Extension;
import hudson.Util;
import hudson.maven.MavenBuild;
import hudson.maven.MavenBuildProxy;
import hudson.maven.MavenBuildProxy.BuildCallable;
import hudson.maven.MavenBuilder;
import hudson.maven.MavenModule;
import hudson.maven.MavenProjectActionBuilder;
import hudson.maven.MavenReporter;
import hudson.maven.MavenReporterDescriptor;
import hudson.maven.MojoInfo;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.tasks.junit.TestResult;
import hudson.tasks.test.TestResultProjectAction;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

/**
* Records the surefire test result.
*
* @author Kohsuke Kawaguchi
*/
public class JooTestArchiver extends MavenReporter {
  private TestResult result;

  public boolean preExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener) throws InterruptedException, IOException {
    if (isJooTest(mojo)) {
      // tell surefire:test to keep going even if there was a failure,
      // so that we can record this as yellow.
      // note that because of the way Maven works, just updating system property at this point is too late
      XmlPlexusConfiguration c = (XmlPlexusConfiguration) mojo.configuration.getChild("testFailureIgnore");
      if (c != null && c.getValue().equals("${maven.test.failure.ignore}") && System.getProperty("maven.test.failure.ignore") == null) {
        c.setValue("true");
      }
    }
    return true;
  }

  @SuppressWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
  public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, final BuildListener listener, Throwable error) throws InterruptedException, IOException {
    if (!isJooTest(mojo)) {
      return true;
    }
    File reportsDir;
    try {
      reportsDir = mojo.getConfigurationValue("testResultOutputDirectory", File.class);
    } catch (ComponentConfigurationException e) {
      e.printStackTrace(listener.fatalError("kein testResultOutputDirectory gefunden"));
      build.setResult(Result.FAILURE);
      return true;
    }


    if (reportsDir.exists()) {
      // surefire:test just skips itself when the current project is not a java project

      FileSet fs = Util.createFileSet(reportsDir, "*.xml", "testng-results.xml,testng-failed.xml");
      DirectoryScanner ds = fs.getDirectoryScanner();

      if (ds.getIncludedFiles().length == 0)
      // no test in this module
      {
        return true;
      }

      if (result == null) {
        result = new TestResult();
      }
      result.parse(System.currentTimeMillis() - build.getMilliSecsSinceBuildStart(), ds);

      int failCount = build.execute(new BuildCallable<Integer, IOException>() { // NOSONAR this is ok for hudson
        public Integer call(MavenBuild build) throws IOException, InterruptedException {
          JooTestReport sr = build.getAction(JooTestReport.class);
          if (sr == null) {
            build.getActions().add(new JooTestReport(build, result, listener));
          } else {
            sr.setResult(result, listener);
          }
          if (result.getFailCount() > 0) {
            build.setResult(Result.UNSTABLE);
          }
          build.registerAsProjectAction(new FactoryImpl());
          return result.getFailCount();
        }
      });

      // if surefire plugin is going to kill maven because of a test failure,
      // intercept that (or otherwise build will be marked as failure)
      if (failCount > 0 && error instanceof MojoFailureException) {
        MavenBuilder.markAsSuccess = true; // NOSONAR
      }
    }

    return true;
  }

  /**
   * Up to 1.372, there was a bug that causes Hudson to persist {@link JooTestArchiver} with the entire test result
   * in it. If we are loading those, fix it up in memory to reduce the memory footprint.
   * <p/>
   * It'd be nice we can save the record to remove problematic portion, but that might have
   * additional side effect.
   */
  public static void fixUp(List<MavenProjectActionBuilder> builders) {
    if (builders == null) {
      return;
    }
    for (ListIterator<MavenProjectActionBuilder> itr = builders.listIterator(); itr.hasNext();) {
      MavenProjectActionBuilder b = itr.next();
      if (b instanceof JooTestArchiver) {
        itr.set(new FactoryImpl());
      }
    }
  }

  /**
   * Part of the serialization data attached to {@link MavenBuild}.
   */
  static final class FactoryImpl implements MavenProjectActionBuilder {
    public Collection<? extends Action> getProjectActions(MavenModule module) {
      return Collections.singleton(new TestResultProjectAction(module));
    }
  }

  private boolean isJooTest(MojoInfo mojo) {
    if (!mojo.is("net.jangaroo", "jangaroo-maven-plugin", "test")) {
      return false;
    }

    try {
      Boolean skip = mojo.getConfigurationValue("skip", Boolean.class);
      if (((skip != null) && (skip))) {
        return false;
      }
      Boolean skipTests = mojo.getConfigurationValue("skipTests", Boolean.class);
      if (((skipTests != null) && (skipTests))) {
        return false;
      }

    } catch (ComponentConfigurationException e) {
      return false;
    }

    return true;
  }

  @Extension
  public static final class DescriptorImpl extends MavenReporterDescriptor {
    public String getDisplayName() {
      return "JooTestArchiver";
    }

    public JooTestArchiver newAutoInstance(MavenModule module) {
      return new JooTestArchiver();
    }
  }

  private static final long serialVersionUID = 1L;
}
TOP

Related Classes of net.jangaroo.hudson.JooTestArchiver$FactoryImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.