提交 a8ba1162 编写于 作者: J Jesse Glick

Introduced MockFolder for testing.

上级 e4ca47e9
......@@ -739,6 +739,14 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction {
return jenkins.createProject(MavenModuleSet.class,name);
}
/**
* Creates a simple folder that other jobs can be placed in.
* @since 1.494
*/
public MockFolder createFolder(String name) throws IOException {
return jenkins.createProject(MockFolder.class, name);
}
protected String createUniqueProjectName() {
return "test"+jenkins.getItems().size();
}
......
/*
* The MIT License
*
* Copyright 2012 Jesse Glick.
*
* 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.Extension;
import hudson.model.AbstractItem;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.ItemGroupMixIn;
import hudson.model.Job;
import hudson.model.TopLevelItem;
import hudson.model.TopLevelItemDescriptor;
import hudson.util.Function1;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.servlet.ServletException;
import jenkins.model.Jenkins;
import jenkins.model.ModifiableTopLevelItemGroup;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
/**
* Minimal implementation of a modifiable item group akin to the CloudBees Folders plugin.
* No UI, just enough implementation to test functionality of code which should deal with item full names, etc.
* @since 1.494
*/
@SuppressWarnings({"unchecked", "rawtypes"}) // the usual API mistakes
public class MockFolder extends AbstractItem implements ModifiableTopLevelItemGroup, TopLevelItem { // could be a ViewGroup too
private transient Map<String,TopLevelItem> items = new TreeMap<String,TopLevelItem>();
private MockFolder(ItemGroup parent, String name) {
super(parent, name);
}
@Override public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException {
super.onLoad(parent, name);
items = ItemGroupMixIn.loadChildren(this, jobs(), new Function1<String,TopLevelItem>() {
public String call(TopLevelItem item) {
return item.getName();
}
});
}
@Override public Collection<TopLevelItem> getItems() {
return items.values(); // could be filtered by Item.READ
}
@Override public TopLevelItem getItem(String name) {
return items.get(name);
}
@Override public Collection<? extends Job> getAllJobs() {
Set<Job> jobs = new HashSet<Job>();
for (TopLevelItem i : getItems()) {
jobs.addAll(i.getAllJobs());
}
return jobs;
}
private File jobs() {
return new File(getRootDir(), "jobs");
}
private ItemGroupMixIn mixin() {
return new ItemGroupMixIn(this, this) {
@Override protected void add(TopLevelItem item) {
items.put(item.getName(), item);
}
@Override protected File getRootDirFor(String name) {
return new File(jobs(), name);
}
};
}
@Override public <T extends TopLevelItem> T copy(T src, String name) throws IOException {
return mixin().copy(src, name);
}
@Override public TopLevelItem createProjectFromXML(String name, InputStream xml) throws IOException {
return mixin().createProjectFromXML(name, xml);
}
@Override public TopLevelItem createProject(TopLevelItemDescriptor type, String name, boolean notify) throws IOException {
return mixin().createProject(type, name, notify);
}
/** Convenience method to create a {@link FreeStyleProject} or similar. */
public <T extends TopLevelItem> T createProject(Class<T> type, String name) throws IOException {
return type.cast(createProject((TopLevelItemDescriptor) Jenkins.getInstance().getDescriptor(type), name, true));
}
@Override public TopLevelItem doCreateItem(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
return mixin().createTopLevelItem(req, rsp);
}
@Override public String getUrlChildPrefix() {
return "job";
}
@Override public File getRootDirFor(TopLevelItem child) {
return new File(jobs(), child.getName());
}
@Override public void onRenamed(TopLevelItem item, String oldName, String newName) throws IOException {
items.remove(oldName);
items.put(newName, item);
}
@Override public void onDeleted(TopLevelItem item) throws IOException {
// could call ItemListener.onDeleted
items.remove(item.getName());
}
@Override public TopLevelItemDescriptor getDescriptor() {
return Jenkins.getInstance().getDescriptorByType(DescriptorImpl.class);
}
@Extension public static class DescriptorImpl extends TopLevelItemDescriptor {
@Override public String getDisplayName() {
return "MockFolder";
}
@Override public TopLevelItem newInstance(ItemGroup parent, String name) {
return new MockFolder(parent, name);
}
}
}
/*
* The MIT License
*
* Copyright 2012 Jesse Glick.
*
* 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.model.FreeStyleProject;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
public class MockFolderTest {
@Rule public JenkinsRule j = new JenkinsRule();
@Test public void basics() throws Exception {
MockFolder dir = j.createFolder("dir");
FreeStyleProject p = dir.createProject(FreeStyleProject.class, "p");
assertEquals("dir/p", p.getFullName());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册