1616package org .apache .commons .cli2 .validation ;
1717
1818import java .io .File ;
19+ import java .io .FileOutputStream ;
20+ import java .io .IOException ;
1921import java .util .Arrays ;
2022import java .util .Iterator ;
2123import java .util .List ;
@@ -64,6 +66,10 @@ public void testValidate_Directory() {
6466 }
6567
6668 public void testValidate_ReadableFile () {
69+ // make file readonly
70+ File file = new File ("src/test/data/readable.txt" );
71+ file .setReadOnly ();
72+
6773 final Object [] array = new Object [] { "src/test/data/readable.txt" , "src/test/data/notreadable.txt" };
6874 final List list = Arrays .asList (array );
6975 final FileValidator validator = FileValidator .getExistingFileInstance ();
@@ -86,7 +92,11 @@ public void testValidate_ReadableFile() {
8692 }
8793
8894 public void testValidate_WritableFile () {
89- final Object [] array = new Object [] { "src/test/data/writable.txt" , "src/test/data/readable.txt" };
95+ // make file readonly
96+ File file = new File ("src/test/data/readable.txt" );
97+ file .setReadOnly ();
98+
99+ final Object [] array = new Object [] { "src/test/data/writable.txt" , "src/test/data/readable.txt" };
90100 final List list = Arrays .asList (array );
91101 final FileValidator validator = FileValidator .getExistingFileInstance ();
92102 validator .setWritable (true );
@@ -108,23 +118,43 @@ public void testValidate_WritableFile() {
108118 }
109119
110120 public void testValidate_HiddenFile () throws InvalidArgumentException {
111- final Object [] array = new Object [] { "src/test/data/hidden.txt" , "src" };
112- final List list = Arrays .asList (array );
113- final FileValidator validator = FileValidator .getExistingFileInstance ();
114- validator .setHidden (true );
115-
116- assertFalse ("is not a directory validator" , validator .isDirectory ());
117- assertTrue ("is a file validator" , validator .isFile ());
118- assertTrue ("is an existing file validator" , validator .isExisting ());
119- assertTrue ("is a hidden file validator" , validator .isHidden ());
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+ }
141+ }
120142
121- try {
122- validator .validate (list );
123- fail ("InvalidArgumentException" );
124- }
125- catch (InvalidArgumentException e ){
126- assertEquals ("src" ,e .getMessage ());
127- }
143+ 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+ }
128158 }
129159
130160 public void testValidate_Existing () {
0 commit comments