<commit_before>/*
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * 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 org.apache.commons.io;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.testtools.FileBasedTestCase;

import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

/**
 * This is used to test FilenameUtils for correctness.
 *
 * @author <NAME>
 * @author <NAME>
 * @version $Id: FilenameUtilsTestCase.java,v 1.9 2004/10/24 00:10:05 martinc Exp $
 * @see FilenameUtils
 */
public class FilenameUtilsTestCase extends FileBasedTestCase {

    private File testFile1;
    private File testFile2;

    private static int testFile1Size;
    private static int testFile2Size;

    public static void main(String[] args) {
        TestRunner.run(suite());
    }

    public static Test suite() {
        return new TestSuite(FilenameUtilsTestCase.class);
    }

    public FilenameUtilsTestCase(String name) throws IOException {
        super(name);

        testFile1 = new File(getTestDirectory(), "file1-test.txt");
        testFile2 = new File(getTestDirectory(), "file1a-test.txt");

        testFile1Size = (int)testFile1.length();
        testFile2Size = (int)testFile2.length();
    }

    /** @see junit.framework.TestCase#setUp() */
    protected void setUp() throws Exception {
        getTestDirectory().mkdirs();
        createFile(testFile1, testFile1Size);
        createFile(testFile2, testFile2Size);
        FileUtils.deleteDirectory(getTestDirectory());
        getTestDirectory().mkdirs();
        createFile(testFile1, testFile1Size);
        createFile(testFile2, testFile2Size);
    }

    /** @see junit.framework.TestCase#tearDown() */
    protected void tearDown() throws Exception {
        FileUtils.deleteDirectory(getTestDirectory());
    }

    // mkdir

    public void testMkdir() {
        File dir = new File(getTestDirectory(), "testdir");
        FilenameUtils.mkdir(dir.getAbsolutePath());
        dir.deleteOnExit();
    }

    // removePath

    public void testRemovePath() {
        String fileName =
            FilenameUtils.removePath(
                new File(getTestDirectory(), getName()).getAbsolutePath());
        assertEquals(getName(), fileName);
    }

    // getPath

    public void testGetPath() {
        String fileName =
            FilenameUtils.getPath(
                new File(getTestDirectory(), getName()).getAbsolutePath());
        assertEquals(getTestDirectory().getAbsolutePath(), fileName);
    }

    // catPath

    public void testCatPath() {
        // TODO StringIndexOutOfBoundsException thrown if file doesn't contain slash.
        // Is this acceptable?
        //assertEquals("", FilenameUtils.catPath("a", "b"));

        assertEquals("/a" + File.separator + "c", FilenameUtils.catPath("/a/b", "c"));
        assertEquals("/a" + File.separator + "d", FilenameUtils.catPath("/a/b/c", "../d"));
        assertEquals("C:\\a" + File.separator + "c", FilenameUtils.catPath("C:\\a\\b", "c"));
        assertEquals("C:\\a" + File.separator + "d", FilenameUtils.catPath("C:\\a\\b\\c", "../d"));
    }

    public void testIndexOfLastPathSeparator() {
        assertEquals(-1, FilenameUtils.indexOfLastPathSeparator("noseperator.inthispath"));
        assertEquals(3, FilenameUtils.indexOfLastPathSeparator("a/b/c"));
        assertEquals(3, FilenameUtils.indexOfLastPathSeparator("a\\b\\c"));
    }

    // resolveFile

    public void testResolveFileDotDot() throws Exception {
        File file = FilenameUtils.resolveFile(getTestDirectory(), "..");
        assertEquals(
            "Check .. operator",
            file,
            getTestDirectory().getParentFile());
    }

    public void testResolveFileDot() throws Exception {
        File file = FilenameUtils.resolveFile(getTestDirectory(), ".");
        assertEquals("Check . operator", file, getTestDirectory());
    }

    // normalize

    public void testNormalize() throws Exception {
        String[] src =
            {
                "",
                "/",
                "///",
                "/foo",
                "/foo//",
                "/./",
                "/foo/./",
                "/foo/./bar",
                "/foo/../bar",
                "/foo/../bar/../baz",
                "/foo/bar/../../baz",
                "/././",
                "/foo/./../bar",
                "/foo/.././bar/",
                "//foo//./bar",
                "/../",
                "/foo/../../" };

        String[] dest =
            {
                "",
                "/",
                "/",
                "/foo",
                "/foo/",
                "/",
                "/foo/",
                "/foo/bar",
                "/bar",
                "/baz",
                "/baz",
                "/",
                "/bar",
                "/bar/",
                "/foo/bar",
                null,
                null };

        assertEquals("Oops, test writer goofed", src.length, dest.length);

        for (int i = 0; i < src.length; i++) {
            assertEquals(
                "Check if '" + src[i] + "' normalized to '" + dest[i] + "'",
                dest[i],
                FilenameUtils.normalize(src[i]));
        }
    }

