/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.io.uri.impl;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import net.ranides.assira.io.uri.URIHandle;
import net.ranides.assira.io.uri.URIResolver;
import net.ranides.assira.text.IOStrings;
import static net.ranides.assira.junit.NewAssert.*;
import net.ranides.assira.text.Charsets;

import org.junit.Test;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class CFTPHandlerTest {
    
    private static final String TEXT_1 = "Źrebię patrzy na słońce.";
    
    private static final String TEXT_2 = "infov1";

    @Test
    public void testUser() throws URISyntaxException, IOException {
        URIResolver resolver = new URIResolver().register(CFTPHandler.class);
        
        URI u1 = new URI("ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/readme.txt");
        
        URIHandle handle1 = resolver.resolve(u1);
        assertEquals(TEXT_1, IOStrings.read(handle1.reader()));
//        handle1.close();
        
        URI u2 = new URI("ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/info.txt");
        URIHandle handle2 = resolver.resolve(u2);
        assertEquals(TEXT_2, IOStrings.read(handle2.reader()));
        
        handle1.close();
        handle2.close();
    }
    
    @Test
    public void testCopy() throws URISyntaxException, IOException {
        URIResolver resolver = new URIResolver().register(CFTPHandler.class);
        
        URI u1 = new URI("ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/readme.txt");
        URI u2 = new URI("ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/out-1.txt");
        String s3 = "ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/out-2.txt?charset=windows-1250";
        try (URIHandle target = resolver.resolve(u2)) {
            
            assertFalse(target.exists());
            
            try (URIHandle source = resolver.resolve(u1)) {
                source.copy(target);
            }
            try (URIHandle source = resolver.resolve(u1)) {
                source.copy(s3);
            }
            
            assertEquals(TEXT_1, IOStrings.read(target.reader()));
            assertTrue(target.exists());
            
            target.delete();
            assertFalse(target.exists());
            
            try(URIHandle u3 = resolver.resolve(s3)) {
                assertNotEquals(TEXT_1, IOStrings.read(u3.reader()));
                assertEquals(cast(TEXT_1, Charsets.UTF8 ,Charsets.PL_WINDOWS), IOStrings.read(u3.reader()));
                u3.delete();
            }
        }
        
    }
    
    private static String cast(String text, Charset source, Charset target) {
        return new String(text.getBytes(source), target);
    }
    
}
