1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.imaging.formats.jpeg.xmp;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.File;
26 import java.util.stream.Stream;
27
28 import org.apache.commons.imaging.bytesource.ByteSource;
29 import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
30 import org.apache.commons.imaging.formats.jpeg.JpegImagingParameters;
31 import org.junit.jupiter.params.ParameterizedTest;
32 import org.junit.jupiter.params.provider.MethodSource;
33
34 public class JpegXmpRewriteTest extends AbstractJpegXmpTest {
35
36 public static Stream<File> data() throws Exception {
37 return getImagesWithXmpData().stream();
38 }
39
40 @ParameterizedTest
41 @MethodSource("data")
42 public void testRemoveInsertUpdate(final File imageFile) throws Exception {
43 final ByteSource byteSource = ByteSource.file(imageFile);
44 final JpegImagingParameters params = new JpegImagingParameters();
45 final String xmpXml = new JpegImageParser().getXmpXml(byteSource, params);
46 assertNotNull(xmpXml);
47
48 byte[] noXmpFile;
49 {
50
51
52 try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
53 new JpegXmpRewriter().removeXmpXml(byteSource, os);
54 noXmpFile = os.toByteArray();
55 }
56
57
58
59
60 final String outXmp = new JpegImageParser().getXmpXml(ByteSource.array(noXmpFile, "test.jpg"), params);
61 assertNull(outXmp);
62 }
63
64 {
65
66
67 final String newXmpXml = "test";
68 byte[] updated;
69 try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
70 new JpegXmpRewriter().updateXmpXml(byteSource, os, newXmpXml);
71 updated = os.toByteArray();
72 }
73
74
75
76
77 final String outXmp = new JpegImageParser().getXmpXml(ByteSource.array(updated, "test.jpg"), params);
78 assertNotNull(outXmp);
79 assertEquals(outXmp, newXmpXml);
80 }
81
82 {
83
84
85 final String newXmpXml = "test";
86 final byte[] updated;
87 try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
88 new JpegXmpRewriter().updateXmpXml(ByteSource.array(noXmpFile, "test.jpg"), os, newXmpXml);
89 updated = os.toByteArray();
90 }
91
92
93
94
95 final String outXmp = new JpegImageParser().getXmpXml(ByteSource.array(updated, "test.jpg"), params);
96 assertNotNull(outXmp);
97 assertEquals(outXmp, newXmpXml);
98 }
99 }
100
101 }