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  
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              // test remove
51  
52              try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
53                  new JpegXmpRewriter().removeXmpXml(byteSource, os);
54                  noXmpFile = os.toByteArray();
55              }
56  
57              // Debug.debug("Source Segments:");
58              // new JpegUtils().dumpJFIF(ByteSource.file(noXmpFile));
59  
60              final String outXmp = new JpegImageParser().getXmpXml(ByteSource.array(noXmpFile, "test.jpg"), params);
61              assertNull(outXmp);
62          }
63  
64          {
65              // test update
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              // Debug.debug("Source Segments:");
75              // new JpegUtils().dumpJFIF(ByteSource.file(updated));
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              // test insert
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              // Debug.debug("Source Segments:");
93              // new JpegUtils().dumpJFIF(ByteSource.file(updated));
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 }