2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.homematic.internal.handler;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.CoreMatchers.not;
17 import static org.hamcrest.MatcherAssert.assertThat;
19 import java.util.ArrayList;
20 import java.util.List;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
26 * Tests for {@link org.openhab.binding.homematic.internal.handler.SimplePortPool}.
28 * @author Florian Stolte - Initial Contribution
30 public class SimplePortPoolTest {
32 private SimplePortPool simplePortPool;
36 this.simplePortPool = new SimplePortPool();
40 public void testPoolOnlyGivesOutPortsNotInUse() {
41 final List<Integer> ports = acquireSomePorts();
43 final int newPort = simplePortPool.getNextPort();
45 for (int port : ports) {
46 assertThat(port, not(is(newPort)));
51 public void testPoolReusesReleasedPort() {
52 final List<Integer> ports = acquireSomePorts();
53 final int firstPort = ports.get(0);
55 simplePortPool.release(firstPort);
56 int newPortAfterRelease = simplePortPool.getNextPort();
58 assertThat(newPortAfterRelease, is(firstPort));
62 public void testPoolSkipsPortsThatAreSetInUse() {
63 final int firstPort = simplePortPool.getNextPort();
65 simplePortPool.setInUse(firstPort + 1);
66 int newPortAfterSetInUse = simplePortPool.getNextPort();
68 assertThat(newPortAfterSetInUse, is(firstPort + 2));
71 private List<Integer> acquireSomePorts() {
72 final int numberOfPorts = 5;
73 final List<Integer> ports = new ArrayList<>(numberOfPorts);
75 for (int i = 0; i < numberOfPorts; i++) {
76 ports.add(simplePortPool.getNextPort());