Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Properties into an InputStream

I have a method that takes an InputStream as a parameter. The method extracts Properties from the InputStream and returns the "version" property. This property holds the version of the application.

public String getVersion(InputStream inputStream) throws IOException {
    Properties properties = new Properties();
    properties.load(inputStream);
    String version = properties.getProperty("version");
    return version;
}

For testing purposes, I'd like to create a Properties object, set some properties, and then load the properties into an InputStream. Finally, the InputStream would be passed to the method under test.

@Test
public void testGetAppVersion() {
    InputStream inputStream = null;
    Properties prop = new Properties();
    prop.setProperty("version", "1.0.0.test");

    // Load properties into InputStream
}

How would I go about this?

like image 908
navig8tr Avatar asked Aug 21 '18 18:08

navig8tr


1 Answers

You can use the store method to write the properties to a stream.

Here's an example that uses byte array streams:

Properties prop = new Properties();
prop.put("version", "1.0.0.test");

ByteArrayOutputStream os = new ByteArrayOutputStream();
props.store(os, "comments");

InputStream s = new ByteArrayInputStream(os.toByteArray());

String version = getVersion(s);

But I believe the following way is simpler (input stream from string file content):

InputStream is = 
    new ByteArrayInputStream("version=1.0.0.test\n".getBytes());
like image 130
ernest_k Avatar answered Nov 15 '22 11:11

ernest_k