diff --git a/core/src/main/java/hudson/model/AllView.java b/core/src/main/java/hudson/model/AllView.java index 9398c307f0..06122f89c6 100644 --- a/core/src/main/java/hudson/model/AllView.java +++ b/core/src/main/java/hudson/model/AllView.java @@ -23,9 +23,16 @@ */ package hudson.model; +import java.util.List; +import java.util.Locale; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import jenkins.util.SystemProperties; +import org.apache.commons.lang.StringUtils; import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; -import org.kohsuke.stapler.Stapler; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; @@ -44,6 +51,21 @@ import org.kohsuke.stapler.interceptor.RequirePOST; * @since 1.269 */ public class AllView extends View { + + /** + * The name of the default {@link AllView}. An {@link AllView} with this name will get a localized display name. + * Other {@link AllView} instances will be assumed to have been created by the user and thus will use the + * name the user created them with. + * + * @since FIXME + */ + public static final String DEFAULT_VIEW_NAME = "all"; + + /** + * Our logger. + */ + private static final Logger LOGGER = Logger.getLogger(AllView.class.getName()); + @DataBoundConstructor public AllView(String name) { super(name); @@ -64,6 +86,11 @@ public class AllView extends View { return true; } + @Override + public String getDisplayName() { + return DEFAULT_VIEW_NAME.equals(name) ? Messages.Hudson_ViewName() : name; + } + @RequirePOST @Override public Item doCreateItem(StaplerRequest req, StaplerResponse rsp) @@ -89,13 +116,73 @@ public class AllView extends View { // noop } + /** + * Corrects the name of the {@link AllView} if and only if the {@link AllView} is the primary view and + * its name is one of the localized forms of {@link Messages#_Hudson_ViewName()} and the user has not opted out of + * fixing the view name by setting the system property {@code hudson.mode.AllView.JENKINS-38606} to {@code false}. + * Use this method to round-trip the primary view name, e.g. + * {@code primaryView = applyJenkins38606Fixup(views, primaryView)} + *

+ * NOTE: we can only fix the localized name of an {@link AllView} if it is the primary view as otherwise urls + * would change, whereas the primary view is special and does not normally get accessed by the + * {@code /view/_name_} url. (Also note that there are some cases where the primary view will get accessed by + * its {@code /view/_name_} url which will then fall back to the primary view) + * + * @param views the list of views. + * @param primaryView the current primary view name. + * @return the primary view name - this will be the same as the provided primary view name unless a JENKINS-38606 + * matching name is detected, in which case this will be the new name of the primary view. + * @since FIXME + */ + @Nonnull + public static String migrateLegacyPrimaryAllViewLocalizedName(@Nonnull List views, + @Nonnull String primaryView) { + if (DEFAULT_VIEW_NAME.equals(primaryView)) { + // modern name, we are safe + return primaryView; + } + if (SystemProperties.getBoolean(AllView.class.getName()+".JENKINS-38606", true)) { + AllView allView = null; + for (View v : views) { + if (DEFAULT_VIEW_NAME.equals(v.getViewName())) { + // name conflict, we cannot rename the all view anyway + return primaryView; + } + if (StringUtils.equals(v.getViewName(), primaryView)) { + if (v instanceof AllView) { + allView = (AllView) v; + } else { + // none of our business fixing as we can only safely fix the primary view + return primaryView; + } + } + } + if (allView != null) { + // the primary view is an AllView but using a non-default name + for (Locale l : Locale.getAvailableLocales()) { + if (primaryView.equals(Messages._Hudson_ViewName().toString(l))) { + // bingo JENKINS-38606 detected + LOGGER.log(Level.INFO, + "JENKINS-38606 detected for AllView in {0}; renaming view from {1} to {2}", + new Object[]{allView.owner.getUrl(), DEFAULT_VIEW_NAME}); + allView.name = DEFAULT_VIEW_NAME; + return DEFAULT_VIEW_NAME; + } + } + } + } + return primaryView; + } + @Extension @Symbol("all") public static final class DescriptorImpl extends ViewDescriptor { @Override - public boolean isInstantiable() { - for (View v : Stapler.getCurrentRequest().findAncestorObject(ViewGroup.class).getViews()) - if(v instanceof AllView) + public boolean isApplicableIn(ViewGroup owner) { + for (View v : owner.getViews()) { + if (v instanceof AllView) { return false; + } + } return true; } diff --git a/core/src/main/java/hudson/model/ListView.java b/core/src/main/java/hudson/model/ListView.java index 2aa955b1a3..68ded29c6f 100644 --- a/core/src/main/java/hudson/model/ListView.java +++ b/core/src/main/java/hudson/model/ListView.java @@ -141,7 +141,9 @@ public class ListView extends View implements DirectlyModifiableView { protected void initColumns() { if (columns == null) - columns = new DescribableList>(this,ListViewColumn.createDefaultInitialColumnList()); + columns = new DescribableList>(this, + ListViewColumn.createDefaultInitialColumnList(getClass()) + ); } protected void initJobFilters() { @@ -459,7 +461,7 @@ public class ListView extends View implements DirectlyModifiableView { */ @Deprecated public static List getDefaultColumns() { - return ListViewColumn.createDefaultInitialColumnList(); + return ListViewColumn.createDefaultInitialColumnList(ListView.class); } @Restricted(NoExternalUse.class) diff --git a/core/src/main/java/hudson/model/MyViewsProperty.java b/core/src/main/java/hudson/model/MyViewsProperty.java index d075a73e81..6220cf0777 100644 --- a/core/src/main/java/hudson/model/MyViewsProperty.java +++ b/core/src/main/java/hudson/model/MyViewsProperty.java @@ -83,9 +83,11 @@ public class MyViewsProperty extends UserProperty implements ModifiableViewGroup // this shouldn't happen, but an error in 1.319 meant the last view could be deleted views = new CopyOnWriteArrayList(); - if (views.isEmpty()) + if (views.isEmpty()) { // preserve the non-empty invariant - views.add(new AllView(Messages.Hudson_ViewName(), this)); + views.add(new AllView(AllView.DEFAULT_VIEW_NAME, this)); + } + primaryViewName = AllView.migrateLegacyPrimaryAllViewLocalizedName(views, primaryViewName); viewGroupMixIn = new ViewGroupMixIn(this) { protected List views() { return views; } diff --git a/core/src/main/java/hudson/model/View.java b/core/src/main/java/hudson/model/View.java index 9683370a73..ea3d911700 100644 --- a/core/src/main/java/hudson/model/View.java +++ b/core/src/main/java/hudson/model/View.java @@ -56,6 +56,7 @@ import hudson.util.RunList; import hudson.util.XStream2; import hudson.views.ListViewColumn; import hudson.widgets.Widget; +import javax.annotation.Nonnull; import jenkins.model.Jenkins; import jenkins.model.ModelObjectWithChildren; import jenkins.model.item_category.Categories; @@ -238,6 +239,7 @@ public abstract class View extends AbstractModelObject implements AccessControll * @see #rename(String) */ @Exported(visibility=2,name="name") + @Nonnull public String getViewName() { return name; } @@ -415,7 +417,7 @@ public abstract class View extends AbstractModelObject implements AccessControll * If this view uses <t:projectView> for rendering, this method returns columns to be displayed. */ public Iterable getColumns() { - return ListViewColumn.createDefaultInitialColumnList(); + return ListViewColumn.createDefaultInitialColumnList(this); } /** @@ -1219,11 +1221,30 @@ public abstract class View extends AbstractModelObject implements AccessControll return Jenkins.getInstance().getDescriptorList(View.class); } + /** + * Returns the {@link ViewDescriptor} instances that can be instantiated for the {@link ViewGroup} in the current + * {@link StaplerRequest}. + *

+ * NOTE: Historically this method is only ever called from a {@link StaplerRequest} + * @return the list of instantiable {@link ViewDescriptor} instances for the current {@link StaplerRequest} + */ + @Nonnull public static List allInstantiable() { List r = new ArrayList(); - for (ViewDescriptor d : all()) - if(d.isInstantiable()) + StaplerRequest request = Stapler.getCurrentRequest(); + if (request == null) { + throw new IllegalStateException("This method can only be invoked from a stapler request"); + } + ViewGroup owner = request.findAncestorObject(ViewGroup.class); + if (owner == null) { + throw new IllegalStateException("This method can only be invoked from a request with a ViewGroup ancestor"); + } + for (ViewDescriptor d : DescriptorVisibilityFilter.apply(owner, all())) { + if (d.isApplicableIn(owner) && d.isInstantiable() + && owner.getACL().hasCreatePermission(Jenkins.getAuthentication(), owner, d)) { r.add(d); + } + } return r; } @@ -1268,6 +1289,7 @@ public abstract class View extends AbstractModelObject implements AccessControll if (mode==null || mode.length()==0) { if(isXmlSubmission) { View v = createViewFromXML(name, req.getInputStream()); + owner.getACL().checkCreatePermission(owner, v.getDescriptor()); v.owner = owner; rsp.setStatus(HttpServletResponse.SC_OK); return v; @@ -1276,7 +1298,7 @@ public abstract class View extends AbstractModelObject implements AccessControll } View v; - if (mode!=null && mode.equals("copy")) { + if ("copy".equals(mode)) { v = copy(req, owner, name); } else { ViewDescriptor descriptor = all().findByName(mode); @@ -1287,6 +1309,7 @@ public abstract class View extends AbstractModelObject implements AccessControll // create a view v = descriptor.newInstance(req,req.getSubmittedForm()); } + owner.getACL().checkCreatePermission(owner, v.getDescriptor()); v.owner = owner; // redirect to the config screen diff --git a/core/src/main/java/hudson/model/ViewDescriptor.java b/core/src/main/java/hudson/model/ViewDescriptor.java index dde3c0b9ed..e4f6dceb56 100644 --- a/core/src/main/java/hudson/model/ViewDescriptor.java +++ b/core/src/main/java/hudson/model/ViewDescriptor.java @@ -23,17 +23,26 @@ */ package hudson.model; +import hudson.util.FormValidation; import hudson.views.ListViewColumn; import hudson.views.ListViewColumnDescriptor; import hudson.views.ViewJobFilter; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; import java.util.Iterator; import java.util.List; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; import jenkins.model.DirectlyModifiableTopLevelItemGroup; import jenkins.model.Jenkins; +import org.apache.commons.lang.StringUtils; +import org.jvnet.tiger_types.Types; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.DoNotUse; import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.QueryParameter; +import org.kohsuke.stapler.Stapler; +import org.kohsuke.stapler.StaplerRequest; /** * {@link Descriptor} for {@link View}. @@ -101,6 +110,12 @@ public abstract class ViewDescriptor extends Descriptor { * Possible {@link ListViewColumnDescriptor}s that can be used with this view. */ public List> getColumnsDescriptors() { + StaplerRequest request = Stapler.getCurrentRequest(); + if (request != null) { + View view = request.findAncestorObject(clazz); + return view == null ? DescriptorVisibilityFilter.applyType(clazz, ListViewColumn.all()) + : DescriptorVisibilityFilter.apply(view, ListViewColumn.all()); + } return ListViewColumn.all(); } @@ -108,6 +123,62 @@ public abstract class ViewDescriptor extends Descriptor { * Possible {@link ViewJobFilter} types that can be used with this view. */ public List> getJobFiltersDescriptors() { + StaplerRequest request = Stapler.getCurrentRequest(); + if (request != null) { + View view = request.findAncestorObject(clazz); + return view == null ? DescriptorVisibilityFilter.applyType(clazz, ViewJobFilter.all()) + : DescriptorVisibilityFilter.apply(view, ViewJobFilter.all()); + } return ViewJobFilter.all(); } + + /** + * Validation of the display name field. + * + * @param view the view to check the new display name of. + * @param value the proposed new display name. + * @return the validation result. + * @since FIXME + */ + @SuppressWarnings("unused") // expose utility check method to subclasses + protected FormValidation checkDisplayName(@Nonnull View view, @CheckForNull String value) { + if (StringUtils.isBlank(value)) { + // no custom name, no need to check + return FormValidation.ok(); + } + for (View v: view.owner.getViews()) { + if (v.getViewName().equals(view.getViewName())) { + continue; + } + if (StringUtils.equals(v.getDisplayName(), value)) { + return FormValidation.warning(Messages.View_DisplayNameNotUniqueWarning(value)); + } + } + return FormValidation.ok(); + } + + /** + * Returns true if this {@link View} type is applicable to the given {@link ViewGroup} type. + *

+ * Default implementation returns {@code true} always. + * + * @return true to indicate applicable, in which case the view will be instantiable within the type of owner. + * @since FIXME + */ + public boolean isApplicable(Class ownerType) { + return true; + } + + /** + * Returns true if this {@link View} type is applicable in the specific {@link ViewGroup}. + *

+ * Default implementation returns {@link #isApplicable(Class)} for the {@link ViewGroup#getClass()}. + * + * @return true to indicate applicable, in which case the view will be instantiable within the given owner. + * @since FIXME + */ + public boolean isApplicableIn(ViewGroup owner) { + return isApplicable(owner.getClass()); + } + } diff --git a/core/src/main/java/hudson/model/ViewGroupMixIn.java b/core/src/main/java/hudson/model/ViewGroupMixIn.java index e390005edb..dcdc5a803c 100644 --- a/core/src/main/java/hudson/model/ViewGroupMixIn.java +++ b/core/src/main/java/hudson/model/ViewGroupMixIn.java @@ -26,6 +26,8 @@ package hudson.model; import hudson.model.ItemGroupMixIn; import hudson.model.View; import hudson.model.ViewGroup; +import java.util.Locale; +import java.util.logging.Level; import org.kohsuke.stapler.export.Exported; import java.io.IOException; @@ -99,6 +101,15 @@ public abstract class ViewGroupMixIn { View pv = getPrimaryView(); if (pv instanceof ViewGroup) return ((ViewGroup)pv).getView(name); + if (pv instanceof AllView && AllView.DEFAULT_VIEW_NAME.equals(pv.name)) { + // JENKINS-38606: primary view is the default AllView, is somebody using an old link to localized form? + for (Locale l : Locale.getAvailableLocales()) { + if (name.equals(Messages._Hudson_ViewName().toString(l))) { + // why yes they are, let's keep that link working + return pv; + } + } + } } return null; } diff --git a/core/src/main/java/hudson/security/ACL.java b/core/src/main/java/hudson/security/ACL.java index 3f892b7289..fd5b1e8e0b 100644 --- a/core/src/main/java/hudson/security/ACL.java +++ b/core/src/main/java/hudson/security/ACL.java @@ -24,6 +24,9 @@ package hudson.security; import hudson.model.User; +import hudson.model.View; +import hudson.model.ViewDescriptor; +import hudson.model.ViewGroup; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; @@ -120,6 +123,42 @@ public abstract class ACL { return true; } + /** + * Checks if the current security principal has the permission to create views within the specified view group. + *

+ * This is just a convenience function. + * + * @param c the container of the item. + * @param d the descriptor of the view to be created. + * @throws AccessDeniedException if the user doesn't have the permission. + * @since 1.607 + */ + public final void checkCreatePermission(@Nonnull ViewGroup c, + @Nonnull ViewDescriptor d) { + Authentication a = Jenkins.getAuthentication(); + if (!hasCreatePermission(a, c, d)) { + throw new AccessDeniedException(Messages.AccessDeniedException2_MissingPermission(a.getName(), + View.CREATE.group.title + "/" + View.CREATE.name + View.CREATE + "/" + d.getDisplayName())); + } + } + + /** + * Checks if the given principal has the permission to create views within the specified view group. + *

+ * Note that {@link #SYSTEM} can be passed in as the authentication parameter, + * in which case you should probably just assume it can create anything anywhere. + * @param a the principal. + * @param c the container of the view. + * @param d the descriptor of the view to be created. + * @return false + * if the user doesn't have the permission. + * @since FIXME + */ + public boolean hasCreatePermission(@Nonnull Authentication a, @Nonnull ViewGroup c, + @Nonnull ViewDescriptor d) { + return true; + } + // // Sid constants // diff --git a/core/src/main/java/hudson/views/ListViewColumn.java b/core/src/main/java/hudson/views/ListViewColumn.java index b5b4b62f9f..094d0f0dae 100644 --- a/core/src/main/java/hudson/views/ListViewColumn.java +++ b/core/src/main/java/hudson/views/ListViewColumn.java @@ -29,6 +29,7 @@ import hudson.ExtensionPoint; import hudson.model.Describable; import hudson.model.Descriptor; import hudson.model.Descriptor.FormException; +import hudson.model.DescriptorVisibilityFilter; import jenkins.model.Jenkins; import hudson.model.Item; import hudson.model.ItemGroup; @@ -121,20 +122,50 @@ public abstract class ListViewColumn implements ExtensionPoint, Describable createDefaultInitialColumnList() { + return createDefaultInitialColumnList(ListViewColumn.all()); + } + + /** + * Creates the list of {@link ListViewColumn}s to be used for newly created {@link ListView}s and their likes. + * + * @see ListView#initColumns() + * @since FIXME + */ + public static List createDefaultInitialColumnList(Class context) { + return createDefaultInitialColumnList(DescriptorVisibilityFilter.applyType(context, ListViewColumn.all())); + } + + /** + * Creates the list of {@link ListViewColumn}s to be used for a {@link ListView}s and their likes. + * + * @see View#getColumns() + * @since FIXME + */ + public static List createDefaultInitialColumnList(View view) { + return createDefaultInitialColumnList(DescriptorVisibilityFilter.apply(view, ListViewColumn.all())); + } + + private static List createDefaultInitialColumnList(List> descriptors) { // OK, set up default list of columns: // create all instances ArrayList r = new ArrayList(); final JSONObject emptyJSON = new JSONObject(); - for (Descriptor d : ListViewColumn.all()) + for (Descriptor d : descriptors) try { if (d instanceof ListViewColumnDescriptor) { ListViewColumnDescriptor ld = (ListViewColumnDescriptor) d; - if (!ld.shownByDefault()) continue; // skip this + if (!ld.shownByDefault()) { + continue; // skip this + } } ListViewColumn lvc = d.newInstance(null, emptyJSON); - if (!lvc.shownByDefault()) continue; // skip this + if (!lvc.shownByDefault()) { + continue; // skip this + } r.add(lvc); } catch (FormException e) { diff --git a/core/src/main/java/hudson/views/MyViewsTabBar.java b/core/src/main/java/hudson/views/MyViewsTabBar.java index c166c71e7d..45cba48b49 100644 --- a/core/src/main/java/hudson/views/MyViewsTabBar.java +++ b/core/src/main/java/hudson/views/MyViewsTabBar.java @@ -28,11 +28,19 @@ import hudson.Extension; import hudson.ExtensionPoint; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; +import hudson.model.View; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import javax.annotation.Nonnull; import jenkins.model.GlobalConfiguration; import jenkins.model.Jenkins; import hudson.model.MyViewsProperty; import net.sf.json.JSONObject; import org.jenkinsci.Symbol; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.stapler.StaplerRequest; /** @@ -63,6 +71,27 @@ public abstract class MyViewsTabBar extends AbstractDescribableImpl sort(@Nonnull List views) { + List result = new ArrayList<>(views); + Collections.sort(result, new Comparator() { + @Override + public int compare(View o1, View o2) { + return o1.getDisplayName().compareTo(o2.getDisplayName()); + } + }); + return result; + } + /** * Configures {@link ViewsTabBar} in the system configuration. * diff --git a/core/src/main/java/hudson/views/ViewsTabBar.java b/core/src/main/java/hudson/views/ViewsTabBar.java index 579f110ce5..f529232672 100644 --- a/core/src/main/java/hudson/views/ViewsTabBar.java +++ b/core/src/main/java/hudson/views/ViewsTabBar.java @@ -29,11 +29,19 @@ import hudson.ExtensionPoint; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; import hudson.model.Descriptor.FormException; +import hudson.model.View; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import javax.annotation.Nonnull; import jenkins.model.GlobalConfiguration; import jenkins.model.Jenkins; import hudson.model.ListView; import net.sf.json.JSONObject; import org.jenkinsci.Symbol; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.stapler.StaplerRequest; /** @@ -65,6 +73,27 @@ public abstract class ViewsTabBar extends AbstractDescribableImpl i return (ViewsTabBarDescriptor)super.getDescriptor(); } + /** + * Sorts the views by {@link View#getDisplayName()}. + * + * @param views the views. + * @return the sorted views + * @since FIXME + */ + @Nonnull + @Restricted(NoExternalUse.class) + @SuppressWarnings("unused") // invoked from stapler view + public List sort(@Nonnull List views) { + List result = new ArrayList<>(views); + Collections.sort(result, new Comparator() { + @Override + public int compare(View o1, View o2) { + return o1.getDisplayName().compareTo(o2.getDisplayName()); + } + }); + return result; + } + /** * Configures {@link ViewsTabBar} in the system configuration. * diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index 266a3f9504..7c61deebdd 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -3086,11 +3086,12 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve // initialize views by inserting the default view if necessary // this is both for clean Jenkins and for backward compatibility. if(views.size()==0 || primaryView==null) { - View v = new AllView(Messages.Hudson_ViewName()); + View v = new AllView(AllView.DEFAULT_VIEW_NAME); setViewOwner(v); views.add(0,v); primaryView = v.getViewName(); } + primaryView = AllView.migrateLegacyPrimaryAllViewLocalizedName(views, primaryView); if (useSecurity!=null && !useSecurity) { // forced reset to the unsecure mode. diff --git a/core/src/main/resources/hudson/model/Messages.properties b/core/src/main/resources/hudson/model/Messages.properties index 676ebb12d0..70bba990cf 100644 --- a/core/src/main/resources/hudson/model/Messages.properties +++ b/core/src/main/resources/hudson/model/Messages.properties @@ -263,6 +263,8 @@ View.ConfigurePermission.Description=\ View.ReadPermission.Description=\ This permission allows users to see views (implied by generic read access). View.MissingMode=No view type is specified +View.DisplayNameNotUniqueWarning=The display name, "{0}", is already in use by another view and \ + could cause confusion and delay. UpdateCenter.Status.CheckingInternet=Checking internet connectivity UpdateCenter.Status.CheckingJavaNet=Checking update center connectivity diff --git a/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly b/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly index 10b55865f6..e49686bf35 100644 --- a/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly +++ b/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly @@ -26,8 +26,8 @@ THE SOFTWARE. - - + + - - + + - - -

+ + + + The name of the tab + + + The url of the tab + + + Whether the tab is active or not + + + The title of the tab + + + diff --git a/core/src/test/java/hudson/model/ListViewTest.java b/core/src/test/java/hudson/model/ListViewTest.java index 7de805913b..f184aac9a2 100644 --- a/core/src/test/java/hudson/model/ListViewTest.java +++ b/core/src/test/java/hudson/model/ListViewTest.java @@ -30,7 +30,7 @@ public class ListViewTest { mockStatic(Items.class); mockStatic(ListViewColumn.class); List columns = Collections.emptyList(); - when(ListViewColumn.createDefaultInitialColumnList()).thenReturn(columns); + when(ListViewColumn.createDefaultInitialColumnList(ListView.class)).thenReturn(columns); ViewGroup owner = mock(ViewGroup.class); ItemGroup itemGroupOwner = mock(ItemGroup.class); when(owner.getItemGroup()).thenReturn(itemGroupOwner); @@ -49,7 +49,7 @@ public class ListViewTest { mockStatic(Items.class); mockStatic(ListViewColumn.class); List columns = Collections.emptyList(); - when(ListViewColumn.createDefaultInitialColumnList()).thenReturn(columns); + when(ListViewColumn.createDefaultInitialColumnList(ListView.class)).thenReturn(columns); ViewGroup owner = mock(ViewGroup.class); ItemGroup ig = mock(ItemGroup.class); when(owner.getItemGroup()).thenReturn(ig); diff --git a/test/src/test/java/hudson/cli/DeleteViewCommandTest.java b/test/src/test/java/hudson/cli/DeleteViewCommandTest.java index d9ff9f708a..20b36e87a5 100644 --- a/test/src/test/java/hudson/cli/DeleteViewCommandTest.java +++ b/test/src/test/java/hudson/cli/DeleteViewCommandTest.java @@ -33,6 +33,7 @@ import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput; import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently; import static hudson.cli.CLICommandInvoker.Matcher.failedWith; +import hudson.model.AllView; import java.io.IOException; import hudson.model.ListView; @@ -116,13 +117,13 @@ public class DeleteViewCommandTest { final CLICommandInvoker.Result result = command .authorizedTo(View.READ, View.DELETE, Jenkins.READ) - .invokeWithArgs("All") + .invokeWithArgs(AllView.DEFAULT_VIEW_NAME) ; assertThat(result, failedWith(4)); assertThat(result, hasNoStandardOutput()); - assertThat(j.jenkins.getView("All"), notNullValue()); - assertThat(result.stderr(), containsString("ERROR: Jenkins does not allow to delete 'All' view")); + assertThat(j.jenkins.getView(AllView.DEFAULT_VIEW_NAME), notNullValue()); + assertThat(result.stderr(), containsString("ERROR: Jenkins does not allow to delete '"+AllView.DEFAULT_VIEW_NAME+"' view")); } @Test public void deleteViewShoudlFailIfViewNameIsEmpty() { @@ -262,15 +263,15 @@ public class DeleteViewCommandTest { final CLICommandInvoker.Result result = command .authorizedTo(View.READ, View.DELETE, Jenkins.READ) - .invokeWithArgs("aView1", "aView2", "All"); + .invokeWithArgs("aView1", "aView2", AllView.DEFAULT_VIEW_NAME); assertThat(result, failedWith(5)); assertThat(result, hasNoStandardOutput()); - assertThat(result.stderr(), containsString("All: Jenkins does not allow to delete 'All' view")); + assertThat(result.stderr(), containsString(AllView.DEFAULT_VIEW_NAME+": Jenkins does not allow to delete '"+ AllView.DEFAULT_VIEW_NAME+"' view")); assertThat(result.stderr(), containsString("ERROR: " + CLICommand.CLI_LISTPARAM_SUMMARY_ERROR_TEXT)); assertThat(j.jenkins.getView("aView1"), nullValue()); assertThat(j.jenkins.getView("aView2"), nullValue()); - assertThat(j.jenkins.getView("All"), notNullValue()); + assertThat(j.jenkins.getView(AllView.DEFAULT_VIEW_NAME), notNullValue()); } } diff --git a/test/src/test/java/hudson/model/ApiTest.java b/test/src/test/java/hudson/model/ApiTest.java index 44dd587878..cffde63571 100644 --- a/test/src/test/java/hudson/model/ApiTest.java +++ b/test/src/test/java/hudson/model/ApiTest.java @@ -99,7 +99,7 @@ public class ApiTest { @Issue("JENKINS-3267") public void wrappedOneItem() throws Exception { Page page = j.createWebClient().goTo("api/xml?wrapper=root&xpath=/hudson/view/name", "application/xml"); - assertEquals("All", page.getWebResponse().getContentAsString()); + assertEquals(""+ AllView.DEFAULT_VIEW_NAME+"", page.getWebResponse().getContentAsString()); } @Test @@ -118,7 +118,7 @@ public class ApiTest { @Test public void unwrappedOneItem() throws Exception { Page page = j.createWebClient().goTo("api/xml?xpath=/hudson/view/name", "application/xml"); - assertEquals("All", page.getWebResponse().getContentAsString()); + assertEquals(""+ AllView.DEFAULT_VIEW_NAME+"", page.getWebResponse().getContentAsString()); } @Test diff --git a/test/src/test/java/hudson/model/MyViewsPropertyTest.java b/test/src/test/java/hudson/model/MyViewsPropertyTest.java index 666a383d20..1abfcacd35 100644 --- a/test/src/test/java/hudson/model/MyViewsPropertyTest.java +++ b/test/src/test/java/hudson/model/MyViewsPropertyTest.java @@ -47,17 +47,17 @@ public class MyViewsPropertyTest { @Test public void testReadResolve() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.setUser(user); user.addProperty(property); property.readResolve(); - assertNotNull("Property should contains " + Messages.Hudson_ViewName() + " defaultly.", property.getView(Messages.Hudson_ViewName())); + assertNotNull("Property should contains " + AllView.DEFAULT_VIEW_NAME + " defaultly.", property.getView(AllView.DEFAULT_VIEW_NAME)); } @Test public void testSave() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); @@ -79,10 +79,10 @@ public class MyViewsPropertyTest { @Test public void testGetViews() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); - assertTrue("Property should contains " + Messages.Hudson_ViewName(), property.getViews().contains(property.getView(Messages.Hudson_ViewName()))); + assertTrue("Property should contains " + AllView.DEFAULT_VIEW_NAME, property.getViews().contains(property.getView(AllView.DEFAULT_VIEW_NAME))); View view = new ListView("foo",property); property.addView(view); assertTrue("Property should contains " + view.name, property.getViews().contains(view)); @@ -91,10 +91,11 @@ public class MyViewsPropertyTest { @Test public void testGetView() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); - assertNotNull("Property should contains " + Messages.Hudson_ViewName(), property.getView(Messages.Hudson_ViewName())); + assertNotNull("Property should contains " + AllView.DEFAULT_VIEW_NAME, property.getView( + AllView.DEFAULT_VIEW_NAME)); View view = new ListView("foo",property); property.addView(view); assertEquals("Property should contains " + view.name, view, property.getView(view.name)); @@ -103,11 +104,11 @@ public class MyViewsPropertyTest { @Test public void testGetPrimaryView() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); - assertEquals("Property should have primary view " + Messages.Hudson_ViewName() + " instead of " + property.getPrimaryView(). name,property.getView(Messages.Hudson_ViewName()), property.getPrimaryView()); + assertEquals("Property should have primary view " + AllView.DEFAULT_VIEW_NAME + " instead of " + property.getPrimaryView(). name,property.getView(AllView.DEFAULT_VIEW_NAME), property.getPrimaryView()); View view = new ListView("foo", property); property.addView(view); property.setPrimaryViewName(view.name); @@ -117,35 +118,35 @@ public class MyViewsPropertyTest { @Test public void testCanDelete() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); - assertFalse("Property should not enable to delete view " + Messages.Hudson_ViewName(), property.canDelete(property.getView(Messages.Hudson_ViewName()))); + assertFalse("Property should not enable to delete view " + AllView.DEFAULT_VIEW_NAME, property.canDelete(property.getView(AllView.DEFAULT_VIEW_NAME))); View view = new ListView("foo", property); property.addView(view); assertTrue("Property should enable to delete view " + view.name , property.canDelete(view)); property.setPrimaryViewName(view.name); assertFalse("Property should not enable to delete view " + view.name , property.canDelete(view)); - assertTrue("Property should enable to delete view " + Messages.Hudson_ViewName(), property.canDelete(property.getView(Messages.Hudson_ViewName()))); + assertTrue("Property should enable to delete view " + AllView.DEFAULT_VIEW_NAME, property.canDelete(property.getView(AllView.DEFAULT_VIEW_NAME))); } @Test public void testDeleteView() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); boolean ex = false; try{ - property.deleteView(property.getView(Messages.Hudson_ViewName())); + property.deleteView(property.getView(AllView.DEFAULT_VIEW_NAME)); } catch(IllegalStateException e){ ex = true; } assertTrue("Property should throw IllegalStateException.", ex); - assertTrue("Property should contain view " + Messages.Hudson_ViewName(), property.getViews().contains(property.getView(Messages.Hudson_ViewName()))); + assertTrue("Property should contain view " + AllView.DEFAULT_VIEW_NAME, property.getViews().contains(property.getView(AllView.DEFAULT_VIEW_NAME))); View view = new ListView("foo", property); property.addView(view); ex = false; @@ -159,14 +160,14 @@ public class MyViewsPropertyTest { property.addView(view); property.setPrimaryViewName(view.name); assertTrue("Property should not contain view " + view.name , property.getViews().contains(view)); - property.deleteView(property.getView(Messages.Hudson_ViewName())); - assertFalse("Property should not contains view " + Messages.Hudson_ViewName(), property.getViews().contains(property.getView(Messages.Hudson_ViewName()))); + property.deleteView(property.getView(AllView.DEFAULT_VIEW_NAME)); + assertFalse("Property should not contains view " + AllView.DEFAULT_VIEW_NAME, property.getViews().contains(property.getView(AllView.DEFAULT_VIEW_NAME))); } @Test public void testOnViewRenamed() throws IOException, Failure, FormException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); @@ -180,7 +181,7 @@ public class MyViewsPropertyTest { @Test public void testAddView() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); @@ -196,7 +197,7 @@ public class MyViewsPropertyTest { @Test public void testDoCreateView() throws Exception { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); @@ -213,7 +214,7 @@ public class MyViewsPropertyTest { @Test public void testGetACL() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); @@ -227,7 +228,7 @@ public class MyViewsPropertyTest { rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm()); User user = User.get("User"); User user2 = User.get("User2"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); GlobalMatrixAuthorizationStrategy auth = new GlobalMatrixAuthorizationStrategy(); @@ -264,7 +265,7 @@ public class MyViewsPropertyTest { rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm()); User user = User.get("User"); User user2 = User.get("User2"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); GlobalMatrixAuthorizationStrategy auth = new GlobalMatrixAuthorizationStrategy();