/*
 * Copyright 2012-2017 MarkLogic Corporation
 *
 * Licensed 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 com.marklogic.client.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;

import org.junit.Test;

import com.marklogic.client.impl.HandleAccessor;
import com.marklogic.client.io.BytesHandle;
import com.marklogic.client.io.FileHandle;
import com.marklogic.client.io.InputStreamHandle;
import com.marklogic.client.io.ReaderHandle;
import com.marklogic.client.io.StringHandle;
import java.io.InputStream;
public class HandleAccessorTest {
    public static boolean fileInputStreamWasClosed;

	@Test
    public void testContentAsString() throws URISyntaxException, IOException {
        // I'm purposely using a string with a non-ascii character to test 
        // charset issues
        String hola = "¡Hola!";
        System.out.println("Default Java Charset: " + Charset.defaultCharset());
        assertEquals("String content mismatch", hola, 
            HandleAccessor.contentAsString(new StringHandle(hola)));
        assertEquals("byte[] content mismatch", hola, 
            HandleAccessor.contentAsString(new BytesHandle(hola.getBytes("UTF-8"))));
        URL filePath = this.getClass().getClassLoader().getResource("hola.txt");
        assertEquals("Reader content mismatch", hola, 
            HandleAccessor.contentAsString(new ReaderHandle(new StringReader(hola))));
        assertEquals("File content mismatch", hola, 
            HandleAccessor.contentAsString(new FileHandle(new File(filePath.toURI()))));      
        assertEquals("InputStream content mismatch", hola, 
            HandleAccessor.contentAsString(new InputStreamHandle(filePath.openStream())));

        InputStream fileInputStream = new FileInputStream(new File(filePath.toURI())) {
                @Override
                public void close() throws IOException {
                    super.close();
                    HandleAccessorTest.fileInputStreamWasClosed = true;
                }
            };
        assertEquals("InputStream content mismatch", hola, 
            HandleAccessor.contentAsString(new InputStreamHandle(fileInputStream)));
        assertTrue(this.fileInputStreamWasClosed);
    }
}

@@ -1290,18 +1290,16 @@
 rTest %7B%0A
-  
   public
@@ -1345,17 +1345,16 @@
 d;%0A%0A
-%09
+  
 @Test%0A
-  
   pu
@@ -1430,20 +1430,16 @@
 n %7B%0A    
-  
-  
 // I'm p
@@ -1498,20 +1498,16 @@
 o test %0A
-    
     // c
@@ -1524,20 +1524,16 @@
 ues%0A    
-   
- 
 String h
@@ -1548,20 +1548,16 @@
 Hola!%22;%0A
-    
     Syst
@@ -1621,28 +1621,24 @@
 Charset());%0A
-    
     assertEq
@@ -1676,36 +1676,32 @@
  hola, %0A        
-    
 HandleAccessor.c
@@ -1737,28 +1737,24 @@
 le(hola)));%0A
-    
     assertEq
@@ -1792,36 +1792,32 @@
  hola, %0A        
- 
-   
 HandleAccessor.c
@@ -1878,20 +1878,16 @@
 ));%0A    
-  
-  
 URL file
@@ -1951,28 +1951,24 @@
 .txt%22);%0A    
-  
-  
 assertEquals
@@ -1994,36 +1994,32 @@
 smatch%22, hola, %0A
-    
         HandleAc
@@ -2085,28 +2085,24 @@
 la))));%0A    
-   
- 
 assertEquals
@@ -2126,36 +2126,32 @@
 smatch%22, hola, %0A
-    
         HandleAc
@@ -2225,20 +2225,16 @@
 ;      %0A
-    
     asse
@@ -2281,36 +2281,32 @@
  hola, %0A        
-    
 HandleAccessor.c
@@ -2373,20 +2373,16 @@
 );%0A%0A    
- 
-   
 InputStr
@@ -2453,26 +2453,16 @@
 I())) %7B%0A
-          
       @O
@@ -2469,26 +2469,16 @@
 verride%0A
-          
       pu
@@ -2516,26 +2516,16 @@
 ption %7B%0A
-          
         
@@ -2551,26 +2551,16 @@
         
-       
-   
 HandleAc
@@ -2613,39 +2613,17 @@
     
-          %7D%0A            %7D;%0A    
+%7D%0A    %7D;%0A
     
@@ -2678,28 +2678,24 @@
 a, %0A        
-  
-  
 HandleAccess
@@ -2755,20 +2755,16 @@
 eam)));%0A
-    
     asse
@@ -2802,16 +2802,14 @@
 losed);%0A
-  
   %7D%0A%7D%0A

change indent to 2 spaces