|
32 | 32 |
|
33 | 33 | package loci.common.utests; |
34 | 34 |
|
| 35 | +import loci.common.ByteArrayHandle; |
35 | 36 | import loci.common.DataTools; |
| 37 | +import loci.common.Location; |
36 | 38 | import java.util.Locale; |
37 | 39 |
|
38 | 40 | import static org.testng.AssertJUnit.assertEquals; |
| 41 | +import static org.testng.AssertJUnit.assertFalse; |
39 | 42 | import static org.testng.AssertJUnit.fail; |
40 | 43 | import org.testng.annotations.DataProvider; |
41 | 44 | import org.testng.annotations.Test; |
@@ -236,4 +239,59 @@ public void testParseDouble(String locale) { |
236 | 239 | public void testThreadSafety() { |
237 | 240 | assertEquals(DataTools.parseDouble("1.0"), 1.0d); |
238 | 241 | } |
| 242 | + |
| 243 | + |
| 244 | + @Test |
| 245 | + public void testReadFileDefaultEncoding() { |
| 246 | + String src = "µ€œẞ"; |
| 247 | + String tmpFile = "utf-test.txt"; |
| 248 | + try { |
| 249 | + byte[] bytes = src.getBytes("UTF-8"); |
| 250 | + Location.mapFile(tmpFile, new ByteArrayHandle(bytes)); |
| 251 | + String result = DataTools.readFile(tmpFile); |
| 252 | + assertEquals(src, result); |
| 253 | + } |
| 254 | + catch (Exception e) { |
| 255 | + fail(e.getMessage()); |
| 256 | + } |
| 257 | + finally { |
| 258 | + Location.mapFile(tmpFile, null); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + @Test |
| 263 | + public void testReadFileDifferentEncoding() { |
| 264 | + String src = "µ€œ"; |
| 265 | + String tmpFile = "windows-1252-test.txt"; |
| 266 | + try { |
| 267 | + byte[] bytes = src.getBytes("windows-1252"); |
| 268 | + Location.mapFile(tmpFile, new ByteArrayHandle(bytes)); |
| 269 | + String result = DataTools.readFile(tmpFile, "windows-1252"); |
| 270 | + assertEquals(src, result); |
| 271 | + } |
| 272 | + catch (Exception e) { |
| 273 | + fail(e.getMessage()); |
| 274 | + } |
| 275 | + finally { |
| 276 | + Location.mapFile(tmpFile, null); |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + @Test |
| 281 | + public void testReadFileWrongEncoding() { |
| 282 | + String src = "µ€œ"; |
| 283 | + String tmpFile = "windows-1252-vs-utf8-test.txt"; |
| 284 | + try { |
| 285 | + byte[] bytes = src.getBytes("UTF-8"); |
| 286 | + Location.mapFile(tmpFile, new ByteArrayHandle(bytes)); |
| 287 | + String result = DataTools.readFile(tmpFile, "windows-1252"); |
| 288 | + assertFalse(src.equals(result)); |
| 289 | + } |
| 290 | + catch (Exception e) { |
| 291 | + fail(e.getMessage()); |
| 292 | + } |
| 293 | + finally { |
| 294 | + Location.mapFile(tmpFile, null); |
| 295 | + } |
| 296 | + } |
239 | 297 | } |
0 commit comments