Skip to content

Commit

Permalink
AuthDBProperties - refactor properties, add logging and 100% code cov…
Browse files Browse the repository at this point in the history
…erage.
  • Loading branch information
dazza-codes committed Feb 16, 2017
1 parent b037f4c commit 3630859
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.logging.log4j.Logger;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -24,7 +25,7 @@ class AuthDBProperties {
private String userPass = null;

public AuthDBProperties() throws IOException {
Properties properties = loadPropertyResource();
Properties properties = loadPropertyResource(PROPERTY_RESOURCE);
initDataSourceProperties(properties);
}

Expand Down Expand Up @@ -86,13 +87,16 @@ private Properties loadPropertyFile(String propertyFile) throws IOException {
}
}

private Properties loadPropertyResource() throws IOException {
private Properties loadPropertyResource(String resourceName) throws IOException {
try {
Class cls = AuthDBProperties.class;
InputStream iStream = cls.getResourceAsStream(PROPERTY_RESOURCE);
InputStream iStream = AuthDBProperties.class.getResourceAsStream(resourceName);
if (iStream == null) {
// cls.getResourceAsStream() does not throw an IOException when the resource is missing.
throw( new FileNotFoundException("Failed to find property resource:" + resourceName) );
}
return loadProperties(iStream);
} catch (IOException e) {
log.fatal("Failed to load property resource:" + PROPERTY_RESOURCE, e);
log.fatal("Failed to load property resource:" + resourceName, e);
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,32 @@

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Properties;

import static java.nio.file.Files.createTempDirectory;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;

import static org.mockito.Matchers.any;
import static org.powermock.api.support.membermodification.MemberMatcher.method;

public class AuthDBPropertiesTest {

Expand All @@ -28,7 +38,6 @@ public class AuthDBPropertiesTest {
Packaged code config: src/main/resources/server.conf
*/

private AuthDBProperties authProps;
private Properties serverConf;
private String serverConfResourceName = "/server.conf";
private File serverConfFile;
Expand All @@ -37,7 +46,11 @@ public class AuthDBPropertiesTest {
@Before
public void setUp() throws IOException {
tmpDir = createTempDirectory("AuthDBPropertiesTest_").toFile();
authProps = new AuthDBProperties();
}

@After
public void tearDown() throws IOException {
FileUtils.deleteDirectory(tmpDir);
}

public void setServerConfFile() {
Expand All @@ -54,14 +67,10 @@ public void setServerConf() throws IOException {
iStream.close();
}

@After
public void tearDown() throws IOException {
FileUtils.deleteDirectory(tmpDir);
}

@Test
public void testConstructor() throws IOException {
setServerConf();
AuthDBProperties authProps = new AuthDBProperties();
assertEquals(serverConf.getProperty("USER"), authProps.getUserName());
assertEquals(serverConf.getProperty("PASS"), authProps.getUserPass());
assertEquals(serverConf.getProperty("SERVER"), authProps.getServer());
Expand All @@ -85,6 +94,7 @@ public void testConstructorWithString() throws IOException {
public void testServer() throws Exception {
// Same code is used to test setter/getter
String server = "test.server.org";
AuthDBProperties authProps = new AuthDBProperties();
authProps.setServer(server);
assertEquals(server, authProps.getServer());
}
Expand All @@ -93,6 +103,7 @@ public void testServer() throws Exception {
public void testService() throws Exception {
// Same code is used to test setter/getter
String service = "service_name";
AuthDBProperties authProps = new AuthDBProperties();
authProps.setService(service);
assertEquals(service, authProps.getService());
}
Expand All @@ -101,6 +112,7 @@ public void testService() throws Exception {
public void testUserName() throws Exception {
// Same code is used to test setter/getter
String userName = "user_name";
AuthDBProperties authProps = new AuthDBProperties();
authProps.setUserName(userName);
assertEquals(userName, authProps.getUserName());
}
Expand All @@ -109,12 +121,14 @@ public void testUserName() throws Exception {
public void testUserPass() throws Exception {
// Same code is used to test setter/getter
String userPass = "user_pass";
AuthDBProperties authProps = new AuthDBProperties();
authProps.setUserPass(userPass);
assertEquals(userPass, authProps.getUserPass());
}

@Test
public void testURL() throws Exception {
AuthDBProperties authProps = new AuthDBProperties();
String url = authProps.getURL();
assertNotNull(url);
assertThat(url, containsString("jdbc:oracle:thin:@"));
Expand All @@ -129,5 +143,18 @@ public void testFailureLoadingProperties() throws IOException {
new AuthDBProperties(customConfigFile);
}

@Test (expected = FileNotFoundException.class)
public void testFailureLoadingPropertyResource() throws Throwable {
try {
// Call private method to load a properties resource that does not exist.
AuthDBProperties authProps = new AuthDBProperties();
Method m = AuthDBProperties.class.getDeclaredMethod("loadPropertyResource", String.class);
m.setAccessible(true);
m.invoke(authProps, "/missing.conf");
} catch(InvocationTargetException e) {
// ignore the java reflection exception, throw it's cause
throw(e.getTargetException());
}
}
}

0 comments on commit 3630859

Please sign in to comment.