1+ package com .baeldung .commons .io ;
2+
3+ import org .apache .commons .io .FileSystemUtils ;
4+ import org .apache .commons .io .FileUtils ;
5+ import org .apache .commons .io .FilenameUtils ;
6+ import org .apache .commons .io .IOCase ;
7+ import org .apache .commons .io .comparator .PathFileComparator ;
8+ import org .apache .commons .io .comparator .SizeFileComparator ;
9+ import org .apache .commons .io .filefilter .AndFileFilter ;
10+ import org .apache .commons .io .filefilter .NameFileFilter ;
11+ import org .apache .commons .io .filefilter .SuffixFileFilter ;
12+ import org .apache .commons .io .filefilter .WildcardFileFilter ;
13+ import org .apache .commons .io .input .TeeInputStream ;
14+ import org .apache .commons .io .output .TeeOutputStream ;
15+ import org .junit .Assert ;
16+ import org .junit .Test ;
17+
18+ import java .io .ByteArrayInputStream ;
19+ import java .io .ByteArrayOutputStream ;
20+ import java .io .File ;
21+ import java .io .FilterOutputStream ;
22+ import java .io .IOException ;
23+ import java .nio .charset .Charset ;
24+
25+ public class CommonsIOUnitTest {
26+
27+ @ Test
28+ public void whenCopyANDReadFileTesttxt_thenMatchExpectedData ()
29+ throws IOException {
30+
31+ String expectedData = "Hello World from fileTest.txt!!!" ;
32+
33+ File file = FileUtils .getFile (getClass ().getClassLoader ()
34+ .getResource ("fileTest.txt" )
35+ .getPath ());
36+ File tempDir = FileUtils .getTempDirectory ();
37+ FileUtils .copyFileToDirectory (file , tempDir );
38+ File newTempFile = FileUtils .getFile (tempDir , file .getName ());
39+ String data = FileUtils .readFileToString (newTempFile ,
40+ Charset .defaultCharset ());
41+
42+ Assert .assertEquals (expectedData , data .trim ());
43+ }
44+
45+ @ Test
46+ public void whenUsingFileNameUtils_thenshowdifferentFileOperations ()
47+ throws IOException {
48+
49+ String path = getClass ().getClassLoader ()
50+ .getResource ("fileTest.txt" )
51+ .getPath ();
52+
53+ String fullPath = FilenameUtils .getFullPath (path );
54+ String extension = FilenameUtils .getExtension (path );
55+ String baseName = FilenameUtils .getBaseName (path );
56+
57+ System .out .println ("full path" + fullPath );
58+ System .out .println ("Extension" + extension );
59+ System .out .println ("Base name" + baseName );
60+ }
61+
62+ @ Test
63+ public void whenUsingFileSystemUtils_thenDriveFreeSpace ()
64+ throws IOException {
65+
66+ long freeSpace = FileSystemUtils .freeSpaceKb ("/" );
67+ }
68+
69+ @ SuppressWarnings ("resource" )
70+ @ Test
71+ public void whenUsingTeeInputOutputStream_thenWriteto2OutputStreams ()
72+ throws IOException {
73+
74+ final String str = "Hello World." ;
75+ ByteArrayInputStream inputStream = new ByteArrayInputStream (str .getBytes ());
76+ ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream ();
77+ ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream ();
78+
79+ FilterOutputStream teeOutputStream = new TeeOutputStream (outputStream1 ,outputStream2 );
80+ new TeeInputStream (inputStream , teeOutputStream , true ).read (new byte [str .length ()]);
81+
82+ Assert .assertEquals (str , String .valueOf (outputStream1 ));
83+ Assert .assertEquals (str , String .valueOf (outputStream2 ));
84+ }
85+
86+ @ Test
87+ public void whenGetFilewithNameFileFilter_thenFindfileTesttxt ()
88+ throws IOException {
89+
90+ final String testFile = "fileTest.txt" ;
91+
92+ String path = getClass ().getClassLoader ()
93+ .getResource (testFile )
94+ .getPath ();
95+ File dir = FileUtils .getFile (FilenameUtils .getFullPath (path ));
96+
97+ String [] possibleNames = { "NotThisOne" , testFile };
98+
99+ Assert .assertEquals (testFile ,
100+ dir .list (new NameFileFilter (possibleNames , IOCase .INSENSITIVE ))[0 ]);
101+ }
102+
103+ @ Test
104+ public void whenGetFilewith_ANDFileFilter_thenFindsampletxt ()
105+ throws IOException {
106+
107+ String path = getClass ().getClassLoader ()
108+ .getResource ("fileTest.txt" )
109+ .getPath ();
110+ File dir = FileUtils .getFile (FilenameUtils .getFullPath (path ));
111+
112+ Assert .assertEquals ("sample.txt" ,
113+ dir .list (new AndFileFilter (
114+ new WildcardFileFilter ("*ple*" , IOCase .INSENSITIVE ),
115+ new SuffixFileFilter ("txt" )))[0 ]);
116+ }
117+
118+ @ Test
119+ public void whenSortDirWithPathFileComparator_thenFirstFileaaatxt ()
120+ throws IOException {
121+
122+ PathFileComparator pathFileComparator = new PathFileComparator (
123+ IOCase .INSENSITIVE );
124+ String path = FilenameUtils .getFullPath (getClass ().getClassLoader ()
125+ .getResource ("fileTest.txt" )
126+ .getPath ());
127+ File dir = new File (path );
128+ File [] files = dir .listFiles ();
129+
130+ pathFileComparator .sort (files );
131+
132+ Assert .assertEquals ("aaa.txt" , files [0 ].getName ());
133+ }
134+
135+ @ Test
136+ public void whenSizeFileComparator_thenLargerFile ()
137+ throws IOException {
138+
139+ SizeFileComparator sizeFileComparator = new SizeFileComparator ();
140+ File largerFile = FileUtils .getFile (getClass ().getClassLoader ()
141+ .getResource ("fileTest.txt" )
142+ .getPath ());
143+ File smallerFile = FileUtils .getFile (getClass ().getClassLoader ()
144+ .getResource ("sample.txt" )
145+ .getPath ());
146+
147+ int i = sizeFileComparator .compare (largerFile , smallerFile );
148+
149+ Assert .assertTrue (i > 0 );
150+ }
151+ }
0 commit comments