Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this and the previous commit constitutes the fix. Also added a test case. Originally-Committed-As: e761d80f4d53a0f368fa8e9fbb06f01db247f8cc
- Loading branch information
Showing
1 changed file
with
142 additions
and
0 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
test/src/main/java/org/jvnet/hudson/test/FakeChangeLogSCM.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2011, CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.jvnet.hudson.test; | ||
|
||
import hudson.FilePath; | ||
import hudson.Launcher; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.AbstractProject; | ||
import hudson.model.BuildListener; | ||
import hudson.model.InvisibleAction; | ||
import hudson.model.JobProperty; | ||
import hudson.model.User; | ||
import hudson.scm.ChangeLogParser; | ||
import hudson.scm.ChangeLogSet; | ||
import hudson.scm.ChangeLogSet.Entry; | ||
import hudson.scm.NullSCM; | ||
import org.xml.sax.SAXException; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
/** | ||
* Fake SCM implementation that can report arbitrary commits from arbitrary users. | ||
* | ||
* @author Kohsuke Kawaguchi | ||
*/ | ||
public class FakeChangeLogSCM extends NullSCM { | ||
|
||
/** | ||
* Changes to be reported in the next build. | ||
*/ | ||
private List<EntryImpl> entries = new ArrayList<EntryImpl>(); | ||
|
||
public EntryImpl addChange() { | ||
EntryImpl e = new EntryImpl(); | ||
entries.add(e); | ||
return e; | ||
} | ||
|
||
@Override | ||
public boolean checkout(AbstractBuild<?, ?> build, Launcher launcher, FilePath remoteDir, BuildListener listener, File changeLogFile) throws IOException, InterruptedException { | ||
new FilePath(changeLogFile).touch(0); | ||
build.addAction(new ChangelogAction(entries)); | ||
entries = new ArrayList<EntryImpl>(); | ||
return true; | ||
} | ||
|
||
@Override | ||
public ChangeLogParser createChangeLogParser() { | ||
return new FakeChangeLogParser(); | ||
} | ||
|
||
public static class ChangelogAction extends InvisibleAction { | ||
private final List<EntryImpl> entries; | ||
|
||
public ChangelogAction(List<EntryImpl> entries) { | ||
this.entries = entries; | ||
} | ||
} | ||
|
||
public static class FakeChangeLogParser extends ChangeLogParser { | ||
@Override | ||
public FakeChangeLogSet parse(AbstractBuild build, File changelogFile) throws IOException, SAXException { | ||
return new FakeChangeLogSet(build, build.getAction(ChangelogAction.class).entries); | ||
} | ||
} | ||
|
||
public static class FakeChangeLogSet extends ChangeLogSet<EntryImpl> { | ||
private List<EntryImpl> entries; | ||
|
||
public FakeChangeLogSet(AbstractBuild<?, ?> build, List<EntryImpl> entries) { | ||
super(build); | ||
this.entries = entries; | ||
} | ||
|
||
@Override | ||
public boolean isEmptySet() { | ||
return entries.isEmpty(); | ||
} | ||
|
||
public Iterator<EntryImpl> iterator() { | ||
return entries.iterator(); | ||
} | ||
} | ||
|
||
public static class EntryImpl extends Entry { | ||
private String msg = "some commit message"; | ||
private String author = "someone"; | ||
|
||
public EntryImpl withAuthor(String author) { | ||
this.author = author; | ||
return this; | ||
} | ||
|
||
public EntryImpl withMsg(String msg) { | ||
this.msg = msg; | ||
return this; | ||
} | ||
|
||
@Override | ||
public String getMsg() { | ||
return msg; | ||
} | ||
|
||
@Override | ||
public User getAuthor() { | ||
return User.get(author); | ||
} | ||
|
||
@Override | ||
public Collection<String> getAffectedPaths() { | ||
return Collections.singleton("path"); | ||
} | ||
} | ||
} |