aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/java/jopt-simple/src/test/java/joptsimple/util/PathConverterTest.java
blob: 6b543936cde166a25f2f56ae3a08026280a4f66f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package joptsimple.util;

import java.nio.file.Files;
import java.nio.file.Path;

import joptsimple.ValueConversionException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static joptsimple.util.PathProperties.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class PathConverterTest {
    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Test
    public void convertsValuesToPaths() throws Exception {
        Path path = Files.createTempFile( "prefix", null );
        path.toFile().deleteOnExit();

        String pathName = path.toString();

        assertEquals( path, new PathConverter( null ).convert( pathName ) );
        assertEquals( path, new PathConverter().convert( pathName ) );
    }

    @Test
    public void answersCorrectValueType() {
        assertEquals( Path.class, new PathConverter().valueType() );
    }

    @Test
    public void testReadableAndOverwritableFile() throws Exception {
        Path path = Files.createTempFile( "prefix", null );
        path.toFile().deleteOnExit();

        String pathName = path.toString();

        assertTrue( Files.isReadable( new PathConverter( READABLE ).convert( pathName ) ) );
        assertTrue( Files.exists( new PathConverter( READABLE ).convert( pathName ) ) );
        assertTrue( Files.isWritable( new PathConverter( READABLE ).convert( pathName ) ) );
        assertTrue( Files.isWritable( new PathConverter( FILE_OVERWRITABLE).convert( pathName ) ) );
    }

    @Test
    public void testNotExisting() throws Exception {
        Path path = Files.createTempFile( "prefix", null );

        Files.deleteIfExists( path );

        assertFalse( Files.exists( new PathConverter( NOT_EXISTING ).convert( path.toString() ) ) );
    }

    @Test
    public void testNotReadable() throws Exception {
        Path path = Files.createTempFile( "prefix", null );
        String pathName = path.toString();
        Files.deleteIfExists( path );

        exception.expect( ValueConversionException.class );
        exception.expectMessage( "File [" + pathName );

        new PathConverter( READABLE ).convert( pathName );
    }

    @Test
    public void testDirectoryExisting() throws Exception {
        Path path = Files.createTempDirectory( "prefix" );
        path.toFile().deleteOnExit();

        String pathName = path.toString();

        assertTrue( Files.isDirectory( new PathConverter( DIRECTORY_EXISTING ).convert( pathName ) ) );
    }

    @Test
    public void testDirectoryNotOverwritable() throws Exception {
        Path path = Files.createTempDirectory( "prefix" );
        path.toFile().deleteOnExit();

        String pathName = path.toString();

        exception.expect( ValueConversionException.class );
        exception.expectMessage( "File [" + pathName );

        new PathConverter( FILE_OVERWRITABLE).convert( pathName );
    }

    @Test
    public void testNotExistingNotOverwritable() throws Exception {
        Path path = Files.createTempDirectory( "prefix" );
        String pathName = path.toString();
        Files.deleteIfExists( path );

        exception.expect( ValueConversionException.class );
        exception.expectMessage( "File [" + pathName );

        new PathConverter( FILE_OVERWRITABLE ).convert( pathName );
    }
}