    private String replaceAll(
        String text,
        String lookFor,
        String replaceWith) {
        StringBuffer sb = new StringBuffer(text);
        while (true) {
            int idx = sb.toString().indexOf(lookFor);
            if (idx < 0) {
                break;
            }
            sb.replace(idx, idx + lookFor.length(), replaceWith);
        }
        return sb.toString();
    }

    public void testGetExtension() {
        String[][] tests = {
                { "filename.ext", "ext" }, 
                { "README", "" }, 
                { "domain.dot.com", "com" }, 
                { "image.jpeg", "jpeg" },
                { "a.b" + File.separator + "c", "" },
                { "a.b" + File.separator + "c.txt", "txt" },
                { "a" + File.separator + "b" + File.separator + "c", "" },
        };
        for (int i = 0; i < tests.length; i++) {
            assertEquals(tests[i][1], FilenameUtils.getExtension(tests[i][0]));
            //assertEquals(tests[i][1], FilenameUtils.extension(tests[i][0]));
        }
    }

    /* TODO: Reenable this test */
    public void DISABLED__testGetExtensionWithPaths() {
        String[][] testsWithPaths =
            { { "/tmp/foo/filename.ext", "ext" }, {
                "C:\\temp\\foo\\filename.ext", "ext" }, {
                "/tmp/foo.bar/filename.ext", "ext" }, {
                "C:\\temp\\foo.bar\\filename.ext", "ext" }, {
                "/tmp/foo.bar/README", "" }, {
                "C:\\temp\\foo.bar\\README", "" }, {
                "../filename.ext", "ext" }
        };
        for (int i = 0; i < testsWithPaths.length; i++) {
            assertEquals(
                testsWithPaths[i][1],
                FilenameUtils.getExtension(testsWithPaths[i][0]));
            //assertEquals(testsWithPaths[i][1], FilenameUtils.extension(testsWithPaths[i][0]));
        }
    }

    public void testRemoveExtension() {
        String[][] tests = { 
                { "filename.ext", "filename" }, 
                { "first.second.third.ext", "first.second.third" }, 
                { "README", "README" }, 
                { "domain.dot.com", "domain.dot" }, 
                { "image.jpeg", "image" },
                { "a.b" + File.separator + "c", "a.b" + File.separator + "c" },
                { "a.b" + File.separator + "c.txt", "a.b" + File.separator + "c" },
                { "a" + File.separator + "b" + File.separator + "c", "a" + File.separator + "b" + File.separator + "c" },
        };

        for (int i = 0; i < tests.length; i++) {
            assertEquals(tests[i][1], FilenameUtils.removeExtension(tests[i][0]));
            //assertEquals(tests[i][1], FilenameUtils.basename(tests[i][0]));
        }
    }

    /* TODO: Reenable this test */
    public void DISABLED__testRemoveExtensionWithPaths() {
        String[][] testsWithPaths =
            { { "/tmp/foo/filename.ext", "filename" }, {
                "C:\\temp\\foo\\filename.ext", "filename" }, {
                "/tmp/foo.bar/filename.ext", "filename" }, {
                "C:\\temp\\foo.bar\\filename.ext", "filename" }, {
                "/tmp/foo.bar/README", "README" }, {
                "C:\\temp\\foo.bar\\README", "README" }, {
                "../filename.ext", "filename" }
        };

        for (int i = 0; i < testsWithPaths.length; i++) {
            assertEquals(
                testsWithPaths[i][1],
                FilenameUtils.removeExtension(testsWithPaths[i][0]));
            //assertEquals(testsWithPaths[i][1], FilenameUtils.basename(testsWithPaths[i][0]));
        }
    }
<commit_msg>Re-enable testGetExtensionWithPaths() and testRemoveExtensionWithPaths(), modifying them to use File.separator. Commented out the Windows paths for now, until we can clarify the intent.<commit_after>/*
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * 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 org.apache.commons.io;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.testtools.FileBasedTestCase;

import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

/**
 * This is used to test FilenameUtils for correctness.
 *
 * @author <NAME>
 * @author <NAME>
 * @version $Id: FilenameUtilsTestCase.java,v 1.10 2004/10/24 01:27:13 martinc Exp $
 * @see FilenameUtils
 */
public class FilenameUtilsTestCase extends FileBasedTestCase {

