File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\main\java\org\jvnet\hudson\test\ExtractChangeLogParser.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.model.AbstractBuild;
27 import hudson.model.User;
28 import hudson.scm.ChangeLogParser;
29 import hudson.scm.ChangeLogSet;
30 import org.apache.commons.digester.Digester;
31 import org.kohsuke.stapler.export.Exported;
32 import org.kohsuke.stapler.export.ExportedBean;
33 import org.xml.sax.SAXException;
34
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.List;
42
43 /**
44 * @author Andrew Bayer
45 */
46 public class ExtractChangeLogParser extends ChangeLogParser {
47 @SuppressWarnings("rawtypes")
48 @Override
49 public ExtractChangeLogSet parse(AbstractBuild build, File changeLogFile) throws IOException, SAXException {
50 if (changeLogFile.exists()) {
51 FileInputStream fis = new FileInputStream(changeLogFile);
52 ExtractChangeLogSet logSet = parse(build, fis);
53 fis.close();
54 return logSet;
55 } else {
56 return new ExtractChangeLogSet(build, new ArrayList<ExtractChangeLogEntry>());
57 }
58 }
59
60 @SuppressWarnings("rawtypes")
61 public ExtractChangeLogSet parse(AbstractBuild build, InputStream changeLogStream) throws IOException, SAXException {
62
63 ArrayList<ExtractChangeLogEntry> changeLog = new ArrayList<ExtractChangeLogEntry>();
64
65 Digester digester = new Digester();
66 digester.setClassLoader(ExtractChangeLogSet.class.getClassLoader());
67 digester.push(changeLog);
68 digester.addObjectCreate("*/extractChanges/entry", ExtractChangeLogEntry.class);
69
70 digester.addBeanPropertySetter("*/extractChanges/entry/zipFile");
71
72 digester.addObjectCreate("*/extractChanges/entry/file",
73 FileInZip.class);
74 digester.addBeanPropertySetter("*/extractChanges/entry/file/fileName");
75 digester.addSetNext("*/extractChanges/entry/file", "addFile");
76 digester.addSetNext("*/extractChanges/entry", "add");
77
78 digester.parse(changeLogStream);
79
80 return new ExtractChangeLogSet(build, changeLog);
81 }
82
83
84 @ExportedBean(defaultVisibility = 999)
85 public static class ExtractChangeLogEntry extends ChangeLogSet.Entry {
86 private List<FileInZip> files = new ArrayList<FileInZip>();
87 private String zipFile;
88
89 public ExtractChangeLogEntry() {
90 }
91
92 public ExtractChangeLogEntry(String zipFile) {
93 this.zipFile = zipFile;
94 }
95
96 public void setZipFile(String zipFile) {
97 this.zipFile = zipFile;
98 }
99
100 @Exported
101 public String getZipFile() {
102 return zipFile;
103 }
104
105 @Override
106 public void setParent(ChangeLogSet parent) {
107 super.setParent(parent);
108 }
109
110 @Override
111 public Collection<String> getAffectedPaths() {
112 Collection<String> paths = new ArrayList<String>(files.size());
113 for (FileInZip file : files) {
114 paths.add(file.getFileName());
115 }
116 return paths;
117 }
118
119 @Override
120 @Exported
121 public User getAuthor() {
122 return User.get("testuser");
123 }
124
125 @Override
126 @Exported
127 public String getMsg() {
128 return "Extracted from " + zipFile;
129 }
130
131 public void addFile(FileInZip fileName) {
132 files.add(fileName);
133 }
134
135 public void addFiles(Collection<FileInZip> fileNames) {
136 this.files.addAll(fileNames);
137 }
138 }
139
140 @ExportedBean(defaultVisibility = 999)
141 public static class FileInZip {
142 private String fileName = "";
143
144 public FileInZip() {
145 }
146
147 public FileInZip(String fileName) {
148 this.fileName = fileName;
149 }
150
151 @Exported
152 public String getFileName() {
153 return fileName;
154 }
155
156 public void setFileName(String fileName) {
157 this.fileName = fileName;
158 }
159 }
160
161 }
162