bug5849: added equality and corresponding test for MongoDB replica sets

This commit is contained in:
Axel Uhl
2023-06-06 10:38:35 +02:00
parent 86e232801c
commit d5dafe456d
14 changed files with 195 additions and 1 deletions
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
+28
View File
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.sap.sse.landscape.test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
@@ -0,0 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
@@ -0,0 +1,3 @@
eclipse.preferences.version=1
pluginProject.extensions=false
resolve.requirebundle=false
@@ -0,0 +1,14 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test
Bundle-SymbolicName: com.sap.sse.landscape.test
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: SAP
Fragment-Host: com.sap.sse.landscape
Import-Package: org.mockito;version="4.8.1",
org.mockito.hamcrest;version="4.8.1",
org.mockito.mock;version="4.8.1",
org.mockito.stubbing;version="4.8.1"
Require-Bundle: org.junit
Automatic-Module-Name: com.sap.sse.landscape.test
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>root</artifactId>
<groupId>com.sap.sailing</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>com.sap.sse.landscape.test</artifactId>
<packaging>eclipse-test-plugin</packaging>
</project>
@@ -15,7 +15,10 @@ import com.sap.sse.landscape.mongodb.impl.SimpleMongoEndpointImpl;
/**
* A MongoDB endpoint that an application can connect to. It can produce a {@link URI} the client can use to connect to,
* e.g., with a {@code ConnectionString}. The endpoint can be a standalone MongoDB instance, represented by a single
* {@link MongoProcess}, or it may be a {@link MongoReplicaSet replica set}.
* {@link MongoProcess}, or it may be a {@link MongoReplicaSet replica set}.<p>
*
* Two MongoDB endpoints are {@link Object#equals(Object) equal} if they are of the same type (e.g., both a replica set,
* or both a single instance) and have at least one instance in common.<p>
*
* @author Axel Uhl (D043530)
*
@@ -145,4 +148,8 @@ public interface MongoEndpoint {
default Database getDatabase(String name) {
return new DatabaseImpl(this, name);
}
boolean equals(Object o);
int hashCode();
}
@@ -24,6 +24,12 @@ import com.sap.sse.landscape.mongodb.MongoEndpoint;
public abstract class MongoEndpointImpl implements MongoEndpoint {
private static final Logger logger = Logger.getLogger(MongoEndpointImpl.class.getName());
@Override
public abstract boolean equals(Object o);
@Override
public abstract int hashCode();
@Override
public Iterable<MongoDatabase> getMongoDatabases() throws URISyntaxException {
final MongoClient client = getClient();
@@ -13,6 +13,12 @@ import com.sap.sse.landscape.common.shared.MongoDBConstants;
import com.sap.sse.landscape.mongodb.MongoMetrics;
import com.sap.sse.landscape.mongodb.MongoProcess;
/**
* Equality and hash code are based on the host and port.
*
* @author Axel Uhl (d043530)
*
*/
public class MongoProcessImpl extends MongoEndpointImpl implements MongoProcess {
private static final String LOCAL_DB_NAME = "local";
private static final Logger logger = Logger.getLogger(MongoProcessImpl.class.getName());
@@ -61,4 +67,31 @@ public class MongoProcessImpl extends MongoEndpointImpl implements MongoProcess
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((host == null) ? 0 : host.hashCode());
result = prime * result + port;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MongoProcessImpl other = (MongoProcessImpl) obj;
if (host == null) {
if (other.host != null)
return false;
} else if (!host.equals(other.host))
return false;
if (port != other.port)
return false;
return true;
}
}
@@ -37,4 +37,35 @@ public class MongoReplicaSetImpl extends MongoEndpointImpl implements MongoRepli
public void removeReplica(MongoProcessInReplicaSet replicaToRemove) {
instances.remove(replicaToRemove);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((instances == null) ? 0 : instances.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MongoReplicaSetImpl other = (MongoReplicaSetImpl) obj;
if (instances == null) {
if (other.instances != null)
return false;
} else if (!instances.equals(other.instances))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
@@ -32,4 +32,38 @@ public class SimpleMongoEndpointImpl extends MongoEndpointImpl {
public URI getURI(Optional<Database> optionalDb, Optional<Duration> timeoutEmptyMeaningForever) throws URISyntaxException {
return getURI(optionalDb);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((hostname == null) ? 0 : hostname.hashCode());
result = prime * result + port;
result = prime * result + ((replicaSetName == null) ? 0 : replicaSetName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SimpleMongoEndpointImpl other = (SimpleMongoEndpointImpl) obj;
if (hostname == null) {
if (other.hostname != null)
return false;
} else if (!hostname.equals(other.hostname))
return false;
if (port != other.port)
return false;
if (replicaSetName == null) {
if (other.replicaSetName != null)
return false;
} else if (!replicaSetName.equals(other.replicaSetName))
return false;
return true;
}
}
+1
View File
@@ -94,6 +94,7 @@
<module>com.sap.sse.landscape.aws</module>
<module>com.sap.sse.landscape.aws.common</module>
<module>com.sap.sse.landscape.aws.common.test</module>
<module>com.sap.sse.landscape.test</module>
<module>com.sap.sse.landscape.aws.test</module>
<module>com.sap.sse.landscape.aws.persistence</module>
<module>com.sap.sailing.landscape</module>