From cda04809e1296c3bbffc7c3a668a37043faa09fb Mon Sep 17 00:00:00 2001 From: stefan Date: Sat, 12 Dec 2020 19:56:56 +0100 Subject: [PATCH 1/9] Adding #hashCode to Parameters (#653) --- .../schmizz/sshj/connection/channel/direct/Parameters.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java b/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java index d6da239c..b7cbab42 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java @@ -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,9 @@ public class Parameters { return localPort; } + @Override + public int hashCode() { + return Objects.hash(localHost, localPort, remoteHost, remotePort); + } + } From bc5a11916997afea683ab2859340fcf711db5c44 Mon Sep 17 00:00:00 2001 From: stefan Date: Sat, 12 Dec 2020 19:57:51 +0100 Subject: [PATCH 2/9] Testing #hashCode of Parameters (#653) --- .../channel/direct/ParametersTest.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java diff --git a/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java new file mode 100644 index 00000000..abb17b39 --- /dev/null +++ b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java @@ -0,0 +1,67 @@ +package net.schmizz.sshj.connection.channel.direct; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; + +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 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 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)); + } + +} From 8337cce382382ccbd14ebce8e09d0b5c193c3d6e Mon Sep 17 00:00:00 2001 From: stefan Date: Sat, 12 Dec 2020 19:58:05 +0100 Subject: [PATCH 3/9] Adding #equals to Parameters (#653) --- .../sshj/connection/channel/direct/Parameters.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java b/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java index b7cbab42..0865bcc1 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java @@ -52,4 +52,13 @@ public class Parameters { 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; + } + } From ee7a65531f7bfe7ea255276a78704d0a0eb412ad Mon Sep 17 00:00:00 2001 From: stefan Date: Sat, 12 Dec 2020 19:58:19 +0100 Subject: [PATCH 4/9] Testing #equals of Parameters (#653) --- .../channel/direct/ParametersTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java index abb17b39..93b9c636 100644 --- a/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java +++ b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java @@ -20,6 +20,35 @@ public class ParametersTest { 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); From 9e9797c326dbbb7350e00edde7f1db5970a3668e Mon Sep 17 00:00:00 2001 From: stefan Date: Sat, 12 Dec 2020 19:58:31 +0100 Subject: [PATCH 5/9] Adding #toString to Parameters (#653) --- .../schmizz/sshj/connection/channel/direct/Parameters.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java b/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java index 0865bcc1..f6c178f6 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/direct/Parameters.java @@ -61,4 +61,10 @@ public class Parameters { Objects.equals(remoteHost, other.remoteHost) && remotePort == other.remotePort; } + @Override + public String toString() { + return "Parameters [localHost=" + localHost + ", localPort=" + localPort + ", "+ + "remoteHost=" + remoteHost + ", remotePort=" + remotePort + "]"; + } + } From 9266b6c04a4e2f8f562281e32d86d2886caa0eab Mon Sep 17 00:00:00 2001 From: stefan Date: Sat, 12 Dec 2020 20:00:43 +0100 Subject: [PATCH 6/9] Testing #toString of Parameters (#653) --- .../connection/channel/direct/ParametersTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java index 93b9c636..da047835 100644 --- a/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java +++ b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java @@ -3,6 +3,7 @@ package net.schmizz.sshj.connection.channel.direct; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.HashMap; @@ -93,4 +94,15 @@ public class ParametersTest { 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")); + } + } From ee68e0a8e655a5cd6d2881b254e1ca6ce0ae4310 Mon Sep 17 00:00:00 2001 From: stefan Date: Sat, 12 Dec 2020 20:03:12 +0100 Subject: [PATCH 7/9] Adopt project's import order (#653) - copied by import order of 'SocketStreamCopyMonitorTest' --- .../connection/channel/direct/ParametersTest.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java index da047835..a3520167 100644 --- a/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java +++ b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java @@ -1,14 +1,11 @@ package net.schmizz.sshj.connection.channel.direct; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import org.junit.Test; + import java.util.HashMap; import java.util.Map; -import org.junit.Test; + +import static org.junit.Assert.*; public class ParametersTest { From be18cc6e6abf3df70090c897b4d77aa280a4f33d Mon Sep 17 00:00:00 2001 From: stefan Date: Sat, 12 Dec 2020 20:03:42 +0100 Subject: [PATCH 8/9] Add license prelude (#653) --- .../connection/channel/direct/ParametersTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java index a3520167..aa2dc448 100644 --- a/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java +++ b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java @@ -1,3 +1,18 @@ +/* + * Copyright (C)2020 - 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; From 7f8328f23fa6c01fcc29819e9e3b820475439488 Mon Sep 17 00:00:00 2001 From: stefan Date: Sat, 12 Dec 2020 20:10:32 +0100 Subject: [PATCH 9/9] Backdate license to prevent build's "License violations" (#653) --- .../schmizz/sshj/connection/channel/direct/ParametersTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java index aa2dc448..f4d34535 100644 --- a/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java +++ b/src/test/java/net/schmizz/sshj/connection/channel/direct/ParametersTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C)2020 - SSHJ Contributors + * 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.