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.tiff.photometricinterpreters.floatingpoint;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.awt.Color;
25  
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Performs unit tests for palette entries based on range of values.
30   */
31  public class PaletteEntryForRangeTest {
32  
33      public PaletteEntryForRangeTest() {
34      }
35  
36      /**
37       * Test of coversSingleEntry method, of class PaletteEntryForRange.
38       */
39      @Test
40      public void testCoversSingleEntry() {
41          final Color c0 = new Color(0xff0000ff);
42          final Color c1 = new Color(0xff00ff00);
43          final PaletteEntryForRange instance = new PaletteEntryForRange(0.0f, 1.0f, c0, c1);
44          assertFalse(instance.coversSingleEntry());
45      }
46  
47      @Test
48      public void testFaultyConstructors() {
49          final Color c0 = new Color(0xff0000ff);
50          final Color c1 = new Color(0xff00ff00);
51  
52          // test the two-color variations
53          assertThrows(IllegalArgumentException.class, () -> new PaletteEntryForRange(0.0f, 0.0f, c0, c1), "Constructor failed to detect invalid range");
54  
55          assertThrows(IllegalArgumentException.class, () -> new PaletteEntryForRange(0.0f, 1.0f, null, c1), "Constructor failed to detect null color");
56  
57          assertThrows(IllegalArgumentException.class, () -> new PaletteEntryForRange(0.0f, 1.0f, c0, null), "Constructor failed to detect invalid color");
58  
59          // test the one-color variations
60          assertThrows(IllegalArgumentException.class, () -> new PaletteEntryForRange(0.0f, 0.0f, c0), "Constructor failed to detect invalid range");
61  
62          assertThrows(IllegalArgumentException.class, () -> new PaletteEntryForRange(0.0f, 1.0f, null), "Constructor failed to detect null color");
63      }
64  
65      /**
66       * Test of getARGB method, of class PaletteEntryForRange.
67       */
68      @Test
69      public void testGetARGB() {
70          final Color c0 = new Color(0xff0000ff);
71          final Color c1 = new Color(0xff00ff00);
72          final PaletteEntryForRange instance = new PaletteEntryForRange(0.0f, 1.0f, c0, c1);
73          final int a0 = instance.getArgb(0.0f);
74          final int a1 = instance.getArgb(0.5f);
75          assertEquals(0xff0000ff, a0, "Invalid value for 0.0f");
76          assertEquals(0xff008080, a1, "Invalid interpolated values");
77      }
78  
79      /**
80       * Test of getColor method, of class PaletteEntryForRange.
81       */
82      @Test
83      public void testGetColor() {
84          Color c0 = new Color(0xff0000ff);
85          Color c1 = new Color(0xff00ff00);
86          final PaletteEntryForRange instance = new PaletteEntryForRange(0.0f, 1.0f, c0, c1);
87          c0 = instance.getColor(0.0f);
88          c1 = instance.getColor(0.5f);
89          final int a0 = c0.getRGB();
90          final int a1 = c1.getRGB();
91          assertEquals(0xff0000ff, a0, "Invalid value for 0.0f");
92          assertEquals(0xff008080, a1, "Invalid interpolated values");
93  
94      }
95  
96      /**
97       * Test of getLowerBound method, of class PaletteEntryForRange.
98       */
99      @Test
100     public void testGetLowerBound() {
101         final Color c0 = new Color(0xff0000ff);
102         final Color c1 = new Color(0xff00ff00);
103         final PaletteEntryForRange instance = new PaletteEntryForRange(0.0f, 1.0f, c0, c1);
104         assertEquals(0.0f, instance.getLowerBound());
105     }
106 
107     /**
108      * Test of getUpperBound method, of class PaletteEntryForRange.
109      */
110     @Test
111     public void testGetUpperBound() {
112         final Color c0 = new Color(0xff0000ff);
113         final Color c1 = new Color(0xff00ff00);
114         final PaletteEntryForRange instance = new PaletteEntryForRange(0.0f, 1.0f, c0, c1);
115         assertEquals(1.0f, instance.getUpperBound());
116     }
117 
118     /**
119      * Test of isCovered method, of class PaletteEntryForRange.
120      */
121     @Test
122     public void testIsCovered() {
123         final Color c0 = new Color(0xff0000ff);
124         final Color c1 = new Color(0xff00ff00);
125         final PaletteEntryForRange instance = new PaletteEntryForRange(0.0f, 1.0f, c0, c1);
126         assertTrue(instance.isCovered(0.0f), "Zero value must be covered");
127         assertFalse(instance.isCovered(1.0f), "Value 1.0 must not be covered");
128     }
129 }