You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:18:15 UTC

[sling-org-apache-sling-testing-jcr-mock] 19/27: SLING-4802 JcrMock: Support Session close and isLive methods

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.testing.jcr-mock-1.1.10
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-jcr-mock.git

commit 07d129d17c3191dca2e9a223acb727ee89001fbf
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Thu Jun 11 15:37:25 2015 +0000

    SLING-4802 JcrMock: Support Session close and isLive methods
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/jcr-mock@1684923 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/sling/testing/mock/jcr/MockSession.java | 47 +++++++++++++++++-----
 .../sling/testing/mock/jcr/MockSessionTest.java    |  7 ++++
 2 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java b/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java
index 41b7db2..45e050d 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java
@@ -55,6 +55,7 @@ class MockSession implements Session {
     private final Workspace workspace;
     private final Map<String, ItemData> items;
     private final String userId;
+    private boolean isLive;
 
     public MockSession(MockRepository repository, Map<String,ItemData> items,
             String userId, String workspaceName) {
@@ -62,15 +63,24 @@ class MockSession implements Session {
         this.workspace = new MockWorkspace(repository, this, workspaceName);
         this.items = items;
         this.userId = userId;
+        isLive = true;
+    }
+    
+    private void checkLive() throws RepositoryException {
+        if (!isLive) {
+            throw new RepositoryException("Session is logged out / not live.");
+        }
     }
 
     @Override
     public ValueFactory getValueFactory() throws RepositoryException {
+        checkLive();
         return ValueFactoryImpl.getInstance();
     }
 
     @Override
     public Item getItem(final String absPath) throws RepositoryException {
+        checkLive();
         final ItemData itemData = getItemData(absPath);
         if (itemData != null) {
             if (itemData.isNode()) {
@@ -86,6 +96,7 @@ class MockSession implements Session {
 
     @Override
     public Node getNode(final String absPath) throws RepositoryException {
+        checkLive();
         Item item = getItem(absPath);
         if (item instanceof Node) {
             return (Node) item;
@@ -96,6 +107,7 @@ class MockSession implements Session {
 
     @Override
     public Node getNodeByIdentifier(final String id) throws RepositoryException {
+        checkLive();
         for (ItemData item : this.items.values()) {
             if (item.isNode() && StringUtils.equals(item.getUuid(), id)) {
                 return new MockNode(item, this);
@@ -106,6 +118,7 @@ class MockSession implements Session {
 
     @Override
     public Property getProperty(final String absPath) throws RepositoryException {
+        checkLive();
         Item item = getItem(absPath);
         if (item instanceof Property) {
             return (Property) item;
@@ -116,26 +129,31 @@ class MockSession implements Session {
 
     @Override
     public boolean nodeExists(final String absPath) throws RepositoryException {
+        checkLive();
         return itemExists(absPath) && getItemData(absPath).isNode();
     }
 
     @Override
     public boolean propertyExists(final String absPath) throws RepositoryException {
+        checkLive();
         return itemExists(absPath) && getItemData(absPath).isProperty();
     }
 
     @Override
     public void removeItem(final String absPath) throws RepositoryException {
+        checkLive();
         removeItemWithChildren(absPath);
     }
 
     @Override
     public Node getRootNode() throws RepositoryException {
+        checkLive();
         return getNode("/");
     }
 
     @Override
     public Node getNodeByUUID(final String uuid) throws RepositoryException {
+        checkLive();
         return getNodeByIdentifier(uuid);
     }
 
@@ -194,11 +212,13 @@ class MockSession implements Session {
 
     @Override
     public boolean hasPendingChanges() throws RepositoryException {
+        checkLive();
         return false;
     }
 
     @Override
     public boolean itemExists(final String absPath) throws RepositoryException {
+        checkLive();
         return getItemData(absPath) != null;
     }
 
@@ -214,21 +234,25 @@ class MockSession implements Session {
 
     @Override
     public String getNamespacePrefix(final String uri) throws RepositoryException {
+        checkLive();
         return getWorkspace().getNamespaceRegistry().getPrefix(uri);
     }
 
     @Override
     public String[] getNamespacePrefixes() throws RepositoryException {
+        checkLive();
         return getWorkspace().getNamespaceRegistry().getPrefixes();
     }
 
     @Override
     public String getNamespaceURI(final String prefix) throws RepositoryException {
+        checkLive();
         return getWorkspace().getNamespaceRegistry().getURI(prefix);
     }
 
     @Override
     public void setNamespacePrefix(final String prefix, final String uri) throws RepositoryException {
+        checkLive();
         getWorkspace().getNamespaceRegistry().registerNamespace(prefix, uri);
     }
 
@@ -239,6 +263,7 @@ class MockSession implements Session {
 
     @Override
     public void save() throws RepositoryException {
+        checkLive();
         // reset new flags
         for (ItemData itemData : this.items.values()) {
             itemData.setIsNew(false);
@@ -248,13 +273,25 @@ class MockSession implements Session {
     @Override
     public void refresh(final boolean keepChanges) throws RepositoryException {
         // do nothing
+        checkLive();
     }
 
     @Override
     public void checkPermission(final String absPath, final String actions) throws RepositoryException {
         // always grant permission
+        checkLive();
+    }
+
+    @Override
+    public boolean isLive() {
+        return isLive;
     }
 
+    @Override
+    public void logout() {
+        isLive = false;
+    }
+    
     // --- unsupported operations ---
     @Override
     public void addLockToken(final String lt) {
@@ -316,16 +353,6 @@ class MockSession implements Session {
     }
 
     @Override
-    public boolean isLive() {
-        throw new UnsupportedOperationException();
-    }
-
-    @Override
-    public void logout() {
-        throw new UnsupportedOperationException();
-    }
-
-    @Override
     public void move(final String srcAbsPath, final String destAbsPath) throws RepositoryException {
         throw new UnsupportedOperationException();
     }
diff --git a/src/test/java/org/apache/sling/testing/mock/jcr/MockSessionTest.java b/src/test/java/org/apache/sling/testing/mock/jcr/MockSessionTest.java
index 51068d7..9a08aaf 100644
--- a/src/test/java/org/apache/sling/testing/mock/jcr/MockSessionTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/jcr/MockSessionTest.java
@@ -259,5 +259,12 @@ public class MockSessionTest {
         assertFalse(node.isNew());
         assertFalse(property.isNew());
     }
+
+    @Test
+    public void testLogout() throws Exception {
+        assertTrue(session.isLive());
+        session.logout();
+        assertFalse(session.isLive());
+    }
     
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.