<commit_before>/*
 * Copyright (C) 2016 <NAME>
 *
 * 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.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();
    }

    public void testCreatingFileFromFileName() throws IOException {
        VirtualFile file = new ResourceVirtualFile("tests/vfs/test.xq");
        assertThat(file.getName(), is("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()"));
    }

    public void testCreatingFileFromFileNameAndClassLoader() throws IOException {
        VirtualFile file = new ResourceVirtualFile(ResourceVirtualFileTest.class.getClassLoader(), "tests/vfs/test.xq");
        assertThat(file.getName(), is("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()"));
    }

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

    public void testDirectory() throws IOException {
        VirtualFile file = new ResourceVirtualFile("tests/vfs");
        assertThat(file.getName(), is("tests/vfs"));
        assertThat(file.isWritable(), is(false));
        assertThat(file.isDirectory(), is(true));
        assertThat(file.isValid(), is(true));
        assertThat(file.getInputStream(), is(nullValue()));
    }
}<commit_msg>ResourceVirtualFile: The current tests are for file system based resources.<commit_after>/*
 * Copyright (C) 2016 <NAME>
 *
 * 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.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.isWritable(), is(false));
        assertThat(file.isDirectory(), is(false));
        assertThat(file.isValid(), is(true));
        assertThat(streamToString(file.getInputStream()), is("xquery version \"3.0\"; true()"));
    }

    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.isWritable(), is(false));
        assertThat(file.isDirectory(), is(false));
        assertThat(file.isValid(), is(true));
        assertThat(streamToString(file.getInputStream()), is("xquery version \"3.0\"; true()"));
    }

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

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

    // endregion
}
{'fraction_non_alphanumeric': 0.12529365700861395, 'fraction_numerical': 0.003758809710258418, 'mean_word_length': 4.03230890464933, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4049312', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1748, 'n_words': 435}