### Question:
UUIDGeneratorImpl implements UUIDGenerator { @Override public String getNextUUID() { return randomUUID(); }  @Override String getNextUUID(); @Override String[] validateUuid(String uuid); static final String DEFAULT_HOSTNAME; }### Answer:
@Test public void testRequestIdsShouldNotBeEqual() throws Exception { final String uuid1 = new UUIDGeneratorImpl().getNextUUID(); final String uuid2 = new UUIDGeneratorImpl().getNextUUID(); assertFalse("Uuids should not be equal", uuid1.equals(uuid2)); }
@Test public void testRequestIdShouldContainFirstPartOfHostname() throws Exception { final String uuid = new UUIDGeneratorImpl().getNextUUID(); String host; try { host = InetAddress.getLocalHost().getHostName(); Pattern HOST_FINDER = Pattern.compile("([\\w\\d\\-]{1,30}+).*", Pattern.CASE_INSENSITIVE); Matcher m = HOST_FINDER.matcher(host); if (m.matches()) { host = m.group(1); } else { host = UUIDGeneratorImpl.DEFAULT_HOSTNAME; } } catch (UnknownHostException e) { host = UUIDGeneratorImpl.DEFAULT_HOSTNAME; } assertTrue("Request id should contain first part of hostname "+host, uuid.contains(host)); }
@Test public void testRequestIdShouldContainTimestamp() throws Exception { final String uuid = new UUIDGeneratorImpl().getNextUUID(); final Pattern pattern = Pattern.compile("-\\d{8}-"); assertTrue("Request id should contain timestamp",pattern.matcher(uuid).find()); }
@Test public void testRequestIdShouldContainCounter() throws Exception { final String uuid = new UUIDGeneratorImpl().getNextUUID(); final Pattern pattern = Pattern.compile("-(\\d|[a-f]){10}"); assertTrue("Request id should contain counter",pattern.matcher(uuid).find()); }
@Test public void testRequestIdShouldHaveCorrectFormat() throws Exception { final String uuid = new UUIDGeneratorImpl().getNextUUID(); final Pattern pattern = Pattern.compile("\\S*-\\d{8}-(\\d|[a-f]){10}"); assertTrue("Request id should have correct format",pattern.matcher(uuid).matches()); }
@Test public void testLengthAlwaysEqual() throws Exception { Field f = UUIDGeneratorImpl.class.getDeclaredField("count"); f.setAccessible(true); AtomicLong c = (AtomicLong)f.get(null); c.set(0); int referenceLength = new UUIDGeneratorImpl().getNextUUID().length(); int numUUIDsCreated = 0; long lastVal = -1; while (true) { UUIDGenerator uuid = new UUIDGeneratorImpl(); ++numUUIDsCreated; assertTrue("Bad uuid: "+ uuid , uuid.getNextUUID().length() == referenceLength); c.set(c.get() * 2); long thisVal = Long.parseLong(uuid.getNextUUID().substring(uuid.getNextUUID().lastIndexOf("-")+1), 16); if (thisVal < lastVal) { if (c.get() < 0xFFFFFFFFFFL) { Assert.fail("Cycled after "+numUUIDsCreated+" uuids. lastVal was "+lastVal+", thisVal is "+thisVal); } else { break; } } else { lastVal = thisVal; } } System.out.println("Length tested OK after "+ numUUIDsCreated+ "iterations"); }