View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  }