DefaultModelStorage implements ModelStorage { static void synchronize(Runnable task, Lock... locks) { synchronize(() -> { task.run(); return null; }, locks); } DefaultModelStorage(ModelStorageProvider storageProvider); @Override void putFile(String path, byte[] data, boolean onlyIfNotExist); @Override byte[] getFile(String path); @Override void mkdir(String path, boolean onlyIfNotExist); @Override void mkdirs(String path); @Override Set<String> listFiles(String path); @Override void remove(String path); @Override boolean exists(String path); @Override boolean isDirectory(String path); @Override boolean isFile(String path); @Override FileStat getFileStat(String path); @Override T lockPaths(Supplier<T> supplier, String... paths);  }
@Test public void testSynchronize() { Lock[] locks = new Lock[10]; for (int i = 0; i < locks.length; i++) locks[i] = mock(Lock.class); DefaultModelStorage.synchronize(() -> {}, locks); for (Lock lock : locks) { verify(lock, times(1)).lock(); verify(lock, times(1)).unlock(); verifyNoMoreInteractions(lock); } }
@Test public void testSynchronizeWithExceptionInTask() { Lock[] locks = new Lock[10]; for (int i = 0; i < locks.length; i++) locks[i] = mock(Lock.class); RuntimeException ex = new RuntimeException(); try { DefaultModelStorage.synchronize(() -> { throw ex; }, locks); fail(); } catch (RuntimeException e) { assertEquals(ex, e); } for (Lock lock : locks) { verify(lock, times(1)).lock(); verify(lock, times(1)).unlock(); verifyNoMoreInteractions(lock); } }
@Test public void testSynchronizeWithExceptionInLock() { Lock[] locks = new Lock[10]; for (int i = 0; i < locks.length; i++) locks[i] = mock(Lock.class); RuntimeException ex = new RuntimeException(); doThrow(ex).when(locks[5]).lock(); try { DefaultModelStorage.synchronize(() -> {}, locks); fail(); } catch (RuntimeException e) { assertEquals(ex, e); } for (int i = 0; i < locks.length; i++) { if (i <= 4) { verify(locks[i], times(1)).lock(); verify(locks[i], times(1)).unlock(); } else if (i > 5) { verify(locks[i], times(0)).lock(); verify(locks[i], times(0)).unlock(); } else { verify(locks[i], times(1)).lock(); verify(locks[i], times(0)).unlock(); } verifyNoMoreInteractions(locks[i]); } }
@Test public void testSynchronizeWithExceptionInUnlock() { Lock[] locks = new Lock[10]; for (int i = 0; i < locks.length; i++) locks[i] = mock(Lock.class); RuntimeException ex = new RuntimeException(); doThrow(ex).when(locks[5]).unlock(); try { DefaultModelStorage.synchronize(() -> {}, locks); fail(); } catch (RuntimeException e) { assertEquals(ex, e); } for (Lock lock : locks) { verify(lock, times(1)).lock(); verify(lock, times(1)).unlock(); verifyNoMoreInteractions(lock); } }