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.assertEquals;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.assertThrows;
16 import static org.mockito.Mockito.mock;
18 import java.util.Collections;
19 import java.util.List;
21 import org.junit.Test;
23 public class CodeHelpersTest {
25 public void testCheckedFieldCast() {
26 assertNull(CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", null));
27 assertSame(this, CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", this));
29 final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
30 () -> CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", new Object()));
31 assertThat(iae.getCause(), instanceOf(ClassCastException.class));
35 public void testCheckListFieldCast() {
36 assertNull(CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", null));
37 assertSame(List.of(), CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of()));
38 final var list = List.of(this);
39 assertSame(list, CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", list));
41 IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
42 () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", Collections.singletonList(null)));
43 assertThat(iae.getCause(), instanceOf(NullPointerException.class));
45 iae = assertThrows(IllegalArgumentException.class,
46 () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of(new Object())));
47 assertThat(iae.getCause(), instanceOf(ClassCastException.class));
51 public void testCheckSetFieldCast() {
52 assertNull(CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", null));
53 assertSame(Set.of(), CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Set.of()));
54 final var list = Set.of(this);
55 assertSame(list, CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", list));
57 IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
58 () -> CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Collections.singleton(null)));
59 assertThat(iae.getCause(), instanceOf(NullPointerException.class));
61 iae = assertThrows(IllegalArgumentException.class,
62 () -> CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Set.of(new Object())));
63 assertThat(iae.getCause(), instanceOf(ClassCastException.class));
67 public void testCheckEnumName() {
68 final var ex = assertThrows(IllegalArgumentException.class, () -> CodeHelpers.checkEnum(null, "xyzzy"));
69 assertEquals("\"xyzzy\" is not a valid name", ex.getMessage());
71 final var obj = mock(EnumTypeObject.class);
72 assertSame(obj, CodeHelpers.checkEnum(obj, "xyzzy"));
76 public void testCheckEnumValue() {
77 final var ex = assertThrows(IllegalArgumentException.class, () -> CodeHelpers.checkEnum(null, 1234));
78 assertEquals("1234 is not a valid value", ex.getMessage());
80 final var obj = mock(EnumTypeObject.class);
81 assertSame(obj, CodeHelpers.checkEnum(obj, 1234));