• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.hamcrest.io;
2 
3 import org.hamcrest.AbstractMatcherTest;
4 import org.hamcrest.Matcher;
5 
6 import java.io.File;
7 
8 import static org.hamcrest.core.IsEqual.equalTo;
9 
10 @SuppressWarnings("ResultOfMethodCallIgnored")
11 public class FileMatchersTest extends AbstractMatcherTest {
12 
13     private File directory;
14     private File file;
15 
16     @Override
setUp()17     protected void setUp() throws Exception {
18         directory = File.createTempFile("myDir", "");
19         directory.delete();
20         directory.mkdirs();
21 
22         file = new File(directory, "myFile");
23         file.createNewFile();
24     }
25 
testAnExistingDirectory()26     public void testAnExistingDirectory() {
27         assertMatches("matches existing directory", FileMatchers.anExistingDirectory(), directory);
28         assertDoesNotMatch("doesn't match existing file", FileMatchers.anExistingDirectory(), file);
29         assertDoesNotMatch("doesn't match missing file", FileMatchers.anExistingDirectory(), new File("foo"));
30     }
31 
testAnExistingFileOrDirectory()32     public void testAnExistingFileOrDirectory() {
33         assertMatches("matches existing file", FileMatchers.anExistingFileOrDirectory(), file);
34         assertMatches("matches existing directory", FileMatchers.anExistingFileOrDirectory(), directory);
35         assertDoesNotMatch("doesn't match missing file", FileMatchers.anExistingFileOrDirectory(), new File("foo"));
36     }
37 
testAnExistingFile()38     public void testAnExistingFile() {
39         assertMatches("matches existing file", FileMatchers.anExistingFile(), file);
40         assertDoesNotMatch("doesn't match existing directory", FileMatchers.anExistingFile(), directory);
41         assertDoesNotMatch("doesn't match missing file", FileMatchers.anExistingFile(), new File("foo"));
42     }
43 
testAReadableFile()44     public void testAReadableFile() {
45         file.setReadable(true);
46         assertMatches("matches readable file", FileMatchers.aReadableFile(), file);
47         if (file.setReadable(false)) {
48             assertDoesNotMatch("doesn't match unreadable file", FileMatchers.aReadableFile(), file);
49         }
50     }
51 
testAWritableFile()52     public void testAWritableFile() {
53         assertMatches("matches writable file", FileMatchers.aWritableFile(), file);
54         file.setWritable(false);
55         assertDoesNotMatch("doesn't match unwritable file", FileMatchers.aWritableFile(), file);
56     }
57 
testAFileWithSizeLong()58     public void testAFileWithSizeLong() {
59         assertMatches("matches file size", FileMatchers.aFileWithSize(0L), file);
60         file.setWritable(false);
61         assertDoesNotMatch("doesn't match incorrect file size", FileMatchers.aFileWithSize(34L), file);
62     }
63 
testAFileWithSizeMatcherOfLong()64     public void testAFileWithSizeMatcherOfLong() {
65         assertMatches("matches file size", FileMatchers.aFileWithSize(equalTo(0L)), file);
66         file.setWritable(false);
67         assertDoesNotMatch("doesn't match incorrect file size", FileMatchers.aFileWithSize(equalTo(23L)), file);
68     }
69 
testAFileNamed()70     public void testAFileNamed() {
71         assertMatches("matches file name", FileMatchers.aFileNamed(equalTo(file.getName())), file);
72         file.setWritable(false);
73         assertDoesNotMatch("doesn't match incorrect file name", FileMatchers.aFileNamed(equalTo("foo")), file);
74     }
75 
testAFileWithCanonicalPath()76     public void testAFileWithCanonicalPath() throws Exception {
77         assertMatches("matches file canonical path", FileMatchers.aFileWithCanonicalPath(equalTo(file.getCanonicalPath())), file);
78         file.setWritable(false);
79         assertDoesNotMatch("doesn't match incorrect canonical path", FileMatchers.aFileWithCanonicalPath(equalTo("foo")), file);
80     }
81 
testAFileWithAbsolutePath()82     public void testAFileWithAbsolutePath() {
83         assertMatches("matches file absolute path", FileMatchers.aFileWithAbsolutePath(equalTo(file.getAbsolutePath())), file);
84         file.setWritable(false);
85         assertDoesNotMatch("doesn't match incorrect absolute path", FileMatchers.aFileWithAbsolutePath(equalTo("foo")), file);
86     }
87 
88     @Override
createMatcher()89     protected Matcher<?> createMatcher() {
90         return FileMatchers.aFileWithSize(1L);
91     }
92 
93 }
94