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

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;
import org.junit.Ignore;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 * @todo (assira #7) PathUtils: inconsistent name separators linux/windows
 */
public class PathUtilsTest {
    
    @Test
    public void testGetExtension() {
        assertEquals(".txt", PathUtils.getExtension("file.txt"));
        assertEquals(".txt", PathUtils.getExtension("\\base\\file.txt"));
        assertEquals(".txt", PathUtils.getExtension("/base/file.txt"));
        assertEquals(".txt", PathUtils.getExtension("/base/file.log.txt"));
    }
    
    @Test
    public void testChangeExtension() {
        assertEquals(Paths.get("file.txt"), PathUtils.changeExtension("file.txt", ".txt"));
        assertEquals(Paths.get("file.dat"), PathUtils.changeExtension("file.txt", ".dat"));
        assertEquals(Paths.get("file.txt.dat"), PathUtils.changeExtension("file.txt.info", ".dat"));
        assertEquals(Paths.get("file.dat"), PathUtils.changeExtension("file", ".dat"));
        
        assertEquals(Paths.get("base","name","file.txt"), PathUtils.changeExtension("base/name/file.txt", ".txt"));
        assertEquals(Paths.get("base","name","file.dat"), PathUtils.changeExtension("base\\name\\file.txt", ".dat"));
        assertEquals(Paths.get("c:","base","name","file.dat"), PathUtils.changeExtension("c:\\base\\name\\file.txt", ".dat"));
        assertEquals(Paths.get("/base","name","file.dat"), PathUtils.changeExtension("/base/name\\file.txt", ".dat"));
    }
    
    @Test
    public void testChangeExtensionP() {
        assertEquals(Paths.get("file.txt"), pce("file.txt", ".txt"));
        assertEquals(Paths.get("file.dat"), pce("file.txt", ".dat"));
        assertEquals(Paths.get("file.txt.dat"), pce("file.txt.info", ".dat"));
        assertEquals(Paths.get("file.dat"), pce("file", ".dat"));
        
        assertEquals(Paths.get("base","name","file.txt"), pce("base/name/file.txt", ".txt"));
        assertEquals(Paths.get("base","name","file.dat"), pce("base\\name\\file.txt", ".dat"));
        assertEquals(Paths.get("c:","base","name","file.dat"), pce("c:\\base\\name\\file.txt", ".dat"));
        assertEquals(Paths.get("/base","name","file.dat"), pce("/base/name\\file.txt", ".dat"));
    }
    
    private static Path pce(String path, String ext) {
        return PathUtils.changeExtension(Paths.get(path), ext);
    }
    
    @Test
    public void testGetSimpleName() {
        assertEquals("file", PathUtils.getSimpleName("c:/ms/file"));
        assertEquals("file", PathUtils.getSimpleName("c:/ms/file.txt"));
        assertEquals("file", PathUtils.getSimpleName("c:\\ms\\file.txt"));
        assertEquals("file", PathUtils.getSimpleName("file.txt"));
        assertEquals("file", PathUtils.getSimpleName("/ms/file.txt"));
        
        assertEquals("file.info", PathUtils.getSimpleName("c:/ms/file.info."));
        assertEquals("file.info", PathUtils.getSimpleName("c:/ms/file.info.txt"));
        assertEquals("file.info", PathUtils.getSimpleName("c:\\ms\\file.info.txt"));
        assertEquals("file.info", PathUtils.getSimpleName("file.info.txt"));
        assertEquals("file.info", PathUtils.getSimpleName("/ms/file.info.txt"));
    }
    
    @Test
    public void testFromURL() throws MalformedURLException {
        assertThrows(FileSystemNotFoundException.class, ()->{
            PathUtils.fromURL(new URL("http://ranides.net/index.htm"));
        });
        assertThrows(FileSystemNotFoundException.class, ()->{
            PathUtils.fromURL(new URL("ftp://ranides.net/index.htm"));
        });

//        System.out.printf("%s%n", PathUtils.fromURL(new URL("file://ranides.net/index.htm")));
//        System.out.printf("%s%n", PathUtils.fromURL(new URL("file://c:/ms/index.htm")));
        assertEquals(Paths.get("/ranides.net/index.htm"), PathUtils.fromURL(new URL("file:///ranides.net/index.htm")));
        assertEquals(Paths.get("c:/ms/index.htm"), PathUtils.fromURL(new URL("file:///c:/ms/index.htm")));
        
                
        assertThrows(IllegalArgumentException.class, ()->{
            PathUtils.fromURL(new URL("file://c:/ms/index.htm?query"));
        });
    }
    
}
