001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.cli2.validation;
018
019import java.io.File;
020import java.io.IOException;
021import java.util.Arrays;
022import java.util.Iterator;
023import java.util.List;
024
025import junit.framework.TestCase;
026
027/**
028 * JUnit test case for the FileValidator.
029 *
030 * @author Rob Oxspring
031 * @author John Keyes
032 */
033public class FileValidatorTest extends TestCase {
034
035    public void testValidate() throws InvalidArgumentException {
036        final Object[] array = new Object[] { "src", "pom.xml", "veryunlikelyfilename" };
037        final List list = Arrays.asList(array);
038        final FileValidator validator = new FileValidator();
039
040        validator.validate(list);
041
042        final Iterator i = list.iterator();
043        assertEquals(new File("src"), i.next());
044        assertEquals(new File("pom.xml"), i.next());
045        assertEquals(new File("veryunlikelyfilename"), i.next());
046        assertFalse(i.hasNext());
047    }
048
049    public void testValidate_Directory() {
050        final Object[] array = new Object[] { "src", "pom.xml" };
051        final List list = Arrays.asList(array);
052        final FileValidator validator = FileValidator
053                .getExistingDirectoryInstance();
054
055        assertTrue("is a directory validator", validator.isDirectory());
056        assertFalse("is not a file validator", validator.isFile());
057        assertTrue("is an existing file validator", validator.isExisting());
058        assertFalse("is not a hidden file validator", validator.isHidden());
059
060        try {
061            validator.validate(list);
062            fail("InvalidArgumentException");
063        } catch (InvalidArgumentException e) {
064            assertEquals("pom.xml", e.getMessage());
065        }
066    }
067
068    public void testValidate_ReadableFile() {
069        // make file readonly
070        File file = new File("src/test/data/readable.txt");
071        file.setReadOnly();
072
073        final Object[] array = new Object[] { "src/test/data/readable.txt",
074                "src/test/data/notreadable.txt" };
075        final List list = Arrays.asList(array);
076        final FileValidator validator = FileValidator.getExistingFileInstance();
077        validator.setReadable(true);
078
079        assertFalse("is not a directory validator", validator.isDirectory());
080        assertTrue("is a file validator", validator.isFile());
081        assertTrue("is an existing file validator", validator.isExisting());
082        assertFalse("is not a hidden file validator", validator.isHidden());
083        assertTrue("is a readable file validator", validator.isReadable());
084        assertFalse("is not a writable file validator", validator.isWritable());
085
086        try {
087            validator.validate(list);
088            fail("InvalidArgumentException");
089        } catch (InvalidArgumentException e) {
090            assertEquals("src/test/data/notreadable.txt", e.getMessage());
091        }
092    }
093
094    public void testValidate_WritableFile() {
095        // make file readonly
096        File file = new File("src/test/data/readable.txt");
097        file.setReadOnly();
098
099        final Object[] array = new Object[] { "src/test/data/writable.txt",
100                "src/test/data/readable.txt" };
101        final List list = Arrays.asList(array);
102        final FileValidator validator = FileValidator.getExistingFileInstance();
103        validator.setWritable(true);
104
105        assertFalse("is not a directory validator", validator.isDirectory());
106        assertTrue("is a file validator", validator.isFile());
107        assertTrue("is an existing file validator", validator.isExisting());
108        assertFalse("is not a hidden file validator", validator.isHidden());
109        assertFalse("is not a readable file validator", validator.isReadable());
110        assertTrue("is a writable file validator", validator.isWritable());
111
112        try {
113            validator.validate(list);
114            fail("InvalidArgumentException");
115        } catch (InvalidArgumentException e) {
116            assertEquals("src/test/data/readable.txt", e.getMessage());
117        }
118    }
119
120    public void testValidate_HiddenFile() throws InvalidArgumentException {
121        // make file hidden on Windows
122        attribute("H");
123
124        final Object[] array = new Object[] { "src/test/data/.hidden.txt", "src" };
125        final List list = Arrays.asList(array);
126        final FileValidator validator = FileValidator.getExistingFileInstance();
127        validator.setHidden(true);
128
129        assertFalse("is not a directory validator", validator.isDirectory());
130        assertTrue("is a file validator", validator.isFile());
131        assertTrue("is an existing file validator", validator.isExisting());
132        assertTrue("is a hidden file validator", validator.isHidden());
133
134        try {
135            validator.validate(list);
136            fail("InvalidArgumentException");
137        } catch (InvalidArgumentException e) {
138            assertEquals("src", e.getMessage());
139        }
140    }
141
142    private void attribute(String attr) {
143        final String os = System.getProperty("os.name").toLowerCase();
144
145        // if the test is run on windows, run the attrib program
146        // to set the hidden attribute
147        if (os.indexOf("windows") != -1) {
148            // windows
149            try {
150                Process proc = Runtime.getRuntime().exec(
151                        "attrib.exe +" + attr + " src/test/data/.hidden.txt",
152                        null, new File("."));
153                proc.waitFor();
154            } catch (InterruptedException e) {
155                System.out.println(e.getMessage());
156                e.printStackTrace();
157            } catch (IOException e) {
158                System.out.println(e.getMessage());
159                e.printStackTrace();
160            }
161        }
162    }
163
164    public void testValidate_Existing() {
165        final Object[] array = new Object[] { "pom.xml", "veryunlikelyfilename" };
166        final List list = Arrays.asList(array);
167        final FileValidator validator = FileValidator.getExistingInstance();
168
169        assertFalse("is not a directory validator", validator.isDirectory());
170        assertFalse("is not a file validator", validator.isFile());
171        assertTrue("is an existing file validator", validator.isExisting());
172        assertFalse("is not a hidden file validator", validator.isHidden());
173
174        try {
175            validator.validate(list);
176            fail("InvalidArgumentException");
177        } catch (InvalidArgumentException e) {
178            assertEquals("veryunlikelyfilename", e.getMessage());
179        }
180    }
181
182    public void testValidate_File() {
183        final Object[] array = new Object[] { "pom.xml", "src" };
184        final List list = Arrays.asList(array);
185        final Validator validator = FileValidator.getExistingFileInstance();
186
187        try {
188            validator.validate(list);
189            fail("InvalidArgumentException");
190        } catch (InvalidArgumentException e) {
191            assertEquals("src", e.getMessage());
192        }
193    }
194}