From a28ddb9dfa4e07d80b2325661fcead8edc976803 Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Fri, 26 Feb 2021 10:40:37 +0100 Subject: [PATCH] TST: add unit testt for etag --- .../java/org/caosdb/server/query/Query.java | 12 ++++++ .../org/caosdb/server/query/QueryTest.java | 37 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/main/java/org/caosdb/server/query/Query.java b/src/main/java/org/caosdb/server/query/Query.java index 60c361be..cd323bd9 100644 --- a/src/main/java/org/caosdb/server/query/Query.java +++ b/src/main/java/org/caosdb/server/query/Query.java @@ -985,4 +985,16 @@ public class Query implements QueryInterface, ToElementable, TransactionInterfac public Role getRole() { return this.role; } + + /** + * Return the ETag. + * + * <p>The ETag tags the query cache and is renewed each time the cache is being cleared, i.e. each + * time the database is being updated. + * + * @return The ETag + */ + public static String getETag() { + return cacheETag; + } } diff --git a/src/test/java/org/caosdb/server/query/QueryTest.java b/src/test/java/org/caosdb/server/query/QueryTest.java index edbefab7..9ca81370 100644 --- a/src/test/java/org/caosdb/server/query/QueryTest.java +++ b/src/test/java/org/caosdb/server/query/QueryTest.java @@ -1,10 +1,15 @@ package org.caosdb.server.query; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; import java.io.IOException; import org.caosdb.server.CaosDBServer; +import org.caosdb.server.database.access.InitAccess; +import org.caosdb.server.transaction.WriteTransaction; import org.junit.BeforeClass; import org.junit.Test; @@ -61,4 +66,36 @@ public class QueryTest { assertNull(q.getEntity()); assertEquals(Query.Role.ENTITY, q.getRole()); } + + /** Assure that {@link WriteTransaction#commit()} calls {@link Query#clearCache()}. */ + @Test + public void testEtagChangesAfterWrite() { + String old = Query.getETag(); + assertNotNull(old); + + WriteTransaction w = + new WriteTransaction(null) { + + @Override + public boolean useCache() { + // this function is being overriden purely for the purpose of calling + // commit() (which is protected) + try { + // otherwise the test fails because getAccess() return null; + setAccess(new InitAccess(null)); + + commit(); + } catch (Exception e) { + fail("this should not happen"); + } + return false; + } + }; + + // trigger commit(); + w.useCache(); + + String neu = Query.getETag(); + assertNotEquals(old, neu, "old and new tag should not be equal"); + } } -- GitLab