diff --git a/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java b/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java
index cad865970cabaf52ffc4a6207e798a986fc205d5..fdf4344cca109c63d2c1e21784b87adc8c5a9298 100644
--- a/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java
+++ b/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java
@@ -23,7 +23,11 @@
*/
package hudson.cli.handlers;
+import hudson.model.ViewGroup;
import hudson.model.View;
+
+import java.util.StringTokenizer;
+
import jenkins.model.Jenkins;
import org.kohsuke.MetaInfServices;
@@ -37,6 +41,20 @@ import org.kohsuke.args4j.spi.Setter;
/**
* Refers to {@link View} by its name.
*
+ *
+ * For example:
+ *
+ * - my_view_name
- refers to a top level view with given name.
+ * - nested/inner
- refers to a view named inner inside of a top level view group named nested.
+ *
+ *
+ *
+ * View name is a non-empty sequence of {@link View} names delimited by '/'.
+ * Handler traverse the view names from left to right. First one is expected to
+ * be a top level view and all but the last one are expected to be instances of
+ * {@link ViewGroup}. Handler fails to resolve view provided a view with given
+ * name does not exist or a user was not granted {@link View.READ} permission.
+ *
* @author ogondza
* @since TODO
*/
@@ -51,13 +69,38 @@ public class ViewOptionHandler extends OptionHandler {
@Override
public int parseArguments(Parameters params) throws CmdLineException {
- String viewName = params.getParameter(0);
+ setter.addValue(getView(params.getParameter(0)));
+ return 1;
+ }
- final View view = Jenkins.getInstance().getView(viewName);
- if (view == null) throw new CmdLineException(owner, "No such view '" + viewName + "'");
+ private View getView(String name) throws CmdLineException {
- setter.addValue(view);
- return 1;
+ View view = null;
+ ViewGroup group = Jenkins.getInstance();
+
+ final StringTokenizer tok = new StringTokenizer(name, "/");
+ while(tok.hasMoreTokens()) {
+
+ String viewName = tok.nextToken();
+
+ view = group.getView(viewName);
+ if (view == null) throw new CmdLineException(owner, String.format(
+ "No view named %s inside view %s",
+ viewName, group.getDisplayName()
+ ));
+
+ view.checkPermission(View.READ);
+
+ if (view instanceof ViewGroup) {
+ group = (ViewGroup) view;
+ } else if (tok.hasMoreTokens()) {
+ throw new CmdLineException(
+ owner, view.getViewName() + " view can not contain views"
+ );
+ }
+ }
+
+ return view;
}
@Override
diff --git a/core/src/test/java/hudson/cli/handlers/ViewOptionHandlerTest.java b/core/src/test/java/hudson/cli/handlers/ViewOptionHandlerTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..98b29e2e1d77ae77807c0d72210f3dfd1e70a5f1
--- /dev/null
+++ b/core/src/test/java/hudson/cli/handlers/ViewOptionHandlerTest.java
@@ -0,0 +1,232 @@
+/*
+ * The MIT License
+ *
+ * Copyright (c) 2013 Red Hat, 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 hudson.cli.handlers;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
+import hudson.model.ViewGroup;
+import hudson.model.View;
+
+import jenkins.model.Jenkins;
+
+import org.acegisecurity.AccessDeniedException;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.kohsuke.args4j.CmdLineException;
+import org.kohsuke.args4j.spi.Parameters;
+import org.kohsuke.args4j.spi.Setter;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@PrepareForTest(Jenkins.class)
+@RunWith(PowerMockRunner.class)
+public class ViewOptionHandlerTest {
+
+ @Mock private Setter setter;
+ private ViewOptionHandler handler;
+
+ // Hierarchy of views used as a shared fixture:
+ // $JENKINS_URL/view/outer/view/nested/view/inner/
+ @Mock private View inner;
+ @Mock private CompositeView nested;
+ @Mock private CompositeView outer;
+ @Mock private Jenkins jenkins;
+
+ @Before public void setUp() {
+
+ MockitoAnnotations.initMocks(this);
+
+ handler = new ViewOptionHandler(null, null, setter);
+
+ when(inner.getViewName()).thenReturn("inner");
+ when(inner.getDisplayName()).thenCallRealMethod();
+
+ when(nested.getViewName()).thenReturn("nested");
+ when(nested.getDisplayName()).thenCallRealMethod();
+ when(nested.getView("inner")).thenReturn(inner);
+
+ when(outer.getViewName()).thenReturn("outer");
+ when(outer.getDisplayName()).thenCallRealMethod();
+ when(outer.getView("nested")).thenReturn(nested);
+
+ PowerMockito.mockStatic(Jenkins.class);
+ PowerMockito.when(Jenkins.getInstance()).thenReturn(jenkins);
+ when(jenkins.getView("outer")).thenReturn(outer);
+ when(jenkins.getDisplayName()).thenReturn("Jenkins");
+ }
+
+ @Test public void resolveTopLevelView() throws Exception {
+
+ parse("outer");
+
+ verify(setter).addValue(outer);
+ }
+
+ @Test public void resolveNestedView() throws Exception {
+
+ parse("outer/nested");
+
+ verify(setter).addValue(nested);
+ }
+
+ @Test public void resolveOuterView() throws Exception {
+
+ parse("outer/nested/inner");
+
+ verify(setter).addValue(inner);
+ }
+
+ @Test public void ignoreLeadingAndTrailingSlashes() throws Exception {
+
+ parse("/outer/nested/inner/");
+
+ verify(setter).addValue(inner);
+ }
+
+ @Test public void reportNonexistentTopLevelView() throws Exception {
+
+ assertEquals(
+ "No view named missing_view inside view Jenkins",
+ parseFailedWith(CmdLineException.class, "missing_view")
+ );
+
+ verifyZeroInteractions(setter);
+ }
+
+ @Test public void reportNonexistentNestedView() throws Exception {
+
+ assertEquals(
+ "No view named missing_view inside view outer",
+ parseFailedWith(CmdLineException.class, "outer/missing_view")
+ );
+
+ verifyZeroInteractions(setter);
+ }
+
+ @Test public void reportNonexistentInnerView() throws Exception {
+
+ assertEquals(
+ "No view named missing_view inside view nested",
+ parseFailedWith(CmdLineException.class, "outer/nested/missing_view")
+ );
+
+ verifyZeroInteractions(setter);
+ }
+
+ @Test public void reportTraversingViewThatIsNotAViewGroup() throws Exception {
+
+ assertEquals(
+ "inner view can not contain views",
+ parseFailedWith(CmdLineException.class, "outer/nested/inner/missing")
+ );
+
+ verifyZeroInteractions(setter);
+ }
+
+ @Test public void refuseToReadOuterView() throws Exception {
+
+ denyAccessOn(outer);
+
+ parseFailedWith(AccessDeniedException.class, "outer/nested/inner");
+
+ verify(outer).checkPermission(View.READ);
+ verifyNoMoreInteractions(outer);
+
+ verifyZeroInteractions(nested);
+ verifyZeroInteractions(inner);
+ verifyZeroInteractions(setter);
+ }
+
+ @Test public void refuseToReadNestedView() throws Exception {
+
+ denyAccessOn(nested);
+
+ parseFailedWith(AccessDeniedException.class, "outer/nested/inner");
+
+ verify(nested).checkPermission(View.READ);
+ verifyNoMoreInteractions(nested);
+
+ verifyZeroInteractions(inner);
+ verifyZeroInteractions(setter);
+ }
+
+ @Test public void refuseToReadInnerView() throws Exception {
+
+ denyAccessOn(inner);
+
+ parseFailedWith(AccessDeniedException.class, "outer/nested/inner");
+
+ verify(inner).checkPermission(View.READ);
+ verifyNoMoreInteractions(inner);
+
+ verifyZeroInteractions(setter);
+ }
+
+ private void denyAccessOn(View view) {
+
+ doThrow(new AccessDeniedException(null)).when(view).checkPermission(View.READ);
+ }
+
+ private String parseFailedWith(Class extends Exception> type, final String... params) throws Exception {
+
+ try {
+ parse(params);
+
+ } catch (Exception ex) {
+
+ if (!type.isAssignableFrom(ex.getClass())) throw ex;
+
+ return ex.getMessage();
+ }
+
+ fail("No exception thrown. Expected " + type.getClass());
+ return null;
+ }
+
+ private void parse(final String... params) throws CmdLineException {
+ handler.parseArguments(new Parameters() {
+ public String getParameter(int idx) throws CmdLineException {
+ return params[idx];
+ }
+ public int size() {
+ return params.length;
+ }
+ });
+ }
+
+ private static abstract class CompositeView extends View implements ViewGroup {
+ protected CompositeView(String name) {
+ super(name);
+ }
+ }
+}
diff --git a/test/src/test/java/hudson/cli/DeleteViewCommandTest.java b/test/src/test/java/hudson/cli/DeleteViewCommandTest.java
index 5b3d8d334f71633a8ef2669ebfe35eaf357fc72b..93998cfa4f705e0ac76c2a235a88811c2275547c 100644
--- a/test/src/test/java/hudson/cli/DeleteViewCommandTest.java
+++ b/test/src/test/java/hudson/cli/DeleteViewCommandTest.java
@@ -60,7 +60,7 @@ public class DeleteViewCommandTest {
j.jenkins.addView(new ListView("aView"));
final CLICommandInvoker.Result result = command
- .authorizedTo(Jenkins.READ)
+ .authorizedTo(View.READ, Jenkins.READ)
.invokeWithArgs("aView")
;
@@ -74,7 +74,7 @@ public class DeleteViewCommandTest {
j.jenkins.addView(new ListView("aView"));
final CLICommandInvoker.Result result = command
- .authorizedTo(View.DELETE, Jenkins.READ)
+ .authorizedTo(View.READ, View.DELETE, Jenkins.READ)
.invokeWithArgs("aView")
;
@@ -87,20 +87,20 @@ public class DeleteViewCommandTest {
@Test public void deleteViewShouldFailIfViewDoesNotExist() {
final CLICommandInvoker.Result result = command
- .authorizedTo(View.DELETE, Jenkins.READ)
+ .authorizedTo(View.READ, View.DELETE, Jenkins.READ)
.invokeWithArgs("never_created")
;
assertThat(result, failedWith(-1));
assertThat(result, hasNoStandardOutput());
- assertThat(result.stderr(), containsString("No such view 'never_created'"));
+ assertThat(result.stderr(), containsString("No view named never_created inside view Jenkins"));
}
// ViewGroup.canDelete()
@Test public void deleteViewShouldFailIfViewGroupDoesNotAllowDeletion() {
final CLICommandInvoker.Result result = command
- .authorizedTo(View.DELETE, Jenkins.READ)
+ .authorizedTo(View.READ, View.DELETE, Jenkins.READ)
.invokeWithArgs("All")
;
diff --git a/test/src/test/java/hudson/cli/GetViewCommandTest.java b/test/src/test/java/hudson/cli/GetViewCommandTest.java
index 8e2016fd31eb877def6c2de47f09dbf6adfded2c..4eb47f1553b11d7530ed8c72f24670e0bba331c5 100644
--- a/test/src/test/java/hudson/cli/GetViewCommandTest.java
+++ b/test/src/test/java/hudson/cli/GetViewCommandTest.java
@@ -26,9 +26,7 @@ package hudson.cli;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
-import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
-import static org.hamcrest.text.IsEmptyString.isEmptyString;
import static hudson.cli.CLICommandInvoker.Matcher.failedWith;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
import static hudson.cli.CLICommandInvoker.Matcher.hasNoErrorOutput;
@@ -94,6 +92,6 @@ public class GetViewCommandTest {
assertThat(result, failedWith(-1));
assertThat(result, hasNoStandardOutput());
- assertThat(result.stderr(), containsString("No such view 'never_created'"));
+ assertThat(result.stderr(), containsString("No view named never_created inside view Jenkins"));
}
}
diff --git a/test/src/test/java/hudson/cli/UpdateViewCommandTest.java b/test/src/test/java/hudson/cli/UpdateViewCommandTest.java
index 7afa8f62fe5391c70fc1fa5465fd1a1a1e89de11..7d46483b88b8eb082c15ee50cd412789ed43bd8c 100644
--- a/test/src/test/java/hudson/cli/UpdateViewCommandTest.java
+++ b/test/src/test/java/hudson/cli/UpdateViewCommandTest.java
@@ -57,7 +57,7 @@ public class UpdateViewCommandTest {
j.jenkins.addView(new ListView("aView"));
final CLICommandInvoker.Result result = command
- .authorizedTo(Jenkins.READ)
+ .authorizedTo(View.READ, Jenkins.READ)
.withStdin(this.getClass().getResourceAsStream("/hudson/cli/view.xml"))
.invokeWithArgs("aView")
;
@@ -72,7 +72,7 @@ public class UpdateViewCommandTest {
j.jenkins.addView(new ListView("aView"));
final CLICommandInvoker.Result result = command
- .authorizedTo(View.CONFIGURE, Jenkins.READ)
+ .authorizedTo(View.READ, View.CONFIGURE, Jenkins.READ)
.withStdin(this.getClass().getResourceAsStream("/hudson/cli/view.xml"))
.invokeWithArgs("aView")
;
@@ -91,13 +91,13 @@ public class UpdateViewCommandTest {
@Test public void updateViewShouldFailIfViewDoesNotExist() {
final CLICommandInvoker.Result result = command
- .authorizedTo(View.CONFIGURE, Jenkins.READ)
+ .authorizedTo(View.READ, View.CONFIGURE, Jenkins.READ)
.withStdin(this.getClass().getResourceAsStream("/hudson/cli/view.xml"))
.invokeWithArgs("not_created")
;
assertThat(result, failedWith(-1));
assertThat(result, hasNoStandardOutput());
- assertThat(result.stderr(), containsString("No such view 'not_created'"));
+ assertThat(result.stderr(), containsString("No view named not_created inside view Jenkins"));
}
}