Open Kilda Java Documentation
Inet4NetworkTest.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.testing.service.traffexam.networkpool;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20 
21 import org.junit.Test;
22 
23 import java.net.Inet4Address;
24 import java.net.InetAddress;
25 import java.net.UnknownHostException;
26 
27 public class Inet4NetworkTest {
28 
29  @Test
30  public void subnet() throws UnknownHostException, Inet4ValueException {
31  Inet4Address address = (Inet4Address) Inet4Address.getByName(
32  "172.16.255.0");
33  Inet4Network subject = new Inet4Network(address, 29);
34 
35  assertEquals(InetAddress.getByName("172.16.255.0"), subject.subnet(0, 30).getNetwork());
36  assertEquals(InetAddress.getByName("172.16.255.4"), subject.subnet(1, 30).getNetwork());
37 
38  boolean raises = false;
39  try {
40  subject.subnet(2, 30);
41  } catch (Inet4ValueException e) {
42  raises = true;
43  }
44 
45  assertTrue(
46  String.format(
47  "Here must be %s exception", Inet4ValueException.class),
48  raises);
49  }
50 
51  @Test
52  public void address() throws UnknownHostException, Inet4ValueException {
53  Inet4Address address = (Inet4Address) Inet4Address.getByName(
54  "172.16.255.0");
55  Inet4Network subject = new Inet4Network(address, 30);
56 
57  assertEquals(InetAddress.getByName("172.16.255.0"), subject.address(0));
58  assertEquals(InetAddress.getByName("172.16.255.1"), subject.address(1));
59  assertEquals(InetAddress.getByName("172.16.255.2"), subject.address(2));
60  assertEquals(InetAddress.getByName("172.16.255.3"), subject.address(3));
61 
62  boolean raises = false;
63  try {
64  subject.address(4);
65  } catch (Inet4ValueException e) {
66  raises = true;
67  }
68  assertTrue(
69  String.format(
70  "Here must be %s exception", Inet4ValueException.class),
71  raises);
72  }
73 }