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.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.awt.Color;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Performs unit tests for palette entries based on single-value targets.
32   */
33  public class PaletteEntryForValueTest {
34  
35      public PaletteEntryForValueTest() {
36      }
37  
38      @Test
39      public void testFaultyConstructors() {
40          assertThrows(IllegalArgumentException.class, () -> new PaletteEntryForValue(0.0f, null), "Constructor failed to detect invalid color");
41      }
42  
43      /**
44       * Test of getARGB method, of class PaletteEntryForValue.
45       */
46      @Test
47      public void testGetARGB() {
48          final Color c0 = new Color(0xff0000ff);
49          PaletteEntryForValue instance = new PaletteEntryForValue(0.0f, c0);
50          int a0 = instance.getArgb(0.0f);
51          int a1 = instance.getArgb(0.5f);
52          final int a2 = instance.getArgb(Float.NaN);
53          assertEquals(0xff0000ff, a0, "Invalid value for target 0.0f");
54          assertEquals(0, a1, "Invalid value for target 0.5f");
55          assertEquals(0, a2, "Invalid value for target NaN");
56          instance = new PaletteEntryForValue(Float.NaN, c0);
57          a0 = instance.getArgb(0.0f);
58          a1 = instance.getArgb(Float.NaN);
59          assertEquals(0, a0, "Invalid value for target 0.0f");
60          assertEquals(0xff0000ff, a1, "Invalid value for target NaN");
61      }
62  
63      /**
64       * Test of getColor method, of class PaletteEntryForValue.
65       */
66      @Test
67      public void testGetColor() {
68          final Color cTest = new Color(0xff0000ff);
69          PaletteEntryForValue instance = new PaletteEntryForValue(0.0f, cTest);
70          Color c0 = instance.getColor(0.0f);
71          final int a0 = c0.getRGB();
72          assertEquals(0xff0000ff, a0, "Invalid value for 0.0f");
73          c0 = instance.getColor(1f);
74          assertNull(c0, "Non-null return for invalid target 1.0f");
75          c0 = instance.getColor(Float.NaN);
76          assertNull(c0, "Non-null return for invalid target 1.0f");
77          instance = new PaletteEntryForValue(Float.NaN, cTest);
78          c0 = instance.getColor(Float.NaN);
79          assertNotNull(c0, "Invalid return for valid target Float.NaN");
80          c0 = instance.getColor(1.0f);
81          assertNull(c0, "Invalid return for invalid valid target 1.0f");
82  
83      }
84  
85      /**
86       * Test of isCovered method, of class PaletteEntryForValue.
87       */
88      @Test
89      public void testIsCovered() {
90          final Color c0 = new Color(0xff0000ff);
91          PaletteEntryForValue instance = new PaletteEntryForValue(0.0f, c0);
92          assertTrue(instance.isCovered(0.0f), "Zero value must be covered");
93          assertFalse(instance.isCovered(1.0f), "Value 1.0 must not be covered");
94          instance = new PaletteEntryForValue(Float.NaN, c0);
95          assertTrue(instance.isCovered(Float.NaN), "NaN value must be covered");
96          assertFalse(instance.isCovered(1.0f), "Value 1.0 must not be covered");
97  
98      }
99  }