44import static org .junit .Assert .assertTrue ;
55
66import java .io .IOException ;
7- import java .nio .file .CopyOption ;
87import java .nio .file .DirectoryNotEmptyException ;
98import java .nio .file .FileAlreadyExistsException ;
109import java .nio .file .Files ;
1615
1716import org .junit .Test ;
1817
19- public class FileTest {
18+ public class FileUnitTest {
2019 private static final String HOME = System .getProperty ("user.home" );
2120
2221 // checking file or dir
2322 @ Test
2423 public void givenExistentPath_whenConfirmsFileExists_thenCorrect () {
25- Path p = Paths .get (HOME );
24+ final Path p = Paths .get (HOME );
2625 assertTrue (Files .exists (p ));
2726 }
2827
2928 @ Test
3029 public void givenNonexistentPath_whenConfirmsFileNotExists_thenCorrect () {
31- Path p = Paths .get (HOME + "/inexistent_file.txt" );
30+ final Path p = Paths .get (HOME + "/inexistent_file.txt" );
3231 assertTrue (Files .notExists (p ));
3332 }
3433
3534 @ Test
3635 public void givenExistentDirPath_whenConfirmsNotRegularFile_thenCorrect () {
37- Path p = Paths .get (HOME );
36+ final Path p = Paths .get (HOME );
3837 assertFalse (Files .isRegularFile (p ));
3938 }
4039
4140 @ Test
4241 public void givenExistentDirPath_whenConfirmsReadable_thenCorrect () {
43- Path p = Paths .get (HOME );
42+ final Path p = Paths .get (HOME );
4443 assertTrue (Files .isReadable (p ));
4544 }
4645
4746 @ Test
4847 public void givenExistentDirPath_whenConfirmsWritable_thenCorrect () {
49- Path p = Paths .get (HOME );
48+ final Path p = Paths .get (HOME );
5049 assertTrue (Files .isWritable (p ));
5150 }
5251
5352 @ Test
5453 public void givenExistentDirPath_whenConfirmsExecutable_thenCorrect () {
55- Path p = Paths .get (HOME );
54+ final Path p = Paths .get (HOME );
5655 assertTrue (Files .isExecutable (p ));
5756 }
5857
5958 @ Test
6059 public void givenSameFilePaths_whenConfirmsIsSame_thenCorrect () throws IOException {
61- Path p1 = Paths .get (HOME );
62- Path p2 = Paths .get (HOME );
60+ final Path p1 = Paths .get (HOME );
61+ final Path p2 = Paths .get (HOME );
6362 assertTrue (Files .isSameFile (p1 , p2 ));
6463 }
6564
6665 // reading, writing and creating files
6766 // creating file
6867 @ Test
6968 public void givenFilePath_whenCreatesNewFile_thenCorrect () throws IOException {
70- String fileName = "myfile_" + new Date ().getTime () + ".txt" ;
71- Path p = Paths .get (HOME + "/" + fileName );
69+ final String fileName = "myfile_" + new Date ().getTime () + ".txt" ;
70+ final Path p = Paths .get (HOME + "/" + fileName );
7271 assertFalse (Files .exists (p ));
7372 Files .createFile (p );
7473 assertTrue (Files .exists (p ));
@@ -77,8 +76,8 @@ public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException {
7776
7877 @ Test
7978 public void givenDirPath_whenCreatesNewDir_thenCorrect () throws IOException {
80- String dirName = "myDir_" + new Date ().getTime ();
81- Path p = Paths .get (HOME + "/" + dirName );
79+ final String dirName = "myDir_" + new Date ().getTime ();
80+ final Path p = Paths .get (HOME + "/" + dirName );
8281 assertFalse (Files .exists (p ));
8382 Files .createDirectory (p );
8483 assertTrue (Files .exists (p ));
@@ -89,20 +88,20 @@ public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException {
8988
9089 @ Test
9190 public void givenDirPath_whenFailsToCreateRecursively_thenCorrect () {
92- String dirName = "myDir_" + new Date ().getTime () + "/subdir" ;
93- Path p = Paths .get (HOME + "/" + dirName );
91+ final String dirName = "myDir_" + new Date ().getTime () + "/subdir" ;
92+ final Path p = Paths .get (HOME + "/" + dirName );
9493 assertFalse (Files .exists (p ));
9594 try {
9695 Files .createDirectory (p );
97- } catch (IOException e ) {
96+ } catch (final IOException e ) {
9897 assertTrue (e instanceof NoSuchFileException );
9998 }
10099 }
101100
102101 @ Test
103102 public void givenDirPath_whenCreatesRecursively_thenCorrect () throws IOException {
104- Path dir = Paths .get (HOME + "/myDir_" + new Date ().getTime ());
105- Path subdir = dir .resolve ("subdir" );
103+ final Path dir = Paths .get (HOME + "/myDir_" + new Date ().getTime ());
104+ final Path subdir = dir .resolve ("subdir" );
106105 assertFalse (Files .exists (dir ));
107106 assertFalse (Files .exists (subdir ));
108107 Files .createDirectories (subdir );
@@ -112,8 +111,8 @@ public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException
112111
113112 @ Test
114113 public void givenFilePath_whenCreatesTempFile_thenCorrect () throws IOException {
115- String prefix = "log_" ;
116- String suffix = ".txt" ;
114+ final String prefix = "log_" ;
115+ final String suffix = ".txt" ;
117116 Path p = Paths .get (HOME + "/" );
118117 p = Files .createTempFile (p , prefix , suffix );
119118 // like log_8821081429012075286.txt
@@ -132,7 +131,7 @@ public void givenFilePath_whenCreatesTempFileWithDefaultsNaming_thenCorrect() th
132131
133132 @ Test
134133 public void givenNoFilePath_whenCreatesTempFileInTempDir_thenCorrect () throws IOException {
135- Path p = Files .createTempFile (null , null );
134+ final Path p = Files .createTempFile (null , null );
136135 // like C:\Users\new\AppData\Local\Temp\6100927974988978748.tmp
137136 assertTrue (Files .exists (p ));
138137
@@ -141,7 +140,7 @@ public void givenNoFilePath_whenCreatesTempFileInTempDir_thenCorrect() throws IO
141140 // delete file
142141 @ Test
143142 public void givenPath_whenDeletes_thenCorrect () throws IOException {
144- Path p = Paths .get (HOME + "/fileToDelete.txt" );
143+ final Path p = Paths .get (HOME + "/fileToDelete.txt" );
145144 assertFalse (Files .exists (p ));
146145 Files .createFile (p );
147146 assertTrue (Files .exists (p ));
@@ -152,14 +151,14 @@ public void givenPath_whenDeletes_thenCorrect() throws IOException {
152151
153152 @ Test
154153 public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect () throws IOException {
155- Path dir = Paths .get (HOME + "/emptyDir" + new Date ().getTime ());
154+ final Path dir = Paths .get (HOME + "/emptyDir" + new Date ().getTime ());
156155 Files .createDirectory (dir );
157156 assertTrue (Files .exists (dir ));
158- Path file = dir .resolve ("file.txt" );
157+ final Path file = dir .resolve ("file.txt" );
159158 Files .createFile (file );
160159 try {
161160 Files .delete (dir );
162- } catch (IOException e ) {
161+ } catch (final IOException e ) {
163162 assertTrue (e instanceof DirectoryNotEmptyException );
164163 }
165164 assertTrue (Files .exists (dir ));
@@ -168,19 +167,19 @@ public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOExcept
168167
169168 @ Test
170169 public void givenInexistentFile_whenDeleteFails_thenCorrect () throws IOException {
171- Path p = Paths .get (HOME + "/inexistentFile.txt" );
170+ final Path p = Paths .get (HOME + "/inexistentFile.txt" );
172171 assertFalse (Files .exists (p ));
173172 try {
174173 Files .delete (p );
175- } catch (IOException e ) {
174+ } catch (final IOException e ) {
176175 assertTrue (e instanceof NoSuchFileException );
177176 }
178177
179178 }
180179
181180 @ Test
182181 public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect () throws IOException {
183- Path p = Paths .get (HOME + "/inexistentFile.txt" );
182+ final Path p = Paths .get (HOME + "/inexistentFile.txt" );
184183 assertFalse (Files .exists (p ));
185184 Files .deleteIfExists (p );
186185
@@ -189,12 +188,12 @@ public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect() throws IOE
189188 // copy file
190189 @ Test
191190 public void givenFilePath_whenCopiesToNewLocation_thenCorrect () throws IOException {
192- Path dir1 = Paths .get (HOME + "/firstdir_" + new Date ().getTime ());
193- Path dir2 = Paths .get (HOME + "/otherdir_" + new Date ().getTime ());
191+ final Path dir1 = Paths .get (HOME + "/firstdir_" + new Date ().getTime ());
192+ final Path dir2 = Paths .get (HOME + "/otherdir_" + new Date ().getTime ());
194193 Files .createDirectory (dir1 );
195194 Files .createDirectory (dir2 );
196- Path file1 = dir1 .resolve ("filetocopy.txt" );
197- Path file2 = dir2 .resolve ("filetocopy.txt" );
195+ final Path file1 = dir1 .resolve ("filetocopy.txt" );
196+ final Path file2 = dir2 .resolve ("filetocopy.txt" );
198197 Files .createFile (file1 );
199198 assertTrue (Files .exists (file1 ));
200199 assertFalse (Files .exists (file2 ));
@@ -205,19 +204,19 @@ public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOExcepti
205204
206205 @ Test
207206 public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect () throws IOException {
208- Path dir1 = Paths .get (HOME + "/firstdir_" + new Date ().getTime ());
209- Path dir2 = Paths .get (HOME + "/otherdir_" + new Date ().getTime ());
207+ final Path dir1 = Paths .get (HOME + "/firstdir_" + new Date ().getTime ());
208+ final Path dir2 = Paths .get (HOME + "/otherdir_" + new Date ().getTime ());
210209 Files .createDirectory (dir1 );
211210 Files .createDirectory (dir2 );
212- Path file1 = dir1 .resolve ("filetocopy.txt" );
213- Path file2 = dir2 .resolve ("filetocopy.txt" );
211+ final Path file1 = dir1 .resolve ("filetocopy.txt" );
212+ final Path file2 = dir2 .resolve ("filetocopy.txt" );
214213 Files .createFile (file1 );
215214 Files .createFile (file2 );
216215 assertTrue (Files .exists (file1 ));
217216 assertTrue (Files .exists (file2 ));
218217 try {
219218 Files .copy (file1 , file2 );
220- } catch (IOException e ) {
219+ } catch (final IOException e ) {
221220 assertTrue (e instanceof FileAlreadyExistsException );
222221 }
223222 Files .copy (file1 , file2 , StandardCopyOption .REPLACE_EXISTING );
@@ -226,12 +225,12 @@ public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOExce
226225 // moving files
227226 @ Test
228227 public void givenFilePath_whenMovesToNewLocation_thenCorrect () throws IOException {
229- Path dir1 = Paths .get (HOME + "/firstdir_" + new Date ().getTime ());
230- Path dir2 = Paths .get (HOME + "/otherdir_" + new Date ().getTime ());
228+ final Path dir1 = Paths .get (HOME + "/firstdir_" + new Date ().getTime ());
229+ final Path dir2 = Paths .get (HOME + "/otherdir_" + new Date ().getTime ());
231230 Files .createDirectory (dir1 );
232231 Files .createDirectory (dir2 );
233- Path file1 = dir1 .resolve ("filetocopy.txt" );
234- Path file2 = dir2 .resolve ("filetocopy.txt" );
232+ final Path file1 = dir1 .resolve ("filetocopy.txt" );
233+ final Path file2 = dir2 .resolve ("filetocopy.txt" );
235234 Files .createFile (file1 );
236235 assertTrue (Files .exists (file1 ));
237236 assertFalse (Files .exists (file2 ));
@@ -243,19 +242,19 @@ public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOExceptio
243242
244243 @ Test
245244 public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect () throws IOException {
246- Path dir1 = Paths .get (HOME + "/firstdir_" + new Date ().getTime ());
247- Path dir2 = Paths .get (HOME + "/otherdir_" + new Date ().getTime ());
245+ final Path dir1 = Paths .get (HOME + "/firstdir_" + new Date ().getTime ());
246+ final Path dir2 = Paths .get (HOME + "/otherdir_" + new Date ().getTime ());
248247 Files .createDirectory (dir1 );
249248 Files .createDirectory (dir2 );
250- Path file1 = dir1 .resolve ("filetocopy.txt" );
251- Path file2 = dir2 .resolve ("filetocopy.txt" );
249+ final Path file1 = dir1 .resolve ("filetocopy.txt" );
250+ final Path file2 = dir2 .resolve ("filetocopy.txt" );
252251 Files .createFile (file1 );
253252 Files .createFile (file2 );
254253 assertTrue (Files .exists (file1 ));
255254 assertTrue (Files .exists (file2 ));
256255 try {
257256 Files .move (file1 , file2 );
258- } catch (IOException e ) {
257+ } catch (final IOException e ) {
259258 assertTrue (e instanceof FileAlreadyExistsException );
260259 }
261260 Files .move (file1 , file2 , StandardCopyOption .REPLACE_EXISTING );
0 commit comments