use of org.springframework.data.mongodb.MongoDatabaseFactory in project spring-data-mongodb by spring-projects.
the class MongoDbFactoryParserIntegrationTests method readsReplicasWriteConcernCorrectly.
// DATAMONGO-331
@Test
public void readsReplicasWriteConcernCorrectly() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("namespace/db-factory-bean-custom-write-concern.xml");
MongoDatabaseFactory factory = ctx.getBean("second", MongoDatabaseFactory.class);
MongoDatabase db = factory.getMongoDatabase();
assertThat(db.getWriteConcern()).isEqualTo(WriteConcern.W2);
ctx.close();
}
use of org.springframework.data.mongodb.MongoDatabaseFactory in project spring-boot by spring-projects.
the class MongoDataAutoConfigurationTests method createsMongoDatabaseFactoryForPreferredMongoClient.
@Test
void createsMongoDatabaseFactoryForPreferredMongoClient() {
this.contextRunner.run((context) -> {
MongoDatabaseFactory dbFactory = context.getBean(MongoDatabaseFactory.class);
assertThat(dbFactory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
});
}
use of org.springframework.data.mongodb.MongoDatabaseFactory in project spring-data-mongodb by spring-projects.
the class SessionBoundMongoTemplateTests method setUp.
@BeforeEach
public void setUp() {
MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(client, "session-bound-mongo-template-tests") {
@Override
public MongoDatabase getMongoDatabase() throws DataAccessException {
MongoDatabase spiedDatabse = Mockito.spy(super.getMongoDatabase());
spiedDatabases.add(spiedDatabse);
return spiedDatabse;
}
};
session = client.startSession(ClientSessionOptions.builder().build());
this.template = new MongoTemplate(factory);
this.sessionBoundTemplate = new SessionBoundMongoTemplate(session, new MongoTemplate(factory, getDefaultMongoConverter(factory))) {
@Override
protected MongoCollection<Document> prepareCollection(MongoCollection<Document> collection) {
injectCollectionSpy(collection);
return super.prepareCollection(collection);
}
@SuppressWarnings({ "ConstantConditions", "unchecked" })
private void injectCollectionSpy(MongoCollection<Document> collection) {
InvocationHandler handler = Proxy.getInvocationHandler(collection);
Advised advised = (Advised) ReflectionTestUtils.getField(handler, "advised");
for (Advisor advisor : advised.getAdvisors()) {
Advice advice = advisor.getAdvice();
if (advice instanceof SessionAwareMethodInterceptor) {
MongoCollection<Document> spiedCollection = Mockito.spy((MongoCollection<Document>) ReflectionTestUtils.getField(advice, "target"));
spiedCollections.add(spiedCollection);
ReflectionTestUtils.setField(advice, "target", spiedCollection);
}
}
}
};
}
use of org.springframework.data.mongodb.MongoDatabaseFactory in project spring-data-mongodb by spring-projects.
the class SimpleMongoClientDatabaseFactoryUnitTests method cascadedWithSessionUsesRootFactory.
// DATAMONGO-1880
@Test
void cascadedWithSessionUsesRootFactory() {
when(mongo.getDatabase("foo")).thenReturn(database);
MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(mongo, "foo");
MongoDatabaseFactory wrapped = factory.withSession(clientSession).withSession(clientSession);
InvocationHandler invocationHandler = Proxy.getInvocationHandler(wrapped.getMongoDatabase());
Object singletonTarget = AopProxyUtils.getSingletonTarget(ReflectionTestUtils.getField(invocationHandler, "advised"));
assertThat(singletonTarget).isSameAs(database);
}
use of org.springframework.data.mongodb.MongoDatabaseFactory in project spring-data-mongodb by spring-projects.
the class SimpleMongoClientDatabaseFactoryUnitTests method mongoUriConstructor.
// DATADOC-295
@Test
void mongoUriConstructor() {
ConnectionString mongoURI = new ConnectionString("mongodb://myUsername:myPassword@localhost/myDatabase.myCollection");
MongoDatabaseFactory mongoDbFactory = new SimpleMongoClientDatabaseFactory(mongoURI);
assertThat(mongoDbFactory).hasFieldOrPropertyWithValue("databaseName", "myDatabase");
}
Aggregations