1616package org .apache .commons .cli2 .validation ;
1717
1818import java .io .File ;
19- import java .io .FileOutputStream ;
2019import java .io .IOException ;
2120import java .util .Arrays ;
2221import java .util .Iterator ;
2625
2726/**
2827 * JUnit test case for the FileValidator.
29- *
28+ *
3029 * @author Rob Oxspring
3130 * @author John Keyes
3231 */
3332public class FileValidatorTest extends TestCase {
3433
3534 public void testValidate () throws InvalidArgumentException {
36- final Object [] array = new Object [] { "src" , "project.xml" , "veryunlikelyfilename" };
35+ final Object [] array = new Object [] { "src" , "project.xml" ,
36+ "veryunlikelyfilename" };
3737 final List list = Arrays .asList (array );
3838 final FileValidator validator = new FileValidator ();
3939
@@ -47,30 +47,31 @@ public void testValidate() throws InvalidArgumentException {
4747 }
4848
4949 public void testValidate_Directory () {
50- final Object [] array = new Object [] { "src" , "project.xml" };
50+ final Object [] array = new Object [] { "src" , "project.xml" };
5151 final List list = Arrays .asList (array );
52- final FileValidator validator = FileValidator .getExistingDirectoryInstance ();
52+ final FileValidator validator = FileValidator
53+ .getExistingDirectoryInstance ();
5354
5455 assertTrue ("is a directory validator" , validator .isDirectory ());
5556 assertFalse ("is not a file validator" , validator .isFile ());
5657 assertTrue ("is an existing file validator" , validator .isExisting ());
5758 assertFalse ("is not a hidden file validator" , validator .isHidden ());
5859
59- try {
60+ try {
6061 validator .validate (list );
6162 fail ("InvalidArgumentException" );
62- }
63- catch (InvalidArgumentException e ){
64- assertEquals ("project.xml" ,e .getMessage ());
63+ } catch (InvalidArgumentException e ) {
64+ assertEquals ("project.xml" , e .getMessage ());
6565 }
6666 }
6767
6868 public void testValidate_ReadableFile () {
69- // make file readonly
70- File file = new File ("src/test/data/readable.txt" );
71- file .setReadOnly ();
69+ // make file readonly
70+ File file = new File ("src/test/data/readable.txt" );
71+ file .setReadOnly ();
7272
73- final Object [] array = new Object [] { "src/test/data/readable.txt" , "src/test/data/notreadable.txt" };
73+ final Object [] array = new Object [] { "src/test/data/readable.txt" ,
74+ "src/test/data/notreadable.txt" };
7475 final List list = Arrays .asList (array );
7576 final FileValidator validator = FileValidator .getExistingFileInstance ();
7677 validator .setReadable (true );
@@ -82,21 +83,21 @@ public void testValidate_ReadableFile() {
8283 assertTrue ("is a readable file validator" , validator .isReadable ());
8384 assertFalse ("is not a writable file validator" , validator .isWritable ());
8485
85- try {
86+ try {
8687 validator .validate (list );
8788 fail ("InvalidArgumentException" );
88- }
89- catch (InvalidArgumentException e ){
90- assertEquals ("src/test/data/notreadable.txt" ,e .getMessage ());
89+ } catch (InvalidArgumentException e ) {
90+ assertEquals ("src/test/data/notreadable.txt" , e .getMessage ());
9191 }
9292 }
9393
9494 public void testValidate_WritableFile () {
95- // make file readonly
96- File file = new File ("src/test/data/readable.txt" );
97- file .setReadOnly ();
95+ // make file readonly
96+ File file = new File ("src/test/data/readable.txt" );
97+ file .setReadOnly ();
9898
99- final Object [] array = new Object [] { "src/test/data/writable.txt" , "src/test/data/readable.txt" };
99+ final Object [] array = new Object [] { "src/test/data/writable.txt" ,
100+ "src/test/data/readable.txt" };
100101 final List list = Arrays .asList (array );
101102 final FileValidator validator = FileValidator .getExistingFileInstance ();
102103 validator .setWritable (true );
@@ -108,57 +109,57 @@ public void testValidate_WritableFile() {
108109 assertFalse ("is not a readable file validator" , validator .isReadable ());
109110 assertTrue ("is a writable file validator" , validator .isWritable ());
110111
111- try {
112+ try {
112113 validator .validate (list );
113114 fail ("InvalidArgumentException" );
114- }
115- catch (InvalidArgumentException e ){
116- assertEquals ("src/test/data/readable.txt" ,e .getMessage ());
115+ } catch (InvalidArgumentException e ) {
116+ assertEquals ("src/test/data/readable.txt" , e .getMessage ());
117117 }
118118 }
119119
120120 public void testValidate_HiddenFile () throws InvalidArgumentException {
121- // make file hidden on Windows
122- attribute ("H" );
123-
124- final Object [] array = new Object [] { ".hidden" , "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- }
138- catch (InvalidArgumentException e ){
139- assertEquals ("src" ,e .getMessage ());
140- }
121+ // make file hidden on Windows
122+ attribute ("H" );
123+
124+ final Object [] array = new Object [] { ".hidden" , "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+ }
141140 }
142141
143142 private void attribute (String attr ) {
144- final String os = System .getProperty ("os.name" ).toLowerCase ();
145-
146- // if the test is run on windows, run the attrib program
147- // to set the hidden attribute
148- if (os .indexOf ("windows" ) != -1 ) {
149- // windows
150- try {
151- Process proc = Runtime .getRuntime ().exec ("attrib.exe +" + attr + " src/test/data/.hidden.txt" , null , new File ("." ));
152- }
153- catch (IOException e ) {
154- System .out .println (e .getMessage ());
155- e .printStackTrace ();
156- }
157- }
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+ } catch (IOException e ) {
154+ System .out .println (e .getMessage ());
155+ e .printStackTrace ();
156+ }
157+ }
158158 }
159159
160160 public void testValidate_Existing () {
161- final Object [] array = new Object [] { "project.xml" , "veryunlikelyfilename" };
161+ final Object [] array = new Object [] { "project.xml" ,
162+ "veryunlikelyfilename" };
162163 final List list = Arrays .asList (array );
163164 final FileValidator validator = FileValidator .getExistingInstance ();
164165
@@ -167,26 +168,24 @@ public void testValidate_Existing() {
167168 assertTrue ("is an existing file validator" , validator .isExisting ());
168169 assertFalse ("is not a hidden file validator" , validator .isHidden ());
169170
170- try {
171+ try {
171172 validator .validate (list );
172173 fail ("InvalidArgumentException" );
173- }
174- catch (InvalidArgumentException e ){
175- assertEquals ("veryunlikelyfilename" ,e .getMessage ());
174+ } catch (InvalidArgumentException e ) {
175+ assertEquals ("veryunlikelyfilename" , e .getMessage ());
176176 }
177177 }
178178
179179 public void testValidate_File () {
180- final Object [] array = new Object [] { "project.xml" , "src" };
180+ final Object [] array = new Object [] { "project.xml" , "src" };
181181 final List list = Arrays .asList (array );
182182 final Validator validator = FileValidator .getExistingFileInstance ();
183183
184- try {
184+ try {
185185 validator .validate (list );
186186 fail ("InvalidArgumentException" );
187- }
188- catch (InvalidArgumentException e ){
189- assertEquals ("src" ,e .getMessage ());
187+ } catch (InvalidArgumentException e ) {
188+ assertEquals ("src" , e .getMessage ());
190189 }
191190 }
192191}
0 commit comments