Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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.

[FIXES JENKINS-42728]

* Remove TestView

* update test
  • Loading branch information
scoheb authored and oleg-nenashev committed May 4, 2017
1 parent d45576d commit 0768361
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
10 changes: 8 additions & 2 deletions core/src/main/java/hudson/model/View.java
Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions test/src/test/java/hudson/cli/UpdateViewCommandTest.java
Expand Up @@ -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;

Expand Down Expand Up @@ -66,6 +68,38 @@ public class UpdateViewCommandTest {
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.addView(tv);
j.jenkins.XSTREAM2.addCompatibilityAlias("org.acme.old.Foo", ListView.class);
final CLICommandInvoker.Result result = command
.authorizedTo(View.READ, View.CONFIGURE, Jenkins.READ)
.withStdin(this.getClass().getResourceAsStream("/hudson/cli/testview-foo.xml"))
.invokeWithArgs("tView");

assertThat(result, succeededSilently());
}

@Test public void updateViewWithWrongViewTypeShouldFail() throws Exception {
TreeView tv = new TreeView("aView");
j.jenkins.addView(tv);
final CLICommandInvoker.Result result = command
.authorizedTo(View.READ, View.CONFIGURE, Jenkins.READ)
.withStdin(this.getClass().getResourceAsStream("/hudson/cli/view.xml"))
.invokeWithArgs("aView")
;

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"));
Expand Down Expand Up @@ -98,4 +132,5 @@ public class UpdateViewCommandTest {
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("ERROR: No view named not_created inside view Jenkins"));
}

}
31 changes: 31 additions & 0 deletions test/src/test/resources/hudson/cli/testview-foo.xml
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<org.acme.old.Foo>
<name>tView</name>
<filterExecutors>true</filterExecutors>
<filterQueue>false</filterQueue>
<properties class="hudson.model.View$PropertyList"/>
<jobNames>
<comparator class="hudson.util.CaseInsensitiveComparator"/>
<string>core_selenium-test</string>
<string>jenkins_lts_branch</string>
<string>jenkins_main_maven-3.1.0</string>
<string>jenkins_main_trunk</string>
<string>jenkins_pom</string>
<string>jenkins_rc_branch</string>
<string>jenkins_ui-changes_branch</string>
<string>remoting</string>
<string>selenium-tests</string>
</jobNames>
<jobFilters/>
<columns>
<hudson.views.StatusColumn/>
<hudson.views.WeatherColumn/>
<hudson.views.JobColumn/>
<hudson.views.LastSuccessColumn/>
<hudson.views.LastFailureColumn/>
<hudson.views.LastDurationColumn/>
<org.jenkins.ci.plugins.column.console.LastBuildColumn plugin="console-column-plugin@1.5"/>
<hudson.views.BuildButtonColumn/>
</columns>
<recurse>false</recurse>
</org.acme.old.Foo>

0 comments on commit 0768361

Please sign in to comment.