Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static List<MethodReplacementClass> getList() {
new MessageBodyReaderClassReplacement(),
new MethodClassReplacement(),
new MongoCollectionClassReplacement(),
new MongoCollectionLegacyCountClassReplacement(),
new MongoCollectionPipelineUpdateClassReplacement(),
new MongoTemplateClassReplacement(),
new OpenSearchClientClassReplacement(),
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.evomaster.client.java.instrumentation.coverage.methodreplacement.thirdpartyclasses;

import org.evomaster.client.java.instrumentation.coverage.methodreplacement.Replacement;
import org.evomaster.client.java.instrumentation.coverage.methodreplacement.ThirdPartyCast;
import org.evomaster.client.java.instrumentation.coverage.methodreplacement.UsageFilter;
import org.evomaster.client.java.instrumentation.shared.ReplacementCategory;
import org.evomaster.client.java.instrumentation.shared.ReplacementType;

import java.util.Arrays;

/**
* Replacements for MongoCollection.count(...), which was deprecated in driver 3.8 in favor of
* countDocuments(...), and removed in 4.x.
* <p>
* This cannot be in {@link MongoCollectionClassReplacement}: all the original methods of a
* replacement class are resolved together, and they all must exist in the driver version
* in use. As count(...) does not exist in recent drivers, having it there would break all the other
* replacements for MongoCollection with those drivers. Here, instead, the methods are resolved
* only if a call to count(...) is replaced, which can only happen with drivers having it.
* <p>
* Old versions of Spring Data (2.1 and 2.2) still use count(...) to implement exists and count.
*/
public class MongoCollectionLegacyCountClassReplacement extends MongoOperationClassReplacement {
private static final MongoCollectionLegacyCountClassReplacement singleton = new MongoCollectionLegacyCountClassReplacement();

@Override
protected String getNameOfThirdPartyTargetClass() {
return "com.mongodb.client.MongoCollection";
}

/**
* Replacement for {@code MongoCollection.count(Bson)}.
* <p>
* Present in the MongoDB Java driver from 3.0.0 (the first version with {@code MongoCollection}) until 3.12.14 (the last 3.x version checked); removed in 4.0.0.
*/
@Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = "countBson", usageFilter = UsageFilter.ANY, category = ReplacementCategory.MONGO)
public static long count(Object mongoCollection, @ThirdPartyCast(actualType = "org.bson.conversions.Bson") Object filter) {
return (long) handleEagerQuery(singleton, "countBson", mongoCollection, Arrays.asList(filter), filter);
}

/**
* Replacement for {@code MongoCollection.count(Bson, CountOptions)}.
* <p>
* Present in the MongoDB Java driver from 3.0.0 (the first version with {@code MongoCollection}) until 3.12.14 (the last 3.x version checked); removed in 4.0.0.
*/
@Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = "countBsonCountOptions", usageFilter = UsageFilter.ANY, category = ReplacementCategory.MONGO)
public static long count(Object mongoCollection, @ThirdPartyCast(actualType = "org.bson.conversions.Bson") Object filter, @ThirdPartyCast(actualType = "com.mongodb.client.model.CountOptions") Object options) {
return (long) handleEagerQuery(singleton, "countBsonCountOptions", mongoCollection, Arrays.asList(filter, options), filter);
}

/**
* Replacement for {@code MongoCollection.count(ClientSession, Bson)}.
* <p>
* Present in the MongoDB Java driver from 3.8.0 (not present in 3.7.1) until 3.12.14 (the last 3.x version checked); removed in 4.0.0.
*/
@Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = "countClientSessionBson", usageFilter = UsageFilter.ANY, category = ReplacementCategory.MONGO)
public static long count_EM_0(Object mongoCollection, @ThirdPartyCast(actualType = "com.mongodb.client.ClientSession") Object clientSession, @ThirdPartyCast(actualType = "org.bson.conversions.Bson") Object filter) {
return (long) handleEagerQuery(singleton, "countClientSessionBson", mongoCollection, Arrays.asList(clientSession, filter), filter);
}

