### Question:
UniqueURIGenerator { String getNewURI(String type, Predicate<XURI> existsChecker) throws Exception { String random = null; boolean exists = true; while (exists) { random = buildUri(type, getRandom()); exists = existsChecker.test(new XURI(random)); } return random; } UniqueURIGenerator();   }### Answer:
@Test public void should_return_new_work_ID() throws Exception { String uri = uriGenerator.getNewURI("Work", s -> false); assertNotNull(uri); assertTrue(uri.contains("/work")); assertTrue(getLastElementOfURI(uri).matches(WORK_IDENTIFIER_REGEX)); }
@Test public void should_return_new_publication_id_even_if_first_attempt_fails() throws Exception { boolean[] existsFlag = new boolean[1]; existsFlag[0] = true; String uri = uriGenerator.getNewURI( "Publication", s -> { boolean result = existsFlag[0]; existsFlag[0] = false; return result; } ); assertNotNull(uri); assertTrue(uri.contains("/publication")); assertTrue(getLastElementOfURI(uri).matches(PUBLICATION_IDENTIFIER_REGEX)); }
@Test public void should_get_new_person_id() throws Exception { String uri = uriGenerator.getNewURI("Person", s -> false); assertNotNull(uri); assertTrue(uri.contains("/person")); assertTrue(getLastElementOfURI(uri).matches(PERSON_IDENTIFIER_REGEX)); }
@Test public void should_get_new_place_id() throws Exception { String uri = uriGenerator.getNewURI("Place", s -> false); assertNotNull(uri); assertTrue(uri.contains("/place")); assertTrue(getLastElementOfURI(uri).matches(PLACE_IDENTIFIER_REGEX)); }