From 3bb56b81e1bf586cd075f691f9981a2166fa7660 Mon Sep 17 00:00:00 2001 From: Wadeck Follonier Date: Fri, 1 Feb 2019 15:17:26 +0100 Subject: [PATCH] [JENKINS-53511] Bump of JTH to include new methods in WebClient - And thus, adapt the code due to the breaking changes in JTH sub-lib --- test-pom/pom.xml | 2 +- .../test/java/hudson/model/ComputerTest.java | 3 ++- .../src/test/java/hudson/model/ProjectTest.java | 4 +++- test/src/test/java/hudson/model/ViewTest.java | 2 +- .../tools/ZipExtractionInstallerTest.java | 3 +-- .../java/jenkins/bugs/Jenkins19124Test.java | 3 +++ .../security/RekeySecretAdminMonitorTest.java | 17 +++++++++++++---- test/src/test/java/lib/form/PasswordTest.java | 6 ++++-- 8 files changed, 28 insertions(+), 12 deletions(-) diff --git a/test-pom/pom.xml b/test-pom/pom.xml index dbbda4bf2a..375f3ee1e4 100644 --- a/test-pom/pom.xml +++ b/test-pom/pom.xml @@ -54,7 +54,7 @@ THE SOFTWARE. ${project.groupId} jenkins-test-harness - 2.41.1 + 2.42 test diff --git a/test/src/test/java/hudson/model/ComputerTest.java b/test/src/test/java/hudson/model/ComputerTest.java index 8502a89832..09119e0165 100644 --- a/test/src/test/java/hudson/model/ComputerTest.java +++ b/test/src/test/java/hudson/model/ComputerTest.java @@ -34,6 +34,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.xml.XmlPage; import java.io.File; +import java.nio.charset.StandardCharsets; import jenkins.model.Jenkins; import hudson.slaves.DumbSlave; @@ -109,7 +110,7 @@ public class ComputerTest { private void verifyOfflineCause(Computer computer) throws Exception { XmlPage page = j.createWebClient().goToXml("computer/" + computer.getName() + "/config.xml"); - String content = page.getWebResponse().getContentAsString("UTF-8"); + String content = page.getWebResponse().getContentAsString(StandardCharsets.UTF_8); assertThat(content, containsString("temporaryOfflineCause")); assertThat(content, containsString("username")); assertThat(content, not(containsString("ApiTokenProperty"))); diff --git a/test/src/test/java/hudson/model/ProjectTest.java b/test/src/test/java/hudson/model/ProjectTest.java index f966f4e56b..662807588d 100644 --- a/test/src/test/java/hudson/model/ProjectTest.java +++ b/test/src/test/java/hudson/model/ProjectTest.java @@ -25,6 +25,7 @@ package hudson.model; import com.gargoylesoftware.htmlunit.HttpMethod; import com.gargoylesoftware.htmlunit.WebRequest; +import com.gargoylesoftware.htmlunit.javascript.host.event.Event; import hudson.*; import hudson.model.queue.QueueTaskFuture; import hudson.security.AccessDeniedException2; @@ -252,7 +253,8 @@ public class ProjectTest { assertEquals("Scm retry count should be the same as global scm retry count.", 6, p.getScmCheckoutRetryCount()); HtmlForm form = j.createWebClient().goTo(p.getUrl() + "/configure").getFormByName("config"); ((HtmlElement)form.getByXPath("//div[@class='advancedLink']//button").get(0)).click(); - form.getInputByName("hasCustomScmCheckoutRetryCount").click(); + // required due to the new default behavior of click + form.getInputByName("hasCustomScmCheckoutRetryCount").click(new Event(), true); form.getInputByName("scmCheckoutRetryCount").setValueAttribute("7"); j.submit(form); assertEquals("Scm retry count was set.", 7, p.getScmCheckoutRetryCount()); diff --git a/test/src/test/java/hudson/model/ViewTest.java b/test/src/test/java/hudson/model/ViewTest.java index 3d73a15d6d..e7099a4100 100644 --- a/test/src/test/java/hudson/model/ViewTest.java +++ b/test/src/test/java/hudson/model/ViewTest.java @@ -246,7 +246,7 @@ public class ViewTest { ListView view = listView("v"); view.description = "one"; WebClient wc = j.createWebClient(); - String xml = wc.goToXml("view/v/config.xml").getContent(); + String xml = wc.goToXml("view/v/config.xml").getWebResponse().getContentAsString(); assertTrue(xml, xml.contains("one")); xml = xml.replace("one", "two"); WebRequest req = new WebRequest(wc.createCrumbedUrl("view/v/config.xml"), HttpMethod.POST); diff --git a/test/src/test/java/hudson/tools/ZipExtractionInstallerTest.java b/test/src/test/java/hudson/tools/ZipExtractionInstallerTest.java index e1e882d10f..7663efdc93 100644 --- a/test/src/test/java/hudson/tools/ZipExtractionInstallerTest.java +++ b/test/src/test/java/hudson/tools/ZipExtractionInstallerTest.java @@ -24,7 +24,6 @@ package hudson.tools; import com.gargoylesoftware.htmlunit.HttpMethod; -import com.gargoylesoftware.htmlunit.InteractivePage; import com.gargoylesoftware.htmlunit.WebRequest; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlTextInput; @@ -148,7 +147,7 @@ public class ZipExtractionInstallerTest { } @Override - public Object callFunction(InteractivePage page, Function function, Scriptable scope, Scriptable thisObject, Object[] args) { + public Object callFunction(HtmlPage page, Function function, Scriptable scope, Scriptable thisObject, Object[] args) { if (thisObject instanceof XMLHttpRequest) { try { WebRequest request = getPrivateWebRequestField((XMLHttpRequest) thisObject); diff --git a/test/src/test/java/jenkins/bugs/Jenkins19124Test.java b/test/src/test/java/jenkins/bugs/Jenkins19124Test.java index 4f976f85ba..4a66ffe7d1 100644 --- a/test/src/test/java/jenkins/bugs/Jenkins19124Test.java +++ b/test/src/test/java/jenkins/bugs/Jenkins19124Test.java @@ -41,7 +41,10 @@ public class Jenkins19124Test { JenkinsRule.WebClient wc = j.createWebClient(); HtmlPage c = wc.getPage(p, "configure"); HtmlTextInput alpha = c.getElementByName("_.alpha"); + // the fireEvent is required as setValueAttribute's new behavior is not triggering the onChange event anymore alpha.setValueAttribute("hello"); + alpha.fireEvent("change"); + WebClientUtil.waitForJSExec(wc); assertEquals("hello", d.alpha); assertEquals("2", d.bravo); diff --git a/test/src/test/java/jenkins/security/RekeySecretAdminMonitorTest.java b/test/src/test/java/jenkins/security/RekeySecretAdminMonitorTest.java index 413b4a6eed..5ce5f1b169 100644 --- a/test/src/test/java/jenkins/security/RekeySecretAdminMonitorTest.java +++ b/test/src/test/java/jenkins/security/RekeySecretAdminMonitorTest.java @@ -11,7 +11,6 @@ import hudson.Util; import hudson.util.Secret; import hudson.util.SecretHelper; import org.apache.commons.io.FileUtils; -import org.hamcrest.CoreMatchers; import org.jvnet.hudson.test.HudsonTestCase; import org.jvnet.hudson.test.recipes.Recipe.Runner; import org.xml.sax.SAXException; @@ -22,8 +21,7 @@ import java.io.File; import java.io.IOException; import java.lang.annotation.Annotation; import java.util.regex.Pattern; - -import static org.junit.Assert.assertThat; +import java.util.stream.Stream; /** * @author Kohsuke Kawaguchi @@ -132,7 +130,18 @@ public class RekeySecretAdminMonitorTest extends HudsonTestCase { } private HtmlButton getButton(HtmlForm form, int index) { - return form.getHtmlElementsByTagName("button").get(index); + // due to the removal of method HtmlElement.getHtmlElementsByTagName + Stream buttonStream = form.getElementsByTagName("button").stream() + .filter(HtmlButton.class::isInstance) + .map(HtmlButton.class::cast); + + if (index > 0) { + buttonStream = buttonStream.skip(index - 1); + } + + return buttonStream + .findFirst() + .orElse(null); } public void testScanOnBoot() throws Exception { diff --git a/test/src/test/java/lib/form/PasswordTest.java b/test/src/test/java/lib/form/PasswordTest.java index 316fdb20d3..198b7b5254 100644 --- a/test/src/test/java/lib/form/PasswordTest.java +++ b/test/src/test/java/lib/form/PasswordTest.java @@ -148,7 +148,7 @@ public class PasswordTest { ByteArrayOutputStream baos = new ByteArrayOutputStream(); String pName = p.getFullName(); getJobCommand.main(Collections.singletonList(pName), Locale.ENGLISH, System.in, new PrintStream(baos), System.err); - assertEquals(xmlAdmin, baos.toString(configXml.getWebResponse().getContentCharset())); + assertEquals(xmlAdmin, baos.toString(configXml.getWebResponse().getContentCharset().name())); CopyJobCommand copyJobCommand = new CopyJobCommand(); copyJobCommand.setTransportAuth(adminAuth); String pAdminName = pName + "-admin"; @@ -171,7 +171,7 @@ public class PasswordTest { getJobCommand.setTransportAuth(devAuth); baos = new ByteArrayOutputStream(); getJobCommand.main(Collections.singletonList(pName), Locale.ENGLISH, System.in, new PrintStream(baos), System.err); - assertEquals(xmlDev, baos.toString(configXml.getWebResponse().getContentCharset())); + assertEquals(xmlDev, baos.toString(configXml.getWebResponse().getContentCharset().name())); copyJobCommand = new CopyJobCommand(); copyJobCommand.setTransportAuth(devAuth); String pDevName = pName + "-dev"; @@ -194,7 +194,9 @@ public class PasswordTest { } VulnerableProperty.DescriptorImpl.incomingURL = null; String secret = "s3cr3t"; + // the fireEvent is required as setText's new behavior is not triggering the onChange event anymore field.setText(secret); + field.fireEvent("change"); while (VulnerableProperty.DescriptorImpl.incomingURL == null) { Thread.sleep(100); // form validation of edited value } -- GitLab