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.png.chunks;
18  
19  import org.apache.commons.imaging.ImageReadException;
20  import org.junit.Test;
21  
22  import java.io.IOException;
23  
24  import static org.junit.Assert.assertEquals;
25  
26  public class PngChunkScalTest {
27     private static final double delta = 0.001;
28     private static final int chunkType = 1933787468;
29  
30     @Test
31     public void testConstructMeters() throws IOException, ImageReadException {
32        final PngChunkScal pngChunkScal = new PngChunkScal(10, chunkType, 0,
33              new byte[]{1, 48, 46, 48, 49, 0, 48, 46, 48, 50});
34  
35        assertEquals("Invalid unit specifier", pngChunkScal.unitSpecifier, 1);
36        assertEquals("Invalid units per pixel x axis", pngChunkScal.unitsPerPixelXAxis, 0.01, delta);
37        assertEquals("Invalid units per pixel y axis", pngChunkScal.unitsPerPixelYAxis, 0.02, delta);
38     }
39  
40     @Test
41     public void testConstructRadians() throws IOException, ImageReadException {
42        final PngChunkScal pngChunkScal = new PngChunkScal(10, chunkType, 0,
43              new byte[]{2, 48, 46, 48, 49, 0, 48, 46, 48, 50});
44  
45        assertEquals("Invalid unit specifier", pngChunkScal.unitSpecifier, 2);
46        assertEquals("Invalid units per pixel x axis", pngChunkScal.unitsPerPixelXAxis, 0.01, delta);
47        assertEquals("Invalid units per pixel y axis", pngChunkScal.unitsPerPixelYAxis, 0.02, delta);
48     }
49  
50     @Test(expected = ImageReadException.class)
51     public void testConstruct_InvalidUnitSpecifier() throws IOException, ImageReadException {
52        new PngChunkScal(10, chunkType, 0, new byte[]{3, 48, 46, 48, 49, 0, 48, 46, 48, 50});
53     }
54  
55     @Test(expected = ImageReadException.class)
56     public void testConstruct_MissingSeparator() throws IOException, ImageReadException {
57        new PngChunkScal(9, chunkType, 0, new byte[]{1, 48, 46, 48, 49, 48, 46, 48, 50});
58     }
59  
60     @Test(expected = ImageReadException.class)
61     public void testConstruct_InvalidDblValue() throws IOException, ImageReadException {
62        new PngChunkScal(10, chunkType, 0, new byte[]{2, 65, 46, 48, 49, 0, 48, 46, 48, 50});
63     }
64  
65     @Test(expected = ImageReadException.class)
66     public void testConstruct_MissingXValue() throws IOException, ImageReadException {
67        new PngChunkScal(2, chunkType, 0, new byte[]{2, 0});
68     }
69  
70     @Test(expected = ImageReadException.class)
71     public void testConstruct_MissingYValue() throws IOException, ImageReadException {
72        new PngChunkScal(6, chunkType, 0, new byte[]{2, 48, 46, 48, 49, 0});
73     }
74  }