INTEXT-40 Add ZIP Transformer
* Add zip-transformer * Add unzip-transformer * Add UnZipResultSplitter * Add sample For reference see: https://jira.springsource.org/browse/INTEXT-40
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
|
||||
xmlns:int-file="http://www.springframework.org/schema/integration/file"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
|
||||
http://www.springframework.org/schema/integration/zip http://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<int:channel id="input"/>
|
||||
|
||||
<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>
|
||||
</int:chain>
|
||||
|
||||
<int:channel id="out">
|
||||
<int:interceptors>
|
||||
<int:wire-tap channel="logger"/>
|
||||
</int:interceptors>
|
||||
</int:channel>
|
||||
|
||||
<int:logging-channel-adapter id="logger" log-full-message="true" level="WARN"/>
|
||||
|
||||
<int-file:outbound-channel-adapter id="write-file" channel="out" directory-expression="'${workDir}/' + headers.zip_entryPath" auto-create-directory="true"/>
|
||||
|
||||
<context:property-placeholder properties-ref="properties"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright 2015 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
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on 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;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
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;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class UnZip2FileTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
private ResourceLoader resourceLoader;
|
||||
private MessageChannel input;
|
||||
|
||||
private static final Properties properties = new Properties();
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder testFolder = new TemporaryFolder();
|
||||
|
||||
private File workDir;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
this.workDir = testFolder.newFolder();
|
||||
properties.put("workDir", workDir);
|
||||
System.out.print(this.workDir.getAbsolutePath());
|
||||
|
||||
context = new AnnotationConfigApplicationContext();
|
||||
context.register(ContextConfiguration.class);
|
||||
context.refresh();
|
||||
input = context.getBean("input", MessageChannel.class);
|
||||
resourceLoader = context;
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
if (context != null) {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void unZipWithOneEntry() throws Exception {
|
||||
|
||||
final Resource resource = resourceLoader.getResource("classpath:testzipdata/single.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
byte[] zipdata = IOUtils.toByteArray(is);
|
||||
|
||||
final Message<byte[]> message = MessageBuilder.withPayload(zipdata).build();
|
||||
|
||||
input.send(message);
|
||||
|
||||
Assert.assertTrue(this.workDir.list().length == 1);
|
||||
|
||||
File fileInWorkDir = this.workDir.listFiles()[0];
|
||||
|
||||
Assert.assertTrue(fileInWorkDir.isFile());
|
||||
Assert.assertEquals("single.txt", fileInWorkDir.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unZipWithMultipleEntries() throws Exception {
|
||||
|
||||
final Resource resource = resourceLoader.getResource("classpath:testzipdata/countries.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
byte[] zipdata = IOUtils.toByteArray(is);
|
||||
|
||||
final Message<byte[]> message = MessageBuilder.withPayload(zipdata).build();
|
||||
|
||||
input.send(message);
|
||||
|
||||
Assert.assertTrue(this.workDir.list().length == 4);
|
||||
|
||||
File[] files = this.workDir.listFiles();
|
||||
|
||||
boolean continents = false;
|
||||
boolean de = false;
|
||||
boolean fr = false;
|
||||
boolean pl = false;
|
||||
|
||||
for (File file : files) {
|
||||
if (file.getName().equals("continents")) {
|
||||
continents = true;
|
||||
Assert.assertTrue(file.isDirectory());
|
||||
Assert.assertTrue(file.list().length == 2);
|
||||
}
|
||||
if (file.getName().equals("de.txt")) {
|
||||
de = true;
|
||||
Assert.assertTrue(file.isFile());
|
||||
}
|
||||
if (file.getName().equals("fr.txt")) {
|
||||
fr = true;
|
||||
Assert.assertTrue(file.isFile());
|
||||
}
|
||||
if (file.getName().equals("pl.txt")) {
|
||||
pl = true;
|
||||
Assert.assertTrue(file.isFile());
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertTrue(continents);
|
||||
Assert.assertTrue(de);
|
||||
Assert.assertTrue(fr);
|
||||
Assert.assertTrue(pl);
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ImportResource("classpath:org/springframework/integration/zip/UnZip2FileTests-context.xml")
|
||||
public static class ContextConfiguration {
|
||||
|
||||
@Bean
|
||||
Properties properties() throws IOException {
|
||||
return properties;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
|
||||
xmlns:int-file="http://www.springframework.org/schema/integration/file"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
|
||||
http://www.springframework.org/schema/integration/zip http://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
|
||||
|
||||
<int:channel id="input">
|
||||
</int:channel>
|
||||
|
||||
<int-zip:zip-transformer input-channel="input" output-channel="write-file" result-type="BYTE_ARRAY"/>
|
||||
<int-file:outbound-channel-adapter id="write-file" directory="${workDir}" auto-create-directory="true"/>
|
||||
|
||||
<context:property-placeholder properties-ref="properties"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright 2015 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
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on 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;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
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;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.integration.file.FileHeaders;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class Zip2FileTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
private MessageChannel input;
|
||||
|
||||
private static final Properties properties = new Properties();
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder testFolder = new TemporaryFolder();
|
||||
|
||||
private File workDir;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
this.workDir = testFolder.newFolder();
|
||||
properties.put("workDir", workDir);
|
||||
System.out.print(this.workDir.getAbsolutePath());
|
||||
|
||||
context = new AnnotationConfigApplicationContext();
|
||||
context.register(ContextConfiguration.class);
|
||||
context.refresh();
|
||||
input = context.getBean("input", MessageChannel.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
if (context != null) {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipStringWithDefaultFileName() throws FileNotFoundException, IOException, InterruptedException {
|
||||
|
||||
final Message<String> message = MessageBuilder.withPayload("Zip me up.").build();
|
||||
|
||||
input.send(message);
|
||||
|
||||
Assert.assertTrue(this.workDir.list().length == 1);
|
||||
|
||||
File fileInWorkDir = this.workDir.listFiles()[0];
|
||||
|
||||
Assert.assertTrue(fileInWorkDir.isFile());
|
||||
Assert.assertTrue(fileInWorkDir.getName().contains(message.getHeaders().getId().toString()));
|
||||
Assert.assertTrue("The created file should have a 'zip' file extension.", fileInWorkDir.getName().endsWith(".zip"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipStringWithExplicitFileName() throws FileNotFoundException, IOException, InterruptedException {
|
||||
input.send(MessageBuilder.withPayload("Zip me up.").setHeader(FileHeaders.FILENAME, "zipString.zip").build());
|
||||
|
||||
Assert.assertTrue(this.workDir.list().length == 1);
|
||||
Assert.assertEquals("zipString.zip", this.workDir.listFiles()[0].getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipBytesWithExplicitFileName() throws FileNotFoundException, IOException, InterruptedException {
|
||||
|
||||
input.send(MessageBuilder.withPayload("Zip me up.".getBytes()).setHeader(FileHeaders.FILENAME, "zipString.zip").build());
|
||||
|
||||
Assert.assertTrue(this.workDir.list().length == 1);
|
||||
Assert.assertEquals("zipString.zip", this.workDir.listFiles()[0].getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipFile() throws FileNotFoundException, IOException, InterruptedException {
|
||||
|
||||
final File fileToCompress = testFolder.newFile();
|
||||
FileUtils.writeStringToFile(fileToCompress, "hello world");
|
||||
|
||||
input.send(MessageBuilder.withPayload(fileToCompress).build());
|
||||
|
||||
Assert.assertTrue(this.workDir.list().length == 1);
|
||||
Assert.assertEquals(fileToCompress.getName() + ".zip", this.workDir.listFiles()[0].getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipIterableWithMultipleStrings() throws FileNotFoundException, IOException, InterruptedException {
|
||||
|
||||
String stringToCompress1 = "String1";
|
||||
String stringToCompress2 = "String2";
|
||||
String stringToCompress3 = "String3";
|
||||
String stringToCompress4 = "String4";
|
||||
|
||||
final List<String> stringsToCompress = new ArrayList<String>(4);
|
||||
|
||||
stringsToCompress.add(stringToCompress1);
|
||||
stringsToCompress.add(stringToCompress2);
|
||||
stringsToCompress.add(stringToCompress3);
|
||||
stringsToCompress.add(stringToCompress4);
|
||||
|
||||
input.send(MessageBuilder.withPayload(stringsToCompress).setHeader(FileHeaders.FILENAME, "zipWith4Strings.zip").build());
|
||||
|
||||
Assert.assertTrue(this.workDir.list().length == 1);
|
||||
Assert.assertEquals("zipWith4Strings.zip", this.workDir.listFiles()[0].getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipIterableWithDifferentTypes() throws FileNotFoundException, IOException, InterruptedException {
|
||||
|
||||
String stringToCompress = "String1";
|
||||
byte[] bytesToCompress = "String2".getBytes();
|
||||
final File fileToCompress = testFolder.newFile();
|
||||
FileUtils.writeStringToFile(fileToCompress, "hello world");
|
||||
|
||||
final List<Object> objectsToCompress = new ArrayList<Object>(3);
|
||||
|
||||
objectsToCompress.add(stringToCompress);
|
||||
objectsToCompress.add(bytesToCompress);
|
||||
objectsToCompress.add(fileToCompress);
|
||||
|
||||
input.send(MessageBuilder.withPayload(objectsToCompress).setHeader(FileHeaders.FILENAME, "objects-to-compress.zip").build());
|
||||
|
||||
Assert.assertTrue(this.workDir.list().length == 1);
|
||||
Assert.assertEquals("objects-to-compress.zip", this.workDir.listFiles()[0].getName());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ImportResource("classpath:org/springframework/integration/zip/Zip2FileTests-context.xml")
|
||||
public static class ContextConfiguration {
|
||||
|
||||
@Bean
|
||||
Properties properties() throws IOException {
|
||||
return properties;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright 2015 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
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
* 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;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.channel.AbstractMessageChannel;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.file.DefaultFileNameGenerator;
|
||||
import org.springframework.integration.file.FileNameGenerator;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.transformer.MessageTransformingHandler;
|
||||
import org.springframework.integration.zip.transformer.UnZipTransformer;
|
||||
import org.springframework.integration.zip.transformer.ZipResultType;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class UnZipTransformerParserTests {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void testUnZiptransformerParserWithDefaults() {
|
||||
|
||||
setUp("UnZipTransformerParserTests.xml", getClass());
|
||||
|
||||
EventDrivenConsumer consumer = this.context.getBean("unzipTransformerWithDefaults", EventDrivenConsumer.class);
|
||||
|
||||
final AbstractMessageChannel inputChannel = TestUtils.getPropertyValue(consumer, "inputChannel", AbstractMessageChannel.class);
|
||||
assertEquals("input", inputChannel.getComponentName());
|
||||
|
||||
final MessageTransformingHandler handler = TestUtils.getPropertyValue(consumer, "handler", MessageTransformingHandler.class);
|
||||
|
||||
final AbstractMessageChannel outputChannel = TestUtils.getPropertyValue(handler, "outputChannel", AbstractMessageChannel.class);
|
||||
assertEquals("output", outputChannel.getComponentName());
|
||||
|
||||
final UnZipTransformer unZipTransformer = TestUtils.getPropertyValue(handler, "transformer", UnZipTransformer.class);
|
||||
|
||||
final Charset charset = TestUtils.getPropertyValue(unZipTransformer, "charset", Charset.class);
|
||||
final FileNameGenerator fileNameGenerator = TestUtils.getPropertyValue(unZipTransformer, "fileNameGenerator", FileNameGenerator.class);
|
||||
final ZipResultType zipResultType = TestUtils.getPropertyValue(unZipTransformer, "zipResultType", ZipResultType.class);
|
||||
final File workDirectory = TestUtils.getPropertyValue(unZipTransformer, "workDirectory", File.class);
|
||||
final Boolean deleteFiles = TestUtils.getPropertyValue(unZipTransformer, "deleteFiles", Boolean.class);
|
||||
final Boolean expectSingleResult = TestUtils.getPropertyValue(unZipTransformer, "expectSingleResult", Boolean.class);
|
||||
|
||||
assertNotNull(charset);
|
||||
assertNotNull(fileNameGenerator);
|
||||
assertNotNull(zipResultType);
|
||||
assertNotNull(workDirectory);
|
||||
assertNotNull(deleteFiles);
|
||||
assertNotNull(expectSingleResult);
|
||||
|
||||
assertEquals(Charset.defaultCharset(), charset);
|
||||
Assert.isInstanceOf(DefaultFileNameGenerator.class, fileNameGenerator);
|
||||
assertEquals(ZipResultType.FILE, zipResultType);
|
||||
assertEquals(new File(System.getProperty("java.io.tmpdir") + File.separator + "ziptransformer"), workDirectory);
|
||||
assertTrue("WorkDirectory should exist.", workDirectory.exists());
|
||||
assertTrue("WorkDirectory should be a directory.", workDirectory.isDirectory());
|
||||
assertFalse("By default the 'deleteFiles' property should be false.", deleteFiles);
|
||||
assertFalse("The 'expectSingleResult' property should be false.", expectSingleResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnZiptransformerParserWithExplicitSettings() {
|
||||
|
||||
setUp("UnZipTransformerParserTests.xml", getClass());
|
||||
|
||||
EventDrivenConsumer consumer = this.context.getBean("unzipTransformer", EventDrivenConsumer.class);
|
||||
|
||||
final AbstractMessageChannel inputChannel = TestUtils.getPropertyValue(consumer, "inputChannel", AbstractMessageChannel.class);
|
||||
assertEquals("input", inputChannel.getComponentName());
|
||||
|
||||
final MessageTransformingHandler handler = TestUtils.getPropertyValue(consumer, "handler", MessageTransformingHandler.class);
|
||||
|
||||
final AbstractMessageChannel outputChannel = TestUtils.getPropertyValue(handler, "outputChannel", AbstractMessageChannel.class);
|
||||
assertEquals("output", outputChannel.getComponentName());
|
||||
|
||||
final UnZipTransformer unZipTransformer = TestUtils.getPropertyValue(handler, "transformer", UnZipTransformer.class);
|
||||
|
||||
final Charset charset = TestUtils.getPropertyValue(unZipTransformer, "charset", Charset.class);
|
||||
final FileNameGenerator fileNameGenerator = TestUtils.getPropertyValue(unZipTransformer, "fileNameGenerator", FileNameGenerator.class);
|
||||
final ZipResultType zipResultType = TestUtils.getPropertyValue(unZipTransformer, "zipResultType", ZipResultType.class);
|
||||
final File workDirectory = TestUtils.getPropertyValue(unZipTransformer, "workDirectory", File.class);
|
||||
final Boolean deleteFiles = TestUtils.getPropertyValue(unZipTransformer, "deleteFiles", Boolean.class);
|
||||
final Boolean expectSingleResult = TestUtils.getPropertyValue(unZipTransformer, "expectSingleResult", Boolean.class);
|
||||
|
||||
assertNotNull(charset);
|
||||
assertNotNull(fileNameGenerator);
|
||||
assertNotNull(zipResultType);
|
||||
assertNotNull(workDirectory);
|
||||
assertNotNull(deleteFiles);
|
||||
assertNotNull(expectSingleResult);
|
||||
|
||||
assertEquals(Charset.defaultCharset(), charset);
|
||||
Assert.isInstanceOf(DefaultFileNameGenerator.class, fileNameGenerator);
|
||||
assertEquals(ZipResultType.FILE, zipResultType);
|
||||
assertEquals(new File(System.getProperty("java.io.tmpdir") + File.separator + "ziptransformer"), workDirectory);
|
||||
assertTrue("WorkDirectory should exist.", workDirectory.exists());
|
||||
assertTrue("WorkDirectory should be a directory.", workDirectory.isDirectory());
|
||||
assertTrue("The 'deleteFiles' property should be true.", deleteFiles);
|
||||
assertTrue("The 'expectSingleResult' property should be true.", expectSingleResult);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown(){
|
||||
if(context != null){
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void setUp(String name, Class<?> cls){
|
||||
context = new ClassPathXmlApplicationContext(name, cls);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/zip http://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd">
|
||||
|
||||
<int:channel id="input"/>
|
||||
<int:channel id="output"/>
|
||||
|
||||
<int-zip:unzip-transformer id="unzipTransformer"
|
||||
delete-files="true" input-channel="input" output-channel="output"
|
||||
result-type="FILE" expect-single-result="true"/>
|
||||
|
||||
<int-zip:unzip-transformer id="unzipTransformerWithDefaults"
|
||||
input-channel="input" output-channel="output" />
|
||||
</beans>
|
||||
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* Copyright 2015 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
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
* 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;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.channel.AbstractMessageChannel;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.file.DefaultFileNameGenerator;
|
||||
import org.springframework.integration.file.FileNameGenerator;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.transformer.MessageTransformingHandler;
|
||||
import org.springframework.integration.zip.transformer.ZipResultType;
|
||||
import org.springframework.integration.zip.transformer.ZipTransformer;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class ZipTransformerParserTests {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void testZiptransformerParserWithDefaults() {
|
||||
|
||||
setUp("ZipTransformerParserTests.xml", getClass());
|
||||
|
||||
EventDrivenConsumer consumer = this.context.getBean("zipTransformerWithDefaults", EventDrivenConsumer.class);
|
||||
|
||||
final AbstractMessageChannel inputChannel = TestUtils.getPropertyValue(consumer, "inputChannel", AbstractMessageChannel.class);
|
||||
assertEquals("input", inputChannel.getComponentName());
|
||||
|
||||
final MessageTransformingHandler handler = TestUtils.getPropertyValue(consumer, "handler", MessageTransformingHandler.class);
|
||||
|
||||
final AbstractMessageChannel outputChannel = TestUtils.getPropertyValue(handler, "outputChannel", AbstractMessageChannel.class);
|
||||
assertEquals("output", outputChannel.getComponentName());
|
||||
|
||||
final ZipTransformer zipTransformer = TestUtils.getPropertyValue(handler, "transformer", ZipTransformer.class);
|
||||
|
||||
final Charset charset = TestUtils.getPropertyValue(zipTransformer, "charset", Charset.class);
|
||||
final FileNameGenerator fileNameGenerator = TestUtils.getPropertyValue(zipTransformer, "fileNameGenerator", FileNameGenerator.class);
|
||||
final ZipResultType zipResultType = TestUtils.getPropertyValue(zipTransformer, "zipResultType", ZipResultType.class);
|
||||
final File workDirectory = TestUtils.getPropertyValue(zipTransformer, "workDirectory", File.class);
|
||||
final Integer compressionLevel = TestUtils.getPropertyValue(zipTransformer, "compressionLevel", Integer.class);
|
||||
final Boolean deleteFiles = TestUtils.getPropertyValue(zipTransformer, "deleteFiles", Boolean.class);
|
||||
|
||||
assertNotNull(charset);
|
||||
assertNotNull(fileNameGenerator);
|
||||
assertNotNull(zipResultType);
|
||||
assertNotNull(workDirectory);
|
||||
assertNotNull(compressionLevel);
|
||||
assertNotNull(deleteFiles);
|
||||
|
||||
assertEquals(Charset.defaultCharset(), charset);
|
||||
Assert.isInstanceOf(DefaultFileNameGenerator.class, fileNameGenerator);
|
||||
assertEquals(ZipResultType.FILE, zipResultType);
|
||||
assertEquals(new File(System.getProperty("java.io.tmpdir") + File.separator + "ziptransformer"), workDirectory);
|
||||
assertTrue("WorkDirectory should exist.", workDirectory.exists());
|
||||
assertTrue("WorkDirectory should be a directory.", workDirectory.isDirectory());
|
||||
assertEquals(Integer.valueOf(Deflater.DEFAULT_COMPRESSION), Integer.valueOf(compressionLevel));
|
||||
assertFalse("By default the 'deleteFiles' property should be false.", deleteFiles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZiptransformerParserWithExplicitSettings() {
|
||||
|
||||
setUp("ZipTransformerParserTests.xml", getClass());
|
||||
|
||||
EventDrivenConsumer consumer = this.context.getBean("zipTransformer", EventDrivenConsumer.class);
|
||||
|
||||
final AbstractMessageChannel inputChannel = TestUtils.getPropertyValue(consumer, "inputChannel", AbstractMessageChannel.class);
|
||||
assertEquals("input", inputChannel.getComponentName());
|
||||
|
||||
final MessageTransformingHandler handler = TestUtils.getPropertyValue(consumer, "handler", MessageTransformingHandler.class);
|
||||
|
||||
final AbstractMessageChannel outputChannel = TestUtils.getPropertyValue(handler, "outputChannel", AbstractMessageChannel.class);
|
||||
assertEquals("output", outputChannel.getComponentName());
|
||||
|
||||
final ZipTransformer zipTransformer = TestUtils.getPropertyValue(handler, "transformer", ZipTransformer.class);
|
||||
|
||||
final Charset charset = TestUtils.getPropertyValue(zipTransformer, "charset", Charset.class);
|
||||
final FileNameGenerator fileNameGenerator = TestUtils.getPropertyValue(zipTransformer, "fileNameGenerator", FileNameGenerator.class);
|
||||
final ZipResultType zipResultType = TestUtils.getPropertyValue(zipTransformer, "zipResultType", ZipResultType.class);
|
||||
final File workDirectory = TestUtils.getPropertyValue(zipTransformer, "workDirectory", File.class);
|
||||
final Integer compressionLevel = TestUtils.getPropertyValue(zipTransformer, "compressionLevel", Integer.class);
|
||||
final Boolean deleteFiles = TestUtils.getPropertyValue(zipTransformer, "deleteFiles", Boolean.class);
|
||||
|
||||
assertNotNull(charset);
|
||||
assertNotNull(fileNameGenerator);
|
||||
assertNotNull(zipResultType);
|
||||
assertNotNull(workDirectory);
|
||||
assertNotNull(compressionLevel);
|
||||
assertNotNull(deleteFiles);
|
||||
|
||||
assertEquals(Charset.defaultCharset(), charset);
|
||||
Assert.isInstanceOf(DefaultFileNameGenerator.class, fileNameGenerator);
|
||||
assertEquals(ZipResultType.BYTE_ARRAY, zipResultType);
|
||||
assertEquals(new File(System.getProperty("java.io.tmpdir") + File.separator + "ziptransformer"), workDirectory);
|
||||
assertTrue("WorkDirectory should exist.", workDirectory.exists());
|
||||
assertTrue("WorkDirectory should be a directory.", workDirectory.isDirectory());
|
||||
assertEquals(Integer.valueOf(2), Integer.valueOf(compressionLevel));
|
||||
assertTrue("The 'deleteFiles' property should be true.", deleteFiles);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZiptransformerParserWithIncorrectResultType() {
|
||||
|
||||
try {
|
||||
setUp("ZipTransformerParserTestsWithIncorrectResultType.xml", getClass());
|
||||
}
|
||||
catch (BeanDefinitionParsingException e) {
|
||||
String expectedErrorMessage = "Unable to convert the provided result-type 'INCORRECT' " +
|
||||
"to the respective ZipResultType enum.";
|
||||
assertTrue(String.format("Expected exception message to contain '%s' but got '%s'", expectedErrorMessage, e.getMessage()),
|
||||
e.getMessage().contains(expectedErrorMessage));
|
||||
return;
|
||||
}
|
||||
|
||||
fail("Expected a BeanDefinitionParsingException to be thrown.");
|
||||
}
|
||||
@After
|
||||
public void tearDown(){
|
||||
if(context != null){
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void setUp(String name, Class<?> cls){
|
||||
context = new ClassPathXmlApplicationContext(name, cls);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/zip http://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd">
|
||||
|
||||
<int:channel id="input"/>
|
||||
<int:channel id="output"/>
|
||||
|
||||
<int-zip:zip-transformer id="zipTransformer" compression-level="2"
|
||||
delete-files="true" input-channel="input" output-channel="output"
|
||||
result-type="BYTE_ARRAY"/>
|
||||
|
||||
<int-zip:zip-transformer id="zipTransformerWithDefaults"
|
||||
input-channel="input" output-channel="output" />
|
||||
</beans>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/zip http://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd">
|
||||
|
||||
<int:channel id="input"/>
|
||||
<int:channel id="output"/>
|
||||
|
||||
<int-zip:zip-transformer id="zipTransformer" compression-level="2"
|
||||
delete-files="true" input-channel="input" output-channel="output"
|
||||
result-type="INCORRECT"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,253 @@
|
||||
/*
|
||||
* Copyright 2015 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
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on 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.transformer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration({"classpath:org/springframework/integration/zip/transformer/UnZipTransformerTests.xml"})
|
||||
public class UnZipTransformerTests {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder testFolder = new TemporaryFolder();
|
||||
|
||||
@Autowired
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
private File workDir;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
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 unzipSingleFileAsInputstreamToByteArray() throws IOException {
|
||||
|
||||
final Resource resource = resourceLoader.getResource("classpath:testzipdata/single.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
final Message<InputStream> message = MessageBuilder.withPayload(is).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<?> resultMessage = unZipTransformer.transform(message);
|
||||
|
||||
Assert.assertNotNull(resultMessage);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, byte[]> unzippedData = (Map<String, byte[]>) resultMessage.getPayload();
|
||||
|
||||
Assert.assertNotNull(unzippedData);
|
||||
Assert.assertTrue(unzippedData.size() == 1);
|
||||
Assert.assertEquals("Spring Integration Rocks!", new String(unzippedData.values().iterator().next()));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void unzipSingleFileToByteArray() throws IOException {
|
||||
|
||||
final Resource resource = resourceLoader.getResource("classpath:testzipdata/single.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
final File inputFile = new File(this.workDir, "unzipSingleFileToByteArray");
|
||||
|
||||
IOUtils.copy(is, new FileOutputStream(inputFile));
|
||||
|
||||
final Message<File> message = MessageBuilder.withPayload(inputFile).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<?> resultMessage = unZipTransformer.transform(message);
|
||||
|
||||
Assert.assertNotNull(resultMessage);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, byte[]> unzippedData = (Map<String, byte[]>) resultMessage.getPayload();
|
||||
|
||||
Assert.assertNotNull(unzippedData);
|
||||
Assert.assertTrue(unzippedData.size() == 1);
|
||||
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 = resourceLoader.getResource("classpath:testzipdata/single.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
final File inputFile = new File(this.workDir, "unzipSingleFileToByteArray");
|
||||
|
||||
IOUtils.copy(is, new FileOutputStream(inputFile));
|
||||
|
||||
final Message<File> message = MessageBuilder.withPayload(inputFile).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.setDeleteFiles(true);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<?> resultMessage = unZipTransformer.transform(message);
|
||||
|
||||
Assert.assertNotNull(resultMessage);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, byte[]> unzippedData = (Map<String, byte[]>) resultMessage.getPayload();
|
||||
|
||||
Assert.assertNotNull(unzippedData);
|
||||
Assert.assertTrue(unzippedData.size() == 1);
|
||||
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 = resourceLoader.getResource("classpath:testzipdata/countries.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
final Message<InputStream> message = MessageBuilder.withPayload(is).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<?> resultMessage = unZipTransformer.transform(message);
|
||||
|
||||
Assert.assertNotNull(resultMessage);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, byte[]> unzippedData = (Map<String, byte[]>) resultMessage.getPayload();
|
||||
|
||||
Assert.assertNotNull(unzippedData);
|
||||
Assert.assertTrue(unzippedData.size() == 5);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = resourceLoader.getResource("classpath:testzipdata/countries.zip");
|
||||
final InputStream is = resource.getInputStream();
|
||||
|
||||
final Message<InputStream> message = MessageBuilder.withPayload(is).build();
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.setExpectSingleResult(true);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
try {
|
||||
unZipTransformer.transform(message);
|
||||
}
|
||||
catch (MessagingException e) {
|
||||
Assert.assertTrue(e.getMessage().contains("The UnZip operation extracted "
|
||||
+ "5 result objects but expectSingleResult was 'true'."));
|
||||
return;
|
||||
}
|
||||
|
||||
Assert.fail("Expected a MessagingException to be thrown.");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unzipInvalidZipFile() throws FileNotFoundException, IOException, InterruptedException {
|
||||
|
||||
final File fileToUnzip = testFolder.newFile();
|
||||
FileUtils.writeStringToFile(fileToUnzip, "hello world");
|
||||
|
||||
final UnZipTransformer unZipTransformer = new UnZipTransformer();
|
||||
unZipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
unZipTransformer.setExpectSingleResult(true);
|
||||
unZipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<File> message = MessageBuilder.withPayload(fileToUnzip).build();
|
||||
|
||||
try {
|
||||
unZipTransformer.transform(message);
|
||||
}
|
||||
catch (MessagingException e) {
|
||||
Assert.assertTrue(e.getMessage().contains(String.format("Not a zip file: '%s'.", fileToUnzip.getAbsolutePath())));
|
||||
return;
|
||||
}
|
||||
|
||||
Assert.fail("Expected a MessagingException to be thrown.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-zip="http://www.springframework.org/schema/integration/zip"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/zip http://www.springframework.org/schema/integration/zip/spring-integration-zip.xsd">
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,284 @@
|
||||
/*
|
||||
* Copyright 2015 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
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on 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.transformer;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.zip.ZipHeaders;
|
||||
import org.zeroturnaround.zip.ZipUtil;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class ZipTransformerTests {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder testFolder = new TemporaryFolder();
|
||||
|
||||
/**
|
||||
* Compress a simple String. The result will be a byte array.
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void zipString() throws FileNotFoundException, IOException {
|
||||
final ZipTransformer zipTransformer = new ZipTransformer();
|
||||
zipTransformer.setBeanFactory(mock(BeanFactory.class));
|
||||
zipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
zipTransformer.afterPropertiesSet();
|
||||
|
||||
final String stringToCompress = "Hello World";
|
||||
|
||||
final Date fileDate = new Date();
|
||||
|
||||
final Message<String> message = MessageBuilder.withPayload(stringToCompress)
|
||||
.setHeader(ZipHeaders.ZIP_ENTRY_FILE_NAME, "test.txt")
|
||||
.setHeader(ZipHeaders.ZIP_ENTRY_LAST_MODIFIED_DATE, fileDate)
|
||||
.build();
|
||||
|
||||
final Message<?> result = zipTransformer.transform(message);
|
||||
|
||||
Object resultPayload = result.getPayload();
|
||||
|
||||
Assert.assertTrue("Expected payload to be an instance of byte but was "
|
||||
+ resultPayload.getClass().getName(), resultPayload instanceof byte[]);
|
||||
|
||||
final File temporaryTestDirectory = testFolder.newFolder();
|
||||
|
||||
ZipUtil.unpack(new ByteArrayInputStream((byte[]) resultPayload), temporaryTestDirectory);
|
||||
|
||||
final File unzippedEntry = new File(temporaryTestDirectory, "test.txt");
|
||||
Assert.assertTrue(unzippedEntry.exists());
|
||||
Assert.assertTrue(unzippedEntry.isFile());
|
||||
|
||||
//See http://stackoverflow.com/questions/3725662/what-is-the-earliest-timestamp-value-that-is-supported-in-zip-file-format
|
||||
Assert.assertTrue((fileDate.getTime() - 3000) < unzippedEntry.lastModified());
|
||||
Assert.assertTrue((fileDate.getTime() + 3000) > unzippedEntry.lastModified());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipStringCollection() throws FileNotFoundException, IOException {
|
||||
final ZipTransformer zipTransformer = new ZipTransformer();
|
||||
zipTransformer.setBeanFactory(mock(BeanFactory.class));
|
||||
zipTransformer.setZipResultType(ZipResultType.BYTE_ARRAY);
|
||||
zipTransformer.afterPropertiesSet();
|
||||
|
||||
final String string1ToCompress = "Cartman";
|
||||
final String string2ToCompress = "Kenny";
|
||||
final String string3ToCompress = "Butters";
|
||||
|
||||
final List<String> strings = new ArrayList<String>(3);
|
||||
|
||||
strings.add(string1ToCompress);
|
||||
strings.add(string2ToCompress);
|
||||
strings.add(string3ToCompress);
|
||||
|
||||
final Date fileDate = new Date();
|
||||
|
||||
final Message<List<String>> message = MessageBuilder.withPayload(strings)
|
||||
.setHeader(ZipHeaders.ZIP_ENTRY_FILE_NAME, "test.txt")
|
||||
.setHeader(ZipHeaders.ZIP_ENTRY_LAST_MODIFIED_DATE, fileDate)
|
||||
.build();
|
||||
|
||||
final Message<?> result = zipTransformer.transform(message);
|
||||
|
||||
Object resultPayload = result.getPayload();
|
||||
|
||||
Assert.assertTrue("Expected payload to be an instance of byte but was "
|
||||
+ resultPayload.getClass().getName(), resultPayload instanceof byte[]);
|
||||
|
||||
final File temporaryTestDirectory = testFolder.newFolder();
|
||||
|
||||
ZipUtil.unpack(new ByteArrayInputStream((byte[]) resultPayload), temporaryTestDirectory);
|
||||
|
||||
File[] files = temporaryTestDirectory.listFiles();
|
||||
|
||||
Assert.assertTrue(files.length >= 3);
|
||||
|
||||
final Set<String> expectedFileNames = new HashSet<String>();
|
||||
|
||||
expectedFileNames.add("test_1.txt");
|
||||
expectedFileNames.add("test_2.txt");
|
||||
expectedFileNames.add("test_3.txt");
|
||||
|
||||
for (File file : files) {
|
||||
|
||||
if (file.getName().startsWith("test")) {
|
||||
Assert.assertTrue(file.exists());
|
||||
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((fileDate.getTime() + 4000) > file.lastModified());
|
||||
|
||||
Assert.assertTrue(
|
||||
String.format("File '%s' did not end with '.txt'.", file.getName()),
|
||||
file.getName().endsWith(".txt"));
|
||||
|
||||
Assert.assertTrue(expectedFileNames.contains(file.getName()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipStringToFile() throws FileNotFoundException, IOException {
|
||||
final ZipTransformer zipTransformer = new ZipTransformer();
|
||||
zipTransformer.setBeanFactory(mock(BeanFactory.class));
|
||||
zipTransformer.setZipResultType(ZipResultType.FILE);
|
||||
zipTransformer.afterPropertiesSet();
|
||||
|
||||
final String stringToCompress = "Hello World";
|
||||
|
||||
final String zipEntryFileName = "test.txt";
|
||||
final Message<String> message = MessageBuilder.withPayload(stringToCompress)
|
||||
.setHeader(ZipHeaders.ZIP_ENTRY_FILE_NAME, zipEntryFileName)
|
||||
.build();
|
||||
|
||||
final Message<?> result = zipTransformer.transform(message);
|
||||
|
||||
Assert.assertTrue("Expected payload to be an instance of file but was "
|
||||
+ result.getPayload().getClass().getName(), result.getPayload() instanceof File);
|
||||
|
||||
final File payload = (File) result.getPayload();
|
||||
|
||||
System.out.println(payload.getAbsolutePath());
|
||||
|
||||
Assert.assertEquals(message.getHeaders().getId().toString() + ".msg.zip", payload.getName());
|
||||
Assert.assertTrue(SpringZipUtils.isValid(payload));
|
||||
|
||||
final byte[] zipEntryData = ZipUtil.unpackEntry(payload, "test.txt");
|
||||
|
||||
Assert.assertNotNull("Entry '" + zipEntryFileName + "' was not found.", zipEntryData);
|
||||
Assert.assertTrue("Hello World".equals(new String(zipEntryData)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipFile() throws IOException {
|
||||
|
||||
final ZipTransformer zipTransformer = new ZipTransformer();
|
||||
zipTransformer.setBeanFactory(mock(BeanFactory.class));
|
||||
zipTransformer.afterPropertiesSet();
|
||||
|
||||
final File testFile = createTestFile(10);
|
||||
|
||||
Assert.assertTrue(testFile.exists());
|
||||
|
||||
final Message<File> message = MessageBuilder.withPayload(testFile).build();
|
||||
|
||||
final Message<?> result = zipTransformer.transform(message);
|
||||
|
||||
Assert.assertTrue(result.getPayload() instanceof File);
|
||||
|
||||
final File payload = (File) result.getPayload();
|
||||
|
||||
Assert.assertEquals(testFile.getName() + ".zip", payload.getName());
|
||||
Assert.assertTrue(SpringZipUtils.isValid(payload));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zipCollection() throws IOException {
|
||||
|
||||
final File testFile1 = createTestFile(1);
|
||||
final File testFile2 = createTestFile(2);
|
||||
final File testFile3 = createTestFile(3);
|
||||
final File testFile4 = createTestFile(4);
|
||||
|
||||
Assert.assertTrue(testFile1.exists());
|
||||
Assert.assertTrue(testFile2.exists());
|
||||
Assert.assertTrue(testFile3.exists());
|
||||
Assert.assertTrue(testFile4.exists());
|
||||
|
||||
final Collection<File> files = new ArrayList<File>();
|
||||
|
||||
files.add(testFile1);
|
||||
files.add(testFile2);
|
||||
files.add(testFile3);
|
||||
files.add(testFile4);
|
||||
|
||||
final ZipTransformer zipTransformer = new ZipTransformer();
|
||||
zipTransformer.setBeanFactory(mock(BeanFactory.class));
|
||||
zipTransformer.afterPropertiesSet();
|
||||
|
||||
final Message<Collection<File>> message = MessageBuilder.withPayload(files)
|
||||
.build();
|
||||
|
||||
final Message<?> result = zipTransformer.transform(message);
|
||||
|
||||
Assert.assertTrue(result.getPayload() instanceof File);
|
||||
|
||||
final File outputZipFile = (File) result.getPayload();
|
||||
|
||||
Assert.assertTrue(outputZipFile.exists());
|
||||
Assert.assertTrue(outputZipFile.isFile());
|
||||
Assert.assertTrue(outputZipFile.getName().endsWith("zip"));
|
||||
Assert.assertTrue(SpringZipUtils.isValid(outputZipFile));
|
||||
|
||||
}
|
||||
|
||||
private File createTestFile(int size) throws IOException {
|
||||
|
||||
final File temporaryTestDirectory = testFolder.newFolder();
|
||||
|
||||
final File testFile = new File(temporaryTestDirectory, "testdata" + UUID.randomUUID().toString() + ".data");
|
||||
|
||||
RandomAccessFile f = null;
|
||||
try {
|
||||
f = new RandomAccessFile(testFile, "rw");
|
||||
f.setLength(size * 1024 * 1024);
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (f != null) {
|
||||
f.close();
|
||||
}
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
return testFile;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
log4j.rootCategory=WARN, stdout
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n
|
||||
|
||||
log4j.category.org.springframework.integration=INFO
|
||||
log4j.category.org.springframework.integration.zip=INFO
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
Asia
|
||||
@@ -0,0 +1 @@
|
||||
Europe
|
||||
@@ -0,0 +1 @@
|
||||
Germany
|
||||
@@ -0,0 +1 @@
|
||||
France
|
||||
@@ -0,0 +1 @@
|
||||
Poland
|
||||
BIN
spring-integration-zip/src/test/resources/testzipdata/single.zip
Normal file
BIN
spring-integration-zip/src/test/resources/testzipdata/single.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user