22
22
import static org .junit .Assert .assertEquals ;
23
23
import static org .junit .Assert .assertFalse ;
24
24
import static org .junit .Assert .assertNotNull ;
25
- import static org .junit .Assert .fail ;
25
+ import static org .junit .Assert .assertThrows ;
26
26
27
27
import java .io .File ;
28
- import java .io .IOException ;
29
28
30
29
import org .apache .maven .enforcer .rule .api .EnforcerRuleException ;
31
30
import org .junit .Rule ;
@@ -46,34 +45,43 @@ public class TestRequireFilesExist
46
45
47
46
@ Test
48
47
public void testFileExists ()
49
- throws EnforcerRuleException , IOException
48
+ throws Exception
50
49
{
51
50
File f = temporaryFolder .newFile ();
52
51
53
- rule .setFiles ( new File [] { f } );
52
+ rule .setFiles ( new File [] { f . getCanonicalFile () } );
54
53
55
54
rule .execute ( EnforcerTestUtils .getHelper () );
56
55
}
56
+
57
+ @ Test
58
+ public void testFileOsIndependentExists ()
59
+ throws Exception
60
+ {
61
+ rule .setFiles ( new File [] { new File ( "POM.xml" ) } );
62
+
63
+ EnforcerRuleException e =
64
+ assertThrows ( EnforcerRuleException .class , () -> rule .execute ( EnforcerTestUtils .getHelper () ) );
65
+
66
+ assertNotNull ( e .getMessage () );
67
+ }
68
+
57
69
58
70
@ Test
59
71
public void testEmptyFile ()
60
- throws EnforcerRuleException , IOException
72
+ throws Exception
61
73
{
62
74
rule .setFiles ( new File [] { null } );
63
- try
64
- {
65
- rule .execute ( EnforcerTestUtils .getHelper () );
66
- fail ( "Should get exception" );
67
- }
68
- catch ( EnforcerRuleException e )
69
- {
70
- assertNotNull ( e .getMessage () );
71
- }
75
+
76
+ EnforcerRuleException e =
77
+ assertThrows ( EnforcerRuleException .class , () -> rule .execute ( EnforcerTestUtils .getHelper () ) );
78
+
79
+ assertNotNull ( e .getMessage () );
72
80
}
73
81
74
82
@ Test
75
83
public void testEmptyFileAllowNull ()
76
- throws EnforcerRuleException , IOException
84
+ throws Exception
77
85
{
78
86
rule .setFiles ( new File [] { null } );
79
87
rule .setAllowNulls ( true );
@@ -82,24 +90,21 @@ public void testEmptyFileAllowNull()
82
90
83
91
@ Test
84
92
public void testEmptyFileList ()
85
- throws EnforcerRuleException , IOException
93
+ throws Exception
86
94
{
87
95
rule .setFiles ( new File [] {} );
88
96
assertEquals ( 0 , rule .getFiles ().length );
89
- try
90
- {
91
- rule .execute ( EnforcerTestUtils .getHelper () );
92
- fail ( "Should get exception" );
93
- }
94
- catch ( EnforcerRuleException e )
95
- {
96
- assertNotNull ( e .getMessage () );
97
- }
97
+
98
+ EnforcerRuleException e =
99
+ assertThrows ( EnforcerRuleException .class , () -> rule .execute ( EnforcerTestUtils .getHelper () ) );
100
+
101
+ assertNotNull ( e .getMessage () );
102
+
98
103
}
99
104
100
105
@ Test
101
106
public void testEmptyFileListAllowNull ()
102
- throws EnforcerRuleException , IOException
107
+ throws Exception
103
108
{
104
109
rule .setFiles ( new File [] {} );
105
110
assertEquals ( 0 , rule .getFiles ().length );
@@ -109,23 +114,19 @@ public void testEmptyFileListAllowNull()
109
114
110
115
@ Test
111
116
public void testFileDoesNotExist ()
112
- throws EnforcerRuleException , IOException
117
+ throws Exception
113
118
{
114
119
File f = temporaryFolder .newFile ();
115
120
f .delete ();
116
121
117
122
assertFalse ( f .exists () );
118
123
rule .setFiles ( new File [] { f } );
119
124
120
- try
121
- {
122
- rule .execute ( EnforcerTestUtils .getHelper () );
123
- fail ( "Should get exception" );
124
- }
125
- catch ( EnforcerRuleException e )
126
- {
127
- assertNotNull ( e .getMessage () );
128
- }
125
+ EnforcerRuleException e =
126
+ assertThrows ( EnforcerRuleException .class , () -> rule .execute ( EnforcerTestUtils .getHelper () ) );
127
+
128
+ assertNotNull ( e .getMessage () );
129
+
129
130
}
130
131
131
132
/**
0 commit comments