Skip to content
Browse files

[JENKINS-42728] Updating view with CLI using input of a different vie…

…w type should fail (#2804)

* [JENKINS-42728] Updating view with CLI using different view type should fail

- Additionally, updating a view using an XML that will be converted by XStream via an alias
  will succeed.


* Remove TestView

* update test
  • Loading branch information...
scoheb authored and oleg-nenashev committed May 4, 2017
1 parent d45576d commit 07683617d3f9277fe274e7a35f745b14b6a35e44
@@ -1186,12 +1186,18 @@ public void updateByXml(Source source) throws IOException {

// try to reflect the changes by reloading

try (InputStream in = new BufferedInputStream(new ByteArrayInputStream(out.toString().getBytes("UTF-8")))){
// Do not allow overwriting view name as it might collide with another
// view in same ViewGroup and might not satisfy Jenkins.checkGoodName.
String oldname = name;
Jenkins.XSTREAM.unmarshal(new Xpp3Driver().createReader(in), this);
Object o = Jenkins.XSTREAM.unmarshal(new Xpp3Driver().createReader(in), this);
if (!o.getClass().equals(getClass())) {
// ensure that we've got the same view type. extending this code to support updating
// to different view type requires destroying & creating a new view type
throw new IOException("Expecting view type: "+this.getClass()+" but got: "+o.getClass()+" instead." +
"\nShould you needed to change to a new view type, you must first delete and then re-create " +
"the view with the new view type.");
name = oldname;
} catch (StreamException | ConversionException | Error e) {// mostly reflection errors
throw new IOException("Unable to read",e);
@@ -31,7 +31,9 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

import hudson.model.ListView;
import hudson.model.TreeView;
import hudson.model.View;
import jenkins.model.Jenkins;

@@ -66,6 +68,38 @@
assertThat(result.stderr(), containsString("ERROR: user is missing the View/Configure permission"));

* This test shows that updating a view using an XML that will be
* converted by XStream via an alias will rightfully succeed.
* @throws Exception
@Test public void updateViewWithRenamedClass() throws Exception {
ListView tv = new ListView("tView");
j.jenkins.XSTREAM2.addCompatibilityAlias("org.acme.old.Foo", ListView.class);
final CLICommandInvoker.Result result = command
.authorizedTo(View.READ, View.CONFIGURE, Jenkins.READ)

assertThat(result, succeededSilently());

@Test public void updateViewWithWrongViewTypeShouldFail() throws Exception {
TreeView tv = new TreeView("aView");
final CLICommandInvoker.Result result = command
.authorizedTo(View.READ, View.CONFIGURE, Jenkins.READ)

assertThat(result, failedWith(1));
assertThat(result.stderr(), containsString("Expecting view type: "+ tv.getClass()
+ " but got: class hudson.model.ListView instead."));

@Test public void updateViewShouldModifyViewConfiguration() throws Exception {

j.jenkins.addView(new ListView("aView"));
@@ -98,4 +132,5 @@
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("ERROR: No view named not_created inside view Jenkins"));

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<properties class="hudson.model.View$PropertyList"/>
<comparator class="hudson.util.CaseInsensitiveComparator"/>
< plugin="console-column-plugin@1.5"/>

0 comments on commit 0768361

Please sign in to comment.
You can’t perform that action at this time.