website=http://www.leveluplunch.comlanguage=Englishmessage=Welcome up to leveluplunch.com
Straight up Java
@Testpublicvoidread_properties_file_java()throwsFileNotFoundException,IOException{Propertiesproperties=newProperties();properties.load(this.getClass().getClassLoader().getResourceAsStream(PROPERTY_FILE_NAME));logger.info(properties);assertEquals("http://www.leveluplunch.com",properties.getProperty("website"));assertEquals("English",properties.getProperty("language"));assertEquals("Welcome up to leveluplunch.com",properties.getProperty("message"));}
Google Guava
@Testpublicvoidread_properties_file_guava()throwsIOException{URLurl=Resources.getResource(PROPERTY_FILE_NAME);InputSupplier<InputStream>inputSupplier=Resources.newInputStreamSupplier(url);Propertiesproperties=newProperties();properties.load(inputSupplier.getInput());logger.info(properties);assertEquals("http://www.leveluplunch.com",properties.getProperty("website"));assertEquals("English",properties.getProperty("language"));assertEquals("Welcome up to leveluplunch.com",properties.getProperty("message"));}
Apache Commons
@Testpublicvoidread_properties_file_apache_commons()throwsConfigurationException{PropertiesConfigurationproperties=newPropertiesConfiguration(PROPERTY_FILE_NAME);logger.info(properties);assertEquals("http://www.leveluplunch.com",properties.getProperty("website"));assertEquals("English",properties.getProperty("language"));assertEquals("Welcome up to leveluplunch.com",properties.getProperty("message"));}
Spring Framework
/**
* This is just one way to get properties in the springframework and
* could be based on environment, xml configuration, java config, etc.
*
* @see PropertyPlaceholderConfigurer
* @see PropertySourcesPlaceholderConfigurer
* @throws IOException
*/@Testpublicvoidread_properties_file_springframework()throwsIOException{Resourceresource=newClassPathResource(PROPERTY_FILE_NAME);Propertiesproperties=PropertiesLoaderUtils.loadProperties(resource);logger.info(properties);assertEquals("http://www.leveluplunch.com",properties.getProperty("website"));assertEquals("English",properties.getProperty("language"));assertEquals("Welcome up to leveluplunch.com",properties.getProperty("message"));}