001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.kernel.impl.lock;
007
008import static org.junit.Assert.assertEquals;
009import static org.junit.Assert.assertThrows;
010import static org.junit.Assert.assertTrue;
011
012import java.util.UUID;
013
014import org.fcrepo.kernel.api.identifiers.FedoraId;
015import org.fcrepo.kernel.api.lock.ResourceLock;
016import org.fcrepo.kernel.api.lock.ResourceLockType;
017
018import org.junit.Before;
019import org.junit.Test;
020
021/**
022 * Test ResourceLock
023 * @author whikloj
024 */
025public class ResourceLockImplTest {
026
027    private String txId;
028    private FedoraId resourceId;
029
030    @Before
031    public void setUp() {
032        txId = "tx" + UUID.randomUUID();
033        resourceId = FedoraId.create(UUID.randomUUID().toString());
034    }
035
036    @Test
037    public void testExclusiveLockString() {
038        final ResourceLockImpl newLock = new ResourceLockImpl("exclusive", txId, resourceId);
039        doLockChecks(newLock, ResourceLockType.EXCLUSIVE);
040    }
041
042    @Test
043    public void testExclusiveLock() {
044        final ResourceLockImpl newLock = new ResourceLockImpl(ResourceLockType.EXCLUSIVE, txId, resourceId);
045        doLockChecks(newLock, ResourceLockType.EXCLUSIVE);
046    }
047
048    @Test
049    public void testNonExclusiveLockString() {
050        final ResourceLockImpl newLock = new ResourceLockImpl("non-exclusive", txId, resourceId);
051        doLockChecks(newLock, ResourceLockType.NONEXCLUSIVE);
052    }
053
054    @Test
055    public void testNonExclusiveLock() {
056        final ResourceLockImpl newLock = new ResourceLockImpl(ResourceLockType.NONEXCLUSIVE, txId, resourceId);
057        doLockChecks(newLock, ResourceLockType.NONEXCLUSIVE);
058    }
059
060    @Test
061    public void testInvalidLockType() {
062        assertThrows(IllegalArgumentException.class, () -> new ResourceLockImpl("create", txId, resourceId));
063    }
064
065    private void doLockChecks(final ResourceLock newLock, final ResourceLockType expectedType) {
066        assertEquals(expectedType, newLock.getLockType());
067        assertEquals(txId, newLock.getTransactionId());
068        assertEquals(resourceId, newLock.getResourceId());
069        assertTrue(newLock.hasLockType(expectedType));
070        assertTrue(newLock.hasResource(resourceId));
071    }
072}