pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / test / org / apache / lucene / benchmark / byTask / utils / StreamUtilsTest.java
1 package org.apache.lucene.benchmark.byTask.utils;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.BufferedReader;
21 import java.io.BufferedWriter;
22 import java.io.File;
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;
30
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;
38
39 public class StreamUtilsTest extends BenchmarkTestCase {
40   private static final String TEXT = "Some-Text..."; 
41   private File testDir;
42   
43   @Test
44   public void testGetInputStreamPlainText() throws Exception {
45     assertReadText(rawTextFile("txt"));
46     assertReadText(rawTextFile("TXT"));
47   }
48
49   @Test
50   public void testGetInputStreamGzip() throws Exception {
51     assertReadText(rawGzipFile("gz"));
52     assertReadText(rawGzipFile("gzip"));
53     assertReadText(rawGzipFile("GZ"));
54     assertReadText(rawGzipFile("GZIP"));
55   }
56
57   @Test
58   public void testGetInputStreamBzip2() throws Exception {
59         assertReadText(rawBzip2File("bz2"));
60         assertReadText(rawBzip2File("bzip"));
61         assertReadText(rawBzip2File("BZ2"));
62         assertReadText(rawBzip2File("BZIP"));
63   }
64
65   @Test
66   public void testGetOutputStreamBzip2() throws Exception {
67         assertReadText(autoOutFile("bz2"));
68         assertReadText(autoOutFile("bzip"));
69         assertReadText(autoOutFile("BZ2"));
70         assertReadText(autoOutFile("BZIP"));
71   }
72   
73   @Test
74   public void testGetOutputStreamGzip() throws Exception {
75         assertReadText(autoOutFile("gz"));
76         assertReadText(autoOutFile("gzip"));
77         assertReadText(autoOutFile("GZ"));
78         assertReadText(autoOutFile("GZIP"));
79   }
80
81   @Test
82   public void testGetOutputStreamPlain() throws Exception {
83         assertReadText(autoOutFile("txt"));
84         assertReadText(autoOutFile("text"));
85         assertReadText(autoOutFile("TXT"));
86         assertReadText(autoOutFile("TEXT"));
87   }
88   
89   private File rawTextFile(String ext) throws Exception {
90     File f = new File(testDir,"testfile." +     ext);
91     BufferedWriter w = new BufferedWriter(new FileWriter(f));
92     w.write(TEXT);
93     w.newLine();
94     w.close();
95     return f;
96   }
97   
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));
101     writeText(os);
102     return f;
103   }
104
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));
108         writeText(os);
109         return f;
110   }
111
112   private File autoOutFile(String ext) throws Exception {
113         File f = new File(testDir,"testfile." + ext);
114         OutputStream os = StreamUtils.outputStream(f);
115         writeText(os);
116         return f;
117   }
118
119         private void writeText(OutputStream os) throws IOException {
120                 BufferedWriter w = new BufferedWriter(new OutputStreamWriter(os));
121         w.write(TEXT);
122         w.newLine();
123         w.close();
124         }
125
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);
132     r.close();
133   }
134   
135   @Override
136   @Before
137   public void setUp() throws Exception {
138     super.setUp();
139     testDir = new File(getWorkDir(),"ContentSourceTest");
140     _TestUtil.rmDir(testDir);
141     assertTrue(testDir.mkdirs());
142   }
143
144   @Override
145   @After
146   public void tearDown() throws Exception {
147     _TestUtil.rmDir(testDir);
148     super.tearDown();
149   }
150
151 }