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.iptc;
19  
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.BufferedOutputStream;
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.OutputStream;
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import org.apache.commons.imaging.ImagingConstants;
34  import org.apache.commons.imaging.common.bytesource.ByteSource;
35  import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
36  import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
37  import org.apache.commons.imaging.formats.jpeg.JpegPhotoshopMetadata;
38  import org.junit.Test;
39  import org.junit.runner.RunWith;
40  import org.junit.runners.Parameterized;
41  
42  @RunWith(Parameterized.class)
43  public class IptcAddTest extends IptcBaseTest {
44  
45      private final File imageFile;
46  
47      @Parameterized.Parameters
48      public static Collection<File> data() throws Exception {
49          return getJpegImages();
50      }
51  
52      public IptcAddTest(final File imageFile) {
53          this.imageFile = imageFile;
54      }
55  
56      /*
57           * Add a few IPTC values to JPEG images, whether or not they have existing
58           * IPTC data.
59           */
60      @Test
61      public void testAddIptcData() throws Exception {
62          final ByteSource byteSource = new ByteSourceFile(imageFile);
63  
64          final Map<String, Object> params = new HashMap<>();
65          final boolean ignoreImageData = isPhilHarveyTestImage(imageFile);
66          params.put(ImagingConstants.PARAM_KEY_READ_THUMBNAILS, Boolean.valueOf(!ignoreImageData));
67  
68          final JpegPhotoshopMetadata metadata = new JpegImageParser().getPhotoshopMetadata(byteSource, params);
69          if (metadata == null) {
70              // FIXME select only files with meta for this test
71              return;
72          }
73  
74          final List<IptcBlock> newBlocks = new ArrayList<>();
75          newBlocks.addAll(metadata.photoshopApp13Data.getNonIptcBlocks());
76          final List<IptcRecord> oldRecords = metadata.photoshopApp13Data.getRecords();
77  
78          final List<IptcRecord> newRecords = new ArrayList<>();
79          for (final IptcRecord record : oldRecords) {
80              if (record.iptcType != IptcTypes.CITY
81                      && record.iptcType != IptcTypes.CREDIT) {
82                  newRecords.add(record);
83              }
84          }
85  
86          newRecords.add(new IptcRecord(IptcTypes.CITY, "Albany, NY"));
87          newRecords.add(new IptcRecord(IptcTypes.CREDIT, "William Sorensen"));
88  
89          final PhotoshopApp13Data newData = new PhotoshopApp13Data(newRecords, newBlocks);
90  
91          final File updated = createTempFile(imageFile.getName() + ".iptc.add.", ".jpg");
92          try (FileOutputStream fos = new FileOutputStream(updated);
93                  OutputStream os = new BufferedOutputStream(fos)) {
94              new JpegIptcRewriter().writeIPTC(byteSource, os, newData);
95          }
96  
97          final ByteSource updateByteSource = new ByteSourceFile(updated);
98          final JpegPhotoshopMetadata outMetadata = new JpegImageParser().getPhotoshopMetadata(
99                  updateByteSource, params);
100 
101         assertNotNull(outMetadata);
102         assertTrue(outMetadata.getItems().size() == newRecords.size());
103     }
104 }