embulk-decoder-commons-compress 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/LICENSE.txt +23 -0
- data/README.md +70 -0
- data/build.gradle +63 -0
- data/gradle/wrapper/gradle-wrapper.jar +0 -0
- data/gradle/wrapper/gradle-wrapper.properties +6 -0
- data/gradlew +164 -0
- data/gradlew.bat +90 -0
- data/lib/embulk/decoder/commons-compress.rb +3 -0
- data/src/main/java/org/embulk/decoder/ArchiveInputStreamIterator.java +68 -0
- data/src/main/java/org/embulk/decoder/CommonsCompressDecoderPlugin.java +52 -0
- data/src/main/java/org/embulk/decoder/CommonsCompressFileInput.java +73 -0
- data/src/main/java/org/embulk/decoder/CommonsCompressProvider.java +182 -0
- data/src/main/java/org/embulk/decoder/CommonsCompressUtil.java +135 -0
- data/src/test/java/org/embulk/decoder/TestArchiveInputStreamIterator.java +106 -0
- data/src/test/java/org/embulk/decoder/TestCommonsCompressDecoderPlugin.java +593 -0
- data/src/test/java/org/embulk/decoder/TestCommonsCompressFileInput.java +152 -0
- data/src/test/java/org/embulk/decoder/TestCommonsCompressProvider.java +369 -0
- data/src/test/java/org/embulk/decoder/TestCommonsCompressUtil.java +80 -0
- data/src/test/resources/org/embulk/decoder/sample_0.tar +0 -0
- data/src/test/resources/org/embulk/decoder/sample_1.csv +1 -0
- data/src/test/resources/org/embulk/decoder/sample_1.csv.bz2 +0 -0
- data/src/test/resources/org/embulk/decoder/sample_1.tar +0 -0
- data/src/test/resources/org/embulk/decoder/sample_2.csv +1 -0
- data/src/test/resources/org/embulk/decoder/samples.ar +5 -0
- data/src/test/resources/org/embulk/decoder/samples.tar +0 -0
- data/src/test/resources/org/embulk/decoder/samples.tar.Z +0 -0
- data/src/test/resources/org/embulk/decoder/samples.tar.bz2 +0 -0
- data/src/test/resources/org/embulk/decoder/samples.tar.gz +0 -0
- data/src/test/resources/org/embulk/decoder/samples.tar.xz +0 -0
- data/src/test/resources/org/embulk/decoder/samples.tgz +0 -0
- data/src/test/resources/org/embulk/decoder/samples.zip +0 -0
- metadata +106 -0
@@ -0,0 +1,106 @@
|
|
1
|
+
package org.embulk.decoder;
|
2
|
+
|
3
|
+
import static org.junit.Assert.assertEquals;
|
4
|
+
import static org.junit.Assert.assertFalse;
|
5
|
+
import static org.junit.Assert.assertNull;
|
6
|
+
import static org.junit.Assert.assertTrue;
|
7
|
+
|
8
|
+
import java.io.ByteArrayOutputStream;
|
9
|
+
import java.io.IOException;
|
10
|
+
import java.io.InputStream;
|
11
|
+
|
12
|
+
import mockit.Mocked;
|
13
|
+
import mockit.NonStrictExpectations;
|
14
|
+
|
15
|
+
import org.apache.commons.compress.archivers.ArchiveEntry;
|
16
|
+
import org.apache.commons.compress.archivers.ArchiveInputStream;
|
17
|
+
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
|
18
|
+
import org.junit.Test;
|
19
|
+
|
20
|
+
public class TestArchiveInputStreamIterator {
|
21
|
+
|
22
|
+
@Test
|
23
|
+
public void testHasNextForNoEntry(@Mocked final ArchiveInputStream ain) throws Exception {
|
24
|
+
new NonStrictExpectations() {{
|
25
|
+
ain.getNextEntry(); result = null;
|
26
|
+
}};
|
27
|
+
ArchiveInputStreamIterator it = new ArchiveInputStreamIterator(ain);
|
28
|
+
assertFalse("Verify there is no next item.", it.hasNext());
|
29
|
+
assertNull("Verify there is no stream.", it.next());
|
30
|
+
}
|
31
|
+
|
32
|
+
@Test
|
33
|
+
public void testHasNextForOneEntry(@Mocked final ArchiveInputStream ain, @Mocked final ArchiveEntry entry) throws Exception {
|
34
|
+
new NonStrictExpectations() {{
|
35
|
+
ain.getNextEntry(); result = entry; result = null;
|
36
|
+
entry.isDirectory(); result = false;
|
37
|
+
}};
|
38
|
+
ArchiveInputStreamIterator it = new ArchiveInputStreamIterator(ain);
|
39
|
+
assertTrue("Verify there is a item.", it.hasNext());
|
40
|
+
assertEquals("Verify ArchiveInputStream is return.", (InputStream)ain, it.next());
|
41
|
+
assertFalse("Verify there is no next item.", it.hasNext());
|
42
|
+
assertNull("Verify there is no stream.", it.next());
|
43
|
+
}
|
44
|
+
|
45
|
+
@Test
|
46
|
+
public void testHasNextForTwoEntries(@Mocked final ArchiveInputStream ain, @Mocked final ArchiveEntry entry) throws Exception {
|
47
|
+
new NonStrictExpectations() {{
|
48
|
+
ain.getNextEntry(); result = entry; result = entry; result = null;
|
49
|
+
entry.isDirectory(); result = false;
|
50
|
+
}};
|
51
|
+
ArchiveInputStreamIterator it = new ArchiveInputStreamIterator(ain);
|
52
|
+
assertTrue("Verify there is 1st item.", it.hasNext());
|
53
|
+
assertEquals("Verify ArchiveInputStream is return.", (InputStream)ain, it.next());
|
54
|
+
assertTrue("Verify there is 2nd item.", it.hasNext());
|
55
|
+
assertEquals("Verify ArchiveInputStream is return.", (InputStream)ain, it.next());
|
56
|
+
assertFalse("Verify there is no next item.", it.hasNext());
|
57
|
+
assertNull("Verify there is no stream.", it.next());
|
58
|
+
}
|
59
|
+
|
60
|
+
@Test
|
61
|
+
public void testHasNextForDirectory(@Mocked final ArchiveInputStream ain, @Mocked final ArchiveEntry entry) throws Exception {
|
62
|
+
new NonStrictExpectations() {{
|
63
|
+
ain.getNextEntry(); result = entry; result = entry; result = null;
|
64
|
+
entry.isDirectory(); result = false; result = true;
|
65
|
+
}};
|
66
|
+
ArchiveInputStreamIterator it = new ArchiveInputStreamIterator(ain);
|
67
|
+
assertTrue("Verify there is 1st item.", it.hasNext());
|
68
|
+
assertEquals("Verify ArchiveInputStream is return.", (InputStream)ain, it.next());
|
69
|
+
assertFalse("Verify there is no next item.", it.hasNext());
|
70
|
+
assertNull("Verify there is no stream.", it.next());
|
71
|
+
}
|
72
|
+
|
73
|
+
@Test
|
74
|
+
public void testArchiveFile() throws Exception {
|
75
|
+
InputStream in = getClass().getResourceAsStream("samples.tar");
|
76
|
+
ArchiveInputStream ain = new ArchiveStreamFactory().createArchiveInputStream(in);
|
77
|
+
ArchiveInputStreamIterator it = new ArchiveInputStreamIterator(ain);
|
78
|
+
assertTrue("Verify there is 1st item.", it.hasNext());
|
79
|
+
assertEquals("Verify ArchiveInputStream is return.", "1,foo", readContents(it.next()));
|
80
|
+
assertTrue("Verify there is 2nd item.", it.hasNext());
|
81
|
+
assertEquals("Verify ArchiveInputStream is return.", "2,bar", readContents(it.next()));
|
82
|
+
assertFalse("Verify there is no next item.", it.hasNext());
|
83
|
+
assertNull("Verify there is no stream.", it.next());
|
84
|
+
|
85
|
+
// Verify calling after no data.
|
86
|
+
assertFalse("Verify there is no next item.", it.hasNext());
|
87
|
+
assertNull("Verify there is no stream.", it.next());
|
88
|
+
}
|
89
|
+
|
90
|
+
@Test(expected=UnsupportedOperationException.class)
|
91
|
+
public void testRemove(@Mocked final ArchiveInputStream ain) {
|
92
|
+
ArchiveInputStreamIterator it = new ArchiveInputStreamIterator(ain);
|
93
|
+
it.remove();
|
94
|
+
}
|
95
|
+
|
96
|
+
private String readContents(InputStream in) throws IOException {
|
97
|
+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
98
|
+
byte[] buff = new byte[128];
|
99
|
+
int len = in.read(buff);
|
100
|
+
while (len != -1) {
|
101
|
+
bout.write(buff, 0, len);
|
102
|
+
len = in.read(buff);
|
103
|
+
}
|
104
|
+
return bout.toString().trim();
|
105
|
+
}
|
106
|
+
}
|
@@ -0,0 +1,593 @@
|
|
1
|
+
package org.embulk.decoder;
|
2
|
+
|
3
|
+
import java.io.ByteArrayInputStream;
|
4
|
+
import java.io.ByteArrayOutputStream;
|
5
|
+
import java.io.File;
|
6
|
+
import java.io.FileInputStream;
|
7
|
+
import java.io.IOException;
|
8
|
+
import java.io.InputStream;
|
9
|
+
import java.lang.reflect.Method;
|
10
|
+
import java.net.URISyntaxException;
|
11
|
+
import java.util.List;
|
12
|
+
import java.util.Map.Entry;
|
13
|
+
|
14
|
+
import mockit.Mocked;
|
15
|
+
import mockit.NonStrictExpectations;
|
16
|
+
import mockit.Verifications;
|
17
|
+
|
18
|
+
import org.apache.commons.compress.archivers.ArchiveEntry;
|
19
|
+
import org.apache.commons.compress.archivers.ArchiveException;
|
20
|
+
import org.apache.commons.compress.archivers.ArchiveOutputStream;
|
21
|
+
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
|
22
|
+
import org.apache.commons.compress.compressors.CompressorException;
|
23
|
+
import org.apache.commons.compress.compressors.CompressorOutputStream;
|
24
|
+
import org.apache.commons.compress.compressors.CompressorStreamFactory;
|
25
|
+
import org.apache.commons.compress.utils.IOUtils;
|
26
|
+
import org.embulk.config.Config;
|
27
|
+
import org.embulk.config.ConfigDefault;
|
28
|
+
import org.embulk.config.ConfigSource;
|
29
|
+
import org.embulk.config.DataSource;
|
30
|
+
import org.embulk.config.TaskSource;
|
31
|
+
import org.embulk.exec.PooledBufferAllocator;
|
32
|
+
import org.embulk.spi.Buffer;
|
33
|
+
import org.embulk.spi.BufferAllocator;
|
34
|
+
import org.embulk.spi.DecoderPlugin;
|
35
|
+
import org.embulk.spi.FileInput;
|
36
|
+
import org.junit.Assert;
|
37
|
+
import org.junit.Test;
|
38
|
+
|
39
|
+
import com.fasterxml.jackson.databind.JsonNode;
|
40
|
+
import com.fasterxml.jackson.databind.node.ObjectNode;
|
41
|
+
|
42
|
+
|
43
|
+
public class TestCommonsCompressDecoderPlugin
|
44
|
+
{
|
45
|
+
private static final String DEFAULT_FORMAT_CONFIG = "\"\"";
|
46
|
+
|
47
|
+
@Mocked
|
48
|
+
CommonsCompressDecoderPlugin.PluginTask task;
|
49
|
+
|
50
|
+
@Mocked
|
51
|
+
TaskSource taskSource;
|
52
|
+
|
53
|
+
@Test
|
54
|
+
public void testPluginTask() throws Exception {
|
55
|
+
Method method = CommonsCompressDecoderPlugin.PluginTask.class.getMethod("getFormat");
|
56
|
+
Config config = method.getAnnotation(Config.class);
|
57
|
+
ConfigDefault configDefault = method.getAnnotation(ConfigDefault.class);
|
58
|
+
|
59
|
+
Assert.assertEquals("Verify the config name.", "format", config.value());
|
60
|
+
Assert.assertEquals("Verify the default config value.", DEFAULT_FORMAT_CONFIG, configDefault.value());
|
61
|
+
}
|
62
|
+
|
63
|
+
@Test
|
64
|
+
public void testTransaction(@Mocked final ConfigSource config, @Mocked final DecoderPlugin.Control control)
|
65
|
+
{
|
66
|
+
new NonStrictExpectations() {{
|
67
|
+
config.loadConfig(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
68
|
+
task.dump(); result = taskSource;
|
69
|
+
}};
|
70
|
+
|
71
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
72
|
+
plugin.transaction(config, control);
|
73
|
+
|
74
|
+
new Verifications() {{
|
75
|
+
control.run(taskSource);
|
76
|
+
}};
|
77
|
+
}
|
78
|
+
|
79
|
+
@Test
|
80
|
+
public void testOpen(@Mocked final FileInput input)
|
81
|
+
{
|
82
|
+
new NonStrictExpectations() {{
|
83
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
84
|
+
}};
|
85
|
+
|
86
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
87
|
+
|
88
|
+
Assert.assertNotNull("Verify a value is returned.", plugin.open(taskSource, input));
|
89
|
+
}
|
90
|
+
|
91
|
+
// sample_0 contains only a directory.
|
92
|
+
@Test
|
93
|
+
public void testOpenForNoFile(@Mocked final FileInput input) throws Exception
|
94
|
+
{
|
95
|
+
new NonStrictExpectations() {{
|
96
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
97
|
+
task.getFormat(); result = "tar";
|
98
|
+
input.nextFile(); result = true; result = false;
|
99
|
+
input.poll(); result = getResourceAsBuffer("sample_0.tar");
|
100
|
+
task.getBufferAllocator(); result = new PooledBufferAllocator();
|
101
|
+
}};
|
102
|
+
|
103
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
104
|
+
FileInput archiveFileInput = plugin.open(taskSource, input);
|
105
|
+
|
106
|
+
Assert.assertFalse("Verify there is no file.", archiveFileInput.nextFile());
|
107
|
+
archiveFileInput.close();
|
108
|
+
|
109
|
+
new Verifications() {{
|
110
|
+
input.nextFile(); times = 2;
|
111
|
+
input.close(); times = 1;
|
112
|
+
}};
|
113
|
+
}
|
114
|
+
|
115
|
+
// sample_1.tar contains 1 csv file.
|
116
|
+
@Test
|
117
|
+
public void testOpenForOneFile(@Mocked final FileInput input) throws Exception
|
118
|
+
{
|
119
|
+
new NonStrictExpectations() {{
|
120
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
121
|
+
task.getFormat(); result = "tar";
|
122
|
+
input.nextFile(); result = true; result = false;
|
123
|
+
input.poll(); result = getResourceAsBuffer("sample_1.tar");
|
124
|
+
task.getBufferAllocator(); result = new PooledBufferAllocator();
|
125
|
+
}};
|
126
|
+
|
127
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
128
|
+
FileInput archiveFileInput = plugin.open(taskSource, input);
|
129
|
+
|
130
|
+
verifyContents(archiveFileInput, "1,foo");
|
131
|
+
|
132
|
+
new Verifications() {{
|
133
|
+
input.nextFile(); times = 2;
|
134
|
+
input.close(); times = 1;
|
135
|
+
}};
|
136
|
+
}
|
137
|
+
|
138
|
+
// input.nextFile() returns true
|
139
|
+
// samples.zip/sample_1.csv (1st)
|
140
|
+
// samples.zip/sample_2.csv (2nd)
|
141
|
+
@Test
|
142
|
+
public void testOpenForTwoFiles(@Mocked final FileInput input) throws Exception
|
143
|
+
{
|
144
|
+
new NonStrictExpectations() {{
|
145
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
146
|
+
task.getFormat(); result = "tar";
|
147
|
+
input.nextFile(); result = true; result = false;
|
148
|
+
input.poll(); result = getResourceAsBuffer("samples.tar");
|
149
|
+
task.getBufferAllocator(); result = new PooledBufferAllocator();
|
150
|
+
}};
|
151
|
+
|
152
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
153
|
+
FileInput archiveFileInput = plugin.open(taskSource, input);
|
154
|
+
|
155
|
+
verifyContents(archiveFileInput, "1,foo", "2,bar");
|
156
|
+
|
157
|
+
new Verifications() {{
|
158
|
+
input.nextFile(); times = 2;
|
159
|
+
input.close(); times = 1;
|
160
|
+
}};
|
161
|
+
}
|
162
|
+
|
163
|
+
// input.nextFile() returns true
|
164
|
+
// samples.zip/sample_1.csv (1st)
|
165
|
+
// samples.zip/sample_2.csv (2nd)
|
166
|
+
// input.nextFile() returns true
|
167
|
+
// samples.zip/sample_1.csv (3rd)
|
168
|
+
// samples.zip/sample_2.csv (4th)
|
169
|
+
@Test
|
170
|
+
public void testOpenForFourFiles(@Mocked final FileInput input) throws Exception
|
171
|
+
{
|
172
|
+
new NonStrictExpectations() {{
|
173
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
174
|
+
task.getFormat(); result = "zip";
|
175
|
+
input.nextFile(); result = true; result = true; result = false; // two files.
|
176
|
+
input.poll(); result = getResourceAsBuffer("samples.zip"); result = getResourceAsBuffer("samples.zip");
|
177
|
+
task.getBufferAllocator(); result = new PooledBufferAllocator();
|
178
|
+
}};
|
179
|
+
|
180
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
181
|
+
FileInput archiveFileInput = plugin.open(taskSource, input);
|
182
|
+
|
183
|
+
verifyContents(archiveFileInput, "1,foo", "2,bar", "1,foo", "2,bar");
|
184
|
+
|
185
|
+
new Verifications() {{
|
186
|
+
input.nextFile(); times = 3;
|
187
|
+
input.close(); times = 1;
|
188
|
+
}};
|
189
|
+
}
|
190
|
+
|
191
|
+
@Test
|
192
|
+
public void testOpenArchiveFormatAutoDetect(@Mocked final FileInput input) throws Exception
|
193
|
+
{
|
194
|
+
new NonStrictExpectations() {{
|
195
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
196
|
+
task.getFormat(); result = "";
|
197
|
+
input.nextFile(); result = true; result = false;
|
198
|
+
input.poll(); result = getResourceAsBuffer("sample_1.tar");
|
199
|
+
task.getBufferAllocator(); result = new PooledBufferAllocator();
|
200
|
+
}};
|
201
|
+
|
202
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
203
|
+
FileInput archiveFileInput = plugin.open(taskSource, input);
|
204
|
+
|
205
|
+
verifyContents(archiveFileInput, "1,foo");
|
206
|
+
|
207
|
+
new Verifications() {{
|
208
|
+
input.nextFile(); times = 2;
|
209
|
+
input.close(); times = 1;
|
210
|
+
}};
|
211
|
+
}
|
212
|
+
|
213
|
+
@Test(expected=RuntimeException.class)
|
214
|
+
public void testOpenAutoDetectFailed(@Mocked final FileInput input) throws Exception
|
215
|
+
{
|
216
|
+
new NonStrictExpectations() {{
|
217
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
218
|
+
task.getFormat(); result = "";
|
219
|
+
input.nextFile(); result = true; result = false;
|
220
|
+
input.poll(); result = getResourceAsBuffer("sample_1.csv"); // This is not an archive.
|
221
|
+
task.getBufferAllocator(); result = new PooledBufferAllocator();
|
222
|
+
}};
|
223
|
+
|
224
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
225
|
+
FileInput archiveFileInput = plugin.open(taskSource, input);
|
226
|
+
archiveFileInput.nextFile();
|
227
|
+
}
|
228
|
+
|
229
|
+
@Test(expected=RuntimeException.class)
|
230
|
+
public void testOpenExplicitConfigFailed(@Mocked final FileInput input) throws Exception
|
231
|
+
{
|
232
|
+
new NonStrictExpectations() {{
|
233
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
234
|
+
task.getFormat(); result = "tar";
|
235
|
+
input.nextFile(); result = true; result = false;
|
236
|
+
input.poll(); result = getResourceAsBuffer("samples.zip"); // This is not tar file.
|
237
|
+
task.getBufferAllocator(); result = new PooledBufferAllocator();
|
238
|
+
}};
|
239
|
+
|
240
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
241
|
+
FileInput archiveFileInput = plugin.open(taskSource, input);
|
242
|
+
archiveFileInput.nextFile();
|
243
|
+
}
|
244
|
+
|
245
|
+
@Test
|
246
|
+
public void testOpenForGeneratedArchives() throws Exception
|
247
|
+
{
|
248
|
+
String[] testFormats = new String[]{
|
249
|
+
ArchiveStreamFactory.AR,
|
250
|
+
// ArchiveStreamFactory.ARJ, // ArchiveException: Archiver: arj not found.
|
251
|
+
ArchiveStreamFactory.CPIO,
|
252
|
+
// ArchiveStreamFactory.DUMP, // ArchiveException: Archiver: dump not found.
|
253
|
+
ArchiveStreamFactory.JAR,
|
254
|
+
// ArchiveStreamFactory.SEVEN_Z, // StreamingNotSupportedException: The 7z doesn't support streaming.
|
255
|
+
ArchiveStreamFactory.TAR,
|
256
|
+
ArchiveStreamFactory.ZIP,
|
257
|
+
};
|
258
|
+
|
259
|
+
for (String format : testFormats) {
|
260
|
+
TaskSource mockTaskSource = new MockTaskSource(format);
|
261
|
+
FileInput mockInput = new MockFileInput(
|
262
|
+
getInputStreamAsBuffer(
|
263
|
+
getArchiveInputStream(format, "sample_1.csv", "sample_2.csv")));
|
264
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
265
|
+
FileInput archiveFileInput = plugin.open(mockTaskSource, mockInput);
|
266
|
+
verifyContents(archiveFileInput, "1,foo", "2,bar");
|
267
|
+
}
|
268
|
+
}
|
269
|
+
|
270
|
+
@Test
|
271
|
+
public void testOpenForGeneratedCompression() throws Exception
|
272
|
+
{
|
273
|
+
String[] testFormats = new String[]{
|
274
|
+
CompressorStreamFactory.BZIP2,
|
275
|
+
CompressorStreamFactory.DEFLATE,
|
276
|
+
CompressorStreamFactory.GZIP,
|
277
|
+
// CompressorStreamFactory.LZMA, // CompressorException: Compressor: lzma not found.
|
278
|
+
// CompressorStreamFactory.PACK200, // Failed to generate compressed file.
|
279
|
+
// CompressorStreamFactory.SNAPPY_FRAMED, // CompressorException: Compressor: snappy-framed not found.
|
280
|
+
// CompressorStreamFactory.SNAPPY_RAW, // CompressorException: Compressor: snappy-raw not found.
|
281
|
+
// CompressorStreamFactory.XZ, // ClassNotFoundException: org.tukaani.xz.FilterOptions
|
282
|
+
// CompressorStreamFactory.Z, // CompressorException: Compressor: z not found.
|
283
|
+
};
|
284
|
+
|
285
|
+
for (String format : testFormats) {
|
286
|
+
TaskSource mockTaskSource = new MockTaskSource(format);
|
287
|
+
FileInput mockInput = new MockFileInput(
|
288
|
+
getInputStreamAsBuffer(
|
289
|
+
getCompressorInputStream(format, "sample_1.csv")));
|
290
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
291
|
+
FileInput archiveFileInput = plugin.open(mockTaskSource, mockInput);
|
292
|
+
verifyContents(archiveFileInput, "1,foo");
|
293
|
+
}
|
294
|
+
}
|
295
|
+
|
296
|
+
@Test
|
297
|
+
public void testOpenForTGZFormat(@Mocked final FileInput input) throws Exception
|
298
|
+
{
|
299
|
+
new NonStrictExpectations() {{
|
300
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
301
|
+
task.getFormat(); result = "tgz";
|
302
|
+
input.nextFile(); result = true; result = false;
|
303
|
+
input.poll(); result = getResourceAsBuffer("samples.tgz");
|
304
|
+
task.getBufferAllocator(); result = new PooledBufferAllocator();
|
305
|
+
}};
|
306
|
+
|
307
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
308
|
+
FileInput archiveFileInput = plugin.open(taskSource, input);
|
309
|
+
|
310
|
+
verifyContents(archiveFileInput, "1,foo", "2,bar");
|
311
|
+
|
312
|
+
new Verifications() {{
|
313
|
+
input.nextFile(); times = 2;
|
314
|
+
input.close(); times = 1;
|
315
|
+
}};
|
316
|
+
}
|
317
|
+
|
318
|
+
@Test
|
319
|
+
public void testOpenForTarGZFormat(@Mocked final FileInput input) throws Exception
|
320
|
+
{
|
321
|
+
new NonStrictExpectations() {{
|
322
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
323
|
+
task.getFormat(); result = "tar.gz";
|
324
|
+
input.nextFile(); result = true; result = false;
|
325
|
+
input.poll(); result = getResourceAsBuffer("samples.tar.gz");
|
326
|
+
task.getBufferAllocator(); result = new PooledBufferAllocator();
|
327
|
+
}};
|
328
|
+
|
329
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
330
|
+
FileInput archiveFileInput = plugin.open(taskSource, input);
|
331
|
+
|
332
|
+
verifyContents(archiveFileInput, "1,foo", "2,bar");
|
333
|
+
|
334
|
+
new Verifications() {{
|
335
|
+
input.nextFile(); times = 2;
|
336
|
+
input.close(); times = 1;
|
337
|
+
}};
|
338
|
+
}
|
339
|
+
|
340
|
+
// NOTE: This may generate a warn relates to log4j...I am not sure why it is generated.
|
341
|
+
@Test
|
342
|
+
public void testOpenForTarBZ2Format(@Mocked final FileInput input) throws Exception
|
343
|
+
{
|
344
|
+
new NonStrictExpectations() {{
|
345
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
346
|
+
task.getFormat(); result = "tar.bz2";
|
347
|
+
input.nextFile(); result = true; result = false;
|
348
|
+
input.poll(); result = getResourceAsBuffer("samples.tar.bz2");
|
349
|
+
task.getBufferAllocator(); result = new PooledBufferAllocator();
|
350
|
+
}};
|
351
|
+
|
352
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
353
|
+
FileInput archiveFileInput = plugin.open(taskSource, input);
|
354
|
+
|
355
|
+
verifyContents(archiveFileInput, "1,foo", "2,bar");
|
356
|
+
|
357
|
+
new Verifications() {{
|
358
|
+
input.nextFile(); times = 2;
|
359
|
+
input.close(); times = 1;
|
360
|
+
}};
|
361
|
+
}
|
362
|
+
|
363
|
+
// This works well. So, uncompress for tar.Z looks to work.
|
364
|
+
@Test
|
365
|
+
public void testOpenForTarZFormat(@Mocked final FileInput input) throws Exception
|
366
|
+
{
|
367
|
+
new NonStrictExpectations() {{
|
368
|
+
taskSource.loadTask(CommonsCompressDecoderPlugin.PluginTask.class); result = task;
|
369
|
+
task.getFormat(); result = "tar.Z";
|
370
|
+
input.nextFile(); result = true; result = false;
|
371
|
+
input.poll(); result = getResourceAsBuffer("samples.tar.Z");
|
372
|
+
task.getBufferAllocator(); result = new PooledBufferAllocator();
|
373
|
+
}};
|
374
|
+
|
375
|
+
CommonsCompressDecoderPlugin plugin = new CommonsCompressDecoderPlugin();
|
376
|
+
FileInput archiveFileInput = plugin.open(taskSource, input);
|
377
|
+
|
378
|
+
verifyContents(archiveFileInput, "1,foo", "2,bar");
|
379
|
+
|
380
|
+
new Verifications() {{
|
381
|
+
input.nextFile(); times = 2;
|
382
|
+
input.close(); times = 1;
|
383
|
+
}};
|
384
|
+
}
|
385
|
+
|
386
|
+
private Buffer getInputStreamAsBuffer(InputStream in) throws IOException {
|
387
|
+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
388
|
+
byte[] buff = new byte[1024];
|
389
|
+
int len = in.read(buff);
|
390
|
+
while (len != -1) {
|
391
|
+
bout.write(buff, 0, len);
|
392
|
+
len = in.read(buff);
|
393
|
+
}
|
394
|
+
in.close();
|
395
|
+
return Buffer.wrap(bout.toByteArray());
|
396
|
+
}
|
397
|
+
|
398
|
+
private Buffer getResourceAsBuffer(String resource) throws IOException {
|
399
|
+
return getInputStreamAsBuffer(getClass().getResourceAsStream(resource));
|
400
|
+
}
|
401
|
+
|
402
|
+
|
403
|
+
private String readFileInput(FileInput input) throws IOException {
|
404
|
+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
405
|
+
Buffer buffer = input.poll();
|
406
|
+
while (buffer != null) {
|
407
|
+
bout.write(buffer.array(), buffer.offset(), buffer.limit());
|
408
|
+
buffer = input.poll();
|
409
|
+
}
|
410
|
+
return bout.toString().trim();
|
411
|
+
}
|
412
|
+
|
413
|
+
private void verifyContents(FileInput input, String ...contents) throws IOException {
|
414
|
+
for (String expected : contents) {
|
415
|
+
Assert.assertTrue("Verify a file can be read.", input.nextFile());
|
416
|
+
String text = readFileInput(input);
|
417
|
+
Assert.assertEquals("Verify a file read correctly. text:" + text, expected, text);
|
418
|
+
}
|
419
|
+
Assert.assertFalse("Verify there is no file.", input.nextFile());
|
420
|
+
input.close();
|
421
|
+
}
|
422
|
+
|
423
|
+
private InputStream getArchiveInputStream(String format, String ...resourceFiles)
|
424
|
+
throws ArchiveException, URISyntaxException, IOException {
|
425
|
+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
426
|
+
ArchiveStreamFactory factory = new ArchiveStreamFactory();
|
427
|
+
ArchiveOutputStream aout = factory.createArchiveOutputStream(format, bout);
|
428
|
+
|
429
|
+
for (String resource : resourceFiles) {
|
430
|
+
File f = new File(getClass().getResource(resource).toURI());
|
431
|
+
ArchiveEntry entry = aout.createArchiveEntry(f, resource);
|
432
|
+
aout.putArchiveEntry(entry);
|
433
|
+
IOUtils.copy(new FileInputStream(f),aout);
|
434
|
+
aout.closeArchiveEntry();
|
435
|
+
}
|
436
|
+
aout.finish();
|
437
|
+
aout.close();
|
438
|
+
|
439
|
+
return new ByteArrayInputStream(bout.toByteArray());
|
440
|
+
}
|
441
|
+
|
442
|
+
private InputStream getCompressorInputStream(String format, String resource)
|
443
|
+
throws CompressorException, URISyntaxException, IOException {
|
444
|
+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
445
|
+
CompressorStreamFactory factory = new CompressorStreamFactory();
|
446
|
+
CompressorOutputStream aout = factory.createCompressorOutputStream(format, bout);
|
447
|
+
|
448
|
+
File f = new File(getClass().getResource(resource).toURI());
|
449
|
+
IOUtils.copy(new FileInputStream(f), aout);
|
450
|
+
aout.close();
|
451
|
+
|
452
|
+
return new ByteArrayInputStream(bout.toByteArray());
|
453
|
+
}
|
454
|
+
|
455
|
+
private class MockTaskSource implements TaskSource {
|
456
|
+
private final String format;
|
457
|
+
|
458
|
+
MockTaskSource(String format) {
|
459
|
+
this.format = format;
|
460
|
+
}
|
461
|
+
|
462
|
+
@Override
|
463
|
+
public <E> E get(Class<E> arg0, String arg1) {
|
464
|
+
return null;
|
465
|
+
}
|
466
|
+
|
467
|
+
@Override
|
468
|
+
public <E> E get(Class<E> arg0, String arg1, E arg2) {
|
469
|
+
return null;
|
470
|
+
}
|
471
|
+
|
472
|
+
@Override
|
473
|
+
public List<String> getAttributeNames() {
|
474
|
+
return null;
|
475
|
+
}
|
476
|
+
|
477
|
+
@Override
|
478
|
+
public Iterable<Entry<String, JsonNode>> getAttributes() {
|
479
|
+
return null;
|
480
|
+
}
|
481
|
+
|
482
|
+
@Override
|
483
|
+
public ObjectNode getObjectNode() {
|
484
|
+
return null;
|
485
|
+
}
|
486
|
+
|
487
|
+
@Override
|
488
|
+
public boolean isEmpty() {
|
489
|
+
return false;
|
490
|
+
}
|
491
|
+
|
492
|
+
@Override
|
493
|
+
public TaskSource deepCopy() {
|
494
|
+
return null;
|
495
|
+
}
|
496
|
+
|
497
|
+
@Override
|
498
|
+
public TaskSource getNested(String arg0) {
|
499
|
+
return null;
|
500
|
+
}
|
501
|
+
|
502
|
+
@Override
|
503
|
+
public TaskSource getNestedOrSetEmpty(String arg0) {
|
504
|
+
return null;
|
505
|
+
}
|
506
|
+
|
507
|
+
@Override
|
508
|
+
public <T> T loadTask(Class<T> clazz) {
|
509
|
+
if (CommonsCompressDecoderPlugin.PluginTask.class.equals(clazz)) {
|
510
|
+
return clazz.cast(new MockPluginTask(format));
|
511
|
+
}
|
512
|
+
return null;
|
513
|
+
}
|
514
|
+
|
515
|
+
@Override
|
516
|
+
public TaskSource merge(DataSource arg0) {
|
517
|
+
return null;
|
518
|
+
}
|
519
|
+
|
520
|
+
@Override
|
521
|
+
public TaskSource set(String arg0, Object arg1) {
|
522
|
+
return null;
|
523
|
+
}
|
524
|
+
|
525
|
+
@Override
|
526
|
+
public TaskSource setAll(DataSource arg0) {
|
527
|
+
return null;
|
528
|
+
}
|
529
|
+
|
530
|
+
@Override
|
531
|
+
public TaskSource setNested(String arg0, DataSource arg1) {
|
532
|
+
return null;
|
533
|
+
}
|
534
|
+
|
535
|
+
}
|
536
|
+
|
537
|
+
private class MockPluginTask implements CommonsCompressDecoderPlugin.PluginTask {
|
538
|
+
private final String format;
|
539
|
+
|
540
|
+
MockPluginTask(String format) {
|
541
|
+
this.format = format;
|
542
|
+
}
|
543
|
+
|
544
|
+
@Override
|
545
|
+
public TaskSource dump() {
|
546
|
+
return null;
|
547
|
+
}
|
548
|
+
|
549
|
+
@Override
|
550
|
+
public void validate() {
|
551
|
+
}
|
552
|
+
|
553
|
+
@Override
|
554
|
+
public String getFormat() {
|
555
|
+
return format;
|
556
|
+
}
|
557
|
+
|
558
|
+
@Override
|
559
|
+
public BufferAllocator getBufferAllocator() {
|
560
|
+
return new PooledBufferAllocator();
|
561
|
+
}
|
562
|
+
}
|
563
|
+
|
564
|
+
private class MockFileInput implements FileInput {
|
565
|
+
Buffer buffer;
|
566
|
+
int pos;
|
567
|
+
|
568
|
+
MockFileInput(Buffer buffer) {
|
569
|
+
this.buffer = buffer;
|
570
|
+
pos = -1;
|
571
|
+
}
|
572
|
+
|
573
|
+
@Override
|
574
|
+
public void close() {
|
575
|
+
}
|
576
|
+
|
577
|
+
@Override
|
578
|
+
public boolean nextFile() {
|
579
|
+
return buffer != null;
|
580
|
+
}
|
581
|
+
|
582
|
+
@Override
|
583
|
+
public Buffer poll() {
|
584
|
+
if (buffer != null) {
|
585
|
+
Buffer ret = buffer;
|
586
|
+
buffer = null;
|
587
|
+
return ret;
|
588
|
+
} else {
|
589
|
+
return null;
|
590
|
+
}
|
591
|
+
}
|
592
|
+
}
|
593
|
+
}
|