PageRenderTime 29ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-test-framework/src/main/java/org/jvnet/hudson/test/SingleFileSCM.java

http://github.com/hudson/hudson
Java | 81 lines | 38 code | 9 blank | 34 comment | 0 complexity | 031f9a399927fd096f683c5a6d90e0ad MD5 | raw file
 1/*
 2 * The MIT License
 3 * 
 4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe
 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 org.jvnet.hudson.test;
25
26import hudson.scm.NullSCM;
27import hudson.scm.SCM;
28import hudson.model.AbstractBuild;
29import hudson.model.BuildListener;
30import hudson.Launcher;
31import hudson.FilePath;
32
33import java.io.File;
34import java.io.IOException;
35import java.io.OutputStream;
36import java.io.UnsupportedEncodingException;
37import java.net.URL;
38
39import org.apache.commons.io.IOUtils;
40
41/**
42 * {@link SCM} useful for testing that puts just one file in the workspace.
43 *
44 * @author Kohsuke Kawaguchi
45 */
46public class SingleFileSCM extends NullSCM {
47    private final String path;
48    private final byte[] contents;
49
50    public SingleFileSCM(String path, byte[] contents) {
51        this.path = path;
52        this.contents = contents;
53    }
54
55    public SingleFileSCM(String path, String contents) throws UnsupportedEncodingException {
56        this.path = path;
57        this.contents = contents.getBytes("UTF-8");
58    }
59
60    /**
61     * When a check out is requested, serve the contents of the URL and place it with the given path name. 
62     */
63    public SingleFileSCM(String path, URL resource) throws IOException {
64        this.path = path;
65        this.contents = IOUtils.toByteArray(resource.openStream());
66    }
67
68    @Override
69    public boolean checkout(AbstractBuild build, Launcher launcher, FilePath workspace, BuildListener listener, File changeLogFile) throws IOException, InterruptedException {
70        listener.getLogger().println("Staging "+path);
71        OutputStream os = workspace.child(path).write();
72        IOUtils.write(contents, os);
73        os.close();
74        return true;
75    }
76
77    /**
78     * Don't write 'this', so that subtypes can be implemented as anonymous class.
79     */
80    private Object writeReplace() { return new Object(); }
81}