@Test public void testLogAllProperties() { ch.qos.logback.classic.Logger utilLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(InitializationUtil.class); @SuppressWarnings("unchecked") Appender<ILoggingEvent> mockAppender = mock(Appender.class); when(mockAppender.getName()).thenReturn("MOCK"); utilLogger.addAppender(mockAppender); String nonSecretValue = "test value"; String secretValue = "secret! secret!"; System.setProperty("sparwings.test", nonSecretValue); System.setProperty("sparwings.test.password", secretValue); System.setProperty("sparwings.test.secret", secretValue); InitializationUtil.logAllProperties(); ArgumentCaptor<LoggingEvent> acptor = ArgumentCaptor.forClass(LoggingEvent.class); verify(mockAppender, atLeastOnce()).doAppend(acptor.capture()); List<LoggingEvent> logEvents = acptor.getAllValues(); assertThat(logEvents.stream().map(LoggingEvent::getFormattedMessage) .anyMatch(m -> m.startsWith("HOME = /")), is(true)); assertThat(logEvents.stream().map(LoggingEvent::getFormattedMessage) .anyMatch(m -> m.startsWith("PWD = ")), is(true)); assertThat(logEvents.stream().map(LoggingEvent::getFormattedMessage) .anyMatch(m -> m.startsWith("TERM = ")), is(true)); assertThat(logEvents.stream().map(LoggingEvent::getFormattedMessage) .anyMatch(m -> m.startsWith("java.vm.name = ")), is(true)); assertThat(logEvents.stream().map(LoggingEvent::getFormattedMessage) .anyMatch(m -> m.startsWith("java.home = ")), is(true)); assertThat(logEvents.stream().map(LoggingEvent::getFormattedMessage) .anyMatch(m -> m.startsWith("user.home = ")), is(true)); assertThat(logEvents.stream().map(LoggingEvent::getFormattedMessage) .anyMatch(m -> m.equals("sparwings.test = " + nonSecretValue)), is(true)); assertThat(logEvents.stream().map(LoggingEvent::getFormattedMessage) .anyMatch(m -> m.equals("sparwings.test.password = " + InitializationUtil.MASK_STRING)), is(true)); assertThat(logEvents.stream().map(LoggingEvent::getFormattedMessage) .anyMatch(m -> m.equals("sparwings.test.secret = " + InitializationUtil.MASK_STRING)), is(true)); assertThat(logEvents.stream().map(LoggingEvent::getFormattedMessage) .anyMatch(m -> m.contains(secretValue)), is(false)); System.clearProperty("sparwings.test"); }
public static void logAllProperties() { log.info("======== Environment Variables ========"); try { System.getenv().entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getKey)) .map(InitializationUtil::toLogMessage) .forEach(log::info); } catch (Exception e) { log.info("unexpected", e); } log.info("======== System Properties ========"); try { System.getProperties().entrySet().stream() .collect(Collectors.toMap(InitializationUtil::toStringKey, Map.Entry::getValue)).entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getKey)) .map(InitializationUtil::toLogMessage) .forEach(log::info); } catch (Exception e) { log.info("unexpected", e); } log.info("==================================="); }
InitializationUtil { public static void logAllProperties() { log.info("======== Environment Variables ========"); try { System.getenv().entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getKey)) .map(InitializationUtil::toLogMessage) .forEach(log::info); } catch (Exception e) { log.info("unexpected", e); } log.info("======== System Properties ========"); try { System.getProperties().entrySet().stream() .collect(Collectors.toMap(InitializationUtil::toStringKey, Map.Entry::getValue)).entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getKey)) .map(InitializationUtil::toLogMessage) .forEach(log::info); } catch (Exception e) { log.info("unexpected", e); } log.info("==================================="); } }
InitializationUtil { public static void logAllProperties() { log.info("======== Environment Variables ========"); try { System.getenv().entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getKey)) .map(InitializationUtil::toLogMessage) .forEach(log::info); } catch (Exception e) { log.info("unexpected", e); } log.info("======== System Properties ========"); try { System.getProperties().entrySet().stream() .collect(Collectors.toMap(InitializationUtil::toStringKey, Map.Entry::getValue)).entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getKey)) .map(InitializationUtil::toLogMessage) .forEach(log::info); } catch (Exception e) { log.info("unexpected", e); } log.info("==================================="); }  }
InitializationUtil { public static void logAllProperties() { log.info("======== Environment Variables ========"); try { System.getenv().entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getKey)) .map(InitializationUtil::toLogMessage) .forEach(log::info); } catch (Exception e) { log.info("unexpected", e); } log.info("======== System Properties ========"); try { System.getProperties().entrySet().stream() .collect(Collectors.toMap(InitializationUtil::toStringKey, Map.Entry::getValue)).entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getKey)) .map(InitializationUtil::toLogMessage) .forEach(log::info); } catch (Exception e) { log.info("unexpected", e); } log.info("==================================="); }  static void logAllProperties(); static void logEnvironmentlProperties(Environment env); static void validateExistRequiredSystemProperties(Collection<String> requiredSystemProperties); }
InitializationUtil { public static void logAllProperties() { log.info("======== Environment Variables ========"); try { System.getenv().entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getKey)) .map(InitializationUtil::toLogMessage) .forEach(log::info); } catch (Exception e) { log.info("unexpected", e); } log.info("======== System Properties ========"); try { System.getProperties().entrySet().stream() .collect(Collectors.toMap(InitializationUtil::toStringKey, Map.Entry::getValue)).entrySet().stream() .sorted(Comparator.comparing(Map.Entry::getKey)) .map(InitializationUtil::toLogMessage) .forEach(log::info); } catch (Exception e) { log.info("unexpected", e); } log.info("==================================="); }  static void logAllProperties(); static void logEnvironmentlProperties(Environment env); static void validateExistRequiredSystemProperties(Collection<String> requiredSystemProperties);  }