InstanceUUID { public static synchronized String get() { if (uuid == null) { try { File file = new File(OpenHAB.getUserDataFolder() + File.separator + UUID_FILE_NAME); if (!file.exists()) { uuid = java.util.UUID.randomUUID().toString(); writeFile(file, uuid); } else { uuid = readFirstLine(file); if (uuid != null && !uuid.isEmpty()) { LOGGER.debug("UUID '{}' has been restored from file '{}'", file.getAbsolutePath(), uuid); } else { uuid = java.util.UUID.randomUUID().toString(); LOGGER.warn("UUID file '{}' has no content, rewriting it now with '{}'", file.getAbsolutePath(), uuid); writeFile(file, uuid); } } } catch (IOException e) { LOGGER.error("Failed writing the UUID file: {}", e.getMessage()); return null; } } return uuid; }  static synchronized String get();  }
@Test public void sameUUID() { String uuid1 = InstanceUUID.get(); String uuid2 = InstanceUUID.get(); assertEquals(uuid1, uuid2); }
@Test public void readFromPersistedFile() throws IOException { InstanceUUID.uuid = null; Path path = Paths.get(OpenHAB.getUserDataFolder(), InstanceUUID.UUID_FILE_NAME); Files.createDirectories(path.getParent()); Files.write(path, "123".getBytes()); String uuid = InstanceUUID.get(); assertEquals("123", uuid); }
@Test public void ignoreEmptyFile() throws IOException { InstanceUUID.uuid = null; Path path = Paths.get(OpenHAB.getUserDataFolder(), InstanceUUID.UUID_FILE_NAME); Files.createDirectories(path.getParent()); Files.write(path, "".getBytes()); String uuid = InstanceUUID.get(); assertNotEquals("", uuid); }