Properties { public static List<Timeout> read(Path propertiesFile, List<Timeout> timeouts) { Objects.requireNonNull(propertiesFile, "The propertiesFile can't be null."); Objects.requireNonNull(timeouts, "The timeouts can't be null."); if (Files.isReadable(propertiesFile)) { try (Reader reader = Files.newBufferedReader(propertiesFile)) { java.util.Properties properties = new java.util.Properties(); properties.load(reader); for (Timeout timeout : timeouts) { String value = properties.getProperty(timeout.getKey() + ".value"); if (value != null) { try { timeout.setValue(new Integer(value)); } catch (NumberFormatException e) { LOGGER.info("The timeout with the key " + timeout.getKey() + " was not read from the properties. The timeout have to be an integer."); } } String timeToRepetition = properties.getProperty(timeout.getKey() + ".timeToRepetition"); if (timeToRepetition != null) { try { timeout.setTimeToRepetition(new Integer(timeToRepetition)); } catch (NumberFormatException e) { LOGGER.info("The timeToRepetition with the key " + timeout.getKey() + " was not read from the properties. The timeToRepetition have to be an integer."); } } } } catch (IOException e) { LOGGER.info("Couldn't read the {@link Timeout} properties."); } } else { LOGGER.info("The file " + propertiesFile.toString() + " is not readable."); } return timeouts; }  static List<Timeout> read(Path propertiesFile, List<Timeout> timeouts); static void write(Path propertiesPath, List<Timeout> timeouts);  }
@Test public void testWriteToProperties() throws Exception { ArrayList<Timeout> timeouts = new ArrayList<>(); timeouts.add(timeout); Properties.read(properties, timeouts); List<Timeout> readTimeouts = Properties.read(properties, timeouts); for (Timeout actualTimeout : readTimeouts) { assertEquals(timeout.getKey(), actualTimeout.getKey()); assertEquals(timeout.getTimeoutInMs(), actualTimeout.getTimeoutInMs()); assertEquals(timeout.getTimeToRepetitionInMs(), actualTimeout.getTimeToRepetitionInMs()); } }
@Test public void testReadFromProperties() throws Exception { ArrayList<Timeout> timeouts = new ArrayList<>(); timeouts.add(timeout); List<Timeout> readTimeouts = Properties.read(properties, timeouts); for (Timeout actualTimeout : readTimeouts) { assertEquals(timeout.getKey(), actualTimeout.getKey()); assertEquals(timeout.getTimeoutInMs(), actualTimeout.getTimeoutInMs()); assertEquals(timeout.getTimeToRepetitionInMs(), actualTimeout.getTimeToRepetitionInMs()); } }
@Test public void testReadFromPropertiesReadable() throws Exception { ArrayList<Timeout> timeouts = new ArrayList<>(); timeouts.add(timeout); properties.toFile().setReadable(false); Properties.read(properties, timeouts); assertEquals("The file " + properties.toString() + " is not readable.", testAppender.messages.get(0)); }
@Test public void testReadFromPropertiesTimeoutNumberFormat() throws Exception { ArrayList<Timeout> timeouts = new ArrayList<>(); timeouts.add(timeout); java.util.Properties timeoutProperties = System.getProperties(); FileWriter writer = new FileWriter(properties.toString()); timeoutProperties.setProperty(timeout.getKey() + ".value", "test"); timeoutProperties.setProperty(timeout.getKey() + ".timeToRepetition", "test"); timeoutProperties.store(writer, "Timeout_properties"); writer.close(); Properties.read(properties, timeouts); assertEquals("The timeout with the key " + timeout.getKey() + " was not read from the properties. The timeout have to be an integer.", testAppender.messages.get(0)); assertEquals("The timeToRepetition with the key " + timeout.getKey() + " was not read from the properties. The timeToRepetition have to be an integer.", testAppender.messages.get(1)); }