1
package org.apache.lucene.benchmark.byTask.utils;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import java.io.BufferedReader;
21
import java.io.BufferedWriter;
23
import java.io.FileOutputStream;
24
import java.io.FileWriter;
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.io.InputStreamReader;
28
import java.io.OutputStream;
29
import java.io.OutputStreamWriter;
31
import org.apache.commons.compress.compressors.CompressorStreamFactory;
32
import org.apache.lucene.benchmark.BenchmarkTestCase;
33
import org.apache.lucene.benchmark.byTask.utils.StreamUtils;
34
import org.apache.lucene.util._TestUtil;
35
import org.junit.After;
36
import org.junit.Before;
37
import org.junit.Test;
39
public class StreamUtilsTest extends BenchmarkTestCase {
40
private static final String TEXT = "Some-Text...";
44
public void testGetInputStreamPlainText() throws Exception {
45
assertReadText(rawTextFile("txt"));
46
assertReadText(rawTextFile("TXT"));
50
public void testGetInputStreamGzip() throws Exception {
51
assertReadText(rawGzipFile("gz"));
52
assertReadText(rawGzipFile("gzip"));
53
assertReadText(rawGzipFile("GZ"));
54
assertReadText(rawGzipFile("GZIP"));
58
public void testGetInputStreamBzip2() throws Exception {
59
assertReadText(rawBzip2File("bz2"));
60
assertReadText(rawBzip2File("bzip"));
61
assertReadText(rawBzip2File("BZ2"));
62
assertReadText(rawBzip2File("BZIP"));
66
public void testGetOutputStreamBzip2() throws Exception {
67
assertReadText(autoOutFile("bz2"));
68
assertReadText(autoOutFile("bzip"));
69
assertReadText(autoOutFile("BZ2"));
70
assertReadText(autoOutFile("BZIP"));
74
public void testGetOutputStreamGzip() throws Exception {
75
assertReadText(autoOutFile("gz"));
76
assertReadText(autoOutFile("gzip"));
77
assertReadText(autoOutFile("GZ"));
78
assertReadText(autoOutFile("GZIP"));
82
public void testGetOutputStreamPlain() throws Exception {
83
assertReadText(autoOutFile("txt"));
84
assertReadText(autoOutFile("text"));
85
assertReadText(autoOutFile("TXT"));
86
assertReadText(autoOutFile("TEXT"));
89
private File rawTextFile(String ext) throws Exception {
90
File f = new File(testDir,"testfile." + ext);
91
BufferedWriter w = new BufferedWriter(new FileWriter(f));
98
private File rawGzipFile(String ext) throws Exception {
99
File f = new File(testDir,"testfile." + ext);
100
OutputStream os = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.GZIP, new FileOutputStream(f));
105
private File rawBzip2File(String ext) throws Exception {
106
File f = new File(testDir,"testfile." + ext);
107
OutputStream os = new CompressorStreamFactory().createCompressorOutputStream(CompressorStreamFactory.BZIP2, new FileOutputStream(f));
112
private File autoOutFile(String ext) throws Exception {
113
File f = new File(testDir,"testfile." + ext);
114
OutputStream os = StreamUtils.outputStream(f);
119
private void writeText(OutputStream os) throws IOException {
120
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os));
126
private void assertReadText(File f) throws Exception {
127
InputStream ir = StreamUtils.inputStream(f);
128
InputStreamReader in = new InputStreamReader(ir);
129
BufferedReader r = new BufferedReader(in);
130
String line = r.readLine();
131
assertEquals("Wrong text found in "+f.getName(), TEXT, line);
137
public void setUp() throws Exception {
139
testDir = new File(getWorkDir(),"ContentSourceTest");
140
_TestUtil.rmDir(testDir);
141
assertTrue(testDir.mkdirs());
146
public void tearDown() throws Exception {
147
_TestUtil.rmDir(testDir);