    private File testFile1;
    private File testFile2;

    private static int testFile1Size;
    private static int testFile2Size;

    public static void main(String[] args) {
        TestRunner.run(suite());
    }

    public static Test suite() {
        return new TestSuite(FilenameUtilsTestCase.class);
    }

    public FilenameUtilsTestCase(String name) throws IOException {
        super(name);

        testFile1 = new File(getTestDirectory(), "file1-test.txt");
        testFile2 = new File(getTestDirectory(), "file1a-test.txt");

        testFile1Size = (int)testFile1.length();
        testFile2Size = (int)testFile2.length();
    }

    /** @see junit.framework.TestCase#setUp() */
    protected void setUp() throws Exception {
        getTestDirectory().mkdirs();
        createFile(testFile1, testFile1Size);
        createFile(testFile2, testFile2Size);
        FileUtils.deleteDirectory(getTestDirectory());
        getTestDirectory().mkdirs();
        createFile(testFile1, testFile1Size);
        createFile(testFile2, testFile2Size);
    }

    /** @see junit.framework.TestCase#tearDown() */
    protected void tearDown() throws Exception {
        FileUtils.deleteDirectory(getTestDirectory());
    }

    // mkdir

    public void testMkdir() {
        File dir = new File(getTestDirectory(), "testdir");
        FilenameUtils.mkdir(dir.getAbsolutePath());
        dir.deleteOnExit();
    }

    // removePath

    public void testRemovePath() {
        String fileName =
            FilenameUtils.removePath(
                new File(getTestDirectory(), getName()).getAbsolutePath());
        assertEquals(getName(), fileName);
    }

    // getPath

    public void testGetPath() {
        String fileName =
            FilenameUtils.getPath(
                new File(getTestDirectory(), getName()).getAbsolutePath());
        assertEquals(getTestDirectory().getAbsolutePath(), fileName);
    }

    // catPath

    public void testCatPath() {
        // TODO StringIndexOutOfBoundsException thrown if file doesn't contain slash.
        // Is this acceptable?
        //assertEquals("", FilenameUtils.catPath("a", "b"));

        assertEquals("/a" + File.separator + "c", FilenameUtils.catPath("/a/b", "c"));
        assertEquals("/a" + File.separator + "d", FilenameUtils.catPath("/a/b/c", "../d"));
        assertEquals("C:\\a" + File.separator + "c", FilenameUtils.catPath("C:\\a\\b", "c"));
        assertEquals("C:\\a" + File.separator + "d", FilenameUtils.catPath("C:\\a\\b\\c", "../d"));
    }

    public void testIndexOfLastPathSeparator() {
        assertEquals(-1, FilenameUtils.indexOfLastPathSeparator("noseperator.inthispath"));
        assertEquals(3, FilenameUtils.indexOfLastPathSeparator("a/b/c"));
        assertEquals(3, FilenameUtils.indexOfLastPathSeparator("a\\b\\c"));
    }

    // resolveFile

    public void testResolveFileDotDot() throws Exception {
        File file = FilenameUtils.resolveFile(getTestDirectory(), "..");
        assertEquals(
            "Check .. operator",
            file,
            getTestDirectory().getParentFile());
    }

    public void testResolveFileDot() throws Exception {
        File file = FilenameUtils.resolveFile(getTestDirectory(), ".");
        assertEquals("Check . operator", file, getTestDirectory());
    }

    // normalize

    public void testNormalize() throws Exception {
        String[] src =
            {
                "",
                "/",
                "///",
                "/foo",
                "/foo//",
                "/./",
                "/foo/./",
                "/foo/./bar",
                "/foo/../bar",
                "/foo/../bar/../baz",
                "/foo/bar/../../baz",
                "/././",
                "/foo/./../bar",
                "/foo/.././bar/",
                "//foo//./bar",
                "/../",
                "/foo/../../" };

        String[] dest =
            {
                "",
                "/",
                "/",
                "/foo",
                "/foo/",
                "/",
                "/foo/",
                "/foo/bar",
                "/bar",
                "/baz",
                "/baz",
                "/",
                "/bar",
                "/bar/",
                "/foo/bar",
                null,
                null };

        assertEquals("Oops, test writer goofed", src.length, dest.length);

        for (int i = 0; i < src.length; i++) {
            assertEquals(
                "Check if '" + src[i] + "' normalized to '" + dest[i] + "'",
                dest[i],
                FilenameUtils.normalize(src[i]));
        }
    }

