Skip to content

Commit a520428

Browse files
committed
Add Base options to ensure consistency between API methods.
JAVA-5737
1 parent 69c4f80 commit a520428

12 files changed

+131
-6
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ ext {
5959
junitBomVersion = '5.10.2'
6060
logbackVersion = '1.3.14'
6161
graalSdkVersion = '24.0.0'
62+
reflectionsVersion = '0.9.10'
6263
gitVersion = getGitVersion()
6364
}
6465

@@ -128,7 +129,7 @@ configure(scalaProjects) {
128129
testImplementation('org.scalatestplus:junit-4-13_%%:3.2.9.0')
129130
testImplementation('org.scalatestplus:mockito-3-12_%%:3.2.10.0')
130131
testImplementation("ch.qos.logback:logback-classic:$logbackVersion")
131-
testImplementation('org.reflections:reflections:0.9.10')
132+
testImplementation("org.reflections:reflections:$reflectionsVersion")
132133
}
133134

134135
test{

driver-core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ dependencies {
6060

6161
testImplementation project(':bson').sourceSets.test.output
6262
testImplementation('org.junit.jupiter:junit-jupiter-api')
63+
testImplementation("org.reflections:reflections:$reflectionsVersion")
6364
testRuntimeOnly "io.netty:netty-tcnative-boringssl-static"
6465

6566
classifiers.forEach {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.mongodb.client.model.bulk;
2+
3+
import com.mongodb.annotations.Sealed;
4+
import com.mongodb.client.model.Collation;
5+
import com.mongodb.lang.Nullable;
6+
import org.bson.conversions.Bson;
7+
8+
@Sealed
9+
interface BaseClientDeleteOptions {
10+
11+
BaseClientDeleteOptions collation(@Nullable Collation collation);
12+
13+
BaseClientDeleteOptions hint(@Nullable Bson hint);
14+
15+
BaseClientDeleteOptions hintString(@Nullable String hintString);
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
* http://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+
package com.mongodb.client.model.bulk;
17+
18+
import com.mongodb.annotations.Sealed;
19+
import com.mongodb.client.model.Collation;
20+
import com.mongodb.lang.Nullable;
21+
import org.bson.conversions.Bson;
22+
23+
24+
@Sealed
25+
interface BaseClientUpdateOptions {
26+
27+
BaseClientUpdateOptions arrayFilters(@Nullable Iterable<? extends Bson> arrayFilters);
28+
29+
BaseClientUpdateOptions collation(@Nullable Collation collation);
30+
31+
BaseClientUpdateOptions hint(@Nullable Bson hint);
32+
33+
BaseClientUpdateOptions hintString(@Nullable String hintString);
34+
35+
BaseClientUpdateOptions upsert(@Nullable Boolean upsert);
36+
}

driver-core/src/main/com/mongodb/client/model/bulk/ClientDeleteManyOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* @since 5.3
2828
*/
2929
@Sealed
30-
public interface ClientDeleteManyOptions {
30+
public interface ClientDeleteManyOptions extends BaseClientDeleteOptions {
3131
/**
3232
* Creates the default options.
3333
*

driver-core/src/main/com/mongodb/client/model/bulk/ClientDeleteOneOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* @since 5.3
2828
*/
2929
@Sealed
30-
public interface ClientDeleteOneOptions {
30+
public interface ClientDeleteOneOptions extends BaseClientDeleteOptions{
3131
/**
3232
* Creates the default options.
3333
*

driver-core/src/main/com/mongodb/client/model/bulk/ClientUpdateManyOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @since 5.3
2929
*/
3030
@Sealed
31-
public interface ClientUpdateManyOptions {
31+
public interface ClientUpdateManyOptions extends BaseClientUpdateOptions {
3232
/**
3333
* Creates the default options.
3434
*

driver-core/src/main/com/mongodb/client/model/bulk/ClientUpdateOneOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @since 5.3
2929
*/
3030
@Sealed
31-
public interface ClientUpdateOneOptions {
31+
public interface ClientUpdateOneOptions extends BaseClientUpdateOptions{
3232
/**
3333
* Creates the default options.
3434
*

driver-core/src/main/com/mongodb/internal/client/model/bulk/ConcreteClientDeleteManyOptions.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.mongodb.client.model.Collation;
1919
import com.mongodb.client.model.bulk.ClientDeleteManyOptions;
20-
import com.mongodb.client.model.bulk.ClientDeleteManyOptions;
2120
import com.mongodb.lang.Nullable;
2221
import org.bson.conversions.Bson;
2322

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.mongodb;
2+
3+
import org.reflections.Reflections;
4+
5+
import java.lang.reflect.Method;
6+
import java.util.Set;
7+
import java.util.stream.Collectors;
8+
9+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
public class AssertUtils {
13+
14+
public static <T> void assertSubInterfaceReturnTypes(String packageName, Class<T> baseClass) {
15+
Reflections reflections = new Reflections(packageName);
16+
17+
Set<Class<? extends T>> subInterfaces = reflections.getSubTypesOf(baseClass).stream()
18+
.filter(Class::isInterface)
19+
.collect(Collectors.toSet());
20+
21+
Method[] baseMethods = baseClass.getDeclaredMethods();
22+
23+
for (Class<? extends T> subInterface : subInterfaces) {
24+
for (Method baseMethod : baseMethods) {
25+
Method method = assertDoesNotThrow(
26+
() -> subInterface.getDeclaredMethod(baseMethod.getName(), baseMethod.getParameterTypes()),
27+
String.format(
28+
"%s does not override %s. The methods must be copied into the implementing interface.",
29+
subInterface.getName(),
30+
baseMethod.getName()
31+
)
32+
);
33+
34+
assertEquals(
35+
subInterface,
36+
method.getReturnType(),
37+
String.format(
38+
"Method %s in %s does not return %s. "
39+
+ "The return type must match the defining class.",
40+
method.getName(),
41+
subInterface.getName(),
42+
subInterface.getName()
43+
)
44+
);
45+
}
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)