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;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import java.util.stream.Stream;
24  
25  import org.junit.jupiter.api.Test;
26  import org.junit.jupiter.params.ParameterizedTest;
27  import org.junit.jupiter.params.provider.MethodSource;
28  
29  /**
30   * Tests for {@link ImagingException}.
31   */
32  public class TestImageWriteException {
33  
34      public static Stream<Object[]> data() {
35          final ImagingException exception = new ImagingException((String) null);
36          // @formatter:off
37          return Stream.of(
38                  new Object[] {null, "null"},
39                  new Object[] {new Object[] {Integer.valueOf(1)}, "[Object[]: 1]"},
40                  new Object[] {new char[] {'a', 'b', 'c'}, "[char[]: 3]"},
41                  new Object[] {new byte[] {0, 1}, "[byte[]: 2]"},
42                  new Object[] {new short[] {0}, "[short[]: 1]"},
43                  new Object[] {new int[] {-1, -2, 4, 100}, "[int[]: 4]"},
44                  new Object[] {new long[] {-1, -2, 4, 100}, "[long[]: 4]"},
45                  new Object[] {new float[] {-1.0f, 2.0f}, "[float[]: 2]"},
46                  new Object[] {new double[] {-1.0d, 2.0d}, "[double[]: 2]"},
47                  new Object[] {new boolean[] {true, false, true}, "[boolean[]: 3]"},
48                  new Object[] {exception, exception.getClass().getName()}
49                  );
50          // @formatter:on
51      }
52  
53      @ParameterizedTest
54      @MethodSource("data")
55      public void testCreateExceptionWithData(final Object data, final String expectedType) {
56          final ImagingException exception = new ImagingException("imaging", data);
57          assertEquals(String.format("imaging: %s (%s)", data, expectedType), exception.getMessage());
58      }
59  
60      @Test
61      public void testCreateExceptionWithMessage() {
62          final ImagingException exception = new ImagingException("imaging");
63          assertEquals("imaging", exception.getMessage());
64          assertNull(exception.getCause());
65      }
66  
67      @Test
68      public void testCreateExceptionWithMessageAndCause() {
69          final ImagingException exception = new ImagingException("imaging", new Exception("cause"));
70          assertEquals("imaging", exception.getMessage());
71          assertNotNull(exception.getCause());
72      }
73  }