7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.ByteArrayOutputStream; 25 import java.io.IOException; 26 import java.nio.charset.Charset; 27 import static java.nio.charset.StandardCharsets.US_ASCII; 28 import static java.nio.charset.StandardCharsets.ISO_8859_1; 29 import static java.nio.charset.StandardCharsets.UTF_8; 30 import java.nio.file.Files; 31 import java.nio.file.OpenOption; 32 import java.nio.file.Path; 33 import java.nio.file.Paths; 34 import java.nio.file.StandardOpenOption; 35 import static java.nio.file.StandardOpenOption.APPEND; 36 import java.util.Random; 37 import java.util.concurrent.Callable; 38 import static org.testng.Assert.assertTrue; 39 import static org.testng.Assert.fail; 40 import org.testng.annotations.AfterClass; 41 import org.testng.annotations.BeforeClass; 42 import org.testng.annotations.DataProvider; 43 import org.testng.annotations.Test; 44 45 /* @test 46 * @bug 8201276 47 * @build ReadWriteString PassThroughFileSystem 48 * @run testng ReadWriteString 49 * @summary Unit test for methods for Files readString and write methods. 50 * @key randomness 51 */ 52 @Test(groups = "readwrite") 53 public class ReadWriteString { 54 55 private static final OpenOption OPTION_CREATE = StandardOpenOption.CREATE; 56 // data for text files 57 private static final String EN_STRING = "The quick brown fox jumps over the lazy dog"; 58 private static final String JA_STRING = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217"; 59 // malformed input: a high surrogate without the low surrogate 60 static char[] illChars = { 61 '\u00fa', '\ud800' 62 }; 63 64 static byte[] data = getData(); 65 66 static byte[] getData() { 67 try { 68 String str1 = "A string that contains "; 69 String str2 = " , an invalid character for UTF-8."; 70 71 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 72 baos.write(str1.getBytes()); 73 baos.write(0xFA); 74 baos.write(str2.getBytes()); 75 return baos.toByteArray(); 76 } catch (IOException ex) { 77 return null; //shouldn't happen 78 } 79 } 80 81 // file used by most tests 82 private Path tmpfile; 83 84 85 /* 86 * DataProvider for malformed write test. Provides the following fields: 87 * file path, malformed input string, charset 88 */ 89 @DataProvider(name = "malformedWrite") 90 public Object[][] getMalformedWrite() throws IOException { 91 Path path = Files.createTempFile("malformedWrite", null); 92 return new Object[][]{ 93 {path, "\ud800", null}, //the default Charset is UTF_8 94 {path, "\u00A0\u00A1", US_ASCII}, 95 {path, "\ud800", UTF_8}, 96 {path, JA_STRING, ISO_8859_1}, 97 }; 108 {path, data, ISO_8859_1, null}, 109 {path, data, ISO_8859_1, UTF_8} 110 }; 111 } 112 113 @BeforeClass 114 void setup() throws IOException { 115 tmpfile = Files.createTempFile("readWriteString", null); 116 } 117 118 @AfterClass 119 void cleanup() throws IOException { 120 Files.deleteIfExists(tmpfile); 121 } 122 123 /** 124 * Verifies that NPE is thrown when one of the parameters is null. 125 */ 126 @Test 127 public void testNulls() { 128 Path path = Paths.get("."); 129 String s = "abc"; 130 131 checkNullPointerException(() -> Files.readString((Path) null)); 132 checkNullPointerException(() -> Files.readString((Path) null, UTF_8)); 133 checkNullPointerException(() -> Files.readString(path, (Charset) null)); 134 135 checkNullPointerException(() -> Files.writeString((Path) null, s, OPTION_CREATE)); 136 checkNullPointerException(() -> Files.writeString(path, (CharSequence) null, OPTION_CREATE)); 137 checkNullPointerException(() -> Files.writeString(path, s, (OpenOption[]) null)); 138 139 checkNullPointerException(() -> Files.writeString((Path) null, s, UTF_8, OPTION_CREATE)); 140 checkNullPointerException(() -> Files.writeString(path, (CharSequence) null, UTF_8, OPTION_CREATE)); 141 checkNullPointerException(() -> Files.writeString(path, s, (Charset) null, OPTION_CREATE)); 142 checkNullPointerException(() -> Files.writeString(path, s, UTF_8, (OpenOption[]) null)); 143 } 144 145 /** 146 * Verifies the readString and write String methods. Writes to files Strings 147 * of various sizes, with/without specifying the Charset, and then compares 148 * the result of reading the files. 149 */ 150 @Test 151 public void testReadWrite() throws IOException { 152 int size = 0; 153 while (size < 16 * 1024) { 154 testReadWrite(size, null, false); 155 testReadWrite(size, null, true); 156 testReadWrite(size, UTF_8, false); 157 testReadWrite(size, UTF_8, true); 158 size += 1024; 159 } 160 } 161 162 /** 163 * Verifies that IOException is thrown (as specified) when giving a malformed 164 * string input. 165 * 166 * @param path the path to write 167 * @param s the string 168 * @param cs the Charset 169 * @throws IOException if the input is malformed 170 */ 171 @Test(dataProvider = "malformedWrite", expectedExceptions = IOException.class) 172 public void testMalformedWrite(Path path, String s, Charset cs) throws IOException { 173 path.toFile().deleteOnExit(); 174 if (cs == null) { 175 Files.writeString(path, s, OPTION_CREATE); 176 } else { 177 Files.writeString(path, s, cs, OPTION_CREATE); 178 } 179 } 180 181 /** 182 * Verifies that IOException is thrown when reading a file using the wrong 183 * Charset. 184 * 185 * @param path the path to write and read 186 * @param data the data used for the test 187 * @param csWrite the Charset to use for writing the test file 188 * @param csRead the Charset to use for reading the file 189 * @throws IOException when the Charset used for reading the file is incorrect 190 */ 191 @Test(dataProvider = "illegalInput", expectedExceptions = IOException.class) 192 public void testMalformedRead(Path path, byte[] data, Charset csWrite, Charset csRead) throws IOException { 193 path.toFile().deleteOnExit(); 194 String temp = new String(data, csWrite); 195 Files.writeString(path, temp, csWrite, OPTION_CREATE); 196 String s; 197 if (csRead == null) { 198 s = Files.readString(path); 199 } else { 200 s = Files.readString(path, csRead); 201 } 202 } 203 204 private void checkNullPointerException(Callable<?> c) { 205 try { 206 c.call(); 207 fail("NullPointerException expected"); 208 } catch (NullPointerException ignore) { 209 } catch (Exception e) { 210 fail(e + " not expected"); 211 } 212 } 213 214 private void testReadWrite(int size, Charset cs, boolean append) throws IOException { 215 StringBuilder sb = new StringBuilder(size); 216 String expected; 217 String str = generateString(size); 218 Path result; 219 if (cs == null) { 220 result = Files.writeString(tmpfile, str); 221 } else { 222 result = Files.writeString(tmpfile, str, cs); 223 } 224 225 //System.out.println(result.toUri().toASCIIString()); 226 assertTrue(result == tmpfile); 227 if (append) { 228 if (cs == null) { 229 Files.writeString(tmpfile, str, APPEND); 230 } else { 231 Files.writeString(tmpfile, str, cs, APPEND); 232 } 233 assertTrue(Files.size(tmpfile) == size * 2); 234 } 235 236 237 if (append) { 238 sb.append(str).append(str); 239 expected = sb.toString(); 240 } else { 241 expected = str; 242 } 243 244 String read; 245 if (cs == null) { 246 read = Files.readString(result); 247 } else { 248 read = Files.readString(result, cs); 249 } 250 //System.out.println("chars read: " + read.length()); 251 //System.out.println(read); 252 //System.out.println("---end---"); 253 assertTrue(read.equals(expected), "String read not the same as written"); 254 } 255 256 static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz \r\n".toCharArray(); 257 StringBuilder sb = new StringBuilder(512); 258 Random random = new Random(); 259 260 private String generateString(int size) { 261 sb.setLength(0); 262 for (int i = 0; i < size; i++) { 263 char c = CHARS[random.nextInt(CHARS.length)]; 264 sb.append(c); 265 } 266 267 return sb.toString(); 268 } 269 } | 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.ByteArrayOutputStream; 25 import java.io.IOException; 26 import java.nio.charset.Charset; 27 import java.nio.charset.MalformedInputException; 28 import java.nio.charset.UnmappableCharacterException; 29 import static java.nio.charset.StandardCharsets.US_ASCII; 30 import static java.nio.charset.StandardCharsets.ISO_8859_1; 31 import static java.nio.charset.StandardCharsets.UTF_8; 32 import java.nio.file.Files; 33 import java.nio.file.OpenOption; 34 import java.nio.file.Path; 35 import java.nio.file.Paths; 36 import static java.nio.file.StandardOpenOption.APPEND; 37 import static java.nio.file.StandardOpenOption.CREATE; 38 import java.util.Random; 39 import java.util.concurrent.Callable; 40 import static org.testng.Assert.assertTrue; 41 import static org.testng.Assert.fail; 42 import org.testng.annotations.AfterClass; 43 import org.testng.annotations.BeforeClass; 44 import org.testng.annotations.DataProvider; 45 import org.testng.annotations.Test; 46 47 /* @test 48 * @bug 8201276 8205058 49 * @build ReadWriteString PassThroughFileSystem 50 * @run testng ReadWriteString 51 * @summary Unit test for methods for Files readString and write methods. 52 * @key randomness 53 */ 54 @Test(groups = "readwrite") 55 public class ReadWriteString { 56 57 // data for text files 58 private static final String EN_STRING = "The quick brown fox jumps over the lazy dog"; 59 private static final String JA_STRING = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217"; 60 // malformed input: a high surrogate without the low surrogate 61 static char[] illChars = { 62 '\u00fa', '\ud800' 63 }; 64 65 static byte[] data = getData(); 66 67 static byte[] getData() { 68 try { 69 String str1 = "A string that contains "; 70 String str2 = " , an invalid character for UTF-8."; 71 72 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 73 baos.write(str1.getBytes()); 74 baos.write(0xFA); 75 baos.write(str2.getBytes()); 76 return baos.toByteArray(); 77 } catch (IOException ex) { 78 // in case it happens, fail the test 79 throw new RuntimeException(ex); 80 } 81 } 82 83 // file used by most tests 84 private Path tmpfile; 85 86 87 /* 88 * DataProvider for malformed write test. Provides the following fields: 89 * file path, malformed input string, charset 90 */ 91 @DataProvider(name = "malformedWrite") 92 public Object[][] getMalformedWrite() throws IOException { 93 Path path = Files.createTempFile("malformedWrite", null); 94 return new Object[][]{ 95 {path, "\ud800", null}, //the default Charset is UTF_8 96 {path, "\u00A0\u00A1", US_ASCII}, 97 {path, "\ud800", UTF_8}, 98 {path, JA_STRING, ISO_8859_1}, 99 }; 110 {path, data, ISO_8859_1, null}, 111 {path, data, ISO_8859_1, UTF_8} 112 }; 113 } 114 115 @BeforeClass 116 void setup() throws IOException { 117 tmpfile = Files.createTempFile("readWriteString", null); 118 } 119 120 @AfterClass 121 void cleanup() throws IOException { 122 Files.deleteIfExists(tmpfile); 123 } 124 125 /** 126 * Verifies that NPE is thrown when one of the parameters is null. 127 */ 128 @Test 129 public void testNulls() { 130 Path path = Paths.get("foo"); 131 String s = "abc"; 132 133 checkNullPointerException(() -> Files.readString((Path) null)); 134 checkNullPointerException(() -> Files.readString((Path) null, UTF_8)); 135 checkNullPointerException(() -> Files.readString(path, (Charset) null)); 136 137 checkNullPointerException(() -> Files.writeString((Path) null, s, CREATE)); 138 checkNullPointerException(() -> Files.writeString(path, (CharSequence) null, CREATE)); 139 checkNullPointerException(() -> Files.writeString(path, s, (OpenOption[]) null)); 140 141 checkNullPointerException(() -> Files.writeString((Path) null, s, UTF_8, CREATE)); 142 checkNullPointerException(() -> Files.writeString(path, (CharSequence) null, UTF_8, CREATE)); 143 checkNullPointerException(() -> Files.writeString(path, s, (Charset) null, CREATE)); 144 checkNullPointerException(() -> Files.writeString(path, s, UTF_8, (OpenOption[]) null)); 145 } 146 147 /** 148 * Verifies the readString and write String methods. Writes to files Strings 149 * of various sizes, with/without specifying the Charset, and then compares 150 * the result of reading the files. 151 */ 152 @Test 153 public void testReadWrite() throws IOException { 154 int size = 0; 155 while (size < 16 * 1024) { 156 testReadWrite(size, null, false); 157 testReadWrite(size, null, true); 158 testReadWrite(size, UTF_8, false); 159 testReadWrite(size, UTF_8, true); 160 size += 1024; 161 } 162 } 163 164 /** 165 * Verifies that IOException is thrown (as specified) when giving a malformed 166 * string input. 167 * 168 * @param path the path to write 169 * @param s the string 170 * @param cs the Charset 171 * @throws IOException if the input is malformed 172 */ 173 @Test(dataProvider = "malformedWrite", expectedExceptions = UnmappableCharacterException.class) 174 public void testMalformedWrite(Path path, String s, Charset cs) throws IOException { 175 path.toFile().deleteOnExit(); 176 if (cs == null) { 177 Files.writeString(path, s, CREATE); 178 } else { 179 Files.writeString(path, s, cs, CREATE); 180 } 181 } 182 183 /** 184 * Verifies that IOException is thrown when reading a file using the wrong 185 * Charset. 186 * 187 * @param path the path to write and read 188 * @param data the data used for the test 189 * @param csWrite the Charset to use for writing the test file 190 * @param csRead the Charset to use for reading the file 191 * @throws IOException when the Charset used for reading the file is incorrect 192 */ 193 @Test(dataProvider = "illegalInput", expectedExceptions = MalformedInputException.class) 194 public void testMalformedRead(Path path, byte[] data, Charset csWrite, Charset csRead) throws IOException { 195 path.toFile().deleteOnExit(); 196 String temp = new String(data, csWrite); 197 Files.writeString(path, temp, csWrite, CREATE); 198 String s; 199 if (csRead == null) { 200 s = Files.readString(path); 201 } else { 202 s = Files.readString(path, csRead); 203 } 204 } 205 206 private void checkNullPointerException(Callable<?> c) { 207 try { 208 c.call(); 209 fail("NullPointerException expected"); 210 } catch (NullPointerException ignore) { 211 } catch (Exception e) { 212 fail(e + " not expected"); 213 } 214 } 215 216 private void testReadWrite(int size, Charset cs, boolean append) throws IOException { 217 String expected; 218 String str = generateString(size); 219 Path result; 220 if (cs == null) { 221 result = Files.writeString(tmpfile, str); 222 } else { 223 result = Files.writeString(tmpfile, str, cs); 224 } 225 226 //System.out.println(result.toUri().toASCIIString()); 227 assertTrue(result == tmpfile); 228 if (append) { 229 if (cs == null) { 230 Files.writeString(tmpfile, str, APPEND); 231 } else { 232 Files.writeString(tmpfile, str, cs, APPEND); 233 } 234 assertTrue(Files.size(tmpfile) == size * 2); 235 } 236 237 238 if (append) { 239 expected = str + str; 240 } else { 241 expected = str; 242 } 243 244 String read; 245 if (cs == null) { 246 read = Files.readString(result); 247 } else { 248 read = Files.readString(result, cs); 249 } 250 251 assertTrue(read.equals(expected), "String read not the same as written"); 252 } 253 254 static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz \r\n".toCharArray(); 255 StringBuilder sb = new StringBuilder(1024 << 4); 256 Random random = new Random(); 257 258 private String generateString(int size) { 259 sb.setLength(0); 260 for (int i = 0; i < size; i++) { 261 char c = CHARS[random.nextInt(CHARS.length)]; 262 sb.append(c); 263 } 264 265 return sb.toString(); 266 } 267 } |