PropertyFileUtils { @NotNull public static Properties loadExistingProperties(File propertiesFile) { Properties properties = new OrderedProperties(); if (propertiesFile.exists()) { try (InputStream stream = Files.newInputStream(propertiesFile.toPath())) { properties.load(stream); } catch (IOException e) { throw new PropertyFileException("Exception reading existing contents from " + propertiesFile.getName() + " file", e); } } return properties; } private  PropertyFileUtils(); @NotNull static Properties loadExistingProperties(File propertiesFile);  }
@Test void loadFileFailure(TemporaryFolder temporaryFolder) throws IOException { if (!POSIX_ENABLED) { return; } File file = new File(temporaryFolder.getRoot(), "static.properties"); FileUtils.touch(file); Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("---------")); assertThrows(PropertyFileException.class, () -> loadExistingProperties(file)); Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("rwxrwxrwx")); Files.delete(file.toPath()); }
@Test void loadNonexistingFile(TemporaryFolder temporaryFolder) { assertTrue(loadExistingProperties(new File(temporaryFolder.getRoot(), "static.properties")).isEmpty()); }
@Test void loadExistingPropertiesFromFile(TemporaryFolder temporaryFolder) throws IOException { File propertiesFile = new File(temporaryFolder.getRoot(), "static.properties"); Properties staticProps = new OrderedProperties(); staticProps.setProperty("my.name", "static value"); staticProps.setProperty("another", "another static value"); try (OutputStream outputStream = new StripFirstLineStream(new FileOutputStream(propertiesFile))) { staticProps.store(outputStream, null); } Properties properties = loadExistingProperties(propertiesFile); assertTrue(properties.containsKey("my.name")); assertTrue(properties.containsKey("another")); }