Committed by
Gerrit Code Review
Unit tests for PortAddresses class
Change-Id: I04334b4cce65e169258de608ccc2461421abaacf
Showing
2 changed files
with
114 additions
and
1 deletions
... | @@ -29,7 +29,7 @@ import com.google.common.base.MoreObjects; | ... | @@ -29,7 +29,7 @@ import com.google.common.base.MoreObjects; |
29 | /** | 29 | /** |
30 | * Represents address information bound to a port. | 30 | * Represents address information bound to a port. |
31 | */ | 31 | */ |
32 | -public class PortAddresses { | 32 | +public final class PortAddresses { |
33 | 33 | ||
34 | private final ConnectPoint connectPoint; | 34 | private final ConnectPoint connectPoint; |
35 | private final Set<InterfaceIpAddress> ipAddresses; | 35 | private final Set<InterfaceIpAddress> ipAddresses; | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.net.host; | ||
17 | + | ||
18 | +import java.util.Set; | ||
19 | + | ||
20 | +import org.junit.Before; | ||
21 | +import org.junit.Test; | ||
22 | +import org.onlab.packet.IpAddress; | ||
23 | +import org.onlab.packet.IpPrefix; | ||
24 | +import org.onlab.packet.MacAddress; | ||
25 | +import org.onlab.packet.VlanId; | ||
26 | +import org.onosproject.net.ConnectPoint; | ||
27 | +import org.onosproject.net.NetTestTools; | ||
28 | + | ||
29 | +import static org.hamcrest.Matchers.is; | ||
30 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; | ||
31 | + | ||
32 | +import com.google.common.collect.ImmutableSet; | ||
33 | +import com.google.common.testing.EqualsTester; | ||
34 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
35 | + | ||
36 | +/** | ||
37 | + * Unit tests for port addresses class. | ||
38 | + */ | ||
39 | +public class PortAddressesTest { | ||
40 | + | ||
41 | + PortAddresses addresses1; | ||
42 | + PortAddresses sameAsAddresses1; | ||
43 | + PortAddresses addresses2; | ||
44 | + PortAddresses addresses3; | ||
45 | + | ||
46 | + private static final ConnectPoint CONNECT_POINT1 = | ||
47 | + NetTestTools.connectPoint("cp1", 1); | ||
48 | + private static final IpAddress IP_ADDRESS1 = IpAddress.valueOf("1.2.3.4"); | ||
49 | + private static final IpPrefix SUBNET_ADDRESS1 = | ||
50 | + IpPrefix.valueOf("1.2.0.0/16"); | ||
51 | + private static final InterfaceIpAddress INTERFACE_ADDRESS_1 = | ||
52 | + new InterfaceIpAddress(IP_ADDRESS1, SUBNET_ADDRESS1); | ||
53 | + | ||
54 | + private static final ConnectPoint CONNECT_POINT2 = | ||
55 | + NetTestTools.connectPoint("cp2", 1); | ||
56 | + private static final IpAddress IP_ADDRESS2 = IpAddress.valueOf("1.2.3.5"); | ||
57 | + private static final IpPrefix SUBNET_ADDRESS2 = | ||
58 | + IpPrefix.valueOf("1.3.0.0/16"); | ||
59 | + private static final InterfaceIpAddress INTERFACE_ADDRESS_2 = | ||
60 | + new InterfaceIpAddress(IP_ADDRESS2, SUBNET_ADDRESS2); | ||
61 | + | ||
62 | + Set<InterfaceIpAddress> ipAddresses; | ||
63 | + | ||
64 | + | ||
65 | + /** | ||
66 | + * Initializes local data used by all test cases. | ||
67 | + */ | ||
68 | + @Before | ||
69 | + public void setUpAddresses() { | ||
70 | + ipAddresses = ImmutableSet.of(INTERFACE_ADDRESS_1, | ||
71 | + INTERFACE_ADDRESS_2); | ||
72 | + addresses1 = new PortAddresses(CONNECT_POINT1, ipAddresses, | ||
73 | + MacAddress.BROADCAST, VlanId.NONE); | ||
74 | + sameAsAddresses1 = new PortAddresses(CONNECT_POINT1, ipAddresses, | ||
75 | + MacAddress.BROADCAST, VlanId.NONE); | ||
76 | + addresses2 = new PortAddresses(CONNECT_POINT2, ipAddresses, | ||
77 | + MacAddress.BROADCAST, VlanId.NONE); | ||
78 | + addresses3 = new PortAddresses(CONNECT_POINT2, ipAddresses, | ||
79 | + MacAddress.ZERO, VlanId.NONE); | ||
80 | + } | ||
81 | + | ||
82 | + /** | ||
83 | + * Checks that the PortAddresses class is immutable. | ||
84 | + */ | ||
85 | + @Test | ||
86 | + public void testImmutability() { | ||
87 | + assertThatClassIsImmutable(PortAddresses.class); | ||
88 | + } | ||
89 | + | ||
90 | + /** | ||
91 | + * Checks the operation of the equals(), hash() and toString() | ||
92 | + * methods. | ||
93 | + */ | ||
94 | + @Test | ||
95 | + public void testEquals() { | ||
96 | + new EqualsTester() | ||
97 | + .addEqualityGroup(addresses1, sameAsAddresses1) | ||
98 | + .addEqualityGroup(addresses2) | ||
99 | + .addEqualityGroup(addresses3) | ||
100 | + .testEquals(); | ||
101 | + } | ||
102 | + | ||
103 | + /** | ||
104 | + * Tests that object are created correctly. | ||
105 | + */ | ||
106 | + @Test | ||
107 | + public void testConstruction() { | ||
108 | + assertThat(addresses1.mac(), is(MacAddress.BROADCAST)); | ||
109 | + assertThat(addresses1.connectPoint(), is(CONNECT_POINT1)); | ||
110 | + assertThat(addresses1.ipAddresses(), is(ipAddresses)); | ||
111 | + assertThat(addresses1.vlan(), is(VlanId.NONE)); | ||
112 | + } | ||
113 | +} |
-
Please register or login to post a comment