mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-25 07:27:18 +01:00
Introduce Node#at static factory and NodeTests
This commit is contained in:
parent
1f136de1a1
commit
655100e69f
4 changed files with 145 additions and 17 deletions
|
@ -18,35 +18,27 @@
|
|||
package io.bitsquare.network;
|
||||
|
||||
public enum BootstrapNode implements Node {
|
||||
LOCALHOST("localhost", "127.0.0.1"),
|
||||
DIGITAL_OCEAN1("digitalocean1.bitsquare.io", "188.226.179.109");
|
||||
LOCALHOST(Node.at("localhost", "127.0.0.1")),
|
||||
DIGITAL_OCEAN1(Node.at("digitalocean1.bitsquare.io", "188.226.179.109"));
|
||||
|
||||
private final String id;
|
||||
private final String ip;
|
||||
private final int port;
|
||||
private final Node self;
|
||||
|
||||
BootstrapNode(String id, String ip) {
|
||||
this(id, ip, DEFAULT_PORT);
|
||||
}
|
||||
|
||||
BootstrapNode(String id, String ip, int port) {
|
||||
this.id = id;
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
BootstrapNode(Node self) {
|
||||
this.self = self;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return id;
|
||||
return self.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIp() {
|
||||
return ip;
|
||||
return self.getIp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return port;
|
||||
return self.getPort();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,5 +27,11 @@ public interface Node {
|
|||
|
||||
int getPort();
|
||||
|
||||
}
|
||||
static Node at(String id, String ip) {
|
||||
return Node.at(id, ip, DEFAULT_PORT);
|
||||
}
|
||||
|
||||
static Node at(String id, String ip, int port) {
|
||||
return new NodeImpl(id, ip, port);
|
||||
}
|
||||
}
|
||||
|
|
76
src/main/java/io/bitsquare/network/NodeImpl.java
Normal file
76
src/main/java/io/bitsquare/network/NodeImpl.java
Normal file
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* This file is part of Bitsquare.
|
||||
*
|
||||
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.bitsquare.network;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
final class NodeImpl implements Node {
|
||||
|
||||
private final String id;
|
||||
private final String ip;
|
||||
private final int port;
|
||||
|
||||
NodeImpl(String id, String ip, int port) {
|
||||
this.id = id;
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (this == object)
|
||||
return true;
|
||||
|
||||
if (object == null || getClass() != object.getClass())
|
||||
return false;
|
||||
|
||||
NodeImpl that = (NodeImpl) object;
|
||||
return Objects.equal(this.id, that.id) &&
|
||||
Objects.equal(this.ip, that.ip) &&
|
||||
Objects.equal(this.port, that.port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(id, ip, port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Objects.toStringHelper(Node.class.getSimpleName())
|
||||
.add("id", id)
|
||||
.add("ip", ip)
|
||||
.add("port", port)
|
||||
.toString();
|
||||
}
|
||||
}
|
54
src/test/java/io/bitsquare/network/NodeTests.java
Normal file
54
src/test/java/io/bitsquare/network/NodeTests.java
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* This file is part of Bitsquare.
|
||||
*
|
||||
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.bitsquare.network;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class NodeTests {
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashCode() {
|
||||
Node node1a = Node.at("bitsquare1.example.com", "203.0.113.1");
|
||||
Node node1b = Node.at("bitsquare1.example.com", "203.0.113.1");
|
||||
|
||||
assertThat(node1a, equalTo(node1a));
|
||||
|
||||
assertThat(node1a, equalTo(node1b));
|
||||
assertThat(node1b, equalTo(node1a));
|
||||
|
||||
assertThat(node1a, not((Object) equalTo(null)));
|
||||
assertThat(node1a, not((Object) equalTo("not a node")));
|
||||
|
||||
assertThat(node1a, not(equalTo(Node.at("bitsquare2.example.com", node1a.getIp()))));
|
||||
assertThat(node1a, not(equalTo(Node.at(node1a.getId(), "203.0.113.2"))));
|
||||
assertThat(node1a, not(equalTo(Node.at(node1a.getId(), node1a.getIp(), Node.DEFAULT_PORT + 1))));
|
||||
|
||||
Node node2 = Node.at("bitsquare2.example.com", "203.0.113.2");
|
||||
assertThat(node1a.hashCode(), equalTo(node1b.hashCode()));
|
||||
assertThat(node1a.hashCode(), not(equalTo(node2.hashCode())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
Node node = Node.at("bitsquare1.example.com", "203.0.113.1", 5001);
|
||||
assertThat(node.toString(), equalTo("Node{id=bitsquare1.example.com, ip=203.0.113.1, port=5001}"));
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue