Merge branch 'informaticum-master'

This commit is contained in:
Jeroen van Erp
2021-01-13 14:30:45 +01:00
2 changed files with 142 additions and 0 deletions

View File

@@ -15,6 +15,8 @@
*/
package net.schmizz.sshj.connection.channel.direct;
import java.util.Objects;
public class Parameters {
private final String localHost;
@@ -45,4 +47,24 @@ public class Parameters {
return localPort;
}
@Override
public int hashCode() {
return Objects.hash(localHost, localPort, remoteHost, remotePort);
}
@Override
public boolean equals(Object obj) {
if (this == obj) { return true; }
if (!(obj instanceof Parameters)) { return false; }
Parameters other = (Parameters) obj;
return Objects.equals(localHost, other.localHost) && localPort == other.localPort &&
Objects.equals(remoteHost, other.remoteHost) && remotePort == other.remotePort;
}
@Override
public String toString() {
return "Parameters [localHost=" + localHost + ", localPort=" + localPort + ", "+
"remoteHost=" + remoteHost + ", remotePort=" + remotePort + "]";
}
}

View File

@@ -0,0 +1,120 @@
/*
* Copyright (C)2009 - SSHJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.connection.channel.direct;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
public class ParametersTest {
@Test
public void testConstructedFields() {
final Parameters p = new Parameters("127.0.0.1", 8080, "github.com", 80);
assertEquals("127.0.0.1", p.getLocalHost());
assertEquals(8080, p.getLocalPort());
assertEquals("github.com", p.getRemoteHost());
assertEquals(80, p.getRemotePort());
}
@Test
public void testFieldsEquality() {
final Parameters first = new Parameters("127.0.0.1", 8080, "github.com", 80);
final Parameters second = new Parameters("127.0.0.1", 8080, "github.com", 80);
assertEquals(first.getLocalHost(), second.getLocalHost());
assertEquals(first.getLocalPort(), second.getLocalPort());
assertEquals(first.getRemoteHost(), second.getRemoteHost());
assertEquals(first.getRemotePort(), second.getRemotePort());
}
@Test
public void testInstanceIdentity() {
final Parameters first = new Parameters("127.0.0.1", 8080, "github.com", 80);
final Parameters second = new Parameters("127.0.0.1", 8080, "github.com", 80);
assertTrue(first == first);
assertTrue(second == second);
assertFalse(first == second);
}
@Test
public void testInstanceEquality() {
final Parameters first = new Parameters("127.0.0.1", 8080, "github.com", 80);
final Parameters second = new Parameters("127.0.0.1", 8080, "github.com", 80);
assertFalse(first == second);
assertTrue(first.equals(second));
assertTrue(second.equals(first));
assertEquals(first, second);
}
@Test
public void testHashCode() {
final Parameters first = new Parameters("127.0.0.1", 8080, "github.com", 80);
final Parameters second = new Parameters("127.0.0.1", 8080, "github.com", 80);
assertEquals(first.hashCode(), first.hashCode());
assertEquals(first.hashCode(), second.hashCode());
assertEquals(second.hashCode(), second.hashCode());
final Parameters third = new Parameters("127.0.0.1", 443, "github.com", 80);
assertEquals(third.hashCode(), third.hashCode());
assertNotEquals(first.hashCode(), third.hashCode());
}
@Test
public void testHashMapApplicability() {
final Parameters first = new Parameters("127.0.0.1", 8080, "github.com", 80);
final Map<Parameters, String> map = new HashMap<>();
assertFalse(map.containsKey(first));
final String none = map.put(first, "is now in the map");
assertNull(none);
assertTrue(map.containsKey(first));
assertEquals("is now in the map", map.get(first));
final Parameters second = new Parameters("127.0.0.1", 8080, "github.com", 80);
assertTrue(map.containsKey(second));
assertEquals("is now in the map", map.get(second));
final String current = map.putIfAbsent(second, "is again in the map");
assertEquals("is now in the map", current);
assertEquals("is now in the map", map.get(first));
assertEquals("is now in the map", map.get(second));
final String previous = map.put(second, "is again in the map");
assertEquals("is now in the map", previous);
assertEquals("is again in the map", map.get(first));
assertEquals("is again in the map", map.get(second));
final Parameters third = new Parameters("127.0.0.1", 443, "github.com", 80);
assertFalse(map.containsKey(third));
assertNull(map.get(third));
}
@Test
public void testToString() {
final Parameters first = new Parameters("127.0.0.1", 8080, "github.com", 443);
assertNotNull(first.toString());
assertFalse(first.toString().isBlank());
assertTrue(first.toString().contains("127.0.0.1"));
assertTrue(first.toString().contains("8080"));
assertTrue(first.toString().contains("github.com"));
assertTrue(first.toString().contains("443"));
}
}