Skip to content

Commit

Permalink
Clean up file parameter tests (#9260)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored May 12, 2024
1 parent 144e6b1 commit ad571f8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 75 deletions.
7 changes: 0 additions & 7 deletions core/src/main/java/hudson/model/FileParameterValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import jenkins.util.SystemProperties;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemHeaders;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.util.FileItemHeadersImpl;
import org.apache.commons.io.FilenameUtils;
import org.kohsuke.accmod.Restricted;
Expand All @@ -55,12 +54,6 @@
/**
* {@link ParameterValue} for {@link FileParameterDefinition}.
*
* <h2>Persistence</h2>
* <p>
* {@link DiskFileItem} is persistable via serialization,
* (although the data may get very large in XML) so this object
* as a whole is persistable.
*
* @author Kohsuke Kawaguchi
*/
public class FileParameterValue extends ParameterValue {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package hudson.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import hudson.Functions;
import hudson.tasks.BatchFile;
import hudson.tasks.Shell;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlInput;
import org.htmlunit.html.HtmlPage;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsSessionRule;

public class FileParameterValuePersistenceTest {

private static final String FILENAME = "file.txt";
private static final String CONTENTS = "foobar";

@Rule
public JenkinsSessionRule sessions = new JenkinsSessionRule();

@Rule
public TemporaryFolder tmp = new TemporaryFolder();

@Issue("JENKINS-13536")
@Test
public void fileParameterValuePersistence() throws Throwable {
sessions.then(j -> {
FreeStyleProject p = j.createFreeStyleProject("p");
p.addProperty(new ParametersDefinitionProperty(new FileParameterDefinition(FILENAME, "The file.")));
p.getBuildersList().add(Functions.isWindows() ? new BatchFile("type " + FILENAME) : new Shell("cat " + FILENAME));
File test = tmp.newFile();
Files.writeString(test.toPath(), CONTENTS, StandardCharsets.UTF_8);
try (JenkinsRule.WebClient wc = j.createWebClient()) {
// ParametersDefinitionProperty/index.jelly sends a 405
wc.setThrowExceptionOnFailingStatusCode(false);
HtmlPage page = wc.goTo("job/" + p.getName() + "/build?delay=0sec");
assertEquals(405, page.getWebResponse().getStatusCode());
HtmlForm form = page.getFormByName("parameters");
HtmlInput input = form.getInputByName("file");
input.setValue(test.getPath());
page = j.submit(form);
assertEquals(200, page.getWebResponse().getStatusCode());
}
FreeStyleBuild b;
while ((b = p.getLastBuild()) == null) {
Thread.sleep(100);
}
j.assertBuildStatusSuccess(j.waitForCompletion(b));
FileParameterValue fpv = (FileParameterValue) b.getAction(ParametersAction.class).getParameter(FILENAME);
fpv.getFile().delete();
verifyPersistence(j);
});
sessions.then(FileParameterValuePersistenceTest::verifyPersistence);
}

private static void verifyPersistence(JenkinsRule j) throws Throwable {
FreeStyleProject p = j.jenkins.getItemByFullName("p", FreeStyleProject.class);
FreeStyleBuild b = p.getLastBuild();
j.assertLogContains(CONTENTS, b);
Path saved = b.getRootDir().toPath().resolve("fileParameters").resolve(FILENAME);
assertTrue(Files.isRegularFile(saved));
assertEquals(CONTENTS, Files.readString(saved, StandardCharsets.UTF_8));
assertTrue(b.getWorkspace().child(FILENAME).exists());
try (JenkinsRule.WebClient wc = j.createWebClient()) {
HtmlPage page = wc.goTo(p.getUrl() + "ws");
assertThat(page.getWebResponse().getContentAsString(), containsString(FILENAME));
}
}
}
68 changes: 0 additions & 68 deletions test/src/test/java/hudson/model/QueueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
import hudson.triggers.SCMTrigger.SCMTriggerCause;
import hudson.triggers.TimerTrigger.TimerTriggerCause;
import hudson.util.OneShotEvent;
import hudson.util.XStream2;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
Expand All @@ -103,30 +102,17 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jenkins.model.BlockedBecauseOfBuildInProgress;
import jenkins.model.Jenkins;
import jenkins.model.queue.QueueIdStrategy;
import jenkins.security.QueueItemAuthenticatorConfiguration;
import org.acegisecurity.acls.sid.PrincipalSid;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.htmlunit.HttpMethod;
import org.htmlunit.Page;
import org.htmlunit.ScriptResult;
import org.htmlunit.WebRequest;
import org.htmlunit.html.DomElement;
import org.htmlunit.html.DomNode;
import org.htmlunit.html.HtmlFileInput;
import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlFormUtil;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.xml.XmlPage;
import org.junit.Assert;
Expand Down Expand Up @@ -294,60 +280,6 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
r.assertBuildStatusSuccess(r.waitForCompletion(b1));
}

public static final class FileItemPersistenceTestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
resp.getWriter().println(
"<html><body><form action='/' method=post name=main enctype='multipart/form-data'>" +
"<input type=file name=test><input type=submit>" +
"</form></body></html>"
);
}

@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
try {
ServletFileUpload f = new ServletFileUpload(new DiskFileItemFactory());
List<?> v = f.parseRequest(req);
assertEquals(1, v.size());
XStream2 xs = new XStream2();
System.out.println(xs.toXML(v.get(0)));
} catch (FileUploadException e) {
throw new ServletException(e);
}
}
}

@Test public void fileItemPersistence() throws Exception {
// TODO: write a synchronous connector?
byte[] testData = new byte[1024];
for (int i = 0; i < testData.length; i++) testData[i] = (byte) i;


Server server = new Server();
ServerConnector connector = new ServerConnector(server);
server.addConnector(connector);

ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(new ServletHolder(new FileItemPersistenceTestServlet()), "/");
server.setHandler(handler);

server.start();

try {
JenkinsRule.WebClient wc = r.createWebClient();
@SuppressWarnings("deprecation")
HtmlPage p = (HtmlPage) wc.getPage("http://localhost:" + connector.getLocalPort() + '/');
HtmlForm f = p.getFormByName("main");
HtmlFileInput input = f.getInputByName("test");
input.setData(testData);
HtmlFormUtil.submit(f);
} finally {
server.stop();
}
}

@Issue("JENKINS-33467")
@Test public void foldableCauseAction() throws Exception {
final OneShotEvent buildStarted = new OneShotEvent();
Expand Down

0 comments on commit ad571f8

Please sign in to comment.