File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\main\java\org\jvnet\hudson\test\SingleFileSCM.java.html

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 */
24 package org.jvnet.hudson.test;
25
26 import hudson.scm.NullSCM;
27 import hudson.scm.SCM;
28 import hudson.model.AbstractBuild;
29 import hudson.model.BuildListener;
30 import hudson.Launcher;
31 import hudson.FilePath;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.OutputStream;
36 import java.io.UnsupportedEncodingException;
37 import java.net.URL;
38
39 import 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 */
46 public 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 }
82