Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ae5cc8

Browse files
Extend database factory to accept sync Kotlin MongoDriver instance.
1 parent ce7f03a commit 8ae5cc8

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.mongodb.core
18+
19+
import com.mongodb.kotlin.client.MongoClient
20+
import org.springframework.beans.DirectFieldAccessor
21+
22+
/**
23+
* Extension for [SimpleMongoClientDatabaseFactory] that accepts a [MongoClient].
24+
*
25+
* @author Christoph Strobl
26+
* @since 4.2
27+
*/
28+
fun SimpleMongoClientDatabaseFactory(client: MongoClient, database: String): SimpleMongoClientDatabaseFactory =
29+
SimpleMongoClientDatabaseFactory(
30+
DirectFieldAccessor(client).getPropertyValue("wrapped") as com.mongodb.client.MongoClient,
31+
database
32+
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.mongodb.core
18+
19+
import com.mongodb.kotlin.client.MongoClient
20+
import org.bson.Document
21+
import org.junit.jupiter.api.Test
22+
import org.springframework.data.mongodb.test.util.Assertions.assertThat
23+
24+
/**
25+
* @author Christoph Strobl
26+
*/
27+
class SimpleMongoClientDatabaseFactoryExtensionTests {
28+
29+
@Test // GH-4393
30+
fun `extension allows to create SimpleMongoClientDatabaseFactory with a Kotlin Driver instance`() {
31+
32+
val factory = SimpleMongoClientDatabaseFactory(MongoClient.create(), "test")
33+
34+
assertThat(factory.mongoDatabase.runCommand(Document("ping", 1))).containsKey("ok")
35+
}
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /