Android Open Source - couchbase-lite-android Document Test






From Project

Back to project page couchbase-lite-android.

License

The source code is released under:

Apache License

If you think the Android project couchbase-lite-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.couchbase.lite;
//from w w w.  ja  v a  2 s . c  om
import com.couchbase.lite.internal.RevisionInternal;
import com.couchbase.lite.util.Log;

import junit.framework.Assert;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.ObjectMapper;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class DocumentTest extends LiteTestCase {

    public void testNewDocumentHasCurrentRevision() throws CouchbaseLiteException {

        Document document = database.createDocument();
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("foo", "foo");
        properties.put("bar", Boolean.FALSE);
        document.putProperties(properties);
        Assert.assertNotNull(document.getCurrentRevisionId());
        Assert.assertNotNull(document.getCurrentRevision());

    }

    /**
     * https://github.com/couchbase/couchbase-lite-android/issues/301
     */
    public void testPutDeletedDocument() throws CouchbaseLiteException {

        Document document = database.createDocument();
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("foo", "foo");
        properties.put("bar", Boolean.FALSE);
        document.putProperties(properties);
        Assert.assertNotNull(document.getCurrentRevision());
        String docId = document.getId();

        properties.put("_rev",document.getCurrentRevisionId());
        properties.put("_deleted", true);
        properties.put("mykey", "myval");
        SavedRevision newRev = document.putProperties(properties);
        newRev.loadProperties();
        assertTrue( newRev.getProperties().containsKey("mykey") );

        Assert.assertTrue(document.isDeleted());
        Document fetchedDoc = database.getExistingDocument(docId);
        Assert.assertNull(fetchedDoc);

        // query all docs and make sure we don't see that document
        database.getAllDocs(new QueryOptions());
        Query queryAllDocs = database.createAllDocumentsQuery();
        QueryEnumerator queryEnumerator = queryAllDocs.run();
        for (Iterator<QueryRow> it = queryEnumerator; it.hasNext();) {
            QueryRow row = it.next();
            Assert.assertFalse(row.getDocument().getId().equals(docId));
        }

    }

    public void testDeleteDocument() throws CouchbaseLiteException {

        Document document = database.createDocument();
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("foo", "foo");
        properties.put("bar", Boolean.FALSE);
        document.putProperties(properties);
        Assert.assertNotNull(document.getCurrentRevision());
        String docId = document.getId();
        document.delete();
        Assert.assertTrue(document.isDeleted());
        Document fetchedDoc = database.getExistingDocument(docId);
        Assert.assertNull(fetchedDoc);

        // query all docs and make sure we don't see that document
        database.getAllDocs(new QueryOptions());
        Query queryAllDocs = database.createAllDocumentsQuery();
        QueryEnumerator queryEnumerator = queryAllDocs.run();
        for (Iterator<QueryRow> it = queryEnumerator; it.hasNext();) {
            QueryRow row = it.next();
            Assert.assertFalse(row.getDocument().getId().equals(docId));
        }

    }

    /**
     * Port test over from:
     * https://github.com/couchbase/couchbase-lite-ios/commit/e0469300672a2087feb46b84ca498facd49e0066
     */
    public void testGetNonExistentDocument() throws CouchbaseLiteException {
        assertNull(database.getExistingDocument("missing"));
        Document doc = database.getDocument("missing");
        assertNotNull(doc);
        assertNull(database.getExistingDocument("missing"));
    }

    // Reproduces issue #167
    // https://github.com/couchbase/couchbase-lite-android/issues/167
    public void testLoadRevisionBody() throws CouchbaseLiteException {

        Document document = database.createDocument();
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("foo", "foo");
        properties.put("bar", Boolean.FALSE);
        document.putProperties(properties);
        Assert.assertNotNull(document.getCurrentRevision());

        boolean deleted = false;
        RevisionInternal revisionInternal = new RevisionInternal(
                document.getId(),
                document.getCurrentRevisionId(),
                deleted,
                database
        );
        EnumSet<Database.TDContentOptions> contentOptions = EnumSet.of(
                Database.TDContentOptions.TDIncludeAttachments,
                Database.TDContentOptions.TDBigAttachmentsFollow
        );
        database.loadRevisionBody(revisionInternal, contentOptions);

        // now lets purge the document, and then try to load the revision body again
        document.purge();

        boolean gotExpectedException = false;
        try {
            database.loadRevisionBody(revisionInternal, contentOptions);
        } catch (CouchbaseLiteException e) {
            if (e.getCBLStatus().getCode() == Status.NOT_FOUND) {
                gotExpectedException = true;
            }
        }

        assertTrue(gotExpectedException);


    }

    /**
     * https://github.com/couchbase/couchbase-lite-android/issues/281
     */
    public void testDocumentWithRemovedProperty() {

        Map<String, Object> props = new HashMap<String, Object>();
        props.put("_id", "fakeid");
        props.put("_removed", true);
        props.put("foo", "bar");

        Document doc = createDocumentWithProperties(database, props);
        assertNotNull(doc);

        Document docFetched = database.getDocument(doc.getId());
        Map<String, Object> fetchedProps = docFetched.getCurrentRevision().getProperties();
        assertNotNull(fetchedProps.get("_removed"));
        assertTrue(docFetched.getCurrentRevision().isGone());

    }

    public void failingTestGetDocumentWithLargeJSON() {

        Map<String, Object> props = new HashMap<String, Object>();
        props.put("_id", "laaargeJSON");
        char[] chars = new char[2500000];//~5MB
        Arrays.fill(chars, 'a');
        props.put("foo", new String(chars));

        Document doc = createDocumentWithProperties(database, props);
        assertNotNull(doc);

        Document docFetched = database.getDocument(doc.getId());
        Map<String, Object> fetchedProps = docFetched.getCurrentRevision().getProperties();
        assertEquals(fetchedProps.get("foo"), new String(chars));

    }



    public void failingTestDocumentPropertiesAreImmutable() throws Exception {
        String jsonString = "{\n" +
                "    \"name\":\"praying mantis\",\n" +
                "    \"wikipedia\":{\n" +
                "        \"behavior\":{\n" +
                "            \"style\":\"predatory\",\n" +
                "            \"attack\":\"ambush\"\n" +
                "        },\n" +
                "        \"evolution\":{\n" +
                "            \"ancestor\":\"proto-roaches\",\n" +
                "            \"cousin\":\"termite\"\n" +
                "        }       \n" +
                "    }   \n" +
                "\n" +
                "}";
        Map map = (Map) Manager.getObjectMapper().readValue(jsonString, Object.class);
        Document doc = createDocumentWithProperties(database, map);

        boolean firstLevelImmutable = false;
        Map<String, Object> props = doc.getProperties();
        try {
            props.put("name", "bug");
        } catch (UnsupportedOperationException e) {
            firstLevelImmutable = true;
        }
        assertTrue(firstLevelImmutable);

        boolean secondLevelImmutable = false;
        Map wikiProps = (Map) props.get("wikipedia");
        try {
            wikiProps.put("behavior", "unknown");
        } catch (UnsupportedOperationException e) {
            secondLevelImmutable = true;
        }
        assertTrue(secondLevelImmutable);

        boolean thirdLevelImmutable = false;
        Map evolutionProps = (Map) wikiProps.get("behavior");
        try {
            evolutionProps.put("movement", "flight");
        } catch (UnsupportedOperationException e) {
            thirdLevelImmutable = true;
        }
        assertTrue(thirdLevelImmutable);
    }

    public void failingTestProvidedMapChangesAreSafe() throws Exception {
        Map<String, Object> originalProps = new HashMap<String, Object>();
        Document doc = createDocumentWithProperties(database, originalProps);

        Map<String, Object> nestedProps = new HashMap<String, Object>();
        nestedProps.put("version", "original");
        UnsavedRevision rev = doc.createRevision();
        rev.getProperties().put("nested", nestedProps);
        rev.save();

        nestedProps.put("version", "changed");
        assertEquals("original", ((Map) doc.getProperty("nested")).get("version"));
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    static public class Foo {
        private String bar;

        public Foo() {
        }

        public String getBar() {
            return bar;
        }

        public void setBar(String bar) {
            this.bar = bar;
        }
    }

    /**
     * Assert that if you add a
     * @throws Exception
     */
    public void testNonPrimitiveTypesInDocument() throws Exception {

        Object fooProperty;
        Map<String, Object> props = new HashMap<String, Object>();

        Foo foo = new Foo();
        foo.setBar("basic");

        props.put("foo", foo);
        Document doc = createDocumentWithProperties(database, props);
        fooProperty = doc.getProperties().get("foo");
        assertTrue(fooProperty instanceof Map);
        assertFalse(fooProperty instanceof Foo);

        Document fetched = database.getDocument(doc.getId());
        fooProperty = fetched.getProperties().get("foo");
        assertTrue(fooProperty instanceof Map);
        assertFalse(fooProperty instanceof Foo);

        ObjectMapper mapper = new ObjectMapper();
        Foo fooResult = mapper.convertValue(fooProperty, Foo.class);
        assertEquals(foo.bar, fooResult.bar);

    }


    public void testDocCustomID() throws Exception {

        Document document = database.getDocument("my_custom_id");
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("foo", "bar");
        document.putProperties(properties);

        Document documentFetched = database.getDocument("my_custom_id");
        assertEquals("my_custom_id", documentFetched.getId());
        assertEquals("bar", documentFetched.getProperties().get("foo"));

    }

}




Java Source Code List

com.couchbase.lite.ApiTest.java
com.couchbase.lite.AttachmentsTest.java
com.couchbase.lite.AuthTest.java
com.couchbase.lite.Base64Test.java
com.couchbase.lite.BlobStoreWriterTest.java
com.couchbase.lite.CRUDOperationsTest.java
com.couchbase.lite.CacheTest.java
com.couchbase.lite.ChangesTest.java
com.couchbase.lite.CollationTest.java
com.couchbase.lite.DatabaseTest.java
com.couchbase.lite.DocumentTest.java
com.couchbase.lite.LitePerfTestCase.java
com.couchbase.lite.LiteTestCase.java
com.couchbase.lite.LiteTestContext.java
com.couchbase.lite.LocalDocsTest.java
com.couchbase.lite.ManagerTest.java
com.couchbase.lite.MiscTest.java
com.couchbase.lite.MultipartReaderTest.java
com.couchbase.lite.RevTreeTest.java
com.couchbase.lite.RevisionsTest.java
com.couchbase.lite.RouterTest.java
com.couchbase.lite.SequenceMapTest.java
com.couchbase.lite.ValidationsTest.java
com.couchbase.lite.ViewsTest.java
com.couchbase.lite.android.AndroidContext.java
com.couchbase.lite.android.AndroidLogger.java
com.couchbase.lite.android.AndroidNetworkReachabilityManager.java
com.couchbase.lite.android.AndroidSQLiteStorageEngineFactory.java
com.couchbase.lite.android.AndroidSQLiteStorageEngine.java
com.couchbase.lite.mockserver.MockBulkDocs.java
com.couchbase.lite.mockserver.MockChangesFeedNoResponse.java
com.couchbase.lite.mockserver.MockChangesFeed.java
com.couchbase.lite.mockserver.MockCheckpointGet.java
com.couchbase.lite.mockserver.MockCheckpointPut.java
com.couchbase.lite.mockserver.MockDispatcher.java
com.couchbase.lite.mockserver.MockDocumentBulkGet.java
com.couchbase.lite.mockserver.MockDocumentGet.java
com.couchbase.lite.mockserver.MockDocumentPut.java
com.couchbase.lite.mockserver.MockFacebookAuthPost.java
com.couchbase.lite.mockserver.MockHelper.java
com.couchbase.lite.mockserver.MockPreloadedPullTarget.java
com.couchbase.lite.mockserver.MockRevsDiff.java
com.couchbase.lite.mockserver.MockSessionGet.java
com.couchbase.lite.mockserver.SmartMockResponse.java
com.couchbase.lite.mockserver.WrappedSmartMockResponse.java
com.couchbase.lite.performance2.Test01_CreateDocs.java
com.couchbase.lite.performance2.Test02_CreateDocsUnoptimizedWay.java
com.couchbase.lite.performance2.Test03_CreateDocsWithAttachments.java
com.couchbase.lite.performance2.Test06_PullReplication.java
com.couchbase.lite.performance2.Test07_PushReplication.java
com.couchbase.lite.performance2.Test08_DocRevisions.java
com.couchbase.lite.performance2.Test09_LoadDB.java
com.couchbase.lite.performance2.Test10_DeleteDB.java
com.couchbase.lite.performance2.Test11_DeleteDocs.java
com.couchbase.lite.performance2.Test12_IndexView.java
com.couchbase.lite.performance2.Test13_QueryView.java
com.couchbase.lite.performance2.Test14_ReduceView.java
com.couchbase.lite.performance2.Test28_KeySizes.java
com.couchbase.lite.performance2.Test29_AllDocQuery.java
com.couchbase.lite.performance2.Test30_LiveQuery.java
com.couchbase.lite.performance2.Test31_CompactDB.java
com.couchbase.lite.performance.Test10_DeleteDB.java
com.couchbase.lite.performance.Test11_DeleteDocs.java
com.couchbase.lite.performance.Test12_IndexView.java
com.couchbase.lite.performance.Test13_QueryView.java
com.couchbase.lite.performance.Test14_ReduceView.java
com.couchbase.lite.performance.Test16_ParallelPushReplication.java
com.couchbase.lite.performance.Test1_CreateDocs.java
com.couchbase.lite.performance.Test2_CreateDocsUnoptimizedWay.java
com.couchbase.lite.performance.Test3_CreateDocsWithAttachments.java
com.couchbase.lite.performance.Test6_PushReplication.java
com.couchbase.lite.performance.Test7_PullReplication.java
com.couchbase.lite.performance.Test8_DocRevisions.java
com.couchbase.lite.performance.Test9_LoadDB.java
com.couchbase.lite.replicator.BulkDownloaderTest.java
com.couchbase.lite.replicator.ChangeTrackerTest.java
com.couchbase.lite.replicator.CustomizableMockHttpClient.java
com.couchbase.lite.replicator.ReplicationTest.java
com.couchbase.lite.replicator.ResponderChain.java
com.couchbase.lite.support.BatcherTest.java
com.couchbase.lite.support.JsonDocumentTest.java
com.couchbase.lite.support.PersistentCookieStoreTest.java
com.couchbase.lite.support.RemoteRequestTest.java
com.couchbase.touchdb.RevCollator.java
com.couchbase.touchdb.TDCollateJSON.java