    private String replaceAll(
        String text,
        String lookFor,
        String replaceWith) {
        StringBuffer sb = new StringBuffer(text);
        while (true) {
            int idx = sb.toString().indexOf(lookFor);
            if (idx < 0) {
                break;
            }
            sb.replace(idx, idx + lookFor.length(), replaceWith);
        }
        return sb.toString();
    }

    public void testGetExtension() {
        String[][] tests = {
                { "filename.ext", "ext" }, 
                { "README", "" }, 
                { "domain.dot.com", "com" }, 
                { "image.jpeg", "jpeg" },
                { "a.b" + File.separator + "c", "" },
                { "a.b" + File.separator + "c.txt", "txt" },
                { "a" + File.separator + "b" + File.separator + "c", "" },
        };
        for (int i = 0; i < tests.length; i++) {
            assertEquals(tests[i][1], FilenameUtils.getExtension(tests[i][0]));
            //assertEquals(tests[i][1], FilenameUtils.extension(tests[i][0]));
        }
    }

    public void testGetExtensionWithPaths() {
        String[][] testsWithPaths =
            { { File.separator + "tmp" + File.separator + "foo" + File.separator + "filename.ext", "ext" }, {
                //"C:\\temp\\foo\\filename.ext", "ext" }, {
                File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "filename.ext", "ext" }, {
                //"C:\\temp\\foo.bar\\filename.ext", "ext" }, {
                File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "README", "" }, {
                //"C:\\temp\\foo.bar\\README", "" }, {
                ".." + File.separator + "filename.ext", "ext" }
        };
        for (int i = 0; i < testsWithPaths.length; i++) {
            assertEquals(
                testsWithPaths[i][1],
                FilenameUtils.getExtension(testsWithPaths[i][0]));
            //assertEquals(testsWithPaths[i][1], FilenameUtils.extension(testsWithPaths[i][0]));
        }
    }

    public void testRemoveExtension() {
        String[][] tests = { 
                { "filename.ext", "filename" }, 
                { "first.second.third.ext", "first.second.third" }, 
                { "README", "README" }, 
                { "domain.dot.com", "domain.dot" }, 
                { "image.jpeg", "image" },
                { "a.b" + File.separator + "c", "a.b" + File.separator + "c" },
                { "a.b" + File.separator + "c.txt", "a.b" + File.separator + "c" },
                { "a" + File.separator + "b" + File.separator + "c", "a" + File.separator + "b" + File.separator + "c" },
        };

        for (int i = 0; i < tests.length; i++) {
            assertEquals(tests[i][1], FilenameUtils.removeExtension(tests[i][0]));
            //assertEquals(tests[i][1], FilenameUtils.basename(tests[i][0]));
        }
    }

    public void testRemoveExtensionWithPaths() {
        String[][] testsWithPaths =
            { { File.separator + "tmp" + File.separator + "foo" + File.separator + "filename.ext",
                File.separator + "tmp" + File.separator + "foo" + File.separator + "filename" }, {
                //"C:\\temp\\foo\\filename.ext", "filename" }, {
                File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "filename.ext",
                File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "filename" }, {
                //"C:\\temp\\foo.bar\\filename.ext", "filename" }, {
                File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "README",
                File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "README" }, {
                //"C:\\temp\\foo.bar\\README", "README" }, {
                ".." + File.separator + "filename.ext",
                ".." + File.separator + "filename" }
        };

        for (int i = 0; i < testsWithPaths.length; i++) {
            assertEquals(
                testsWithPaths[i][1],
                FilenameUtils.removeExtension(testsWithPaths[i][0]));
            //assertEquals(testsWithPaths[i][1], FilenameUtils.basename(testsWithPaths[i][0]));
        }
    }

{'fraction_non_alphanumeric': 0.15775237317546187, 'fraction_numerical': 0.007502296621414719, 'mean_word_length': 1.8316473988439306, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '2557712', 'n_tokens_mistral': 5783, 'n_tokens_neox': 5409, 'n_words': 1139}