1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.xmp;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21
22 import java.awt.image.BufferedImage;
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.nio.file.Files;
26 import java.util.List;
27
28 import org.apache.commons.imaging.AbstractImageParser;
29 import org.apache.commons.imaging.AbstractImagingTest;
30 import org.apache.commons.imaging.ImageFormat;
31 import org.apache.commons.imaging.ImageFormats;
32 import org.apache.commons.imaging.Imaging;
33 import org.apache.commons.imaging.common.XmpImagingParameters;
34 import org.apache.commons.imaging.internal.Debug;
35 import org.apache.commons.imaging.internal.ImageParserFactory;
36 import org.junit.jupiter.api.Test;
37
38 public class XmpUpdateTest<E extends XmpImagingParameters<E>> extends AbstractImagingTest {
39
40 @Test
41 public void test() throws Exception {
42 final List<File> images = getTestImages();
43 for (final File imageFile : images) {
44
45 if (imageFile.getName().toLowerCase().endsWith(".png") && isInvalidPngTestFile(imageFile)) {
46 continue;
47 }
48
49 Debug.debug("imageFile", imageFile);
50 Debug.debug();
51
52 final ImageFormat imageFormat = Imaging.guessFormat(imageFile);
53 if (!imageFormat.equals(ImageFormats.PNG) || !imageFormat.equals(ImageFormats.TIFF) || !imageFormat.equals(ImageFormats.GIF)) {
54 continue;
55 }
56
57 String xmpXml = Imaging.getXmpXml(imageFile);
58 if (null == xmpXml && imageFormat.equals(ImageFormats.GIF)) {
59 xmpXml = "temporary test until I can locate a GIF with XMP in the wild.";
60 }
61 if (null == xmpXml) {
62 continue;
63 }
64
65 final File tempFile = Files.createTempFile(imageFile.getName() + ".", "." + imageFormat.getDefaultExtension()).toFile();
66 final BufferedImage image = Imaging.getBufferedImage(imageFile);
67
68 final AbstractImageParser<E> parser = ImageParserFactory.getImageParser("." + imageFormat.getDefaultExtension());
69 final E params = parser.getDefaultParameters();
70 params.setXmpXml(xmpXml);
71 try (FileOutputStream fos = new FileOutputStream(tempFile)) {
72 parser.writeImage(image, fos, params);
73 }
74
75 final String xmpXmlOut = Imaging.getXmpXml(tempFile);
76
77 assertNotNull(xmpXmlOut);
78 assertEquals(xmpXmlOut, xmpXml);
79 }
80 }
81 }