<commit_before>/*
 * Copyright 2004-2005 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.cli2.validation;

import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

/**
 * JUnit test case for the FileValidator.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class FileValidatorTest extends TestCase {

    public void testValidate() throws InvalidArgumentException {
        final Object[] array = new Object[] { "src", "project.xml", "veryunlikelyfilename"};
        final List list = Arrays.asList(array);
        final FileValidator validator = new FileValidator();

        validator.validate(list);

        final Iterator i = list.iterator();
        assertEquals(new File("src"), i.next());
        assertEquals(new File("project.xml"), i.next());
        assertEquals(new File("veryunlikelyfilename"), i.next());
        assertFalse(i.hasNext());
    }

    public void testValidate_Directory() {
        final Object[] array = new Object[] { "src", "project.xml"};
        final List list = Arrays.asList(array);
        final FileValidator validator = FileValidator.getExistingDirectoryInstance();

        assertTrue("is a directory validator", validator.isDirectory());
        assertFalse("is not a file validator", validator.isFile());
        assertTrue("is an existing file validator", validator.isExisting());
        assertFalse("is not a hidden file validator", validator.isHidden());

        try{
            validator.validate(list);
            fail("InvalidArgumentException");
        }
        catch(InvalidArgumentException e){
            assertEquals("project.xml",e.getMessage());
        }
    }

    public void testValidate_ReadableFile() {
        final Object[] array = new Object[] { "src/test/data/readable.txt", "src/test/data/notreadable.txt"};
        final List list = Arrays.asList(array);
        final FileValidator validator = FileValidator.getExistingFileInstance();
        validator.setReadable(true);

        assertFalse("is not a directory validator", validator.isDirectory());
        assertTrue("is a file validator", validator.isFile());
        assertTrue("is an existing file validator", validator.isExisting());
        assertFalse("is not a hidden file validator", validator.isHidden());
        assertTrue("is a readable file validator", validator.isReadable());
        assertFalse("is not a writable file validator", validator.isWritable());

        try{
            validator.validate(list);
            fail("InvalidArgumentException");
        }
        catch(InvalidArgumentException e){
            assertEquals("src/test/data/notreadable.txt",e.getMessage());
        }
    }

    public void testValidate_WritableFile() {
        final Object[] array = new Object[] { "src/test/data/writable.txt", "src/test/data/readable.txt"};
        final List list = Arrays.asList(array);
        final FileValidator validator = FileValidator.getExistingFileInstance();
        validator.setWritable(true);

        assertFalse("is not a directory validator", validator.isDirectory());
        assertTrue("is a file validator", validator.isFile());
        assertTrue("is an existing file validator", validator.isExisting());
        assertFalse("is not a hidden file validator", validator.isHidden());
        assertFalse("is not a readable file validator", validator.isReadable());
        assertTrue("is a writable file validator", validator.isWritable());

        try{
            validator.validate(list);
            fail("InvalidArgumentException");
        }
        catch(InvalidArgumentException e){
            assertEquals("src/test/data/readable.txt",e.getMessage());
        }
    }

    public void testValidate_HiddenFile() throws InvalidArgumentException {
        final Object[] array = new Object[] { "src/test/data/hidden.txt", "src"};
        final List list = Arrays.asList(array);
        final FileValidator validator = FileValidator.getExistingFileInstance();
        validator.setHidden(true);

        assertFalse("is not a directory validator", validator.isDirectory());
        assertTrue("is a file validator", validator.isFile());
        assertTrue("is an existing file validator", validator.isExisting());
        assertTrue("is a hidden file validator", validator.isHidden());

        try{
            validator.validate(list);
            fail("InvalidArgumentException");
        }
        catch(InvalidArgumentException e){
            assertEquals("src",e.getMessage());
        }
    }

    public void testValidate_Existing() {
        final Object[] array = new Object[] { "project.xml", "veryunlikelyfilename"};
        final List list = Arrays.asList(array);
        final FileValidator validator = FileValidator.getExistingInstance();

        assertFalse("is not a directory validator", validator.isDirectory());
        assertFalse("is not a file validator", validator.isFile());
        assertTrue("is an existing file validator", validator.isExisting());
        assertFalse("is not a hidden file validator", validator.isHidden());

        try{
            validator.validate(list);
            fail("InvalidArgumentException");
        }
        catch(InvalidArgumentException e){<commit_msg>- set hidden attribute when running on windows - set readable.txt to read-only prior to running tests<commit_after>/*
 * Copyright 2004-2005 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.cli2.validation;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

/**
 * JUnit test case for the FileValidator.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class FileValidatorTest extends TestCase {

    public void testValidate() throws InvalidArgumentException {
        final Object[] array = new Object[] { "src", "project.xml", "veryunlikelyfilename"};
        final List list = Arrays.asList(array);
        final FileValidator validator = new FileValidator();

        validator.validate(list);

        final Iterator i = list.iterator();
        assertEquals(new File("src"), i.next());
        assertEquals(new File("project.xml"), i.next());
        assertEquals(new File("veryunlikelyfilename"), i.next());
        assertFalse(i.hasNext());
    }

    public void testValidate_Directory() {
        final Object[] array = new Object[] { "src", "project.xml"};
        final List list = Arrays.asList(array);
        final FileValidator validator = FileValidator.getExistingDirectoryInstance();

        assertTrue("is a directory validator", validator.isDirectory());
        assertFalse("is not a file validator", validator.isFile());
        assertTrue("is an existing file validator", validator.isExisting());
        assertFalse("is not a hidden file validator", validator.isHidden());

        try{
            validator.validate(list);
            fail("InvalidArgumentException");
        }
        catch(InvalidArgumentException e){
            assertEquals("project.xml",e.getMessage());
        }
    }

    public void testValidate_ReadableFile() {
    	// make file readonly
    	File file = new File("src/test/data/readable.txt");
    	file.setReadOnly();

        final Object[] array = new Object[] { "src/test/data/readable.txt", "src/test/data/notreadable.txt"};
        final List list = Arrays.asList(array);
        final FileValidator validator = FileValidator.getExistingFileInstance();
        validator.setReadable(true);

        assertFalse("is not a directory validator", validator.isDirectory());
        assertTrue("is a file validator", validator.isFile());
        assertTrue("is an existing file validator", validator.isExisting());
        assertFalse("is not a hidden file validator", validator.isHidden());
        assertTrue("is a readable file validator", validator.isReadable());
        assertFalse("is not a writable file validator", validator.isWritable());

        try{
            validator.validate(list);
            fail("InvalidArgumentException");
        }
        catch(InvalidArgumentException e){
            assertEquals("src/test/data/notreadable.txt",e.getMessage());
        }
    }

    public void testValidate_WritableFile() {
    	// make file readonly
    	File file = new File("src/test/data/readable.txt");
    	file.setReadOnly();

    	final Object[] array = new Object[] { "src/test/data/writable.txt", "src/test/data/readable.txt"};
        final List list = Arrays.asList(array);
        final FileValidator validator = FileValidator.getExistingFileInstance();
        validator.setWritable(true);

        assertFalse("is not a directory validator", validator.isDirectory());
        assertTrue("is a file validator", validator.isFile());
        assertTrue("is an existing file validator", validator.isExisting());
        assertFalse("is not a hidden file validator", validator.isHidden());
        assertFalse("is not a readable file validator", validator.isReadable());
        assertTrue("is a writable file validator", validator.isWritable());

        try{
            validator.validate(list);
            fail("InvalidArgumentException");
        }
        catch(InvalidArgumentException e){
            assertEquals("src/test/data/readable.txt",e.getMessage());
        }
    }

    public void testValidate_HiddenFile() throws InvalidArgumentException {
    	// make file hidden on Windows
    	attribute("H");
    	
		final Object[] array = new Object[] { ".hidden", "src"};
		final List list = Arrays.asList(array);
		final FileValidator validator = FileValidator.getExistingFileInstance();
		validator.setHidden(true);
		
		assertFalse("is not a directory validator", validator.isDirectory());
		assertTrue("is a file validator", validator.isFile());
		assertTrue("is an existing file validator", validator.isExisting());
		assertTrue("is a hidden file validator", validator.isHidden());
	
		try{
			validator.validate(list);
			fail("InvalidArgumentException");
		}
		catch(InvalidArgumentException e){
			assertEquals("src",e.getMessage());
		}
    }

    private void attribute(String attr) {
		final String os = System.getProperty("os.name").toLowerCase();

		// if the test is run on windows, run the attrib program
		// to set the hidden attribute
		if (os.indexOf("windows") != -1) {
			// windows
			try {
				Process proc = Runtime.getRuntime().exec("attrib.exe +" + attr + " src/test/data/.hidden.txt", null, new File("."));
			} 
			catch (IOException e) {
					System.out.println(e.getMessage());
				e.printStackTrace();
			}
		} 
    }

    public void testValidate_Existing() {
        final Object[] array = new Object[] { "project.xml", "veryunlikelyfilename"};
        final List list = Arrays.asList(array);
        final FileValidator validator = FileValidator.getExistingInstance();

        assertFalse("is not a directory validator", validator.isDirectory());
        assertFalse("is not a file validator", validator.isFile());
        assertTrue("is an existing file validator", validator.isExisting());
        assertFalse("is not a hidden file validator", validator.isHidden());

        try{
            validator.validate(list);
            fail("InvalidArgumentException");
        }
        catch(InvalidArgumentException e){
{'fraction_non_alphanumeric': 0.11621772839900887, 'fraction_numerical': 0.0021581008712333147, 'mean_word_length': 3.1874163319946454, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, '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': '5396286', 'n_tokens_mistral': 3296, 'n_tokens_neox': 3198, 'n_words': 920}