Improve CodeHelpers coverage
[mdsal.git] / binding / yang-binding / src / test / java / org / opendaylight / yangtools / yang / binding / CodeHelpersTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.binding;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertThrows;
15
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Set;
19 import org.junit.Test;
20
21 public class CodeHelpersTest {
22     @Test
23     public void testCheckedFieldCast() {
24         assertNull(CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", null));
25         assertSame(this, CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", this));
26
27         final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
28             () -> CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", new Object()));
29         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
30     }
31
32     @Test
33     public void testCheckListFieldCast() {
34         assertNull(CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", null));
35         assertSame(List.of(), CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of()));
36         final var list = List.of(this);
37         assertSame(list, CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", list));
38
39         IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
40             () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", Collections.singletonList(null)));
41         assertThat(iae.getCause(), instanceOf(NullPointerException.class));
42
43         iae = assertThrows(IllegalArgumentException.class,
44             () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of(new Object())));
45         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
46     }
47
48     @Test
49     public void testCheckSetFieldCast() {
50         assertNull(CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", null));
51         assertSame(Set.of(), CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Set.of()));
52         final var list = Set.of(this);
53         assertSame(list, CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", list));
54
55         IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
56             () -> CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Collections.singleton(null)));
57         assertThat(iae.getCause(), instanceOf(NullPointerException.class));
58
59         iae = assertThrows(IllegalArgumentException.class,
60             () -> CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Set.of(new Object())));
61         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
62     }
63 }