Skip to content
Permalink
Browse files
[FIXED JENKINS-17575] Redirecting properly after deleting jobs in fol…
…ders.

Expanded MockFolder to be a ViewGroup so that it is possible to test views inside folders,
and generally fixed it to service Stapler hierarchies properly so that WebClient-based tests can work.
Originally-Committed-As: 1205e9f6c492c53983a7f02926a53739b50b92fd
  • Loading branch information
jglick committed Apr 11, 2013
1 parent 0c96006 commit c2059409d3075b3dd0217b33b88e0427720c1667
Showing with 88 additions and 1 deletion.
  1. +88 −1 test/src/main/java/org/jvnet/hudson/test/MockFolder.java
@@ -26,37 +26,53 @@

import hudson.Extension;
import hudson.model.AbstractItem;
import hudson.model.Action;
import hudson.model.AllView;
import hudson.model.FreeStyleProject;
import hudson.model.Hudson;
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.model.View;
import hudson.model.ViewGroup;
import hudson.model.ViewGroupMixIn;
import hudson.util.Function1;
import hudson.views.DefaultViewsTabBar;
import hudson.views.ViewsTabBar;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
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.StaplerFallback;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.WebMethod;

/**
* 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
public class MockFolder extends AbstractItem implements ModifiableTopLevelItemGroup, TopLevelItem, ViewGroup, StaplerFallback {

private transient Map<String,TopLevelItem> items = new TreeMap<String,TopLevelItem>();
private final List<View> views = new ArrayList<View>(Collections.singleton(new AllView("All", this)));
private String primaryView;
private ViewsTabBar viewsTabBar;

private MockFolder(ItemGroup parent, String name) {
super(parent, name);
@@ -102,6 +118,20 @@ private ItemGroupMixIn mixin() {
};
}

private ViewGroupMixIn vgmixin() {
return new ViewGroupMixIn(this) {
@Override protected List<View> views() {
return views;
}
@Override protected String primaryView() {
return primaryView != null ? primaryView : views.get(0).getViewName();
}
@Override protected void primaryView(String newName) {
primaryView = newName;
}
};
}

@Override public <T extends TopLevelItem> T copy(T src, String name) throws IOException {
return mixin().copy(src, name);
}
@@ -145,6 +175,63 @@ private ItemGroupMixIn mixin() {
return Jenkins.getInstance().getDescriptorByType(DescriptorImpl.class);
}

public void addView(View view) throws IOException {
vgmixin().addView(view);
}

@Override public boolean canDelete(View view) {
return vgmixin().canDelete(view);
}

@Override public void deleteView(View view) throws IOException {
vgmixin().deleteView(view);
}

@Override public Collection<View> getViews() {
return vgmixin().getViews();
}

@Override public View getView(String name) {
return vgmixin().getView(name);
}

@Override public View getPrimaryView() {
return vgmixin().getPrimaryView();
}

@Override public void onViewRenamed(View view, String oldName, String newName) {
vgmixin().onViewRenamed(view, oldName, newName);
}

@Override public ViewsTabBar getViewsTabBar() {
if (viewsTabBar == null) {
viewsTabBar = new DefaultViewsTabBar();
}
return viewsTabBar;
}

@Override public ItemGroup<? extends TopLevelItem> getItemGroup() {
return this;
}

@Override public List<Action> getViewActions() {
// XXX what should the default be? View.getOwnerViewActions uses Jenkins.actions; Jenkins.viewActions would make more sense as a default;
// or should it be empty by default since non-top-level folders probably do not need the same actions as root?
return Collections.emptyList();
}

@Override public Object getStaplerFallback() {
return getPrimaryView();
}

/**
* Same as {@link #getItem} but named this way as a {@link WebMethod}.
* @see Hudson#getJob
*/
public TopLevelItem getJob(String name) {
return getItem(name);
}

@Extension public static class DescriptorImpl extends TopLevelItemDescriptor {

@Override public String getDisplayName() {

0 comments on commit c205940

Please sign in to comment.