|
1 | 1 | /* |
2 | | - * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
| 2 | + * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
3 | 3 | */ |
4 | 4 | package com.marklogic.client.test; |
5 | 5 |
|
|
8 | 8 | import com.marklogic.client.document.DocumentManager.Metadata; |
9 | 9 | import com.marklogic.client.io.*; |
10 | 10 | import com.marklogic.client.test.junit5.RequiresML11; |
| 11 | +import jakarta.xml.bind.DatatypeConverter; |
11 | 12 | import org.custommonkey.xmlunit.exceptions.XpathException; |
12 | | -import org.junit.jupiter.api.AfterAll; |
13 | 13 | import org.junit.jupiter.api.BeforeAll; |
14 | 14 | import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 16 | +import org.junit.jupiter.api.condition.OS; |
15 | 17 | import org.junit.jupiter.api.extension.ExtendWith; |
16 | 18 | import org.w3c.dom.Document; |
17 | 19 |
|
18 | | -import jakarta.xml.bind.DatatypeConverter; |
19 | 20 | import java.io.File; |
20 | 21 | import java.io.IOException; |
21 | 22 | import java.util.Random; |
22 | 23 |
|
23 | 24 | import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; |
24 | 25 | import static org.junit.jupiter.api.Assertions.*; |
25 | 26 |
|
26 | | -public class BinaryDocumentTest { |
27 | | - @BeforeAll |
28 | | - public static void beforeClass() { |
29 | | - Common.connect(); |
30 | | - } |
31 | | - @AfterAll |
32 | | - public static void afterClass() { |
33 | | - } |
34 | | - |
35 | | - // a simple base64-encoded binary |
36 | | - final static public String ENCODED_BINARY = |
37 | | - "iVBORw0KGgoAAAANSUhEUgAAAA0AAAATCAYAAABLN4eXAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oIEQEjMtAYogQAAAKvSURBVCjPlZLLbhxFAEVPVVdXVz/G8zCOn0CsKGyQkSIIKzas8xfsWbLkp/gJhCKheIlAJDaj2MYez6u7p7vrxQKUPVc6+yOdK77/4cfXQohJqlOVZdmBSpKY6jQKBM45oVMlgHvrvMuNWRljvlNKq69G2YyqLDg4mLE/2yPNYFRWlFXF/nTC2clRWbc7Fss1IcZzqTA8eWY5eu7p1Hv+WvyBVjnGZOQmI9UKISUqSXDO0bS7Tko0xfGSp18kjM7v+P3+NUMr8T5grWMYLCEErHM474khoCw1t78eU/8mEOpjXpxekJUORIZSCbkxSCnRWpPnBikTqbx31E1DjJHpeIzRhnW9xceI857H5Yr1Zku765jf3DIMtlUAIQRCiFhnabsOH1IEAmstAGWRY11ApykmM0oplTKZjNGZREpJoUueHI0ZFRV7exX7+1Nm0yn9YLm5u2fX96lUseLwxQ0vX8H04i2/XP9Et5H44OkHS920hBDo+56u77GDjcrHjvV1ya3TDO2M01mOUAEAhED+R5IkpKmCiFCOjoc/p+xuLbPpCc+P95HaEqIBIhHoB8t2W/PwsKBudl5FH7GxwUYYouJh5ci7nLbtWW02LBaPvLuef1AdrItKKolJpkivwGrG5QxTCsq8pCxLqqrk7PiIwTmW6y0xRCVTSg4vFnz+raM4+5ur1RtSUZHnOUWeMx5VVFWJTlOstfTWRuk96NIyOUgRRc188RZvgRg/3OffjoFESohxUMvmjqufP+X+MqDTU77+5EvMKKBUQpZpijxHSkluDHvjMW8uL79Rnz07bwSyzDLFqCzwDNw/PNI0O9bbhvVmQ7vb0bQdi+Wq327rl+rko8krodKnCHnofJju+r5oupBstg1KJT7Vuruev185O9zVm/WVUmouYoz83/0DxhRmafe2kasAAAAASUVORK5CYII="; |
38 | | - final static public byte[] BYTES_BINARY = DatatypeConverter.parseBase64Binary(ENCODED_BINARY); |
39 | | - |
40 | | - @Test |
41 | | - // Requires MarkLogic 11 or higher now that we're using Docker; the INSTALL_CONVERTERS flag does not work for MarkLogic 10. |
42 | | - @ExtendWith(RequiresML11.class) |
43 | | - public void testReadWrite() throws IOException, XpathException { |
44 | | - String docId = "/test/binary-sample.png"; |
45 | | - String mimetype = "image/png"; |
46 | | - |
47 | | - for (int i: new int[]{0, 1}) { |
48 | | - BinaryDocumentManager docMgr = Common.client.newBinaryDocumentManager(); |
49 | | - docMgr.setMetadataExtraction(MetadataExtraction.PROPERTIES); |
50 | | - |
51 | | - BytesHandle handle = new BytesHandle().with(BYTES_BINARY).withMimetype(mimetype); |
52 | | - switch (i) { |
53 | | - case 0: |
54 | | - docMgr.write(docId, handle); |
55 | | - break; |
56 | | - case 1: |
57 | | - docMgr.write(docMgr.newWriteSet().add(docId, handle)); |
58 | | - break; |
59 | | - default: |
60 | | - fail("unknown case: "+i); |
61 | | - } |
62 | | - |
63 | | - DocumentDescriptor desc = docMgr.exists(docId); |
64 | | - assertTrue(desc.getByteLength() != DocumentDescriptor.UNKNOWN_LENGTH); |
65 | | - assertEquals(BYTES_BINARY.length, desc.getByteLength()); |
66 | | - |
67 | | - byte[] buf = docMgr.read(docId, new BytesHandle()).get(); |
68 | | - assertEquals(BYTES_BINARY.length, buf.length); |
69 | | - |
70 | | - buf = Common.streamToBytes(docMgr.read(docId, new InputStreamHandle()).get()); |
71 | | - assertTrue(buf.length > 0); |
72 | | - |
73 | | - switch (i) { |
74 | | - case 0: |
75 | | - handle = new BytesHandle(); |
76 | | - buf = docMgr.read(docId, handle, 9, 10).get(); |
77 | | - assertEquals(10, buf.length); |
78 | | - assertEquals(10, handle.getByteLength()); |
79 | | - break; |
80 | | - case 1: |
81 | | - break; |
82 | | - default: |
83 | | - fail("unknown case: "+i); |
84 | | - } |
85 | | - |
86 | | - docMgr.setMetadataCategories(Metadata.PROPERTIES); |
87 | | - Document metadataDocument = docMgr.readMetadata(docId, new DOMHandle()).get(); |
88 | | - assertXpathEvaluatesTo("image/png","string(/*[local-name()='metadata']/*[local-name()='properties']/*[local-name()='content-type'])", metadataDocument); |
89 | | - assertXpathEvaluatesTo("text HD-HTML","string(/*[local-name()='metadata']/*[local-name()='properties']/*[local-name()='filter-capabilities'])", metadataDocument); |
90 | | - assertXpathEvaluatesTo("815","string(/*[local-name()='metadata']/*[local-name()='properties']/*[local-name()='size'])", metadataDocument); |
91 | | - |
92 | | - docMgr.delete(docId); |
93 | | - } |
94 | | - } |
95 | | - |
96 | | - @Test |
97 | | - public void test_issue_758() { |
98 | | - BinaryDocumentManager docMgr = Common.client.newBinaryDocumentManager(); |
99 | | - DocumentWriteSet writeset = docMgr.newWriteSet(); |
100 | | - FileHandle h1 = new FileHandle(new File( |
101 | | - "../marklogic-client-api-functionaltests/src/test/java/com/marklogic" + |
102 | | - "/client" + |
103 | | - "/functionaltest/data" + |
104 | | - "/Sega-4MB.jpg")); |
105 | | - String uri = "BinaryDocumentTest_" + new Random().nextInt(10000) + "/" + "Sega-4MB.jpg"; |
106 | | - writeset.add(uri, h1); |
107 | | - docMgr.write(writeset); |
108 | | - DocumentPage page = docMgr.read(uri); |
109 | | - DocumentRecord rec = page.next(); |
110 | | - assertNotNull(rec); |
111 | | - assertEquals(rec.getFormat(),Format.BINARY); |
112 | | - } |
| 27 | +class BinaryDocumentTest { |
| 28 | + |
| 29 | + @BeforeAll |
| 30 | + public static void beforeClass() { |
| 31 | + Common.connect(); |
| 32 | + } |
| 33 | + |
| 34 | + // a simple base64-encoded binary |
| 35 | + final static public String ENCODED_BINARY = |
| 36 | + "iVBORw0KGgoAAAANSUhEUgAAAA0AAAATCAYAAABLN4eXAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9oIEQEjMtAYogQAAAKvSURBVCjPlZLLbhxFAEVPVVdXVz/G8zCOn0CsKGyQkSIIKzas8xfsWbLkp/gJhCKheIlAJDaj2MYez6u7p7vrxQKUPVc6+yOdK77/4cfXQohJqlOVZdmBSpKY6jQKBM45oVMlgHvrvMuNWRljvlNKq69G2YyqLDg4mLE/2yPNYFRWlFXF/nTC2clRWbc7Fss1IcZzqTA8eWY5eu7p1Hv+WvyBVjnGZOQmI9UKISUqSXDO0bS7Tko0xfGSp18kjM7v+P3+NUMr8T5grWMYLCEErHM474khoCw1t78eU/8mEOpjXpxekJUORIZSCbkxSCnRWpPnBikTqbx31E1DjJHpeIzRhnW9xceI857H5Yr1Zku765jf3DIMtlUAIQRCiFhnabsOH1IEAmstAGWRY11ApykmM0oplTKZjNGZREpJoUueHI0ZFRV7exX7+1Nm0yn9YLm5u2fX96lUseLwxQ0vX8H04i2/XP9Et5H44OkHS920hBDo+56u77GDjcrHjvV1ya3TDO2M01mOUAEAhED+R5IkpKmCiFCOjoc/p+xuLbPpCc+P95HaEqIBIhHoB8t2W/PwsKBudl5FH7GxwUYYouJh5ci7nLbtWW02LBaPvLuef1AdrItKKolJpkivwGrG5QxTCsq8pCxLqqrk7PiIwTmW6y0xRCVTSg4vFnz+raM4+5ur1RtSUZHnOUWeMx5VVFWJTlOstfTWRuk96NIyOUgRRc188RZvgRg/3OffjoFESohxUMvmjqufP+X+MqDTU77+5EvMKKBUQpZpijxHSkluDHvjMW8uL79Rnz07bwSyzDLFqCzwDNw/PNI0O9bbhvVmQ7vb0bQdi+Wq327rl+rko8krodKnCHnofJju+r5oupBstg1KJT7Vuruev185O9zVm/WVUmouYoz83/0DxhRmafe2kasAAAAASUVORK5CYII="; |
| 37 | + final static public byte[] BYTES_BINARY = DatatypeConverter.parseBase64Binary(ENCODED_BINARY); |
| 38 | + |
| 39 | + @Test |
| 40 | + // Requires MarkLogic 11 or higher now that we're using Docker; the INSTALL_CONVERTERS flag does not work for MarkLogic 10. |
| 41 | + @ExtendWith(RequiresML11.class) |
| 42 | + @DisabledOnOs(value = OS.LINUX, architectures = "aarch64", disabledReason = "MarkLogic converters not available when " + |
| 43 | + "ARM regression tests are run in Jenkins.") |
| 44 | + void testReadWrite() throws IOException, XpathException { |
| 45 | + String docId = "/test/binary-sample.png"; |
| 46 | + String mimetype = "image/png"; |
| 47 | + |
| 48 | + for (int i : new int[]{0, 1}) { |
| 49 | + BinaryDocumentManager docMgr = Common.client.newBinaryDocumentManager(); |
| 50 | + docMgr.setMetadataExtraction(MetadataExtraction.PROPERTIES); |
| 51 | + |
| 52 | + BytesHandle handle = new BytesHandle().with(BYTES_BINARY).withMimetype(mimetype); |
| 53 | + switch (i) { |
| 54 | + case 0: |
| 55 | + docMgr.write(docId, handle); |
| 56 | + break; |
| 57 | + case 1: |
| 58 | + docMgr.write(docMgr.newWriteSet().add(docId, handle)); |
| 59 | + break; |
| 60 | + default: |
| 61 | + fail("unknown case: " + i); |
| 62 | + } |
| 63 | + |
| 64 | + DocumentDescriptor desc = docMgr.exists(docId); |
| 65 | + assertTrue(desc.getByteLength() != DocumentDescriptor.UNKNOWN_LENGTH); |
| 66 | + assertEquals(BYTES_BINARY.length, desc.getByteLength()); |
| 67 | + |
| 68 | + byte[] buf = docMgr.read(docId, new BytesHandle()).get(); |
| 69 | + assertEquals(BYTES_BINARY.length, buf.length); |
| 70 | + |
| 71 | + buf = Common.streamToBytes(docMgr.read(docId, new InputStreamHandle()).get()); |
| 72 | + assertTrue(buf.length > 0); |
| 73 | + |
| 74 | + switch (i) { |
| 75 | + case 0: |
| 76 | + handle = new BytesHandle(); |
| 77 | + buf = docMgr.read(docId, handle, 9, 10).get(); |
| 78 | + assertEquals(10, buf.length); |
| 79 | + assertEquals(10, handle.getByteLength()); |
| 80 | + break; |
| 81 | + case 1: |
| 82 | + break; |
| 83 | + default: |
| 84 | + fail("unknown case: " + i); |
| 85 | + } |
| 86 | + |
| 87 | + docMgr.setMetadataCategories(Metadata.PROPERTIES); |
| 88 | + Document metadataDocument = docMgr.readMetadata(docId, new DOMHandle()).get(); |
| 89 | + assertXpathEvaluatesTo("image/png", "string(/*[local-name()='metadata']/*[local-name()='properties']/*[local-name()='content-type'])", metadataDocument); |
| 90 | + assertXpathEvaluatesTo("text HD-HTML", "string(/*[local-name()='metadata']/*[local-name()='properties']/*[local-name()='filter-capabilities'])", metadataDocument); |
| 91 | + assertXpathEvaluatesTo("815", "string(/*[local-name()='metadata']/*[local-name()='properties']/*[local-name()='size'])", metadataDocument); |
| 92 | + |
| 93 | + docMgr.delete(docId); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void test_issue_758() { |
| 99 | + BinaryDocumentManager docMgr = Common.client.newBinaryDocumentManager(); |
| 100 | + DocumentWriteSet writeset = docMgr.newWriteSet(); |
| 101 | + FileHandle h1 = new FileHandle(new File( |
| 102 | + "../marklogic-client-api-functionaltests/src/test/java/com/marklogic" + |
| 103 | + "/client" + |
| 104 | + "/functionaltest/data" + |
| 105 | + "/Sega-4MB.jpg")); |
| 106 | + String uri = "BinaryDocumentTest_" + new Random().nextInt(10000) + "/" + "Sega-4MB.jpg"; |
| 107 | + writeset.add(uri, h1); |
| 108 | + docMgr.write(writeset); |
| 109 | + DocumentPage page = docMgr.read(uri); |
| 110 | + DocumentRecord rec = page.next(); |
| 111 | + assertNotNull(rec); |
| 112 | + assertEquals(rec.getFormat(), Format.BINARY); |
| 113 | + } |
113 | 114 | } |
0 commit comments