WordTemplateRepository implements TemplateRepository { @Override public <E> Template<E> lockForEdit(Template<E> aTemplate, User user) throws LockingException { PersistentTemplate unwrapped = unwrap(aTemplate); unwrapped.lock(user); unwrapped = update(unwrapped); return new WordTemplate<E>(unwrapped, contextFactory); } WordTemplateRepository();  WordTemplateRepository(EntityManager em, TemplateContextFactory contextFactory); void setFileStore(FileStore fileStore); @Override TemplateQuery queryTemplates(); @Override Template<Object> getTemplate(String documentName, String language); @Override Template<E> getTemplate(String documentName, String language,
			Class<? extends E> inputType); @Override Template<E> lockForEdit(Template<E> aTemplate, User user); @Override Template<E> unlock(Template<E> template); @Override Template<E> persist(Template<E> toPersist); @Override void delete(Template<?> template); @Transactional List<Template<Object>> execute(WordTemplateQuery query);  }
@Test @Transactional public void locking() { WordTemplate<?> template = unlockedTemplate(); User lockingUser = new User("locking", "Locking User"); Template<?> locked = repository.lockForEdit(template, lockingUser); assertEquals(lockingUser, locked.getLockingUser()); assertTrue(isVersionIncontext(locked)); }
@Test @Transactional public void lockingLockedTemplate() { WordTemplate<?> toLock = lockedTemplate(); User second = new User("second", "Second User"); try { repository.lockForEdit(toLock, second); fail(LockingException.class + " expected"); } catch (LockingException e) { assertTrue(isVersionIncontext(toLock)); } }
@Test(expected = LockingException.class) @Transactional public void lockingTemplateParallel() { Template<?> template1 = unlockedTemplate(); Template<?> template2 = unlockedTemplate(); template1 = repository.lockForEdit(template1, new User("first", "First")); User second = new User("second", "Second User"); repository.lockForEdit(template2, second); fail(LockingException.class + " expected"); }
@Test @Transactional public void lockingTwice() { Template<?> template = unlockedTemplate(); User user = new User("user", "User"); template = repository.lockForEdit(template, user); template = repository.lockForEdit(template, user); assertEquals(user, template.getLockingUser()); }