diff --git a/jaxrs-tck/src/main/java/ee/jakarta/tck/ws/rs/jaxrs31/ee/multipart/MultipartSupportIT.java b/jaxrs-tck/src/main/java/ee/jakarta/tck/ws/rs/jaxrs31/ee/multipart/MultipartSupportIT.java index 2029a8d9..46c12e81 100644 --- a/jaxrs-tck/src/main/java/ee/jakarta/tck/ws/rs/jaxrs31/ee/multipart/MultipartSupportIT.java +++ b/jaxrs-tck/src/main/java/ee/jakarta/tck/ws/rs/jaxrs31/ee/multipart/MultipartSupportIT.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Supplier; import jakarta.ws.rs.ApplicationPath; import jakarta.ws.rs.Consumes; @@ -136,7 +137,7 @@ public void basicTest() throws Exception { .request(MediaType.MULTIPART_FORM_DATA_TYPE) .post(Entity.entity(new GenericEntity<>(multipart) { }, MediaType.MULTIPART_FORM_DATA))) { - Assertions.assertEquals(Response.Status.OK, response.getStatusInfo()); + Assertions.assertEquals(200, response.getStatus()); final List entityParts = response.readEntity(new GenericType<>() { }); if (entityParts.size() != 3) { @@ -151,8 +152,23 @@ public void basicTest() throws Exception { Assertions.assertNotNull(part, getMessage(entityParts)); Assertions.assertEquals("test string", part.getContent(String.class)); + // The javadoc for EntityPart.getContent(Class type) states: + // "Subsequent invocations will result in an {@code IllegalStateException}. + // Likewise this method will throw an {@code IllegalStateException} if it is + // called after calling {@link #getContent} or {@link #getContent(GenericType)}. + try { + part.getContent(String.class); + Assertions.fail("IllegalStateException is expected when getContent() is " + + "invoked more than once."); + } catch (IllegalStateException e) { + // expected exception + } catch (Throwable t) { + Assertions.fail("Incorrect Throwable received: " + t); + } + part = find(entityParts, "received-file"); Assertions.assertNotNull(part, getMessage(entityParts)); + Assertions.assertEquals("test file", part.getFileName().get()); Assertions.assertTrue(part.getContent(String.class).contains("value6")); part = find(entityParts, "added-input-stream"); @@ -187,11 +203,11 @@ public void singleFormParamTest() throws Exception { .mediaType(MediaType.TEXT_PLAIN_TYPE) .build()); try ( - Response response = client.target(createCombinedUri(uri, "test//single-form-param")) + Response response = client.target(createCombinedUri(uri, "test/single-form-param")) .request(MediaType.MULTIPART_FORM_DATA_TYPE) .post(Entity.entity(new GenericEntity<>(multipart) { }, MediaType.MULTIPART_FORM_DATA))) { - Assertions.assertEquals(Response.Status.OK, response.getStatusInfo()); + Assertions.assertEquals(200, response.getStatus()); final List entityParts = response.readEntity(new GenericType<>() { }); if (entityParts.size() != 3) { @@ -240,7 +256,7 @@ public void multiFormParamTest() throws Exception { .request(MediaType.MULTIPART_FORM_DATA_TYPE) .post(Entity.entity(new GenericEntity<>(multipart) { }, MediaType.MULTIPART_FORM_DATA))) { - Assertions.assertEquals(Response.Status.OK, response.getStatusInfo()); + Assertions.assertEquals(200, response.getStatus()); final List entityParts = response.readEntity(new GenericType<>() { }); if (entityParts.size() != 3) { @@ -266,30 +282,36 @@ private static void verifyEntityPart(final List parts, final String Assertions.assertEquals(text, part.getContent(String.class)); } - private static String getMessage(final List parts) throws IOException { - final StringBuilder msg = new StringBuilder(); - final Iterator iter = parts.iterator(); - while (iter.hasNext()) { - final EntityPart part = iter.next(); - msg.append('[') - .append(part.getName()) - .append("={") - .append("headers=") - .append(part.getHeaders()) - .append(", mediaType=") - .append(part.getMediaType()) - .append(", body=") - .append(toString(part.getContent())) - .append('}'); - if (iter.hasNext()) { - msg.append("], "); - } else { - msg.append(']'); + private static Supplier getMessage(final List parts) { + return () -> { + final StringBuilder msg = new StringBuilder(); + final Iterator iter = parts.iterator(); + while (iter.hasNext()) { + final EntityPart part = iter.next(); + try { + msg.append('[') + .append(part.getName()) + .append("={") + .append("headers=") + .append(part.getHeaders()) + .append(", mediaType=") + .append(part.getMediaType()) + .append(", body=") + .append(toString(part.getContent())) + .append('}'); + } catch (IOException e) { + Assertions.fail("Unable to proces Entityparts." + e); + } + if (iter.hasNext()) { + msg.append("], "); + } else { + msg.append(']'); + } } - } - return msg.toString(); + return msg.toString(); + }; } - + private static String toString(final InputStream in) throws IOException { // try-with-resources fails here due to a bug in the //noinspection TryFinallyCanBeTryWithResources @@ -360,7 +382,7 @@ public Response basicTest(final List parts) throws IOException { .mediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE) .build(), EntityPart.withName("received-file") - .content(find(parts,"file")) + .content(find(parts,"file").getFileName().get(),find(parts,"file").getContent()) .mediaType(MediaType.APPLICATION_XML) .build(), EntityPart.withName("added-input-stream")