Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ If you are using Maven without the BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.28.0')
implementation platform('com.google.cloud:libraries-bom:26.29.0')

implementation 'com.google.cloud:google-cloud-firestore'
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ private T performCreate(

private void verifyNotCommitted() {
Preconditions.checkState(
!isCommitted(), "Cannot modify a WriteBatch that has already been committed.");
!isCommitted(),
String.format(
"Cannot modify a %s that has already been committed.",
this.getClass().getSimpleName()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1242,4 +1242,20 @@ public static List<String> bundleToElementList(ByteBuffer bundle) {

return result;
}

@FunctionalInterface
interface VoidFunction {
void apply();
}

static void assertException(VoidFunction voidFunction, String expectedErrorMessage) {
String errorMessage = "";
try {
voidFunction.apply();
} catch (Exception e) {
errorMessage = e.getMessage();
} finally {
assertEquals(errorMessage, expectedErrorMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static com.google.cloud.firestore.LocalFirestoreHelper.rollbackResponse;
import static com.google.cloud.firestore.LocalFirestoreHelper.set;
import static com.google.cloud.firestore.LocalFirestoreHelper.update;
import static com.google.cloud.firestore.it.ITQueryTest.map;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -959,6 +960,43 @@ public void getShouldThrowWhenInvokedAfterAWriteWithAnAggregateQuery() throws Ex
assertThat(executionException.getCause()).isInstanceOf(IllegalStateException.class);
}

@Test
public void givesProperErrorMessageForCommittedTransaction() throws Exception {
doReturn(beginResponse())
.doReturn(commitResponse(0, 0))
.when(firestoreMock)
.sendRequest(
requestCapture.capture(), ArgumentMatchers.<UnaryCallable<Message, Message>>any());
String expectedErrorMessage = "Cannot modify a Transaction that has already been committed.";

DocumentReference docRef = firestoreMock.collection("foo").document("bar");

// Commit a transaction.
Transaction t = firestoreMock.runTransaction(transaction -> transaction).get();

// Then run other operations in the same transaction.
LocalFirestoreHelper.assertException(
() -> {
t.set(docRef, map("foo", "bar"));
},
expectedErrorMessage);
LocalFirestoreHelper.assertException(
() -> {
t.update(docRef, map("foo", "bar"));
},
expectedErrorMessage);
LocalFirestoreHelper.assertException(
() -> {
t.create(docRef, map("foo", "bar"));
},
expectedErrorMessage);
LocalFirestoreHelper.assertException(
() -> {
t.delete(docRef);
},
expectedErrorMessage);
}

private ApiException exception(Status.Code code, boolean shouldRetry) {
return exception(code, "Test exception", shouldRetry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,43 @@ public void deleteDocument() throws Exception {
CommitRequest commitRequest = commitCapture.getValue();
assertEquals(commit(writes.toArray(new Write[] {})), commitRequest);
}

@Test
public void throwsWhenModifyingACommittedWriteBatch() throws Exception {
doReturn(commitResponse(0, 0))
.when(firestoreMock)
.sendRequest(
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

String expectedErrorMessage = "Cannot modify a WriteBatch that has already been committed.";

DocumentReference docRef = firestoreMock.collection("foo").document("bar");

// Commit a batch.
WriteBatch batch = firestoreMock.batch();
batch.commit().get();

// Then run other operations in the same batch.
LocalFirestoreHelper.assertException(
() -> {
batch.set(docRef, map("foo", "bar"));
},
expectedErrorMessage);
LocalFirestoreHelper.assertException(
() -> {
batch.update(docRef, map("foo", "bar"));
},
expectedErrorMessage);
LocalFirestoreHelper.assertException(
() -> {
batch.create(docRef, map("foo", "bar"));
},
expectedErrorMessage);
LocalFirestoreHelper.assertException(
() -> {
batch.delete(docRef);
},
expectedErrorMessage);
}
}