### Question:
DefaultLockService implements LockService { @Override public synchronized Lock borrowLock(String key) { logger.debug("BorrowLock called with key [{}]", key); ReferenceTrackingLock lock = locks.get(key); if (lock == null) { logger.debug("Existing lock not found under key [{}] - creating new lock", key); lock = new ReferenceTrackingLock(key); locks.put(key, lock); } else { logger.debug("Existing lock found under key [{}] - returning existing lock", key); } lock.incrementReferenceCount(); logger.debug("Returning lock with key [{}] - and referenceCount [{}]", lock.getKey(), lock.getReferenceCount()); return lock; }  @Override synchronized Lock borrowLock(String key); @Override synchronized Lock borrowLock(String discriminator, String id); @Override synchronized void returnLock(Lock lock);  }### Answer:
@Test public void borrowLockByKey() { Lock lock = service.borrowLock(KEY); assertThat(lock, is(notNullValue())); }
@Test public void borrowLockByDiscriminatorAndId() { Lock lock = service.borrowLock(DISCRIMINATOR, String.valueOf(ID)); assertThat(lock, is(notNullValue())); }
@Test public void verifyComputedKeyContract() { String expectedKey = DISCRIMINATOR + "-" + ID; DefaultLockService.ReferenceTrackingLock lock = (DefaultLockService.ReferenceTrackingLock) service.borrowLock(DISCRIMINATOR, String.valueOf(ID)); assertThat(lock.getKey(), is(expectedKey)); Lock secondLock = service.borrowLock(expectedKey); assertThat(lock, is(sameInstance(secondLock))); }
@Test public void borrowLockByEmptyDiscriminatorAndId() { Lock lock = service.borrowLock("", String.valueOf(ID)); assertThat(lock, is(notNullValue())); }
@Test public void borrowAndNoReturnTwiceGivesSameInstances() { Lock lock = service.borrowLock(KEY); Lock lock2 = service.borrowLock(KEY); assertThat(lock, is(CoreMatchers.sameInstance(lock2))); }