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

import java.io.File;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author ranides
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class PathHelperTest {

    public PathHelperTest() {
    }

    @Test
    public void testExtension() throws PathConvertException {
        assertEquals(".txt", PathHelper.getExtension("file.txt"));
        assertEquals(".txt", PathHelper.getExtension("\\base\\file.txt"));
        assertEquals(".txt", PathHelper.getExtension("/base/file.txt"));
        assertEquals(".txt", PathHelper.getExtension("/base/file.log.txt"));
    }


    @Test
    public void testRelative() throws PathConvertException {

        assertPathEquals("..\\..\\temp", PathHelper.asRelative("base/home/system/drivers", "base/home/temp"));

        
        assertPathEquals("..\\..\\temp", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp"));

        assertPathEquals("..\\..\\temp\\data\\int\\value", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp\\data\\int\\value"));

        assertPathEquals("..\\..\\temp\\data\\int", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp\\data\\int"));

        assertPathEquals("..\\..\\temp\\data", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp\\data"));

        assertPathEquals("..\\..\\temp", PathHelper.asRelative("home\\system\\drivers", "home\\temp"));

        assertPathEquals("..\\..\\temp", PathHelper.asRelative("\\system\\drivers", "\\temp"));

        assertPathEquals("..\\temp", PathHelper.asRelative("base\\home\\system", "base\\home\\temp"));

        assertPathEquals("..\\temp", PathHelper.asRelative("base\\home\\system\\", "base\\home\\temp"));

        assertPathEquals("..\\temp", PathHelper.asRelative("base\\home\\system\\", "base\\home\\temp\\"));

        assertPathEquals("..\\temp", PathHelper.asRelative("base\\home\\system", "base\\home\\temp\\"));

        assertPathEquals("..", PathHelper.asRelative("base\\home\\system", "base\\home\\"));

        assertPathEquals("..", PathHelper.asRelative("base\\home\\system", "base\\home"));

    }
    
    @Test
    public void testConcat() throws PathConvertException {
        assertPathEquals("home\\file\\name", PathHelper.join("home", "file", "name"));
        assertPathEquals("\\root\\home\\file\\name", PathHelper.join("/root/home", "file", "name"));
        
        assertPathEquals("home\\file\\name", PathHelper.join(new File("home"), new File("file"), new File("name")));
        assertPathEquals("\\root\\home\\file\\name", PathHelper.join(new File("/root/home"), new File("file"), new File("name")));
        
        assertPathEquals("home\\file\\name", PathHelper.join(new File("home"), "file", "name"));
        assertPathEquals("home\\path\\file\\name", PathHelper.join(new File("home"), "path/file", "name"));

    }

    private static void assertPathEquals(String expected, String value) {
        assertEquals(PathHelper.asFile(expected), PathHelper.asFile(value));
    }
}
