This is an automated email from the ASF dual-hosted git repository.
apupier pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 94f914df0b39 CAMEL-27642: Fix flaky ZipFileDataFormatTest by comparing
zip content instead of raw bytes
94f914df0b39 is described below
commit 94f914df0b3955180026c8955031546db09bb17f
Author: Aurélien Pupier <[email protected]>
AuthorDate: Fri Nov 27 14:00:37 2026 +0100
CAMEL-27642: Fix flaky ZipFileDataFormatTest by comparing zip content
instead of raw bytes
The tests built an expected zip with getZippedText() and compared it byte
for byte with the zip produced by the route. A zip entry stores its last
modified time in the DOS format, which has a resolution of 2 seconds. When
the expected zip and the actual one are created in different 2 second
slots (more likely on loaded CI machines with parallel builds), the time
field differs (byte index 10 of the local file header, always off by one)
and the assertion fails.
The tests using expectedBodiesReceived(byte[]) failed for the same
reason: MockEndpoint compares arrays with Objects.deepEquals, so the
different entry time made the bodies differ (the [B@... in the failure
message is only the toString() of the arrays).
Compare the entries (name and content) of the zips with a new
assertZipEquals() helper instead:
- testZipAndStreamCaching, testZipWithoutFileName,
testZipToFileWithoutFileName and testZipToFileWithFileName used
assertArrayEquals
- testZipWithFileName, testZipWithPathElements,
testZipWithPreservedPathElements and testDslZip used
expectedBodiesReceived, they now expect one message and assert the
body after the mock is satisfied
Verified by inserting a 2.5 seconds sleep between the creation of the
expected zip and the actual one: the original test then fails in the 8
tests above (same errors as reported in the ticket), the fixed test
passes.
Co-authored-by: IBM Bob 2.2.1
---
.../dataformat/zipfile/ZipFileDataFormatTest.java | 62 +++++++++++++++++-----
1 file changed, 49 insertions(+), 13 deletions(-)
diff --git
a/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileDataFormatTest.java
b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileDataFormatTest.java
index cb44eae17aef..ca629b82f16e 100644
---
a/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileDataFormatTest.java
+++
b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileDataFormatTest.java
@@ -27,10 +27,13 @@ import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
+import java.util.ArrayList;
import java.util.Iterator;
+import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.camel.CamelExecutionException;
@@ -47,7 +50,6 @@ import org.junit.jupiter.api.Test;
import static org.apache.camel.Exchange.FILE_NAME;
import static org.apache.camel.test.junit6.TestSupport.assertIsInstanceOf;
import static org.apache.camel.test.junit6.TestSupport.deleteDirectory;
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -85,7 +87,8 @@ public class ZipFileDataFormatTest extends CamelTestSupport {
Exchange exchange = mock.getReceivedExchanges().get(0);
assertEquals(exchange.getIn().getMessageId() + ".zip",
exchange.getIn().getHeader(FILE_NAME));
assertIsInstanceOf(InputStreamCache.class, exchange.getIn().getBody());
- assertArrayEquals(getZippedText(exchange.getIn().getMessageId()),
exchange.getIn().getMandatoryBody(byte[].class));
+ assertZipEquals(getZippedText(exchange.getIn().getMessageId()),
exchange.getIn().getMandatoryBody(byte[].class),
+ "Get a wrong message content.");
}
@Test
@@ -99,39 +102,49 @@ public class ZipFileDataFormatTest extends
CamelTestSupport {
Exchange exchange = mock.getReceivedExchanges().get(0);
assertEquals(exchange.getIn().getMessageId() + ".zip",
exchange.getIn().getHeader(FILE_NAME));
- assertArrayEquals(getZippedText(exchange.getIn().getMessageId()),
exchange.getIn().getBody(byte[].class));
+ assertZipEquals(getZippedText(exchange.getIn().getMessageId()),
exchange.getIn().getBody(byte[].class),
+ "Get a wrong message content.");
}
@Test
public void testZipWithFileName() throws Exception {
-
getMockEndpoint("mock:zip").expectedBodiesReceived(getZippedText("poem.txt"));
- getMockEndpoint("mock:zip").expectedHeaderReceived(FILE_NAME,
"poem.txt.zip");
+ MockEndpoint mock = getMockEndpoint("mock:zip");
+ mock.expectedMessageCount(1);
+ mock.expectedHeaderReceived(FILE_NAME, "poem.txt.zip");
template.sendBodyAndHeader("direct:zip", TEXT, FILE_NAME, "poem.txt");
MockEndpoint.assertIsSatisfied(context);
+ assertZipEquals(getZippedText("poem.txt"),
mock.getReceivedExchanges().get(0).getIn().getBody(byte[].class),
+ "Get a wrong message content.");
}
@Test
public void testZipWithPathElements() throws Exception {
-
getMockEndpoint("mock:zip").expectedBodiesReceived(getZippedText("poem.txt"));
- getMockEndpoint("mock:zip").expectedHeaderReceived(FILE_NAME,
"poem.txt.zip");
+ MockEndpoint mock = getMockEndpoint("mock:zip");
+ mock.expectedMessageCount(1);
+ mock.expectedHeaderReceived(FILE_NAME, "poem.txt.zip");
template.sendBodyAndHeader("direct:zip", TEXT, FILE_NAME,
"poems/poem.txt");
MockEndpoint.assertIsSatisfied(context);
+ assertZipEquals(getZippedText("poem.txt"),
mock.getReceivedExchanges().get(0).getIn().getBody(byte[].class),
+ "Get a wrong message content.");
}
@Test
public void testZipWithPreservedPathElements() throws Exception {
zip.setPreservePathElements(true);
-
getMockEndpoint("mock:zip").expectedBodiesReceived(getZippedTextInFolder("poems/",
"poems/poem.txt"));
- getMockEndpoint("mock:zip").expectedHeaderReceived(FILE_NAME,
"poem.txt.zip");
+ MockEndpoint mock = getMockEndpoint("mock:zip");
+ mock.expectedMessageCount(1);
+ mock.expectedHeaderReceived(FILE_NAME, "poem.txt.zip");
template.sendBodyAndHeader("direct:zip", TEXT, FILE_NAME,
"poems/poem.txt");
MockEndpoint.assertIsSatisfied(context);
+ assertZipEquals(getZippedTextInFolder("poems/", "poems/poem.txt"),
+
mock.getReceivedExchanges().get(0).getIn().getBody(byte[].class), "Get a wrong
message content.");
}
@Test
@@ -247,7 +260,7 @@ public class ZipFileDataFormatTest extends CamelTestSupport
{
Exchange exchange = mock.getReceivedExchanges().get(0);
File file = new File(TEST_DIR, exchange.getIn().getMessageId() +
".zip");
assertTrue(file.exists(), "The file should exist.");
- assertArrayEquals(getZippedText(exchange.getIn().getMessageId()),
getBytes(file), "Get a wrong message content.");
+ assertZipEquals(getZippedText(exchange.getIn().getMessageId()),
getBytes(file), "Get a wrong message content.");
}
@Test
@@ -269,17 +282,20 @@ public class ZipFileDataFormatTest extends
CamelTestSupport {
assertTrue(notify.matches(5, TimeUnit.SECONDS), "The exchange is not
done in time.");
assertTrue(file.exists(), "The file should exist.");
- assertArrayEquals(getZippedText("poem.txt"), getBytes(file), "Get a
wrong message content.");
+ assertZipEquals(getZippedText("poem.txt"), getBytes(file), "Get a
wrong message content.");
}
@Test
public void testDslZip() throws Exception {
-
getMockEndpoint("mock:dslZip").expectedBodiesReceived(getZippedText("poem.txt"));
- getMockEndpoint("mock:dslZip").expectedHeaderReceived(FILE_NAME,
"poem.txt.zip");
+ MockEndpoint mock = getMockEndpoint("mock:dslZip");
+ mock.expectedMessageCount(1);
+ mock.expectedHeaderReceived(FILE_NAME, "poem.txt.zip");
template.sendBodyAndHeader("direct:dslZip", TEXT, FILE_NAME,
"poem.txt");
MockEndpoint.assertIsSatisfied(context);
+ assertZipEquals(getZippedText("poem.txt"),
mock.getReceivedExchanges().get(0).getIn().getBody(byte[].class),
+ "Get a wrong message content.");
}
@Test
@@ -416,6 +432,26 @@ public class ZipFileDataFormatTest extends
CamelTestSupport {
};
}
+ /**
+ * Compares the entries (name and content) of two zip archives, and not
their bytes, as the bytes contain the last
+ * modified time of the entries (with a resolution of 2 seconds) which
differs when the expected zip is created in
+ * another time slot than the actual one.
+ */
+ private static void assertZipEquals(byte[] expected, byte[] actual, String
message) throws IOException {
+ assertEquals(describeZip(expected), describeZip(actual), message);
+ }
+
+ private static List<String> describeZip(byte[] zipped) throws IOException {
+ List<String> entries = new ArrayList<>();
+ try (ZipInputStream zis = new ZipInputStream(new
ByteArrayInputStream(zipped))) {
+ ZipEntry entry;
+ while ((entry = zis.getNextEntry()) != null) {
+ entries.add(entry.getName() + "=" + new
String(zis.readAllBytes(), StandardCharsets.UTF_8));
+ }
+ }
+ return entries;
+ }
+
private static byte[] getZippedText(String entryName) throws IOException {
ByteArrayInputStream bais = new
ByteArrayInputStream(TEXT.getBytes("UTF-8"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();