提交 07683617 编写于 作者: S Scott Hebert 提交者: Oleg Nenashev

[JENKINS-42728] Updating view with CLI using input of a different view 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
上级 d45576d8
......@@ -1186,12 +1186,18 @@ public abstract class View extends AbstractModelObject implements AccessControll
}
// 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.MatcherAssert.assertThat;
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 @@ 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"));
......@@ -98,4 +132,5 @@ public class UpdateViewCommandTest {
assertThat(result, hasNoStandardOutput());
assertThat(result.stderr(), containsString("ERROR: No view named not_created inside view Jenkins"));
}
}
<?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>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册