Specialize relative leafref types during instantiation
[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 org.junit.Test;
19
20 public class CodeHelpersTest {
21     @Test
22     public void testCheckedFieldCast() {
23         assertNull(CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", null));
24         assertSame(this, CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", this));
25
26         final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
27             () -> CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", new Object()));
28         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
29     }
30
31     @Test
32     public void testCheckListFieldCast() {
33         assertNull(CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", null));
34         assertSame(List.of(), CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of()));
35         final var list = List.of(this);
36         assertSame(list, CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", list));
37
38         IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
39             () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", Collections.singletonList(null)));
40         assertThat(iae.getCause(), instanceOf(NullPointerException.class));
41
42         iae = assertThrows(IllegalArgumentException.class,
43             () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of(new Object())));
44         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
45     }
46 }