View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.beanutils2.converters;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.awt.Color;
25  
26  import org.apache.commons.beanutils2.ConversionException;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link ColorConverter}.
32   *
33   * @since 2.0.0
34   */
35  public class ColorConverterTest {
36  
37      private ColorConverter converter;
38  
39      @BeforeEach
40      public void before() {
41          converter = new ColorConverter();
42      }
43  
44      @Test
45      public void testColorBlank() {
46          assertThrows(ConversionException.class, () -> converter.convert(Color.class, "#"));
47      }
48  
49      @Test
50      public void testColorInvalidLength() {
51          assertThrows(ConversionException.class, () -> converter.convert(Color.class, "#F"));
52      }
53  
54      @Test
55      public void testConvertingColorName() {
56          final Color expected = Color.WHITE;
57          final Color actual = converter.convert(Color.class, "white");
58  
59          assertEquals(expected, actual);
60      }
61  
62      @Test
63      public void testConvertingColorNameCaps() {
64          final Color expected = Color.LIGHT_GRAY;
65          final Color actual = converter.convert(Color.class, "LIGHTGRAY");
66  
67          assertEquals(expected, actual);
68      }
69  
70      @Test
71      public void testConvertingJavaColorStringFull() {
72          final Color expected = Color.WHITE;
73          final Color actual = converter.convert(Color.class, "java.awt.Color[r=255,g=255,b=255]");
74  
75          assertEquals(expected, actual);
76      }
77  
78      @Test
79      public void testConvertingJavaColorStringWithoutBrackets() {
80          final Color expected = Color.DARK_GRAY;
81          final Color actual = converter.convert(Color.class, "r=64,g=64,b=64");
82  
83          assertEquals(expected, actual);
84      }
85  
86      @Test
87      public void testConvertingJavaColorStringWithoutColorPrefixes() {
88          final Color expected = Color.PINK;
89          final Color actual = converter.convert(Color.class, "255,175,175");
90  
91          assertEquals(expected, actual);
92      }
93  
94      @Test
95      public void testConvertingJavaColorStringWithoutPackage() {
96          final Color expected = Color.GREEN;
97          final Color actual = converter.convert(Color.class, "[r=0,g=255,b=0]");
98  
99          assertEquals(expected, actual);
100     }
101 
102     /**
103      * Color can be extended without the {@link Override overriding} the {@link Color#toString()} method. This tests that it can continue to parse the
104      * {@link String} from an inherited class.
105      */
106     @Test
107     public void testConvertingJavaExtendsColorString() {
108         final Color expected = Color.MAGENTA;
109         final Color actual = converter.convert(Color.class, "org.apache.ExtendedColor[r=255,g=0,b=255]");
110 
111         assertEquals(expected, actual);
112     }
113 
114     @Test
115     public void testConvertingLiteralHex() {
116         final Color expected = Color.BLUE;
117         final Color actual = converter.convert(Color.class, "0x0000FF");
118 
119         assertEquals(expected, actual);
120     }
121 
122     @Test
123     public void testConvertingPattern() {
124         final Color expected = Color.BLACK;
125         final Color actual = converter.convert(Color.class, "#000000");
126 
127         assertEquals(expected, actual);
128     }
129 
130     @Test
131     public void testConvertingPattern3Digit() {
132         final Color expected = Color.WHITE;
133         final Color actual = converter.convert(Color.class, "#FFF");
134 
135         assertEquals(expected, actual);
136     }
137 
138     @Test
139     public void testConvertingPattern4Digit() {
140         final Color expected = Color.YELLOW;
141         final Color actual = converter.convert(Color.class, "#FF0F");
142 
143         assertEquals(expected, actual);
144     }
145 
146     @Test
147     public void testConvertingPatternWithAlpha() {
148         final Color expected = Color.LIGHT_GRAY;
149         final Color actual = converter.convert(Color.class, "#C0C0C0FF");
150 
151         assertEquals(expected, actual);
152     }
153 
154     @Test
155     public void testInvalidNumber3() {
156         assertThrows(ConversionException.class, () -> converter.convert(Color.class, "#FFZ"));
157     }
158 
159     @Test
160     public void testInvalidNumber4() {
161         assertThrows(ConversionException.class, () -> converter.convert(Color.class, "#FFFY"));
162     }
163 }