|
27 | 27 | import static org.junit.jupiter.api.Assertions.assertEquals; |
28 | 28 | import static org.junit.jupiter.api.Assertions.assertFalse; |
29 | 29 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
30 | 31 | import static org.junit.jupiter.api.Assertions.assertNotSame; |
31 | 32 | import static org.junit.jupiter.api.Assertions.assertThrows; |
32 | 33 | import static org.junit.jupiter.api.Assertions.assertTrue; |
33 | 34 |
|
| 35 | +import java.io.BufferedReader; |
34 | 36 | import java.io.FileNotFoundException; |
35 | 37 | import java.io.IOException; |
| 38 | +import java.io.StringReader; |
36 | 39 | import java.nio.charset.Charset; |
37 | 40 | import java.nio.file.Files; |
38 | 41 | import java.nio.file.Path; |
| 42 | +import java.util.Objects; |
39 | 43 | import java.util.stream.Stream; |
40 | 44 |
|
| 45 | +import javax.xml.parsers.DocumentBuilder; |
| 46 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 47 | +import javax.xml.parsers.ParserConfigurationException; |
| 48 | + |
41 | 49 | import org.apache.commons.io.FileSystem.NameLengthStrategy; |
42 | 50 | import org.apache.commons.lang3.JavaVersion; |
| 51 | +import org.apache.commons.lang3.StringUtils; |
43 | 52 | import org.apache.commons.lang3.SystemProperties; |
44 | 53 | import org.apache.commons.lang3.SystemUtils; |
45 | 54 | import org.junit.jupiter.api.Test; |
|
48 | 57 | import org.junit.jupiter.params.provider.Arguments; |
49 | 58 | import org.junit.jupiter.params.provider.EnumSource; |
50 | 59 | import org.junit.jupiter.params.provider.MethodSource; |
| 60 | +import org.w3c.dom.Document; |
| 61 | +import org.xml.sax.InputSource; |
| 62 | +import org.xml.sax.SAXException; |
51 | 63 |
|
52 | 64 | /** |
53 | 65 | * Tests {@link FileSystem}. |
@@ -252,6 +264,20 @@ static Stream<Arguments> testNameLengthStrategyTruncate_Throws() { |
252 | 264 | Arguments.of(UTF16_CODE_UNITS, 30, CHAR_UTF8_69B, UTF_8, "truncated to 30 characters"))); |
253 | 265 | } |
254 | 266 |
|
| 267 | + private String parseXmlRootValue(final Path xmlPath, final Charset charset) throws SAXException, IOException, ParserConfigurationException { |
| 268 | + final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 269 | + try (BufferedReader reader = Files.newBufferedReader(xmlPath, charset)) { |
| 270 | + final Document document = builder.parse(new InputSource(reader)); |
| 271 | + return document.getDocumentElement().getTextContent(); |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + private String parseXmlRootValue(final String xmlString) throws SAXException, IOException, ParserConfigurationException { |
| 276 | + final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 277 | + final Document document = builder.parse(new InputSource(new StringReader(xmlString))); |
| 278 | + return document.getDocumentElement().getTextContent(); |
| 279 | + } |
| 280 | + |
255 | 281 | @Test |
256 | 282 | void testGetBlockSize() { |
257 | 283 | assertTrue(FileSystem.getCurrent().getBlockSize() >= 0); |
@@ -487,4 +513,60 @@ void testToLegalFileNameWindows() { |
487 | 513 | assertThrows(IllegalArgumentException.class, () -> fs.toLegalFileName("test", '\0')); |
488 | 514 | assertThrows(IllegalArgumentException.class, () -> fs.toLegalFileName("test", ':')); |
489 | 515 | } |
| 516 | + |
| 517 | + @ParameterizedTest |
| 518 | + @EnumSource(FileSystem.class) |
| 519 | + void testXmlRoundtrip(final FileSystem fs, @TempDir final Path tempDir) throws Exception { |
| 520 | + final Charset charset = Charset.defaultCharset(); |
| 521 | + assertEquals("a", fs.toLegalFileName("a", '_', charset)); |
| 522 | + assertEquals("abcdefghijklmno", fs.toLegalFileName("abcdefghijklmno", '_', charset)); |
| 523 | + assertEquals("\u4F60\u597D\u55CE", fs.toLegalFileName("\u4F60\u597D\u55CE", '_', charset)); |
| 524 | + assertEquals("\u2713\u2714", fs.toLegalFileName("\u2713\u2714", '_', charset)); |
| 525 | + assertEquals("\uD83D\uDE80\u2728\uD83C\uDF89", fs.toLegalFileName("\uD83D\uDE80\u2728\uD83C\uDF89", '_', charset)); |
| 526 | + assertEquals("\uD83D\uDE03", fs.toLegalFileName("\uD83D\uDE03", '_', charset)); |
| 527 | + assertEquals("\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03", |
| 528 | + fs.toLegalFileName("\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03", '_', charset)); |
| 529 | + for (int i = 1; i <= 100; i++) { |
| 530 | + final String name1 = fs.toLegalFileName(StringUtils.repeat("π¦", i), '_', charset); |
| 531 | + assertNotNull(name1); |
| 532 | + final byte[] name1Bytes = name1.getBytes(); |
| 533 | + final String xmlString1 = toXmlString(name1, charset); |
| 534 | + final Path path = tempDir.resolve(name1); |
| 535 | + Files.write(path, xmlString1.getBytes(charset)); |
| 536 | + final String xmlFromPath = parseXmlRootValue(path, charset); |
| 537 | + assertEquals(name1, xmlFromPath, "i = " + i); |
| 538 | + final String name2 = new String(name1Bytes, charset); |
| 539 | + assertEquals(name1, name2); |
| 540 | + final String xmlString2 = toXmlString(name2, charset); |
| 541 | + assertEquals(xmlString1, xmlString2); |
| 542 | + final String parsedValue = Objects.toString(parseXmlRootValue(xmlString2), ""); |
| 543 | + assertEquals(name1, parsedValue, "i = " + i); |
| 544 | + assertEquals(name2, parsedValue, "i = " + i); |
| 545 | + } |
| 546 | + for (int i = 1; i <= 100; i++) { |
| 547 | + final String name1 = fs.toLegalFileName(fs.getNameLengthStrategy().truncate( |
| 548 | + "π©π»βπ¨π»βπ¦π»βπ¦π»π©πΌβπ¨πΌβπ¦πΌβπ¦πΌπ©π½βπ¨π½βπ¦π½βπ¦π½π©πΎβπ¨πΎβπ¦πΎβπ¦πΎπ©πΏβπ¨πΏβπ¦πΏβπ¦πΏπ©π»βπ¨π»βπ¦π»βπ¦π»π©πΌβπ¨πΌβπ¦πΌβπ¦πΌπ©π½βπ¨π½βπ¦π½βπ¦π½π©πΎβπ¨πΎβπ¦πΎβπ¦πΎπ©πΏβπ¨πΏβπ¦πΏβπ¦πΏ", |
| 549 | + // TODO hack 100: truncate blows up when it can't. |
| 550 | + 100 + i, charset), '_', charset); |
| 551 | + assertNotNull(name1); |
| 552 | + final byte[] name1Bytes = name1.getBytes(); |
| 553 | + final String xmlString1 = toXmlString(name1, charset); |
| 554 | + final Path path = tempDir.resolve(name1); |
| 555 | + Files.write(path, xmlString1.getBytes(charset)); |
| 556 | + final String xmlFromPath = parseXmlRootValue(path, charset); |
| 557 | + assertEquals(name1, xmlFromPath, "i = " + i); |
| 558 | + final String name2 = new String(name1Bytes, charset); |
| 559 | + assertEquals(name1, name2); |
| 560 | + final String xmlString2 = toXmlString(name2, charset); |
| 561 | + assertEquals(xmlString1, xmlString2); |
| 562 | + final String parsedValue = Objects.toString(parseXmlRootValue(xmlString2), ""); |
| 563 | + assertEquals(name1, parsedValue, "i = " + i); |
| 564 | + assertEquals(name2, parsedValue, "i = " + i); |
| 565 | + } |
| 566 | + } |
| 567 | + |
| 568 | + private String toXmlString(final String s, final Charset charset) { |
| 569 | + return String.format("<?xml version=\"1.0\" encoding=\"%s\"?><data>%s</data>", charset.name(), s); |
| 570 | + } |
| 571 | + |
490 | 572 | } |
0 commit comments