Introduce Node#at static factory and NodeTests

This commit is contained in:
Chris Beams 2014-11-06 10:47:39 +01:00
parent 1f136de1a1
commit 655100e69f
No known key found for this signature in database
GPG key ID: 3D214F8F5BC5ED73
4 changed files with 145 additions and 17 deletions

View file

@ -18,35 +18,27 @@
package io.bitsquare.network; package io.bitsquare.network;
public enum BootstrapNode implements Node { public enum BootstrapNode implements Node {
LOCALHOST("localhost", "127.0.0.1"), LOCALHOST(Node.at("localhost", "127.0.0.1")),
DIGITAL_OCEAN1("digitalocean1.bitsquare.io", "188.226.179.109"); DIGITAL_OCEAN1(Node.at("digitalocean1.bitsquare.io", "188.226.179.109"));
private final String id; private final Node self;
private final String ip;
private final int port;
BootstrapNode(String id, String ip) { BootstrapNode(Node self) {
this(id, ip, DEFAULT_PORT); this.self = self;
}
BootstrapNode(String id, String ip, int port) {
this.id = id;
this.ip = ip;
this.port = port;
} }
@Override @Override
public String getId() { public String getId() {
return id; return self.getId();
} }
@Override @Override
public String getIp() { public String getIp() {
return ip; return self.getIp();
} }
@Override @Override
public int getPort() { public int getPort() {
return port; return self.getPort();
} }
} }

View file

@ -27,5 +27,11 @@ public interface Node {
int getPort(); 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);
}
}

View 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();
}
}

View 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}"));
}
}