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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, CloudBees, 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.model.AbstractBuild;
29 import hudson.model.BuildListener;
30 import hudson.model.InvisibleAction;
31 import hudson.model.User;
32 import hudson.scm.ChangeLogParser;
33 import hudson.scm.ChangeLogSet;
34 import hudson.scm.ChangeLogSet.Entry;
35 import hudson.scm.NullSCM;
36 import org.xml.sax.SAXException;
37
38 import java.io.File;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.Iterator;
44 import java.util.List;
45
46 /**
47 * Fake SCM implementation that can report arbitrary commits from arbitrary users.
48 *
49 * @author Kohsuke Kawaguchi
50 */
51 public class FakeChangeLogSCM extends NullSCM {
52
53 /**
54 * Changes to be reported in the next build.
55 */
56 private List<EntryImpl> entries = new ArrayList<EntryImpl>();
57
58 public EntryImpl addChange() {
59 EntryImpl e = new EntryImpl();
60 entries.add(e);
61 return e;
62 }
63
64 @Override
65 public boolean checkout(AbstractBuild<?, ?> build, Launcher launcher, FilePath remoteDir, BuildListener listener, File changeLogFile) throws IOException, InterruptedException {
66 new FilePath(changeLogFile).touch(0);
67 build.addAction(new ChangelogAction(entries));
68 entries = new ArrayList<EntryImpl>();
69 return true;
70 }
71
72 @Override
73 public ChangeLogParser createChangeLogParser() {
74 return new FakeChangeLogParser();
75 }
76
77 public static class ChangelogAction extends InvisibleAction {
78 private final List<EntryImpl> entries;
79
80 public ChangelogAction(List<EntryImpl> entries) {
81 this.entries = entries;
82 }
83 }
84
85 public static class FakeChangeLogParser extends ChangeLogParser {
86 @SuppressWarnings("rawtypes")
87 @Override
88 public FakeChangeLogSet parse(AbstractBuild build, File changelogFile) throws IOException, SAXException {
89 return new FakeChangeLogSet(build, build.getAction(ChangelogAction.class).entries);
90 }
91 }
92
93 public static class FakeChangeLogSet extends ChangeLogSet<EntryImpl> {
94 private List<EntryImpl> entries;
95
96 public FakeChangeLogSet(AbstractBuild<?, ?> build, List<EntryImpl> entries) {
97 super(build);
98 this.entries = entries;
99 }
100
101 @Override
102 public boolean isEmptySet() {
103 return entries.isEmpty();
104 }
105
106 public Iterator<EntryImpl> iterator() {
107 return entries.iterator();
108 }
109 }
110
111 public static class EntryImpl extends Entry {
112 private String msg = "some commit message";
113 private String author = "someone";
114
115 public EntryImpl withAuthor(String author) {
116 this.author = author;
117 return this;
118 }
119
120 public EntryImpl withMsg(String msg) {
121 this.msg = msg;
122 return this;
123 }
124
125 @Override
126 public String getMsg() {
127 return msg;
128 }
129
130 @Override
131 public User getAuthor() {
132 return User.get(author);
133 }
134
135 @Override
136 public Collection<String> getAffectedPaths() {
137 return Collections.singleton("path");
138 }
139 }
140 }
141