Update tests to also be compatible with Mongo 3.2.x

This commit is contained in:
Andy Wilkinson
2016-06-28 09:24:43 +01:00
parent 9b8ebbc6c1
commit faebcf2924
4 changed files with 29 additions and 15 deletions

View File

@@ -3,12 +3,12 @@ package org.springframework.cloud.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.test.util.ReflectionTestUtils;
import com.mongodb.MongoClient;
import com.mongodb.WriteConcern;
/**
*
* @author Ramnivas Laddad
@@ -16,7 +16,7 @@ import org.springframework.test.util.ReflectionTestUtils;
*/
public class MongoDbFactoryCloudConfigTestHelper {
public static void assertConfigProperties(MongoDbFactory connector, Integer writeConcernW, Integer connectionsPerHost, Integer maxWaitTime) {
public static void assertConfigProperties(MongoDbFactory connector, String writeConcern, Integer connectionsPerHost, Integer maxWaitTime) {
if (connectionsPerHost == null) {
connectionsPerHost = 100; // default
}
@@ -25,11 +25,7 @@ public class MongoDbFactoryCloudConfigTestHelper {
}
assertNotNull(connector);
WriteConcern writeConcern = (WriteConcern) ReflectionTestUtils.getField(connector, "writeConcern");
if (writeConcernW != null) {
assertNotNull(writeConcern);
assertEquals(writeConcernW.intValue(), writeConcern.getW());
}
assertEquals(ReflectionTestUtils.getField(connector, "writeConcern"), writeConcern == null ? null : WriteConcern.valueOf(writeConcern));
MongoClient mongoClient = (MongoClient) ReflectionTestUtils.getField(connector, "mongo");
assertEquals(connectionsPerHost.intValue(), mongoClient.getMongoClientOptions().getConnectionsPerHost());

View File

@@ -32,7 +32,7 @@ public class MongoDbFactoryJavaConfigTest extends AbstractServiceJavaConfigTest<
getTestApplicationContext(MongoDbFactoryConfigWithServiceConfig.class, createService("my-service"));
MongoDbFactory connector = testContext.getBean("connectionPerHost50_MaxWait200_WriteConcernNone", getConnectorType());
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, -1, 50, 200);
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, "none", 50, 200);
}
@Test
@@ -41,7 +41,7 @@ public class MongoDbFactoryJavaConfigTest extends AbstractServiceJavaConfigTest<
getTestApplicationContext(MongoDbFactoryConfigWithServiceConfig.class, createService("my-service"));
MongoDbFactory connector = testContext.getBean("connectionPerHost50_MaxWait200_WriteConcernSafe", getConnectorType());
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, 1, 50, 200);
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, "safe", 50, 200);
}
@Test

View File

@@ -35,7 +35,7 @@ public class MongoDbFactoryXmlConfigTest extends AbstractServiceXmlConfigTest<Mo
ApplicationContext testContext = getTestApplicationContext("cloud-mongo-with-config.xml", createService("my-service"));
MongoDbFactory connector = testContext.getBean("service-connectionPerHost50-maxWait200-WriteConcernNone", getConnectorType());
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, -1, 50, 200);
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, "none", 50, 200);
}
@Test
@@ -43,7 +43,7 @@ public class MongoDbFactoryXmlConfigTest extends AbstractServiceXmlConfigTest<Mo
ApplicationContext testContext = getTestApplicationContext("cloud-mongo-with-config.xml", createService("my-service"));
MongoDbFactory connector = testContext.getBean("service-maxWait200-connectionPerHost50-WriteConcernSafe", getConnectorType());
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, 1, 50, 200);
MongoDbFactoryCloudConfigTestHelper.assertConfigProperties(connector, "safe", 50, 200);
}
@Test

View File

@@ -3,6 +3,7 @@ package org.springframework.cloud.service.mongo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.lang.reflect.Method;
import java.util.List;
import org.junit.Test;
@@ -10,6 +11,8 @@ import org.springframework.cloud.service.common.MongoServiceInfo;
import org.springframework.cloud.service.document.MongoDbFactoryCreator;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import com.mongodb.MongoClient;
@@ -49,7 +52,7 @@ public class MongoServiceConnectorCreatorTest {
MongoCredential credentials = mongo.getCredentialsList().get(0);
List<ServerAddress> addresses = mongo.getAllAddress();
List<ServerAddress> addresses = extractServerAddresses(mongo);
assertEquals(1, addresses.size());
ServerAddress address = addresses.get(0);
@@ -77,7 +80,7 @@ public class MongoServiceConnectorCreatorTest {
MongoClient mongo = (MongoClient) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
assertNotNull(mongo);
List<ServerAddress> addresses = mongo.getAllAddress();
List<ServerAddress> addresses = extractServerAddresses(mongo);
assertEquals(3, addresses.size());
MongoCredential credentials = mongo.getCredentialsList().get(0);
@@ -99,4 +102,19 @@ public class MongoServiceConnectorCreatorTest {
assertEquals(TEST_HOST_2, address3.getHost());
assertEquals(TEST_PORT, address3.getPort());
}
@SuppressWarnings("unchecked")
private List<ServerAddress> extractServerAddresses(MongoClient client) {
if (ClassUtils.isPresent("com.mongodb.connection.Cluster",
getClass().getClassLoader())) {
Object cluster = ReflectionTestUtils.getField(client, "cluster");
Object clusterSettings = ReflectionTestUtils.getField(cluster, "settings");
Method getHostsMethod = ReflectionUtils.findMethod(clusterSettings.getClass(),
"getHosts");
return (List<ServerAddress>) ReflectionUtils.invokeMethod(getHostsMethod,
clusterSettings);
}
return client.getAllAddress();
}
}