CnameGenerator { public static String generateCname() { UUID uuid = UUID.randomUUID(); long leastSignificantBits = uuid.getLeastSignificantBits(); long mostSignificantBits = uuid.getMostSignificantBits(); ByteBuffer buffer = ByteBuffer.allocate(16); buffer.putLong(leastSignificantBits).putLong(mostSignificantBits); buffer.flip(); byte[] data = new byte[12]; buffer.get(data, 0, 12); return DatatypeConverter.printBase64Binary(data); }  static String generateCname();  }
@Test(timeout = 10) public void testCnameGeneratorSpeed() { CnameGenerator.generateCname(); }
@Test public void testCnameGenerator() { int minCnameSize = 98; int capacity = 10000; List<String> cnameList = new ArrayList<String>(capacity); for (int i = 0; i < capacity; i++) { long start = System.currentTimeMillis(); String cname = CnameGenerator.generateCname(); long time = System.currentTimeMillis() - start; assertEquals((minCnameSize / 8), Base64.decode(cname).length); assertFalse(cnameList.contains(cname)); cnameList.add(cname); } cnameList.clear(); }