Skip to content
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
7 changes: 5 additions & 2 deletions common/main/java/com/couchbase/lite/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ static Document getDocumentWithRevisions(@NonNull Collection collection, @NonNul

// Set by setData(FLSliceResult,boolean) to keep the Fleece backing store from being GC'd.
// (This is kind of a hack, and it's only used ephemerally by Kotlin serialization.)
@SuppressFBWarnings("URF_UNREAD_FIELD")
@SuppressWarnings("PMD.SingularField")
@GuardedBy("lock")
@Nullable
private FLSliceResult extraBackingStore;
Expand Down Expand Up @@ -632,9 +634,10 @@ private void setC4Document(@Nullable C4Document c4doc, boolean mutable) {
// for use by CollectionExtensions.kt
void setContent(@NonNull FLSliceResult fleeceData, boolean mutable) {
synchronized (lock) {
var data = FLValue.fromData(fleeceData).asFLDict();
final FLValue body = FLValue.fromData(fleeceData);
if (body == null) { throw new CouchbaseLiteError("Failed parsing fleece data"); }
extraBackingStore = fleeceData;
setContentLocked(data, mutable);
setContentLocked(body.asFLDict(), mutable);
}
}

Expand Down
50 changes: 0 additions & 50 deletions common/test/java/com/couchbase/lite/CollectionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package com.couchbase.lite

import com.couchbase.lite.internal.utils.SlowTest
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import org.junit.Assert
import org.junit.Test

Expand Down Expand Up @@ -1215,52 +1213,4 @@ class CollectionTest : BaseDbTest() {

Assert.assertEquals(scope, testDatabase.getScope(Scope.DEFAULT_NAME))
}

//---------------------------------------------
// Model-based (serialization) API
//---------------------------------------------

@Test
fun saveNewDocInCollectionFromModel() {
val id = getUniqueName("test_doc")

// Create a new model and save it:
val model = TestModel("Nigel", 12)
testCollection.save(model, id)
Assert.assertEquals(testCollection, model.documentMeta?.collection)
Assert.assertEquals(id, model.documentMeta?.id)

// Read it back:
Assert.assertEquals(1, testCollection.count)
val gotModel = testCollection.getDocumentAs<TestModel>(id)!!
Assert.assertEquals(model, gotModel)

// Modify and save again:
gotModel.favorites = listOf("XTC", "Elvis Costello")
Assert.assertTrue(testCollection.save(gotModel))
Assert.assertNotEquals(model.documentMeta?.revisionID, gotModel.documentMeta?.revisionID)

// Get it as a regular Document and verify the contents:
val doc = testCollection.getDocument(id)!!
Assert.assertEquals(gotModel.documentMeta?.revisionID, doc.revisionID)
Assert.assertEquals("Nigel", doc.getString("name"))
Assert.assertEquals(12, doc.getInt("age"))
val faves = doc.getArray("favorites")!!
Assert.assertEquals(2, faves.count())
Assert.assertEquals("XTC", faves.getString(0))
Assert.assertEquals("Elvis Costello", faves.getString(1))

// Delete the model. `model` is out of date, so it will fail, but `gotModel` is OK:
Assert.assertFalse(testCollection.delete(model, ConcurrencyControl.FAIL_ON_CONFLICT))
Assert.assertTrue(testCollection.delete(gotModel, ConcurrencyControl.FAIL_ON_CONFLICT))
}
}


// Simple Model class for tests
@Serializable
data class TestModel(var name: String,
var age: Int,
var favorites: List<String>? = null): DocumentModel {
@Transient override var documentMeta: DocumentMeta? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// Copyright (c) 2026 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:// www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package com.couchbase.lite

import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import org.junit.Assert
import org.junit.Test


/** Tests serialization-based Collection accessors implemented in CollectionExtensions.kt. */
class CollectionSerializationTest : BaseDbTest() {
//---------------------------------------------
// Model-based (serialization) API
//---------------------------------------------

@Test
fun saveNewDocInCollectionFromModel() {
val id = getUniqueName("test_doc")

// Create a new model and save it:
val model = TestModel("Nigel", 12)
testCollection.save(model, id)
Assert.assertEquals(testCollection, model.documentMeta?.collection)
Assert.assertEquals(id, model.documentMeta?.id)

// Read it back:
Assert.assertEquals(1, testCollection.count)
val gotModel = testCollection.getDocumentAs<TestModel>(id)!!
Assert.assertEquals(model, gotModel)

// Modify and save again:
gotModel.favorites = listOf("XTC", "Elvis Costello")
Assert.assertTrue(testCollection.save(gotModel))
Assert.assertNotEquals(model.documentMeta?.revisionID, gotModel.documentMeta?.revisionID)

// Get it as a regular Document and verify the contents:
val doc = testCollection.getDocument(id)!!
Assert.assertEquals(gotModel.documentMeta?.revisionID, doc.revisionID)
Assert.assertEquals("Nigel", doc.getString("name"))
Assert.assertEquals(12, doc.getInt("age"))
val faves = doc.getArray("favorites")!!
Assert.assertEquals(2, faves.count())
Assert.assertEquals("XTC", faves.getString(0))
Assert.assertEquals("Elvis Costello", faves.getString(1))

// Delete the model. `model` is out of date, so it will fail, but `gotModel` is OK:
Assert.assertFalse(testCollection.delete(model, ConcurrencyControl.FAIL_ON_CONFLICT))
Assert.assertTrue(testCollection.delete(gotModel, ConcurrencyControl.FAIL_ON_CONFLICT))
}
}


// Simple Model class for tests
@Serializable
data class TestModel(var name: String,
var age: Int,
var favorites: List<String>? = null): DocumentModel {
@Transient override var documentMeta: DocumentMeta? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ class ResultSerializationTest : BaseQueryTest() {

assertFalse(iter.hasNext())
}
}
}
Loading