@@ -0,0 +1,3687 @@
+/*%0A * Licensed to the Apache Software Foundation (ASF) under one or more%0A * contributor license agreements.  See the NOTICE file distributed with%0A * this work for additional information regarding copyright ownership.%0A * The ASF licenses this file to You under the Apache License, Version 2.0%0A * (the %22License%22); you may not use this file except in compliance with%0A * the License.  You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage org.apache.tika.io;%0A%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0A%0Aimport junit.framework.TestCase;%0A%0Apublic class TikaInputStreamTest extends TestCase %7B%0A%0A    public void testFileBased() throws IOException %7B%0A        File file = createTempFile(%22Hello, World!%22);%0A        InputStream stream = new TikaInputStream(file);%0A%0A        assertEquals(%0A                %22The file returned by the getFile() method should%22%0A                + %22 be the file used to instantiate a TikaInputStream%22,%0A                file, TikaInputStream.get(stream).getFile());%0A%0A        assertEquals(%0A                %22The contents of the TikaInputStream should equal the%22%0A                + %22 contents of the underlying file%22,%0A                %22Hello, World!%22, readStream(stream));%0A%0A        stream.close();%0A        assertTrue(%0A                %22The close() method must not remove the file used to%22%0A                + %22 instantiate a TikaInputStream%22,%0A                file.exists());%0A%0A        file.delete();%0A    %7D%0A%0A    public void testStreamBased() throws IOException %7B%0A        InputStream input =%0A            new ByteArrayInputStream(%22Hello, World!%22.getBytes(%22UTF-8%22));%0A        InputStream stream = new TikaInputStream(input);%0A%0A        File file = TikaInputStream.get(stream).getFile();%0A        assertTrue(file != null && file.isFile());%0A%0A        assertEquals(%0A                %22The contents of the file returned by the getFile method%22%0A                + %22 should equal the contents of the TikaInputStream%22,%0A                %22Hello, World!%22, readFile(file));%0A%0A        assertEquals(%0A                %22The contents of the TikaInputStream should not get modified%22%0A                + %22 by reading the file first%22,%0A                %22Hello, World!%22, readStream(stream));%0A%0A        stream.close();%0A        assertFalse(%0A                %22The close() method must remove the temporary file created%22%0A                + %22 by a TikaInputStream%22,%0A                file.exists());%0A    %7D%0A%0A    private File createTempFile(String data) throws IOException %7B%0A        File file = File.createTempFile(%22tika-%22, %22.tmp%22);%0A        OutputStream stream = new FileOutputStream(file);%0A        try %7B%0A            stream.write(data.getBytes(%22UTF-8%22));%0A        %7D finally %7B%0A            stream.close();%0A        %7D%0A        return file;%0A    %7D%0A%0A    private String readFile(File file) throws IOException %7B%0A        InputStream stream = new FileInputStream(file);%0A        try %7B%0A            return readStream(stream);%0A        %7D finally %7B%0A            stream.close();%0A        %7D%0A    %7D%0A%0A    private String readStream(InputStream stream) throws IOException %7B%0A        ByteArrayOutputStream buffer = new ByteArrayOutputStream();%0A        IOUtils.copy(stream, buffer);%0A        return buffer.toString(%22UTF-8%22);%0A    %7D%0A%0A%7D%0A

Allow passing of files or memory buffers to parsers