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;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.Collection;
25  
26  import org.apache.commons.io.FilenameUtils;
27  import org.junit.Test;
28  import org.junit.runner.RunWith;
29  import org.junit.runners.Parameterized;
30  
31  @RunWith(Parameterized.class)
32  public class ImagingGuessFormatTest extends ImagingTest {
33  
34      public static final String BMP_IMAGE_FILE = "bmp\\1\\Oregon Scientific DS6639 - DSC_0307 - small.bmp";
35      public static final String PNG_IMAGE_FILE = "png\\1\\Oregon Scientific DS6639 - DSC_0307 - small.png";
36      public static final String GIF_IMAGE_FILE = "gif\\1\\Oregon Scientific DS6639 - DSC_0307 - small.gif";
37      public static final String ICNS_IMAGE_FILE = "icns\\1\\poedit48x48.icns";
38      public static final String ICO_IMAGE_FILE = "ico\\1\\Oregon Scientific DS6639 - DSC_0307 - small.ico";
39      public static final String TIFF_IMAGE_FILE = "tiff\\1\\Oregon Scientific DS6639 - DSC_0307 - small.tif";
40      public static final String JPEG_IMAGE_FILE = "jpg\\1\\Oregon Scientific DS6639 - DSC_0307 - small.jpg";
41      public static final String PSD_IMAGE_FILE = "psd\\1\\Oregon Scientific DS6639 - DSC_0307 - small.psd";
42      public static final String PBM_IMAGE_FILE = "pbm\\1\\Oregon Scientific DS6639 - DSC_0307 - small.pbm";
43      public static final String PGM_IMAGE_FILE = "pbm\\1\\Oregon Scientific DS6639 - DSC_0307 - small.pgm";
44      public static final String PPM_IMAGE_FILE = "pbm\\1\\Oregon Scientific DS6639 - DSC_0307 - small.ppm";
45      public static final String TGA_IMAGE_FILE = "tga\\1\\Oregon Scientific DS6639 - DSC_0307 - small.tga";
46      public static final String UNKNOWN_IMAGE_FILE = "info.txt";
47  
48      private final ImageFormats expectedFormat;
49      private final String pathToFile;
50  
51      @Parameterized.Parameters
52      public static Collection<Object[]> data() {
53          return Arrays.asList(
54                  new Object[] { ImageFormats.PNG, PNG_IMAGE_FILE },
55                  new Object[] { ImageFormats.GIF, GIF_IMAGE_FILE },
56                  new Object[] { ImageFormats.ICNS, ICNS_IMAGE_FILE },
57                  // TODO(cmchen): add ability to sniff ICOs if possible.
58                  // new Object[] { ImageFormat.IMAGE_FORMAT_ICO, ICO_IMAGE_FILE },
59                  new Object[] { ImageFormats.TIFF, TIFF_IMAGE_FILE },
60                  new Object[] { ImageFormats.JPEG, JPEG_IMAGE_FILE },
61                  new Object[] { ImageFormats.BMP, BMP_IMAGE_FILE },
62                  new Object[] { ImageFormats.PSD, PSD_IMAGE_FILE },
63                  new Object[] { ImageFormats.PBM, PBM_IMAGE_FILE },
64                  new Object[] { ImageFormats.PGM, PGM_IMAGE_FILE },
65                  new Object[] { ImageFormats.PPM, PPM_IMAGE_FILE },
66                  // TODO(cmchen): add ability to sniff TGAs if possible.
67                  // new Object[] { ImageFormat.IMAGE_FORMAT_TGA, TGA_IMAGE_FILE },
68                  // TODO(cmchen): Add test images for these formats.
69                  // new Object[] { ImageFormat.IMAGE_FORMAT_PNM, PNM_IMAGE_FILE },
70                  // new Object[] { ImageFormat.IMAGE_FORMAT_JBIG2, JBIG2_IMAGE_FILE },
71                  new Object[] { ImageFormats.UNKNOWN, UNKNOWN_IMAGE_FILE }
72          );
73      }
74  
75      public ImagingGuessFormatTest(final ImageFormats expectedFormat, final String pathToFile) {
76          this.expectedFormat = expectedFormat;
77          this.pathToFile = pathToFile;
78      }
79  
80      @Test
81      public void testGuessFormat() throws Exception {
82          final String imagePath = FilenameUtils.separatorsToSystem(pathToFile);
83          final File imageFile = new File(TEST_IMAGE_FOLDER, imagePath);
84  
85          final ImageFormat guessedFormat = Imaging.guessFormat(imageFile);
86          assertEquals(expectedFormat, guessedFormat);
87      }
88  
89  }