ZIP: More Polishing

This commit is contained in:
Artem Bilan
2016-01-08 12:15:31 -05:00
parent 0a5fedcd7a
commit cd37ecae9a
12 changed files with 71 additions and 75 deletions

View File

@@ -26,7 +26,7 @@ The following input data types can be **compressed**:
* Iterable
In input data types can be mixed as part of an Iterable. E.g. you should be
easily be able to compress a collection containing Strings, byte arrays and Files.
easily able to compress a collection containing Strings, byte arrays and Files.
It is important to note that nested Iterables are *NOT SUPPORTED* at present time.
The zip transformer can be customized by setting several properties:

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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.
@@ -17,21 +17,17 @@ package org.springframework.integration.zip;
/**
* Zip adapter specific message headers.
*
* @author Gunnar Hillert
* @since 1.0
*/
public final class ZipHeaders {
public abstract class ZipHeaders {
private static final String PREFIX = "zip_";
public static final String PREFIX = "zip_";
public static final String ZIP_ENTRY_FILE_NAME = PREFIX + "entryFilename";
public static final String ZIP_ENTRY_PATH = PREFIX + "entryPath";
public static final String ZIP_ENTRY_LAST_MODIFIED_DATE = PREFIX + "entryLastModifiedDate";
/** Noninstantiable utility class */
private ZipHeaders() {
throw new AssertionError();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.zip.config.xml;
import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler;
@@ -26,9 +27,6 @@ import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHa
*/
public class ZipNamespaceHandler extends AbstractIntegrationNamespaceHandler {
/* (non-Javadoc)
* @see org.springframework.beans.factory.xml.NamespaceHandler#init()
*/
@Override
public void init() {
this.registerBeanDefinitionParser("zip-transformer", new ZipTransformerParser());

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.zip.transformer.splitter;
package org.springframework.integration.zip.splitter;
import java.util.ArrayList;
import java.util.List;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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.
@@ -37,10 +37,8 @@ import org.zeroturnaround.zip.ZipException;
/**
* Once the Spring Integration Zip support matures, we need to contribute the
* methods in this utility class back to the ZT Zip project.
*
* @author Gunnar Hillert
* @since 1.0
*
*/
public class SpringZipUtils {
@@ -60,18 +58,17 @@ public class SpringZipUtils {
return outputStream.toByteArray();
}
public static void pack(Collection<ZipEntrySource> entries, File zip,
int compressionLevel) {
public static void pack(Collection<ZipEntrySource> entries, File zip, int compressionLevel) {
if (logger.isDebugEnabled()) {
logger.debug("Creating '" + zip + "' from "
+ entries + ".");
logger.debug("Creating '" + zip + "' from " + entries + ".");
}
final FileOutputStream outputStream;
try {
outputStream = new FileOutputStream(zip);
} catch (FileNotFoundException e) {
}
catch (FileNotFoundException e) {
throw new IllegalStateException(String.format("File '%s' not found.", zip.getAbsolutePath()), e);
}
@@ -90,9 +87,11 @@ public class SpringZipUtils {
for (ZipEntrySource entry : entries) {
addEntry(entry, out);
}
} catch (IOException e) {
}
catch (IOException e) {
throw rethrow(e);
} finally {
}
finally {
IOUtils.closeQuietly(out);
}
@@ -105,7 +104,8 @@ public class SpringZipUtils {
if (in != null) {
try {
IOUtils.copy(in, out);
} finally {
}
finally {
IOUtils.closeQuietly(in);
}
}
@@ -120,7 +120,8 @@ public class SpringZipUtils {
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try {
IOUtils.copy(in, out);
} finally {
}
finally {
IOUtils.closeQuietly(out);
}
}
@@ -134,16 +135,13 @@ public class SpringZipUtils {
try {
zipfile = new ZipFile(file);
return true;
} catch (IOException e) {
}
catch (IOException e) {
return false;
} finally {
try {
if (zipfile != null) {
zipfile.close();
zipfile = null;
}
} catch (IOException e) {
}
}
finally {
IOUtils.closeQuietly(zipfile);
}
}
}

View File

@@ -12,10 +12,10 @@
schemaLocation="http://www.springframework.org/schema/integration/spring-integration.xsd" />
<xsd:annotation>
<xsd:documentation><![CDATA[
<xsd:documentation>
Defines the configuration elements for the Spring Integration
Zip Adapter.
]]></xsd:documentation>
</xsd:documentation>
</xsd:annotation>
<xsd:element name="zip-transformer">
@@ -74,25 +74,27 @@
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[Identifies the underlying Spring bean definition (EventDrivenConsumer)]]></xsd:documentation>
<xsd:documentation>
Identifies the underlying Spring bean definition (EventDrivenConsumer)
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attributeGroup ref="integration:inputOutputChannelGroup"/>
<xsd:attribute name="delete-files" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
<![CDATA[If the payload is an instance of {@link File}, this
If the payload is an instance of {@link File}, this
attribute specifies whether to delete the {@link File} after
transformation. The default is 'false'.]]>
transformation. The default is 'false'.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="expect-single-result" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
<![CDATA[If set to 'true', this property indicates
If set to 'true', this property indicates
that only one result object shall be returned as a result
from the executed Unzip operation. Defaults to 'false'.]]>
from the executed Unzip operation. Defaults to 'false'.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>

View File

@@ -15,8 +15,8 @@
<int:chain input-channel="input" output-channel="out">
<int-zip:unzip-transformer result-type="BYTE_ARRAY"/>
<int:splitter method="splitUnzippedMap">
<bean class="org.springframework.integration.zip.transformer.splitter.UnZipResultSplitter"/>
<int:splitter>
<bean class="org.springframework.integration.zip.splitter.UnZipResultSplitter"/>
</int:splitter>
</int:chain>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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.
@@ -25,10 +25,10 @@ import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -60,29 +60,28 @@ public class UnZip2FileTests {
@Before
public void setup() throws IOException {
this.workDir = testFolder.newFolder();
properties.put("workDir", workDir);
this.workDir = this.testFolder.newFolder();
properties.put("workDir", this.workDir);
System.out.print(this.workDir.getAbsolutePath());
context = new AnnotationConfigApplicationContext();
context.register(ContextConfiguration.class);
context.refresh();
input = context.getBean("input", MessageChannel.class);
resourceLoader = context;
this.context = new AnnotationConfigApplicationContext();
this.context.register(ContextConfiguration.class);
this.context.refresh();
this.input = this.context.getBean("input", MessageChannel.class);
this.resourceLoader = this.context;
}
@After
public void cleanup() {
if (context != null) {
context.close();
if (this.context != null) {
this.context.close();
}
}
@Test
@Ignore
public void unZipWithOneEntry() throws Exception {
final Resource resource = resourceLoader.getResource("classpath:testzipdata/single.zip");
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/single.zip");
final InputStream is = resource.getInputStream();
byte[] zipdata = IOUtils.toByteArray(is);
@@ -157,4 +156,5 @@ public class UnZip2FileTests {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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. You may obtain a copy of the License at
@@ -10,6 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.zip.config.xml;
import static org.junit.Assert.assertEquals;
@@ -45,7 +46,7 @@ public class UnZipTransformerParserTests {
private ConfigurableApplicationContext context;
@Test
public void testUnZiptransformerParserWithDefaults() {
public void testUnZipTransformerParserWithDefaults() {
setUp("UnZipTransformerParserTests.xml", getClass());
@@ -86,7 +87,7 @@ public class UnZipTransformerParserTests {
}
@Test
public void testUnZiptransformerParserWithExplicitSettings() {
public void testUnZipTransformerParserWithExplicitSettings() {
setUp("UnZipTransformerParserTests.xml", getClass());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 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. You may obtain a copy of the License at
@@ -10,6 +10,7 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.integration.zip.config.xml;
import static org.hamcrest.Matchers.containsString;
@@ -49,7 +50,7 @@ public class ZipTransformerParserTests {
private ConfigurableApplicationContext context;
@Test
public void testZiptransformerParserWithDefaults() {
public void testZipTransformerParserWithDefaults() {
setUp("ZipTransformerParserTests.xml", getClass());
@@ -90,7 +91,7 @@ public class ZipTransformerParserTests {
}
@Test
public void testZiptransformerParserWithExplicitSettings() {
public void testZipTransformerParserWithExplicitSettings() {
setUp("ZipTransformerParserTests.xml", getClass());

View File

@@ -73,7 +73,7 @@ public class UnZipTransformerTests {
@Test
public void unzipSingleFileAsInputStreamToByteArray() throws IOException {
final Resource resource = resourceLoader.getResource("classpath:testzipdata/single.zip");
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/single.zip");
final InputStream is = resource.getInputStream();
final Message<InputStream> message = MessageBuilder.withPayload(is).build();
@@ -103,7 +103,7 @@ public class UnZipTransformerTests {
@Test
public void unzipSingleFileToByteArray() throws IOException {
final Resource resource = resourceLoader.getResource("classpath:testzipdata/single.zip");
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/single.zip");
final InputStream is = resource.getInputStream();
final File inputFile = new File(this.workDir, "unzipSingleFileToByteArray");
@@ -138,7 +138,7 @@ public class UnZipTransformerTests {
@Test
public void unzipSingleFileToByteArrayWithDeleteFilesTrue() throws IOException {
final Resource resource = resourceLoader.getResource("classpath:testzipdata/single.zip");
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/single.zip");
final InputStream is = resource.getInputStream();
final File inputFile = new File(this.workDir, "unzipSingleFileToByteArray");
@@ -177,7 +177,7 @@ public class UnZipTransformerTests {
@Test
public void unzipMultipleFilesAsInputStreamToByteArray() throws IOException {
final Resource resource = resourceLoader.getResource("classpath:testzipdata/countries.zip");
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/countries.zip");
final InputStream is = resource.getInputStream();
final Message<InputStream> message = MessageBuilder.withPayload(is).build();
@@ -205,9 +205,9 @@ public class UnZipTransformerTests {
* @throws IOException
*/
@Test
public void unzipMultipleFilesAsInputstreamWithExpectSingleResultTrue() throws IOException {
public void unzipMultipleFilesAsInputStreamWithExpectSingleResultTrue() throws IOException {
final Resource resource = resourceLoader.getResource("classpath:testzipdata/countries.zip");
final Resource resource = this.resourceLoader.getResource("classpath:testzipdata/countries.zip");
final InputStream is = resource.getInputStream();
final Message<InputStream> message = MessageBuilder.withPayload(is).build();
@@ -233,7 +233,7 @@ public class UnZipTransformerTests {
@Test
public void unzipInvalidZipFile() throws IOException, InterruptedException {
File fileToUnzip = testFolder.newFile();
File fileToUnzip = this.testFolder.newFile();
FileUtils.writeStringToFile(fileToUnzip, "hello world");
UnZipTransformer unZipTransformer = new UnZipTransformer();

View File

@@ -148,7 +148,8 @@ public class ZipTransformerTests {
Assert.assertTrue(file.isFile());
//See http://stackoverflow.com/questions/3725662/what-is-the-earliest-timestamp-value-that-is-supported-in-zip-file-format
Assert.assertTrue(String.format("%s : %s", fileDate.getTime() - 4000, file.lastModified()), (fileDate.getTime() - 4000) < file.lastModified());
Assert.assertTrue(String.format("%s : %s", fileDate.getTime() - 4000, file.lastModified()),
(fileDate.getTime() - 4000) < file.lastModified());
Assert.assertTrue((fileDate.getTime() + 4000) > file.lastModified());
Assert.assertTrue(
@@ -241,8 +242,7 @@ public class ZipTransformerTests {
zipTransformer.setBeanFactory(mock(BeanFactory.class));
zipTransformer.afterPropertiesSet();
final Message<Collection<File>> message = MessageBuilder.withPayload(files)
.build();
final Message<Collection<File>> message = MessageBuilder.withPayload(files).build();
final Message<?> result = zipTransformer.transform(message);
@@ -259,7 +259,7 @@ public class ZipTransformerTests {
private File createTestFile(int size) throws IOException {
final File temporaryTestDirectory = testFolder.newFolder();
final File temporaryTestDirectory = this.testFolder.newFolder();
final File testFile = new File(temporaryTestDirectory, "testdata" + UUID.randomUUID().toString() + ".data");