1+ package com .baeldung .file ;
2+
3+ import org .junit .Test ;
4+
5+ import java .io .*;
6+ import java .util .Arrays ;
7+ import java .util .List ;
8+
9+ import static org .junit .Assert .*;
10+
11+ public class FileClassDemoUnitTest {
12+
13+ @ Test
14+ public void givenDirectoryCreated_whenMkdirIsInvoked_thenDirectoryIsDeleted () {
15+ File directory = new File ("testDirectory" );
16+ if (!directory .isDirectory () || !directory .exists ()) {
17+ directory .mkdir ();
18+ }
19+
20+ assertTrue (directory .delete ());
21+ }
22+
23+ @ Test
24+ public void givenFileCreated_whenCreateNewFileIsInvoked_thenFileIsDeleted () throws IOException {
25+ File file = new File ("testFile.txt" );
26+ if (!file .isFile () || !file .exists ()) {
27+ file .createNewFile ();
28+ }
29+
30+ assertTrue (file .delete ());
31+ }
32+
33+
34+ @ Test
35+ public void givenFileCreated_whenCreateNewFileInvoked_thenMetadataIsAsExpected () throws IOException {
36+
37+ // different Operating systems have different separator characters
38+ String separatorCharacter = System .getProperty ("file.separator" );
39+
40+ File parentDirectory = makeDirectory ("filesDirectory" );
41+
42+ File childFile = new File (parentDirectory , "file1.txt" );
43+ childFile .createNewFile ();
44+
45+ assertTrue (childFile .getName ().equals ("file1.txt" ));
46+ assertTrue (childFile .getParentFile ().getName ().equals (parentDirectory .getName ()));
47+ assertTrue (childFile .getPath ().equals (parentDirectory .getPath () + separatorCharacter + "file1.txt" ));
48+
49+ removeDirectory (parentDirectory );
50+ }
51+
52+
53+ @ Test (expected = FileNotFoundException .class )
54+ public void givenReadOnlyFileCreated_whenCreateNewFileInvoked_thenFileCannotBeWrittenTo () throws IOException {
55+ File parentDirectory = makeDirectory ("filesDirectory" );
56+
57+ File childFile = new File (parentDirectory , "file1.txt" );
58+ childFile .createNewFile ();
59+
60+ childFile .setWritable (false );
61+
62+ FileOutputStream fos = new FileOutputStream (childFile );
63+ fos .write ("Hello World" .getBytes ()); // write operation
64+ fos .flush ();
65+ fos .close ();
66+
67+ removeDirectory (parentDirectory );
68+ }
69+
70+ @ Test (expected = FileNotFoundException .class )
71+ public void givenWriteOnlyFileCreated_whenCreateNewFileInvoked_thenFileCannotBeReadFrom () throws IOException {
72+ File parentDirectory = makeDirectory ("filesDirectory" );
73+
74+ File childFile = new File (parentDirectory , "file1.txt" );
75+ childFile .createNewFile ();
76+
77+ childFile .setReadable (false );
78+
79+ FileInputStream fis = new FileInputStream (childFile );
80+ fis .read (); // read operation
81+ fis .close ();
82+
83+ removeDirectory (parentDirectory );
84+ }
85+
86+ @ Test
87+ public void givenFilesCreatedInDirectory_whenCreateNewFileInvoked_thenTheyCanBeListedAsExpected () throws IOException {
88+ File directory = makeDirectory ("filtersDirectory" );
89+
90+ File csvFile = new File (directory , "csvFile.csv" );
91+ csvFile .createNewFile ();
92+
93+ File txtFile = new File (directory , "txtFile.txt" );
94+ txtFile .createNewFile ();
95+
96+ //normal listing
97+ assertEquals (2 , directory .list ().length );
98+
99+ //filtered listing
100+ FilenameFilter csvFilter = (dir , ext ) -> ext .endsWith (".csv" );
101+ assertEquals (1 , directory .list (csvFilter ).length );
102+
103+ removeDirectory (directory );
104+ }
105+
106+ @ Test
107+ public void givenDirectoryIsCreated_whenMkdirInvoked_thenDirectoryCanBeRenamed () {
108+
109+ File source = makeDirectory ("source" );
110+ File destination = makeDirectory ("destination" );
111+ source .renameTo (destination );
112+
113+ assertFalse (source .isDirectory ());
114+ assertTrue (destination .isDirectory ());
115+
116+ removeDirectory (destination );
117+ }
118+
119+ @ Test
120+ public void givenDataIsWrittenToFile_whenWriteIsInvoked_thenFreeSpaceOnSystemDecreases () throws IOException {
121+
122+ String name = System .getProperty ("user.home" ) + System .getProperty ("file.separator" ) + "test" ;
123+ File testDir = makeDirectory (name );
124+ File sample = new File (testDir , "sample.txt" );
125+
126+ long freeSpaceBeforeWrite = testDir .getFreeSpace ();
127+ writeSampleDataToFile (sample );
128+
129+ long freeSpaceAfterWrite = testDir .getFreeSpace ();
130+ assertTrue (freeSpaceAfterWrite < freeSpaceBeforeWrite );
131+
132+ removeDirectory (testDir );
133+ }
134+
135+ private static File makeDirectory (String directoryName ) {
136+ File directory = new File (directoryName );
137+ directory .mkdir ();
138+ if (directory .isDirectory ()) {
139+ return directory ;
140+ }
141+ throw new RuntimeException ("Directory not created for " + directoryName );
142+ }
143+
144+ private static void removeDirectory (File directory ) {
145+ // make sure you don't delete your home directory here
146+ if (directory .getPath ().equals (System .getProperty ("user.home" ))) {
147+ return ;
148+ }
149+
150+ // remove directory and its files from system
151+ if (directory != null && directory .exists ()) {
152+ // delete all files inside the directory
153+ File [] filesInDirectory = directory .listFiles ();
154+ if (filesInDirectory != null ) {
155+ List <File > files = Arrays .asList (filesInDirectory );
156+ files .forEach (f -> deleteFile (f ));
157+ }
158+
159+ // finally delete the directory itself
160+ deleteFile (directory );
161+ }
162+ }
163+
164+ private static void deleteFile (File fileToDelete ) {
165+ if (fileToDelete != null && fileToDelete .exists ()) {
166+ fileToDelete .delete ();
167+ }
168+ }
169+
170+ private static void writeSampleDataToFile (File sample ) throws IOException {
171+ //write sample text to file
172+ try (FileOutputStream out = new FileOutputStream (sample )) {
173+ for (int i = 1 ; i <= 100000 ; i ++) {
174+ String sampleText = "Sample line number " + i + "\n " ;
175+ out .write (sampleText .getBytes ());
176+ }
177+ }
178+ }
179+ }
0 commit comments