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.pnm;
18  
19  import java.io.IOException;
20  import java.nio.charset.Charset;
21  import java.nio.charset.StandardCharsets;
22  import java.util.Collections;
23  import java.util.Map;
24  
25  import org.apache.commons.imaging.ImageInfo;
26  import org.apache.commons.imaging.ImageReadException;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.assertEquals;
30  
31  public class PnmImageParserTest {
32  
33      private static final Charset US_ASCII = StandardCharsets.US_ASCII;
34  
35      @Test
36      public void testGetImageInfo_happyCase() throws ImageReadException, IOException {
37          final byte[] bytes = "P1\n3 2\n0 1 0\n1 0 1\n".getBytes(US_ASCII);
38          final Map<String, Object> params = Collections.emptyMap();
39          final PnmImageParser underTest = new PnmImageParser();
40          final ImageInfo results = underTest.getImageInfo(bytes, params);
41          assertEquals(results.getBitsPerPixel(), 1);
42          assertEquals(results.getWidth(), 3);
43          assertEquals(results.getHeight(), 2);
44          assertEquals(results.getNumberOfImages(), 1);
45      }
46  
47      /**
48       * If an invalid width is specified, should throw {@link ImageReadException} rather than
49       * {@link NumberFormatException}.
50       */
51      @Test(expected = ImageReadException.class)
52      public void testGetImageInfo_invalidWidth() throws ImageReadException, IOException {
53          final byte[] bytes = "P1\na 2\n0 0 0 0 0 0 0 0 0 0 0\n1 1 1 1 1 1 1 1 1 1 1\n".getBytes(US_ASCII);
54          final Map<String, Object> params = Collections.emptyMap();
55          final PnmImageParser underTest = new PnmImageParser();
56          underTest.getImageInfo(bytes, params);
57      }
58  
59      @Test(expected = ImageReadException.class)
60      public void testGetImageInfo_invalidHeight() throws ImageReadException, IOException {
61          final byte[] bytes = "P1\n2 a\n0 0\n0 0\n0 0\n0 0\n0 0\n0 1\n1 1\n1 1\n1 1\n1 1\n1 1\n".getBytes(US_ASCII);
62          final Map<String, Object> params = Collections.emptyMap();
63          final PnmImageParser underTest = new PnmImageParser();
64          underTest.getImageInfo(bytes, params);
65      }
66  
67      @Test(expected = ImageReadException.class)
68      public void testGetImageInfo_missingWidthValue() throws ImageReadException, IOException {
69          final byte[] bytes = "P7\nWIDTH \n".getBytes(US_ASCII);
70          final Map<String, Object> params = Collections.emptyMap();
71          final PnmImageParser underTest = new PnmImageParser();
72          underTest.getImageInfo(bytes, params);
73      }
74  }