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

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 */
24 package org.jvnet.hudson.test;
25
26 import hudson.FilePath;
27 import hudson.Launcher;
28 import hudson.Util;
29 import hudson.model.AbstractBuild;
30 import hudson.model.BuildListener;
31 import hudson.scm.ChangeLogParser;
32 import hudson.scm.NullSCM;
33 import hudson.scm.SCM;
34
35 import java.io.File;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.PrintStream;
39 import java.net.URL;
40 import java.util.zip.ZipEntry;
41 import java.util.zip.ZipInputStream;
42
43 /**
44 * {@link SCM} useful for testing that extracts the given resource as a zip file.
45 *
46 * @author Kohsuke Kawaguchi
47 */
48 public class ExtractResourceWithChangesSCM extends NullSCM {
49 private final URL firstZip;
50 private final URL secondZip;
51 private final String moduleRoot;
52
53 public ExtractResourceWithChangesSCM(URL firstZip, URL secondZip) {
54 if ((firstZip == null) || (secondZip == null))
55 throw new IllegalArgumentException();
56 this.firstZip = firstZip;
57 this.secondZip = secondZip;
58 this.moduleRoot = null;
59 }
60
61 public ExtractResourceWithChangesSCM(URL firstZip, URL secondZip, String moduleRoot) {
62 if ((firstZip == null) || (secondZip == null))
63 throw new IllegalArgumentException();
64 this.firstZip = firstZip;
65 this.secondZip = secondZip;
66 this.moduleRoot = moduleRoot;
67 }
68
69 @Override
70 public FilePath getModuleRoot(FilePath workspace) {
71 if (moduleRoot!=null) {
72 return workspace.child(moduleRoot);
73 }
74 return workspace;
75 }
76
77 @Override
78 public boolean checkout(AbstractBuild<?,?> build, Launcher launcher, FilePath workspace, BuildListener listener, File changeLogFile) throws IOException, InterruptedException {
79 if (workspace.exists()) {
80 listener.getLogger().println("Deleting existing workspace " + workspace.getRemote());
81 workspace.deleteRecursive();
82 }
83 listener.getLogger().println("Staging first zip: " + firstZip);
84 workspace.unzipFrom(firstZip.openStream());
85 listener.getLogger().println("Staging second zip: " + secondZip);
86 workspace.unzipFrom(secondZip.openStream());
87
88 // Get list of files changed in secondZip.
89 ZipInputStream zip = new ZipInputStream(secondZip.openStream());
90 ZipEntry e;
91 ExtractChangeLogParser.ExtractChangeLogEntry changeLog = new ExtractChangeLogParser.ExtractChangeLogEntry(secondZip.toString());
92
93 try {
94 while ((e = zip.getNextEntry()) != null) {
95 if (!e.isDirectory())
96 changeLog.addFile(new ExtractChangeLogParser.FileInZip(e.getName()));
97 }
98 }
99 finally {
100 zip.close();
101 }
102 saveToChangeLog(changeLogFile, changeLog);
103
104 return true;
105 }
106
107 @Override
108 public ChangeLogParser createChangeLogParser() {
109 return new ExtractChangeLogParser();
110 }
111
112 private static String escapeForXml(String string) {
113 return Util.xmlEscape(Util.fixNull(string));
114 }
115
116 public void saveToChangeLog(File changeLogFile, ExtractChangeLogParser.ExtractChangeLogEntry changeLog) throws IOException {
117 FileOutputStream outputStream = new FileOutputStream(changeLogFile);
118
119 PrintStream stream = new PrintStream(outputStream, false, "UTF-8");
120
121 stream.println("<?xml version='1.0' encoding='UTF-8'?>");
122 stream.println("<extractChanges>");
123 stream.println("<entry>");
124 stream.println("<zipFile>" + escapeForXml(changeLog.getZipFile()) + "</zipFile>");
125 stream.println("<file>");
126
127 for (String fileName : changeLog.getAffectedPaths()) {
128 stream.println("<fileName>" + escapeForXml(fileName) + "</fileName>");
129 }
130
131 stream.println("</file>");
132 stream.println("</entry>");
133 stream.println("</extractChanges>");
134
135 stream.close();
136 }
137
138 /**
139 * Don't write 'this', so that subtypes can be implemented as anonymous class.
140 */
141 private Object writeReplace() { return new Object(); }
142 }
143