GuiceUtils { public static void bindProperties( @Nonnull final Binder binder, @Nonnull final String primaryPropertySourceUrl, @Nonnull final String... secondaryPropertySourceUrls) { assert binder != null; assert primaryPropertySourceUrl != null; assert secondaryPropertySourceUrls != null; LOG.info("bindProperties:"); LOG.info("------------------------------------------------------------------------------------"); final Properties properties = new Properties(); loadUrl(binder, properties, primaryPropertySourceUrl); for (final String url : secondaryPropertySourceUrls) { final Properties secondaryProperties = new Properties(); loadUrl(binder, secondaryProperties, url); for (final String name : secondaryProperties.stringPropertyNames()) { if (!properties.containsKey(name)) { final String msg = "Unknown property " + name + " in property file " + url + '.'; LOG.error("{}", msg); binder.addError(msg); } properties.setProperty(name, secondaryProperties.getProperty(name)); } } final String emptyString = properties.getProperty(HasProperties.PROPERTIES_EMPTY_STRING); if ((emptyString == null) || emptyString.trim().isEmpty()) { final String msg = "Missing value for special 'empty string' property " + HasProperties.PROPERTIES_EMPTY_STRING + '.'; LOG.error("{}", msg); binder.addError(msg); } LOG.info(" (In the property files, empty strings were specified as '{}' by {}.)", emptyString, HasProperties.PROPERTIES_EMPTY_STRING); expandProperties(binder, properties); for (final String name : properties.stringPropertyNames()) { final String value = properties.getProperty(name); if ((value == null) || value.trim().isEmpty()) { final String msg = "Missing value for property " + name + " (use '" + emptyString + "' for empty properties instead, defined by " + HasProperties.PROPERTIES_EMPTY_STRING + ')'; LOG.error("{}", msg); binder.addError(msg); } else if (value.equals(emptyString)) { properties.setProperty(name, ""); } else { } } final Collection<String> propertyNames = properties.stringPropertyNames(); final List<String> sortedPropertyNames = new ArrayList<>(); sortedPropertyNames.addAll(propertyNames); Collections.sort(sortedPropertyNames); int n = 1; for (final String propertyName : sortedPropertyNames) { boolean found = false; for (final String hide : PROPERTY_VALUES_TO_HIDE) { final String propertyNameLowerCase = propertyName.toLowerCase(); if (propertyNameLowerCase.startsWith(hide) || propertyNameLowerCase.endsWith(hide) || propertyNameLowerCase.contains('.' + hide) || propertyNameLowerCase.contains(hide + '.')) { found = true; break; } } final String propertyValue; if (found) { propertyValue = PROPERTY_VALUES_HIDDEN; } else { propertyValue = properties.getProperty(propertyName); } LOG.info(" {}. {} = {}", n, propertyName, propertyValue); ++n; } LOG.info("------------------------------------------------------------------------------------"); Names.bindProperties(binder, properties); } private  GuiceUtils(); static void bindProperties(
            @Nonnull final Binder binder,
            @Nonnull final String primaryPropertySourceUrl,
            @Nonnull final String... secondaryPropertySourceUrls);  }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testBindProperties() throws IOException, NamingException { LOG.info("testBindProperties"); final Binder binder = Mockito.mock(Binder.class); Mockito.when(binder.skipSources(Names.class)).thenReturn(binder); Mockito.when(binder.bind(Mockito.any(Key.class))).thenReturn(Mockito.mock(LinkedBindingBuilder.class)); final String propertyFile = "classpath:" + PROPERTY_FILE_ENVVAR; GuiceUtils.bindProperties(binder, propertyFile); Mockito.verify(binder, Mockito.times(5)).addError(Mockito.any(String.class)); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testSecretProperties() throws IOException, NamingException { LOG.info("testSecretProperties"); final Binder binder = Mockito.mock(Binder.class); Mockito.when(binder.skipSources(Names.class)).thenReturn(binder); Mockito.when(binder.bind(Mockito.any(Key.class))).thenReturn(Mockito.mock(LinkedBindingBuilder.class)); final String propertyFile = "classpath:" + PROPERTY_FILE_SECRET; GuiceUtils.bindProperties(binder, propertyFile); Mockito.verify(binder, Mockito.times(0)).addError(Mockito.any(String.class)); }