From f5245eb7d765841e0ac4e77f6738719125e1a75d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingo=20D=C3=BCppe?= Date: Sun, 16 Jun 2019 22:51:31 +0200 Subject: [PATCH] GH-215: Fix for flat zip structure Fixes https://github.com/spring-projects/spring-integration-extensions/issues/215 * Add test of unzipping flat entry zip * Changed indent from key to tap * Fix formatting * Enhance author information * Add extra test files * Polishing code style --- .../zip/transformer/UnZipTransformer.java | 21 ++++-- .../transformer/UnZipTransformerTests.java | 70 ++++++++---------- .../src/test/resources/testzipdata/flat.txt | 1 + .../resources/testzipdata/flatfileentry.zip | Bin 0 -> 213 bytes .../src/test/resources/testzipdata/single.txt | 1 + 5 files changed, 45 insertions(+), 48 deletions(-) create mode 100644 spring-integration-zip/src/test/resources/testzipdata/flat.txt create mode 100644 spring-integration-zip/src/test/resources/testzipdata/flatfileentry.zip create mode 100644 spring-integration-zip/src/test/resources/testzipdata/single.txt diff --git a/spring-integration-zip/src/main/java/org/springframework/integration/zip/transformer/UnZipTransformer.java b/spring-integration-zip/src/main/java/org/springframework/integration/zip/transformer/UnZipTransformer.java index 50f3152..a82570b 100644 --- a/spring-integration-zip/src/main/java/org/springframework/integration/zip/transformer/UnZipTransformer.java +++ b/spring-integration-zip/src/main/java/org/springframework/integration/zip/transformer/UnZipTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2018 the original author or authors. + * Copyright 2015-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,7 @@ import org.springframework.messaging.MessagingException; * * @author Gunnar Hillert * @author Artem Bilan + * @author Ingo Dueppe * * @since 1.0 * @@ -58,24 +59,19 @@ public class UnZipTransformer extends AbstractZipTransformer { * a result from the executed Unzip operation. If set to true and * more than 1 element is returned, then that * 1 element is extracted and returned as payload. - * * If the result map contains more than 1 element and * {@link #expectSingleResult} is true, then a * {@link MessagingException} is thrown. - * * If set to false, the complete result list is returned as the * payload. This is the {@code default}. - * * @param expectSingleResult If not set explicitly, will default to false - * */ public void setExpectSingleResult(boolean expectSingleResult) { this.expectSingleResult = expectSingleResult; } @Override - protected Object doZipTransform(final Message message) throws Exception { - + protected Object doZipTransform(final Message message) { try { final Object payload = message.getPayload(); final Object unzippedData; @@ -135,6 +131,7 @@ public class UnZipTransformer extends AbstractZipTransformer { destinationFile.mkdirs(); //NOSONAR false positive } else { + mkDirOfAncestorDirectories(destinationFile); SpringZipUtils.copy(zipEntryInputStream, destinationFile); uncompressedData.put(zipEntryName, destinationFile); } @@ -182,7 +179,8 @@ public class UnZipTransformer extends AbstractZipTransformer { else { throw new MessagingException(message, String.format("The UnZip operation extracted %s " - + "result objects but expectSingleResult was 'true'.", uncompressedData.size())); + + "result objects but expectSingleResult was 'true'.", uncompressedData + .size())); } } else { @@ -209,4 +207,11 @@ public class UnZipTransformer extends AbstractZipTransformer { } } + private static void mkDirOfAncestorDirectories(File destinationFile) { + File parentDirectory = destinationFile.getParentFile(); + if (parentDirectory != null) { + parentDirectory.mkdirs(); + } + } + } diff --git a/spring-integration-zip/src/test/java/org/springframework/integration/zip/transformer/UnZipTransformerTests.java b/spring-integration-zip/src/test/java/org/springframework/integration/zip/transformer/UnZipTransformerTests.java index c48eaaa..00a8876 100644 --- a/spring-integration-zip/src/test/java/org/springframework/integration/zip/transformer/UnZipTransformerTests.java +++ b/spring-integration-zip/src/test/java/org/springframework/integration/zip/transformer/UnZipTransformerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2018 the original author or authors. + * Copyright 2015-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,6 +50,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * * @author Gunnar Hillert * @author Artem Bilan + * @author Ingo Dueppe * * @since 1.0 * @@ -71,15 +72,30 @@ public class UnZipTransformerTests { this.workDir = testFolder.newFolder(); } - /** - * UnCompress a ZIP archive containing a single file only. The result will be - * a byte array. - * - * @throws IOException - */ + @Test + public void unzipFlatFileEntryZip() throws IOException { + final Resource zipResource = this.resourceLoader.getResource("classpath:testzipdata/flatfileentry.zip"); + final InputStream is = zipResource.getInputStream(); + + final Message message = MessageBuilder.withPayload(is).build(); + + final UnZipTransformer unZipTransformer = new UnZipTransformer(); + unZipTransformer.setZipResultType(ZipResultType.FILE); + unZipTransformer.afterPropertiesSet(); + + final Message resultMessage = unZipTransformer.transform(message); + + Assert.assertNotNull(resultMessage); + + @SuppressWarnings("unchecked") + Map unzippedData = (Map) resultMessage.getPayload(); + + Assert.assertNotNull(unzippedData); + Assert.assertEquals(1, unzippedData.size()); + } + @Test public void unzipSingleFileAsInputStreamToByteArray() throws IOException { - final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/single.zip"); final InputStream is = resource.getInputStream(); @@ -97,16 +113,11 @@ public class UnZipTransformerTests { Map unzippedData = (Map) resultMessage.getPayload(); Assert.assertNotNull(unzippedData); - Assert.assertTrue(unzippedData.size() == 1); + Assert.assertEquals(1, unzippedData.size()); Assert.assertEquals("Spring Integration Rocks!", new String(unzippedData.values().iterator().next())); } - /** - * - * - * @throws IOException - */ @Test public void unzipSingleFileToByteArray() throws IOException { @@ -131,20 +142,14 @@ public class UnZipTransformerTests { Map unzippedData = (Map) resultMessage.getPayload(); Assert.assertNotNull(unzippedData); - Assert.assertTrue(unzippedData.size() == 1); + Assert.assertEquals(1, unzippedData.size()); Assert.assertTrue(inputFile.exists()); Assert.assertEquals("Spring Integration Rocks!", new String(unzippedData.values().iterator().next())); } - /** - * - * - * @throws IOException - */ @Test public void unzipSingleFileToByteArrayWithDeleteFilesTrue() throws IOException { - final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/single.zip"); final InputStream is = resource.getInputStream(); @@ -169,21 +174,14 @@ public class UnZipTransformerTests { Map unzippedData = (Map) resultMessage.getPayload(); Assert.assertNotNull(unzippedData); - Assert.assertTrue(unzippedData.size() == 1); + Assert.assertEquals(1, unzippedData.size()); Assert.assertFalse(inputFile.exists()); Assert.assertEquals("Spring Integration Rocks!", new String(unzippedData.values().iterator().next())); } - /** - * UnCompress a ZIP archive containing multiple files. The result will be - * a collection of files. - * - * @throws IOException - */ @Test public void unzipMultipleFilesAsInputStreamToByteArray() throws IOException { - final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/countries.zip"); final InputStream is = resource.getInputStream(); @@ -201,19 +199,11 @@ public class UnZipTransformerTests { Map unzippedData = (Map) resultMessage.getPayload(); Assert.assertNotNull(unzippedData); - Assert.assertTrue(unzippedData.size() == 5); - + Assert.assertEquals(5, unzippedData.size()); } - /** - * UnCompress a ZIP archive containing multiple files. The result will be - * a collection of files. - * - * @throws IOException - */ @Test public void unzipMultipleFilesAsInputStreamWithExpectSingleResultTrue() throws IOException { - final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/countries.zip"); final InputStream is = resource.getInputStream(); @@ -238,8 +228,7 @@ public class UnZipTransformerTests { } @Test - public void unzipInvalidZipFile() throws IOException, InterruptedException { - + public void unzipInvalidZipFile() throws IOException { File fileToUnzip = this.testFolder.newFile(); FileUtils.writeStringToFile(fileToUnzip, "hello world"); @@ -281,4 +270,5 @@ public class UnZipTransformerTests { containsString("is trying to leave the target output directory")); } } + } diff --git a/spring-integration-zip/src/test/resources/testzipdata/flat.txt b/spring-integration-zip/src/test/resources/testzipdata/flat.txt new file mode 100644 index 0000000..8758da1 --- /dev/null +++ b/spring-integration-zip/src/test/resources/testzipdata/flat.txt @@ -0,0 +1 @@ +Spring Integration Rocks! \ No newline at end of file diff --git a/spring-integration-zip/src/test/resources/testzipdata/flatfileentry.zip b/spring-integration-zip/src/test/resources/testzipdata/flatfileentry.zip new file mode 100644 index 0000000000000000000000000000000000000000..22d2d829e9699c14872b424e0931899c02ad055d GIT binary patch literal 213 zcmWIWW@Zs#;Nak3;MjJ_j{yk?F$ggfmnNm<=cJ?->8Ir+mgtpKl;oB2FkVn3T1g-d@MdHZVL(`i aEC;d<6%b>2fHx}}NEagz1_9|@un_=tAT4bG literal 0 HcmV?d00001 diff --git a/spring-integration-zip/src/test/resources/testzipdata/single.txt b/spring-integration-zip/src/test/resources/testzipdata/single.txt new file mode 100644 index 0000000..8758da1 --- /dev/null +++ b/spring-integration-zip/src/test/resources/testzipdata/single.txt @@ -0,0 +1 @@ +Spring Integration Rocks! \ No newline at end of file