PageRenderTime 27ms CodeModel.GetById 2ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/tasks/test/DefaultTestResultParserImpl.java

http://github.com/hudson/hudson
Java | 119 lines | 49 code | 12 blank | 58 comment | 5 complexity | 9a3b29abb7cc4c34a350de49861a4741 MD5 | raw file
  1/*
  2 * The MIT License
  3 *
  4 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
  5 *
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 *
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 *
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.tasks.test;
 25
 26import hudson.AbortException;
 27import hudson.FilePath;
 28import hudson.FilePath.FileCallable;
 29import hudson.Launcher;
 30import hudson.Util;
 31import hudson.model.AbstractBuild;
 32import hudson.model.TaskListener;
 33import hudson.remoting.VirtualChannel;
 34
 35import java.io.File;
 36import java.io.IOException;
 37import java.io.Serializable;
 38import java.util.ArrayList;
 39import java.util.List;
 40
 41/**
 42 * Default partial implementation of {@link TestResultParser} that handles GLOB dereferencing
 43 * and other checks for user errors, such as misconfigured GLOBs, up-to-date checks on test reports.
 44 *
 45 * <p>
 46 * The instance of the parser will be serialized to the node that performed the build and the parsing will be done
 47 * remotely on that slave.
 48 *
 49 * @since 1.343
 50 * @author Kohsuke Kawaguchi
 51 */
 52public abstract class DefaultTestResultParserImpl extends TestResultParser implements Serializable {
 53    /**
 54     * This method is executed on the slave that has the report files to parse test reports and builds {@link TestResult}.
 55     *
 56     * @param reportFiles
 57     *      List of files to be parsed. Never be empty nor null.
 58     * @param launcher
 59     *      Can be used to fork processes on the machine where the build is running. Never null.
 60     * @param listener
 61     *      Use this to report progress and other problems. Never null.
 62     *
 63     * @throws InterruptedException
 64     *      If the user cancels the build, it will be received as a thread interruption. Do not catch
 65     *      it, and instead just forward that through the call stack.
 66     * @throws IOException
 67     *      If you don't care about handling exceptions gracefully, you can just throw IOException
 68     *      and let the default exception handling in Hudson takes care of it.
 69     * @throws AbortException
 70     *      If you encounter an error that you handled gracefully, throw this exception and Hudson
 71     *      will not show a stack trace.
 72     */
 73    protected abstract TestResult parse(List<File> reportFiles, Launcher launcher, TaskListener listener) throws InterruptedException, IOException;
 74
 75    @Override
 76    public TestResult parse(final String testResultLocations, final AbstractBuild build, final Launcher launcher, final TaskListener listener) throws InterruptedException, IOException {
 77        return build.getWorkspace().act(new FileCallable<TestResult>() {
 78            final boolean ignoreTimestampCheck = IGNORE_TIMESTAMP_CHECK; // so that the property can be set on the master
 79            final long buildTime = build.getTimestamp().getTimeInMillis();
 80            final long nowMaster = System.currentTimeMillis();
 81
 82            public TestResult invoke(File dir, VirtualChannel channel) throws IOException, InterruptedException {
 83                final long nowSlave = System.currentTimeMillis();
 84
 85                // files older than this timestamp is considered stale
 86                long localBuildTime = buildTime + (nowSlave - nowMaster);
 87
 88                FilePath[] paths = new FilePath(dir).list(testResultLocations);
 89                if (paths.length==0)
 90                    throw new AbortException("No test reports that matches "+testResultLocations+" found. Configuration error?");
 91
 92                // since dir is local, paths all point to the local files
 93                List<File> files = new ArrayList<File>(paths.length);
 94                for (FilePath path : paths) {
 95                    File report = new File(path.getRemote());
 96                    if (ignoreTimestampCheck || localBuildTime - 3000 /*error margin*/ < report.lastModified()) {
 97                        // this file is created during this build
 98                        files.add(report);
 99                    }
100                }
101
102                if (files.isEmpty()) {
103                    // none of the files were new
104                    throw new AbortException(
105                        String.format(
106                        "Test reports were found but none of them are new. Did tests run? \n"+
107                        "For example, %s is %s old\n", paths[0].getRemote(),
108                        Util.getTimeSpanString(localBuildTime-paths[0].lastModified())));
109                }
110
111                return parse(files,launcher,listener);
112            }
113        });
114    }
115
116    private static final long serialVersionUID = 1L;
117
118    public static final boolean IGNORE_TIMESTAMP_CHECK = Boolean.getBoolean(TestResultParser.class.getName()+".ignoreTimestampCheck");
119}