Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Commit

Permalink
Use "unknown" product version instead of throwing exception (#14)
Browse files Browse the repository at this point in the history
* Use "unknown" product version instead of throwing execption

In the case where the '.properties' file can't be found, we should use
"unknown" instead of throwing an exception and making callers catch it.

Addresses #13.

* Fix test for non-existent properties file
  • Loading branch information
andybradshaw authored and zoubeiri committed Jul 11, 2016
1 parent 22213c9 commit a9d39c1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
* Bundle to read product version from a file and expose it as a resource.
Expand All @@ -24,6 +27,7 @@
*/
public final class VersionInfoBundle implements Bundle {

private static final Logger LOGGER = LoggerFactory.getLogger(VersionInfoBundle.class);
private static final String DEFAULT_PATH = "version.properties";
private static final String UNKNOWN = "unknown";

Expand Down Expand Up @@ -53,15 +57,13 @@ public void run(Environment environment) {

public static String readVersion(String resourcePath) {
Properties properties = new Properties();
String result;
try {
URL url = Resources.getResource(resourcePath);
InputStream versionProperties = Resources.asByteSource(url).openStream();
properties.load(versionProperties);
result = properties.getProperty("productVersion", UNKNOWN);
} catch (IOException | IllegalArgumentException e) {
throw new RuntimeException("Could not read properties file '" + resourcePath + "'.", e);
LOGGER.warn("Could not read properties file '" + resourcePath + "'.", e);
}
return result;
return properties.getProperty("productVersion", UNKNOWN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package com.palantir.versioninfo;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
Expand All @@ -21,7 +19,6 @@
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import org.hamcrest.core.IsInstanceOf;
import org.junit.ClassRule;
import org.junit.Test;

Expand All @@ -46,13 +43,8 @@ public void testAddsVersionInfoResource() {

@Test
public void testVersionPropertiesFileDoesNotExist() {
try {
new VersionInfoBundle("filedoesntexist.properties");
fail();
} catch (RuntimeException e) {
assertThat(e.getCause(), IsInstanceOf.instanceOf(IllegalArgumentException.class));
assertEquals(e.getMessage(), "Could not read properties file 'filedoesntexist.properties'.");
}
String versionInfo = VersionInfoBundle.readVersion("filedoesntexist.properties");
assertEquals(versionInfo, "unknown");
}

@Test
Expand Down

0 comments on commit a9d39c1

Please sign in to comment.