/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.pdfbox.text;

import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Test for the PDButton class.
 *
 */
public class BidiTest
{
    private static final File IN_DIR = new File("src/test/resources/org/apache/pdfbox/text/");
    private static final String NAME_OF_PDF = "BidiSample.pdf";
    private static final String NAME_OF_TXT = "BidiSample.pdf.txt";
    private static final String NAME_OF_TXT_SORTED = "BidiSample.pdf-sorted.txt";

    private PDDocument document;
    private PDFTextStripper stripper;

    @Before
    public void setUp() throws IOException
    {
        document = PDDocument.load(new File(IN_DIR, NAME_OF_PDF));
        stripper = new PDFTextStripper();
        stripper.setLineSeparator("\n");
    }

    @Test
    public void testSorted() throws IOException
    {
        stripper.setSortByPosition(true);
        String extractedText = stripper.getText(document);

        FileReader compareTextReader = new FileReader(new File(IN_DIR, NAME_OF_TXT_SORTED));
        BufferedReader bufferedCompareTextReader = new BufferedReader(compareTextReader);

        StringBuilder compareTextBuilder = new StringBuilder();

        String line = bufferedCompareTextReader.readLine();

        while (line != null)
        {
            compareTextBuilder.append(line);
            compareTextBuilder.append('\n');
            line = bufferedCompareTextReader.readLine();
        }

        bufferedCompareTextReader.close();

        assertEquals(extractedText, compareTextBuilder.toString());

    }

    @Test
    public void testNotSorted() throws IOException
    {
        stripper.setSortByPosition(false);
        String extractedText = stripper.getText(document);

        FileReader compareTextReader = new FileReader(new File(IN_DIR, NAME_OF_TXT));
        BufferedReader bufferedCompareTextReader = new BufferedReader(compareTextReader);

        StringBuilder compareTextBuilder = new StringBuilder();
        String line = bufferedCompareTextReader.readLine();

        while (line != null)
        {
            compareTextBuilder.append(line);
            compareTextBuilder.append('\n');
            line = bufferedCompareTextReader.readLine();
        }

        bufferedCompareTextReader.close();

        assertEquals(extractedText, compareTextBuilder.toString());

    }

    @After
    public void tearDown() throws IOException
    {
        document.close();
    }

}

@@ -947,22 +947,27 @@
 .io.File
-Reader
+InputStream
 ;%0Aimport
@@ -987,16 +987,73 @@
 ception;
+%0Aimport java.io.InputStreamReader;%0Aimport java.io.Reader;
 %0A%0Aimport
@@ -1539,16 +1539,73 @@
 ed.txt%22;
+%0A    %0A    private static final String ENCODING = %22UTF-8%22;
 %0A%0A    pr
@@ -2054,36 +2054,32 @@
 ment);%0A%0A        
-File
 Reader compareTe
@@ -2085,36 +2085,43 @@
 extReader = new 
-File
+InputStream
 Reader(new File(
@@ -2107,32 +2107,52 @@
 treamReader(new 
+FileInputStream(new 
 File(IN_DIR, NAM
@@ -2168,16 +2168,28 @@
 SORTED))
+, ENCODING);
 ;%0A      
@@ -2894,20 +2894,16 @@
         
-File
 Reader c
@@ -2925,20 +2925,27 @@
 r = new 
-File
+InputStream
 Reader(n
@@ -2943,24 +2943,44 @@
 mReader(new 
+FileInputStream(new 
 File(IN_DIR,
@@ -2993,16 +2993,28 @@
 OF_TXT))
+, ENCODING);
 ;%0A      

define encoding for Reader