Skip to content

Commit 527f3c9

Browse files
Add a few tests for DataTools.readFile(...)
1 parent 925e887 commit 527f3c9

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

src/test/java/loci/common/utests/DataToolsTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232

3333
package loci.common.utests;
3434

35+
import loci.common.ByteArrayHandle;
3536
import loci.common.DataTools;
37+
import loci.common.Location;
3638
import java.util.Locale;
3739

3840
import static org.testng.AssertJUnit.assertEquals;
41+
import static org.testng.AssertJUnit.assertFalse;
3942
import static org.testng.AssertJUnit.fail;
4043
import org.testng.annotations.DataProvider;
4144
import org.testng.annotations.Test;
@@ -236,4 +239,59 @@ public void testParseDouble(String locale) {
236239
public void testThreadSafety() {
237240
assertEquals(DataTools.parseDouble("1.0"), 1.0d);
238241
}
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+
}
239297
}

0 commit comments

Comments
 (0)