/**
* Replacement for {@code MongoCollection.count(ClientSession, Bson, CountOptions)}.
* <p>
* Present in the MongoDB Java driver from 3.8.0 (not present in 3.7.1) until 3.12.14 (the last 3.x version checked); removed in 4.0.0.
*/
@Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = "countClientSessionBsonCountOptions", usageFilter = UsageFilter.ANY, category = ReplacementCategory.MONGO)
public static long count(Object mongoCollection, @ThirdPartyCast(actualType = "com.mongodb.client.ClientSession") Object clientSession, @ThirdPartyCast(actualType = "org.bson.conversions.Bson") Object filter, @ThirdPartyCast(actualType = "com.mongodb.client.model.CountOptions") Object options) {
return (long) handleEagerQuery(singleton, "countClientSessionBsonCountOptions", mongoCollection, Arrays.asList(clientSession, filter, options), filter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* only if a call with a pipeline is replaced, which can only happen with drivers having it.
*/
public class MongoCollectionPipelineUpdateClassReplacement extends MongoOperationClassReplacement {

private static final MongoCollectionPipelineUpdateClassReplacement singleton = new MongoCollectionPipelineUpdateClassReplacement();

@Override
Expand Down Expand Up @@ -109,25 +108,43 @@ public static Object updateMany(Object mongoCollection, @ThirdPartyCast(actualTy
return handleEagerQuery(singleton, "updateManyClientSessionBsonPipelineUpdateOptions", mongoCollection, Arrays.asList(clientSession, filter, pipeline, options), filter);
}

/**
* Replacement for {@code MongoCollection.findOneAndUpdate(Bson, List<? extends Bson>)}.
* <p>
* Present in the MongoDB Java driver from 3.11.0 (not present in 3.10.2).
*/
@Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = "findOneAndUpdateBsonPipeline", usageFilter = UsageFilter.ANY, category = ReplacementCategory.MONGO)
public static Object findOneAndUpdate(Object mongoCollection, @ThirdPartyCast(actualType = "org.bson.conversions.Bson") Object filter, List<?> pipeline) {
return handleEagerQuery(singleton,"findOneAndUpdateBsonPipeline", mongoCollection, Arrays.asList(filter, pipeline), filter);
return handleEagerQuery(singleton, "findOneAndUpdateBsonPipeline", mongoCollection, Arrays.asList(filter, pipeline), filter);
}

/**
* Replacement for {@code MongoCollection.findOneAndUpdate(Bson, List<? extends Bson>, FindOneAndUpdateOptions)}.
* <p>
* Present in the MongoDB Java driver from 3.11.0 (not present in 3.10.2).
*/
@Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = "findOneAndUpdateBsonPipelineFindOneAndUpdateOptions", usageFilter = UsageFilter.ANY, category = ReplacementCategory.MONGO)
public static Object findOneAndUpdate(Object mongoCollection, @ThirdPartyCast(actualType = "org.bson.conversions.Bson") Object filter, List<?> pipeline, @ThirdPartyCast(actualType = "com.mongodb.client.model.FindOneAndUpdateOptions") Object options) {
return handleEagerQuery(singleton,"findOneAndUpdateBsonPipelineFindOneAndUpdateOptions", mongoCollection, Arrays.asList(filter, pipeline, options), filter);
return handleEagerQuery(singleton, "findOneAndUpdateBsonPipelineFindOneAndUpdateOptions", mongoCollection, Arrays.asList(filter, pipeline, options), filter);
}

/**
* Replacement for {@code MongoCollection.findOneAndUpdate(ClientSession, Bson, List<? extends Bson>)}.
* <p>
* Present in the MongoDB Java driver from 3.11.0 (not present in 3.10.2).
*/
@Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = "findOneAndUpdateClientSessionBsonPipeline", usageFilter = UsageFilter.ANY, category = ReplacementCategory.MONGO)
public static Object findOneAndUpdate(Object mongoCollection, @ThirdPartyCast(actualType = "com.mongodb.client.ClientSession") Object clientSession, @ThirdPartyCast(actualType = "org.bson.conversions.Bson") Object filter, List<?> pipeline) {
return handleEagerQuery(singleton,"findOneAndUpdateClientSessionBsonPipeline", mongoCollection, Arrays.asList(clientSession, filter, pipeline), filter);
return handleEagerQuery(singleton, "findOneAndUpdateClientSessionBsonPipeline", mongoCollection, Arrays.asList(clientSession, filter, pipeline), filter);
}

/**
* Replacement for {@code MongoCollection.findOneAndUpdate(ClientSession, Bson, List<? extends Bson>, FindOneAndUpdateOptions)}.
* <p>
* Present in the MongoDB Java driver from 3.11.0 (not present in 3.10.2).
*/
@Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = "findOneAndUpdateClientSessionBsonPipelineFindOneAndUpdateOptions", usageFilter = UsageFilter.ANY, category = ReplacementCategory.MONGO)
public static Object findOneAndUpdate(Object mongoCollection, @ThirdPartyCast(actualType = "com.mongodb.client.ClientSession") Object clientSession, @ThirdPartyCast(actualType = "org.bson.conversions.Bson") Object filter, List<?> pipeline, @ThirdPartyCast(actualType = "com.mongodb.client.model.FindOneAndUpdateOptions") Object options) {
return handleEagerQuery(singleton,"findOneAndUpdateClientSessionBsonPipelineFindOneAndUpdateOptions", mongoCollection, Arrays.asList(clientSession, filter, pipeline, options), filter);
return handleEagerQuery(singleton, "findOneAndUpdateClientSessionBsonPipelineFindOneAndUpdateOptions", mongoCollection, Arrays.asList(clientSession, filter, pipeline, options), filter);
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.evomaster.client.java.instrumentation.coverage.methodreplacement;

import org.evomaster.client.java.instrumentation.coverage.methodreplacement.thirdpartyclasses.MongoCollectionLegacyCountClassReplacement;
import org.evomaster.client.java.instrumentation.shared.ReplacementType;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -27,6 +28,14 @@ public void testReplacementMethods() {

for (MethodReplacementClass mrc : ReplacementList.getList()) {

if (mrc instanceof MongoCollectionLegacyCountClassReplacement) {
/**
* This is a special case, because the legacy count method is deprecated and removed in MongoDB 4.0,
* so we cannot test it in all environments
*/
continue;
}

//make sure that during testing all third-party libraries are available
assertTrue(mrc.isAvailable(), "Not available: " + mrc.getClass().getName());

Expand Down Expand Up @@ -193,4 +202,4 @@ private static void checkBaseJdkType(Method m, Class typeToCheck) {
m.getDeclaringClass().getName() + "#" + m.getName()
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,6 @@ public void testFindOneAndUpdateWithClientSessionFilterAndOptions() {
}





@Test
public void testFindOneAndUpdateWithFilterNoMatch() {
final MongoCollection<Document> collection = getMongoCollection();
Expand Down
Loading