/*
 * Copyright (C) 2016 Reece H. Dunn
 *
 * 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 uk.co.reecedunn.intellij.plugin.core.tests.vfs;

import com.intellij.openapi.vfs.VirtualFile;
import junit.framework.TestCase;
import org.apache.xmlbeans.impl.common.IOUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;

import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

public class ResourceVirtualFileTest extends TestCase {
    public String streamToString(InputStream stream) throws IOException {
        StringWriter writer = new StringWriter();
        IOUtil.copyCompletely(new InputStreamReader(stream), writer);
        return writer.toString();
    }

    // region File System

    public void testFileSystem_CreatingFileFromFileName() throws IOException {
        VirtualFile file = new ResourceVirtualFile("tests/vfs/test.xq");
        assertThat(file.getName(), is("tests/vfs/test.xq"));
        assertThat(file.getPath(), endsWith("/tests/vfs/test.xq"));
        assertThat(file.isWritable(), is(false));
        assertThat(file.isDirectory(), is(false));
        assertThat(file.isValid(), is(true));
        assertThat(streamToString(file.getInputStream()), is("xquery version \"3.0\"; true()"));

        VirtualFile parent = file.getParent();
        assertThat(parent, is(notNullValue()));
        assertThat(parent.getName(), is("tests/vfs"));
        assertThat(parent.isDirectory(), is(true));
        assertThat(parent.isValid(), is(true));

        assertThat(parent.getParent(), is(notNullValue()));

        VirtualFile[] children = file.getChildren();
        assertThat(children, is(nullValue()));
    }

    public void testFileSystem_CreatingFileFromFileNameAndClassLoader() throws IOException {
        VirtualFile file = new ResourceVirtualFile(ResourceVirtualFileTest.class.getClassLoader(), "tests/vfs/test.xq");
        assertThat(file.getName(), is("tests/vfs/test.xq"));
        assertThat(file.getPath(), endsWith("/tests/vfs/test.xq"));
        assertThat(file.isWritable(), is(false));
        assertThat(file.isDirectory(), is(false));
        assertThat(file.isValid(), is(true));
        assertThat(streamToString(file.getInputStream()), is("xquery version \"3.0\"; true()"));

        VirtualFile parent = file.getParent();
        assertThat(parent, is(notNullValue()));
        assertThat(parent.getName(), is("tests/vfs"));
        assertThat(parent.isDirectory(), is(true));
        assertThat(parent.isValid(), is(true));

        assertThat(parent.getParent(), is(notNullValue()));

        VirtualFile[] children = file.getChildren();
        assertThat(children, is(nullValue()));
    }

    public void testFileSystem_InvalidFilePath() throws IOException {
        VirtualFile file = new ResourceVirtualFile("tests/vfs/test.xqy");
        assertThat(file.getName(), is("tests/vfs/test.xqy"));
        assertThat(file.getPath(), is(""));
        assertThat(file.isWritable(), is(false));
        assertThat(file.isDirectory(), is(false));
        assertThat(file.isValid(), is(false));
        assertThat(file.getInputStream(), is(nullValue()));

        VirtualFile parent = file.getParent();
        assertThat(parent, is(notNullValue()));
        assertThat(parent.getName(), is("tests/vfs"));
        assertThat(parent.isDirectory(), is(true));
        assertThat(parent.isValid(), is(true));

        assertThat(parent.getParent(), is(notNullValue()));

        VirtualFile[] children = file.getChildren();
        assertThat(children, is(nullValue()));
    }

    public void testFileSystem_Directory() throws IOException {
        VirtualFile file = new ResourceVirtualFile("tests/vfs");
        assertThat(file.getName(), is("tests/vfs"));
        assertThat(file.getPath(), endsWith("/tests/vfs"));
        assertThat(file.isWritable(), is(false));
        assertThat(file.isDirectory(), is(true));
        assertThat(file.isValid(), is(true));
        assertThat(file.getInputStream(), is(nullValue()));

        VirtualFile parent = file.getParent();
        assertThat(parent, is(notNullValue()));
        assertThat(parent.getName(), is("tests"));
        assertThat(parent.isDirectory(), is(true));
        assertThat(parent.isValid(), is(true));

        assertThat(parent.getParent(), is(nullValue()));

        VirtualFile[] children = file.getChildren();
        assertThat(children, is(notNullValue()));
        assertThat(children.length, is(1));
        assertThat(children[0].getName(), is("tests/vfs/test.xq"));
    }

    // endregion
}

@@ -937,121 +937,9 @@
 ers.
-endsWith;%0Aimport static org.hamcrest.CoreMatchers.notNullValue;%0Aimport static org.hamcrest.CoreMatchers.nullValue
+*
 ;%0Aim
@@ -1587,32 +1587,38 @@
 file.getPath(), 
+anyOf(
 endsWith(%22/tests
@@ -1623,32 +1623,68 @@
 ts/vfs/test.xq%22)
+, endsWith(%22%5C%5Ctests%5C%5Cvfs%5C%5Ctest.xq%22))
 );%0A        asser
@@ -2636,32 +2636,38 @@
 file.getPath(), 
+anyOf(
 endsWith(%22/tests
@@ -2672,32 +2672,68 @@
 ts/vfs/test.xq%22)
+, endsWith(%22%5C%5Ctests%5C%5Cvfs%5C%5Ctest.xq%22))
 );%0A        asser
@@ -4478,16 +4478,22 @@
 Path(), 
+anyOf(
 endsWith
@@ -4498,32 +4498,59 @@
 th(%22/tests/vfs%22)
+, endsWith(%22%5C%5Ctests%5C%5Cvfs%22))
 );%0A        asser

Fix running the tests on Windows.