24
24
import java .io .ByteArrayOutputStream ;
25
25
import java .io .IOException ;
26
26
import java .nio .charset .Charset ;
27
+ import java .nio .charset .CharacterCodingException ;
27
28
import java .nio .charset .MalformedInputException ;
28
29
import java .nio .charset .UnmappableCharacterException ;
29
- import static java .nio .charset .StandardCharsets .US_ASCII ;
30
30
import static java .nio .charset .StandardCharsets .ISO_8859_1 ;
31
+ import static java .nio .charset .StandardCharsets .US_ASCII ;
32
+ import static java .nio .charset .StandardCharsets .UTF_16 ;
31
33
import static java .nio .charset .StandardCharsets .UTF_8 ;
32
34
import java .nio .file .Files ;
33
35
import java .nio .file .OpenOption ;
46
48
import org .testng .annotations .Test ;
47
49
48
50
/* @test
49
- * @bug 8201276 8205058 8209576 8287541
51
+ * @bug 8201276 8205058 8209576 8287541 8288589
50
52
* @build ReadWriteString PassThroughFileSystem
51
53
* @run testng ReadWriteString
52
54
* @summary Unit test for methods for Files readString and write methods.
@@ -60,6 +62,8 @@ public class ReadWriteString {
60
62
final String TEXT_UNICODE = "\u201C Hello\u201D " ;
61
63
final String TEXT_ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n abcdefghijklmnopqrstuvwxyz\n 1234567890\n " ;
62
64
private static final String JA_STRING = "\u65e5 \u672c \u8a9e \u6587 \u5b57 \u5217 " ;
65
+ private static final Charset WINDOWS_1252 = Charset .forName ("windows-1252" );
66
+ private static final Charset WINDOWS_31J = Charset .forName ("windows-31j" );
63
67
64
68
static byte [] data = getData ();
65
69
@@ -88,14 +92,14 @@ static byte[] getData() {
88
92
*/
89
93
@ DataProvider (name = "malformedWrite" )
90
94
public Object [][] getMalformedWrite () throws IOException {
91
- Path path = Files .createTempFile ( "malformedWrite" , null );
95
+ Path path = Files .createFile ( Path . of ( "malformedWrite" ) );
92
96
return new Object [][]{
93
97
{path , "\ud800 " , null }, //the default Charset is UTF_8
94
98
{path , "\u00A0 \u00A1 " , US_ASCII },
95
99
{path , "\ud800 " , UTF_8 },
96
100
{path , JA_STRING , ISO_8859_1 },
97
- {path , "\u041e " , Charset . forName ( "windows-1252" ) }, // cyrillic capital letter O
98
- {path , "\u091c " , Charset . forName ( "windows-31j" ) }, // devanagari letter ja
101
+ {path , "\u041e " , WINDOWS_1252 }, // cyrillic capital letter O
102
+ {path , "\u091c " , WINDOWS_31J }, // devanagari letter ja
99
103
};
100
104
}
101
105
@@ -105,13 +109,26 @@ public Object[][] getMalformedWrite() throws IOException {
105
109
*/
106
110
@ DataProvider (name = "illegalInput" )
107
111
public Object [][] getIllegalInput () throws IOException {
108
- Path path = Files .createTempFile ( "illegalInput" , null );
112
+ Path path = Files .createFile ( Path . of ( "illegalInput" ) );
109
113
return new Object [][]{
110
114
{path , data , ISO_8859_1 , null },
111
115
{path , data , ISO_8859_1 , UTF_8 }
112
116
};
113
117
}
114
118
119
+ /*
120
+ * DataProvider for illegal input bytes test
121
+ */
122
+ @ DataProvider (name = "illegalInputBytes" )
123
+ public Object [][] getIllegalInputBytes () throws IOException {
124
+ return new Object [][]{
125
+ {new byte [] {(byte )0x00 , (byte )0x20 , (byte )0x00 }, UTF_16 , MalformedInputException .class },
126
+ {new byte [] {-50 }, UTF_16 , MalformedInputException .class },
127
+ {new byte [] {(byte )0x81 }, WINDOWS_1252 , UnmappableCharacterException .class }, // unused in Cp1252
128
+ {new byte [] {(byte )0x81 , (byte )0xff }, WINDOWS_31J , UnmappableCharacterException .class }, // invalid trailing byte
129
+ };
130
+ }
131
+
115
132
/*
116
133
* DataProvider for writeString test
117
134
* Writes the data using both the existing and new method and compares the results.
@@ -143,16 +160,9 @@ public Object[][] getReadString() {
143
160
144
161
@ BeforeClass
145
162
void setup () throws IOException {
146
- testFiles [0 ] = Files .createTempFile ("readWriteString" , null );
147
- testFiles [1 ] = Files .createTempFile ("writeString_file1" , null );
148
- testFiles [2 ] = Files .createTempFile ("writeString_file2" , null );
149
- }
150
-
151
- @ AfterClass
152
- void cleanup () throws IOException {
153
- for (Path path : testFiles ) {
154
- Files .deleteIfExists (path );
155
- }
163
+ testFiles [0 ] = Files .createFile (Path .of ("readWriteString" ));
164
+ testFiles [1 ] = Files .createFile (Path .of ("writeString_file1" ));
165
+ testFiles [2 ] = Files .createFile (Path .of ("writeString_file2" ));
156
166
}
157
167
158
168
/**
@@ -241,11 +251,10 @@ public void testReadString(Path path, String text, Charset cs, Charset cs2) thro
241
251
*/
242
252
@ Test (dataProvider = "malformedWrite" , expectedExceptions = UnmappableCharacterException .class )
243
253
public void testMalformedWrite (Path path , String s , Charset cs ) throws IOException {
244
- path .toFile ().deleteOnExit ();
245
254
if (cs == null ) {
246
- Files .writeString (path , s , CREATE );
255
+ Files .writeString (path , s );
247
256
} else {
248
- Files .writeString (path , s , cs , CREATE );
257
+ Files .writeString (path , s , cs );
249
258
}
250
259
}
251
260
@@ -261,16 +270,40 @@ public void testMalformedWrite(Path path, String s, Charset cs) throws IOExcepti
261
270
*/
262
271
@ Test (dataProvider = "illegalInput" , expectedExceptions = MalformedInputException .class )
263
272
public void testMalformedRead (Path path , byte [] data , Charset csWrite , Charset csRead ) throws IOException {
264
- path .toFile ().deleteOnExit ();
265
273
String temp = new String (data , csWrite );
266
- Files .writeString (path , temp , csWrite , CREATE );
274
+ Files .writeString (path , temp , csWrite );
267
275
if (csRead == null ) {
268
276
Files .readString (path );
269
277
} else {
270
278
Files .readString (path , csRead );
271
279
}
272
280
}
273
281
282
+ /**
283
+ * Verifies that IOException is thrown when reading a file containing
284
+ * illegal bytes
285
+ *
286
+ * @param data the data used for the test
287
+ * @param csRead the Charset to use for reading the file
288
+ * @param expected exception class
289
+ * @throws IOException when the Charset used for reading the file is incorrect
290
+ */
291
+ @ Test (dataProvider = "illegalInputBytes" )
292
+ public void testMalformedReadBytes (byte [] data , Charset csRead , Class <CharacterCodingException > expected )
293
+ throws IOException {
294
+ Path path = Path .of ("illegalInputBytes" );
295
+ Files .write (path , data );
296
+ try {
297
+ Files .readString (path , csRead );
298
+ } catch (MalformedInputException | UnmappableCharacterException e ) {
299
+ if (expected .isInstance (e )) {
300
+ // success
301
+ return ;
302
+ }
303
+ }
304
+ throw new RuntimeException ("An instance of " + expected + " should be thrown" );
305
+ }
306
+
274
307
private void checkNullPointerException (Callable <?> c ) {
275
308
try {
276
309
c .call ();
0 commit comments