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.Dimension;
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 DimensionConverter}.
32   *
33   * @since 2.0.0
34   */
35  public class DimensionConverterTest {
36  
37      private DimensionConverter converter;
38  
39      @BeforeEach
40      public void before() {
41          converter = new DimensionConverter();
42      }
43  
44      @Test
45      public void testConvertingDimension() {
46          final Dimension expected = new Dimension(1920, 1080);
47          final Dimension actual = converter.convert(Dimension.class, "1920x1080");
48  
49          assertEquals(expected, actual);
50      }
51  
52      @Test
53      public void testConvertingSquare() {
54          final Dimension expected = new Dimension(512, 512);
55          final Dimension actual = converter.convert(Dimension.class, "512");
56  
57          assertEquals(expected, actual);
58      }
59  
60      @Test
61      public void testInvalidDimensions() {
62          assertThrows(ConversionException.class, () -> converter.convert(Dimension.class, "512n512"));
63      }
64  
65      @Test
66      public void testInvalidNumberFormatException() {
67          assertThrows(ConversionException.class, () -> converter.convert(Dimension.class, "3000000000x100"));
68      }
69  
70      @Test
71      public void testNegativeDimension() {
72          assertThrows(ConversionException.class, () -> converter.convert(Dimension.class, "-512x512"));
73      }
74  }