2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.binding;
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;
16 import java.util.Collections;
17 import java.util.List;
18 import org.junit.Test;
20 public class CodeHelpersTest {
22 public void testCheckedFieldCast() {
23 assertNull(CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", null));
24 assertSame(this, CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", this));
26 final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
27 () -> CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", new Object()));
28 assertThat(iae.getCause(), instanceOf(ClassCastException.class));
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));
38 IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
39 () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", Collections.singletonList(null)));
40 assertThat(iae.getCause(), instanceOf(NullPointerException.class));
42 iae = assertThrows(IllegalArgumentException.class,
43 () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of(new Object())));
44 assertThat(iae.getCause(), instanceOf(ClassCastException.class));