BUG-6028: iterate over component types to instantiate union
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / yangtools / binding / data / codec / impl / UnionValueOptionContextTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.binding.data.codec.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertTrue;
14 import java.lang.reflect.Method;
15 import org.junit.Before;
16 import org.junit.Test;
17
18 public class UnionValueOptionContextTest {
19     private static UnionValueOptionContext TEST_UVOC_1;
20     private static UnionValueOptionContext TEST_UVOC_2;
21
22     @Before
23     public void setUp() throws Exception {
24         final Method methodFoo1 = TestDataObject1.class.getMethod("foo");
25         final Method methodFoo2 = TestDataObject2.class.getMethod("foo");
26         TEST_UVOC_1 = new UnionValueOptionContext(TestUnion.class, TestDataObject1.class, methodFoo1,
27             ValueTypeCodec.EMPTY_CODEC);
28         TEST_UVOC_2 = new UnionValueOptionContext(TestUnion.class, TestDataObject2.class, methodFoo2,
29             ValueTypeCodec.EMPTY_CODEC);
30     }
31
32     @Test
33     public void hashCodeTest() throws Exception {
34         final Method methodFoo1 = TestDataObject1.class.getMethod("foo");
35         final UnionValueOptionContext test_uvoc = new UnionValueOptionContext(TestUnion.class, TestDataObject1.class,
36             methodFoo1, ValueTypeCodec.EMPTY_CODEC);
37
38         assertEquals("HashCode", test_uvoc.hashCode(), TEST_UVOC_1.hashCode());
39         assertNotEquals("HashCode", TEST_UVOC_1.hashCode(), TEST_UVOC_2.hashCode());
40     }
41
42     @Test
43     public void equalsTest() throws Exception {
44         final Method methodFoo1 = TestDataObject1.class.getMethod("foo");
45         final UnionValueOptionContext test_uvoc = new UnionValueOptionContext(TestUnion.class, TestDataObject1.class,
46             methodFoo1, ValueTypeCodec.EMPTY_CODEC);
47
48         assertTrue("Equals", TEST_UVOC_1.equals(test_uvoc));
49         assertFalse("Not equals", TEST_UVOC_1.equals(TEST_UVOC_2));
50     }
51
52     protected static final class TestDataObject1 {
53         public void foo() {}
54     }
55
56     protected static final class TestDataObject2 {
57         public void foo() {}
58     }
59
60     public static final class TestUnion {
61         public TestUnion(final TestDataObject1 arg) { }
62         public TestUnion(final TestDataObject2 arg) { }
63     }
64
65 }