MuxLockManager implements LockManager { @Override public CloseLock lockOrFail(Path path) throws IOException { try (Owner<MuxLock, IOException> lockOwner = own(new MuxLock(), IOException.class)) { for (LockManager lockManager : lockManagers) { CloseLock lock = lockManager.lockOrFail(path); lockOwner.borrow().addLock(lock); } return lockOwner.release(); } } MuxLockManager(LockManager... lockManagers); private  MuxLockManager(List<LockManager> lockManagers); @Override CloseLock lockOrFail(Path path);  }
@Test public void multiLock() throws Exception { when(lockManager1.lockOrFail(path)).thenReturn(lock1); when(lockManager2.lockOrFail(path)).thenReturn(lock2); MuxLockManager lockManager = new MuxLockManager(lockManager1, lockManager2); try (CloseLock lock = lockManager.lockOrFail(path)) { assertNotNull(lock); } InOrder inOrder = inOrder(lockManager1, lockManager2, lock1, lock2); inOrder.verify(lockManager1).lockOrFail(path); inOrder.verify(lockManager2).lockOrFail(path); inOrder.verify(lock2).close(); inOrder.verify(lock1).close(); }
@Test @SuppressWarnings("try") public void lockFailure() throws Exception { when(lockManager1.lockOrFail(path)).thenReturn(lock1); when(lockManager2.lockOrFail(path)).thenThrow(IOException.class); MuxLockManager lockManager = new MuxLockManager(lockManager1, lockManager2); try (CloseLock lock = lockManager.lockOrFail(path)) { fail("Lock acquisition should have failed"); } catch (IOException e) { } InOrder inOrder = inOrder(lockManager1, lockManager2, lock1); inOrder.verify(lockManager1).lockOrFail(path); inOrder.verify(lockManager2).lockOrFail(path); inOrder.verify(lock1).close(); }