18
18
import java .io .File ;
19
19
import java .nio .charset .Charset ;
20
20
import java .nio .charset .StandardCharsets ;
21
+ import java .nio .file .FileSystem ;
21
22
import java .nio .file .FileSystems ;
22
23
import java .nio .file .Path ;
23
24
import java .nio .file .Paths ;
@@ -94,7 +95,7 @@ protected Formatter create() {
94
95
}.testEquals ();
95
96
}
96
97
97
- // new File("") can be used if there is no File representing this content. It should not conflict with rootDir.relativize(...)
98
+ // new File("") as filePath is known to fail
98
99
@ Test
99
100
public void testExceptionWithEmptyPath () throws Exception {
100
101
LineEnding .Policy lineEndingsPolicy = LineEnding .UNIX .createPolicy ();
@@ -116,7 +117,32 @@ public void testExceptionWithEmptyPath() throws Exception {
116
117
.exceptionPolicy (exceptionPolicy )
117
118
.build ();
118
119
119
- formatter .compute ("someFileContent" , new File ("" ));
120
+ Assertions .assertThrows (IllegalArgumentException .class , () -> formatter .compute ("someFileContent" , new File ("" )));
121
+ }
122
+
123
+ // If there is no File actually holding the content, one may rely on Formatter.NO_FILE_ON_DISK
124
+ @ Test
125
+ public void testExceptionWithSentinelNoFileOnDisk () throws Exception {
126
+ LineEnding .Policy lineEndingsPolicy = LineEnding .UNIX .createPolicy ();
127
+ Charset encoding = StandardCharsets .UTF_8 ;
128
+ FormatExceptionPolicy exceptionPolicy = FormatExceptionPolicy .failOnlyOnError ();
129
+
130
+ Path rootDir = Paths .get (StandardSystemProperty .USER_DIR .value ());
131
+
132
+ FormatterStep step = Mockito .mock (FormatterStep .class );
133
+ Mockito .when (step .getName ()).thenReturn ("someFailingStep" );
134
+ Mockito .when (step .format (Mockito .anyString (), Mockito .any (File .class ))).thenThrow (new IllegalArgumentException ("someReason" ));
135
+ List <FormatterStep > steps = Collections .singletonList (step );
136
+
137
+ Formatter formatter = Formatter .builder ()
138
+ .lineEndingsPolicy (lineEndingsPolicy )
139
+ .encoding (encoding )
140
+ .rootDir (rootDir )
141
+ .steps (steps )
142
+ .exceptionPolicy (exceptionPolicy )
143
+ .build ();
144
+
145
+ formatter .compute ("someFileContent" , Formatter .SENTINEL_NO_FILE_ON_DISK );
120
146
}
121
147
122
148
// rootDir may be a path not from the default FileSystem
@@ -127,6 +153,12 @@ public void testExceptionWithRootDirIsNotFileSystem() throws Exception {
127
153
FormatExceptionPolicy exceptionPolicy = FormatExceptionPolicy .failOnlyOnError ();
128
154
129
155
Path rootDir = Mockito .mock (Path .class );
156
+ FileSystem customFileSystem = Mockito .mock (FileSystem .class );
157
+ Mockito .when (rootDir .getFileSystem ()).thenReturn (customFileSystem );
158
+
159
+ Path pathFromFile = Mockito .mock (Path .class );
160
+ Mockito .when (customFileSystem .getPath (Mockito .anyString ())).thenReturn (pathFromFile );
161
+
130
162
Path relativized = Mockito .mock (Path .class );
131
163
Mockito .when (rootDir .relativize (Mockito .any (Path .class ))).then (invok -> {
132
164
Path filePath = invok .getArgument (0 );
@@ -150,7 +182,7 @@ public void testExceptionWithRootDirIsNotFileSystem() throws Exception {
150
182
.exceptionPolicy (exceptionPolicy )
151
183
.build ();
152
184
153
- formatter .compute ("someFileContent" , new File ("" ));
185
+ formatter .compute ("someFileContent" , new File ("/some/folder/some.file " ));
154
186
}
155
187
156
188
}
0 commit comments