Skip to content
Permalink
Browse files
Merge pull request #485 from joerghoh/improvement/OAK-9612-no-runtime…
…-exceptions

OAK-9612 Convert ReadOnlyBuilder's exceptions into RepositoryExcept…
  • Loading branch information
anchela committed Feb 15, 2022
2 parents ef9daef + 00aece1 commit 25f57f152785897fcd0349d2430a36786547fa3a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
@@ -59,6 +59,7 @@
import org.apache.jackrabbit.oak.spi.security.authorization.AuthorizationConfiguration;
import org.apache.jackrabbit.oak.spi.security.authorization.permission.PermissionAware;
import org.apache.jackrabbit.oak.spi.security.authorization.permission.PermissionProvider;
import org.apache.jackrabbit.oak.spi.state.ReadyOnlyBuilderException;
import org.apache.jackrabbit.oak.stats.Clock;
import org.apache.jackrabbit.oak.stats.StatisticManager;
import org.apache.jackrabbit.oak.stats.MeterStats;
@@ -212,6 +213,8 @@ public <T> T perform(@NotNull SessionOperation<T> sessionOperation) throws Repos
} finally {
postPerform(sessionOperation, t0);
}
} catch (ReadyOnlyBuilderException e) {
throw new ConstraintViolationException(e);
} finally {
lock.unlock();
}
@@ -247,6 +250,8 @@ public <T> T performNullable(@NotNull SessionOperation<T> sessionOperation) thro
} finally {
postPerform(sessionOperation, t0);
}
} catch (ReadyOnlyBuilderException e) {
throw new ConstraintViolationException(e);
} finally {
lock.unlock();
}
@@ -277,6 +282,8 @@ public void performVoid(SessionOperation<Void> sessionOperation) throws Reposito
} finally {
postPerform(sessionOperation, t0);
}
} catch (ReadyOnlyBuilderException e) {
throw new ConstraintViolationException(e);
} finally {
lock.unlock();
}
@@ -17,14 +17,21 @@
package org.apache.jackrabbit.oak.jcr.delegate;

import org.apache.jackrabbit.oak.api.Root;
import org.apache.jackrabbit.oak.jcr.session.operation.SessionOperation;
import org.apache.jackrabbit.oak.spi.security.authorization.permission.PermissionAware;
import org.apache.jackrabbit.oak.spi.security.authorization.permission.PermissionProvider;
import org.apache.jackrabbit.oak.spi.state.ReadyOnlyBuilderException;
import org.junit.Test;

import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import javax.jcr.RepositoryException;
import javax.jcr.nodetype.ConstraintViolationException;

public class SessionDelegateTest extends AbstractDelegatorTest {

@@ -68,4 +75,38 @@ public void testRefreshUnaware() {
delegate.refresh(false);
verify(pp, times(6)).refresh();
}

@Test(expected = ConstraintViolationException.class)
public void testModificationOnReadonlyBuilder() throws RepositoryException {
PermissionProvider pp = mock(PermissionProvider.class);
Root r = mockRoot(pp, false);
SessionDelegate delegate = mockSessionDelegate(r, pp);
@SuppressWarnings("unchecked")
SessionOperation<Long> op = (SessionOperation<Long>) mock(SessionOperation.class);
when(op.perform()).thenThrow(new ReadyOnlyBuilderException("readonly builder"));
delegate.perform(op);
}

@Test(expected = ConstraintViolationException.class)
public void testModificationOnReadonlyBuilder_nullable() throws RepositoryException {
PermissionProvider pp = mock(PermissionProvider.class);
Root r = mockRoot(pp, false);
SessionDelegate delegate = mockSessionDelegate(r, pp);
@SuppressWarnings("unchecked")
SessionOperation<Long> op = (SessionOperation<Long>) mock(SessionOperation.class);
when(op.performNullable()).thenThrow(new ReadyOnlyBuilderException("readonly builder"));
delegate.performNullable(op);
}

@Test(expected = ConstraintViolationException.class)
public void testModificationOnReadonlyBuilder_void() throws RepositoryException {
PermissionProvider pp = mock(PermissionProvider.class);
Root r = mockRoot(pp, false);
SessionDelegate delegate = mockSessionDelegate(r, pp);
@SuppressWarnings("unchecked")
SessionOperation<Void> op = (SessionOperation<Void>) mock(SessionOperation.class);
doThrow(new ReadyOnlyBuilderException("readonly builder")).when(op).performVoid();
delegate.performVoid(op);
}

}
@@ -142,7 +142,7 @@ public static void checkLibsIsReadOnly(Persistence p) throws RepositoryException
try {
libsNode.addNode("illegal");
Assert.fail();
} catch (UnsupportedOperationException e) {
} catch (RepositoryException e) {
// expected
}
}
@@ -41,7 +41,7 @@ public ReadOnlyBuilder(@NotNull NodeState state) {
}

protected RuntimeException unsupported() {
return new UnsupportedOperationException("This builder is read-only.");
return new ReadyOnlyBuilderException("This builder is read-only.");
}

@Override
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.jackrabbit.oak.spi.state;

/**
* Indicates that a modification operation was tried to execute on a read-only builder.
* It should be used instead of throwing plain UnsupportedOperationExceptions in that situation.
*/
public class ReadyOnlyBuilderException extends UnsupportedOperationException {

public ReadyOnlyBuilderException (String reason) {
super(reason);
}
}

0 comments on commit 25f57f1

Please sign in to comment.