5730dc75e9884d39247001f24e0d345bb67b197c
[controller.git] / opendaylight / commons / liblldp / src / test / java / org / opendaylight / controller / sal / packet / address / EthernetAddressTest.java
1 /*
2  * Copyright (c) 2014, 2015 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
9 /**
10  * @file   EthernetAddressTest.java
11  *
12  * @brief  Unit Tests for EthernetAddress class
13  *
14  * Unit Tests for EthernetAddress class
15  */
16 package org.opendaylight.controller.sal.packet.address;
17
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.controller.liblldp.ConstructionException;
21 import org.opendaylight.controller.liblldp.EthernetAddress;
22
23 public class EthernetAddressTest {
24     @Test
25     public void testNonValidConstructor() {
26         @SuppressWarnings("unused")
27         EthernetAddress ea1;
28         // Null input array
29         try {
30             ea1 = new EthernetAddress((byte[]) null);
31
32             // Exception is expected if NOT raised test will fail
33             Assert.assertTrue(false);
34         } catch (final ConstructionException e) {
35         }
36
37         // Array too short
38         try {
39             ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0 });
40
41             // Exception is expected if NOT raised test will fail
42             Assert.assertTrue(false);
43         } catch (final ConstructionException e) {
44         }
45
46         // Array too long
47         try {
48             ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
49                     (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
50                     (byte) 0x0 });
51
52             // Exception is expected if NOT raised test will fail
53             Assert.assertTrue(false);
54         } catch (final ConstructionException e) {
55         }
56     }
57
58     @Test
59     public void testEquality() {
60         EthernetAddress ea1;
61         EthernetAddress ea2;
62         try {
63             ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
64                     (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
65
66             ea2 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
67                     (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
68             Assert.assertTrue(ea1.equals(ea2));
69         } catch (final ConstructionException e) {
70             // Exception is NOT expected if raised test will fail
71             Assert.assertTrue(false);
72         }
73
74         try {
75             ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
76                     (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
77
78             ea2 = ea1.clone();
79             Assert.assertTrue(ea1.equals(ea2));
80         } catch (final ConstructionException e) {
81             // Exception is NOT expected if raised test will fail
82             Assert.assertTrue(false);
83         }
84
85         // Check for well knowns
86         try {
87             ea1 = EthernetAddress.BROADCASTMAC;
88             ea2 = new EthernetAddress(new byte[] { (byte) 0xff, (byte) 0xff,
89                     (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff });
90             Assert.assertTrue(ea1.equals(ea2));
91         } catch (final ConstructionException e) {
92             // Exception is NOT expected if raised test will fail
93             Assert.assertTrue(false);
94         }
95     }
96
97     @Test
98     public void testUnEquality() {
99         EthernetAddress ea1;
100         EthernetAddress ea2;
101         try {
102             ea1 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
103                     (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x2 });
104
105             ea2 = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
106                     (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1 });
107             Assert.assertTrue(!ea1.equals(ea2));
108         } catch (final ConstructionException e) {
109             // Exception is NOT expected if raised test will fail
110             Assert.assertTrue(false);
111         }
112     }
113 }