Skip to content

Commit

Permalink
#5: properties file configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
firaja committed Sep 9, 2020
1 parent cb6ec12 commit 313c43c
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 20 deletions.
121 changes: 101 additions & 20 deletions src/main/java/com/password4j/PropertyReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/
package com.password4j;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
Expand All @@ -28,32 +31,18 @@ class PropertyReader

private static final Logger LOG = LoggerFactory.getLogger(PropertyReader.class);

private static final String FILE_NAME = "psw4j.properties";
private static final String FILE_NAME = "/psw4j.properties";

private static final String CONFIGURATION_KEY = "psw4j.configuration";

private static final String MESSAGE = "{}. Default value is used ({}). Please set property {} in your " + FILE_NAME + " file.";


static final Properties PROPERTIES;
protected static Properties properties;

static
{
InputStream in = PropertyReader.class.getResourceAsStream("/" + FILE_NAME);
Properties props = new Properties();


if (in != null)
{
try
{
props.load(in);
}
catch (IOException e)
{
//
}
}

PROPERTIES = props;
init();
}

static int readInt(String key, int defaultValue, String message)
Expand Down Expand Up @@ -116,9 +105,101 @@ private static String readString(String key)
{
throw new BadParametersException("Key cannot be null");
}
return PROPERTIES.getProperty(key);
return properties.getProperty(key);
}

static void init()
{
String customPath = System.getProperty(CONFIGURATION_KEY, null);

InputStream in;
if (StringUtils.isEmpty(customPath))
{
in = getResource(FILE_NAME);
}
else
{
in = getResource(customPath);
}

Properties props = new Properties();


if (in != null)
{
try
{
props.load(in);
}
catch (IOException e)
{
//
}
}
else
{
LOG.debug("Cannot find any properties file.");
}

properties = props;
}


private static InputStream getResource(String resource)
{
ClassLoader classLoader;
InputStream in;

try
{
classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader != null)
{
in = classLoader.getResourceAsStream(resource);
if (in != null)
{
return in;
}
}


// Try with the class loader that loaded this class
classLoader = PropertyReader.class.getClassLoader();
if (classLoader != null)
{
in = classLoader.getResourceAsStream(resource);
if (in != null)
{
return in;
}
}
}
catch (Exception e)
{
LOG.warn("",e);
}

// Get the resource from the class path in case that the class is loaded
// by the Extension class loader which the parent of the system class loader.
in = ClassLoader.getSystemResourceAsStream(resource);
if(in != null)
{
return in;
}


try
{
return new FileInputStream(resource);
}
catch (FileNotFoundException e)
{
return PropertyReader.class.getResourceAsStream(resource);
}

}


private PropertyReader()
{
//
Expand Down
65 changes: 65 additions & 0 deletions src/test/com/password4j/PropertyReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,30 @@
*/
package com.password4j;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;


public class PropertyReaderTest
{

@After
@Before
public void setup()
{
System.clearProperty("psw4j.configuration");
PropertyReader.init();
}

@Test
public void testInt()
{
Expand Down Expand Up @@ -91,4 +109,51 @@ public void testNull()
{
PropertyReader.readString(null, "null", null);
}

@Test
public void testInitInvalidPath()
{
// GIVEN
System.setProperty("psw4j.configuration", "/my/improbable/path/xyz.properties");

// WHEN
PropertyReader.init();

// THEN
Assert.assertTrue(PropertyReader.properties.isEmpty());
}

@Test
public void testInitCustomPath() throws Exception
{
// GIVEN
String path = new File(".").getCanonicalPath() + "/src/test/my/custom/path/to/some.properties";
System.out.println(path);
System.setProperty("psw4j.configuration", path);

// WHEN
PropertyReader.init();

// THEN
Assert.assertFalse(PropertyReader.properties.isEmpty());
Assert.assertEquals("hello!!", PropertyReader.readString("check.this.out", "kappa", null));
}

@Test
public void testNoThreadClassLoader() throws Exception
{
// GIVEN
String path = new File(".").getCanonicalPath() + "/src/test/my/custom/path/to/some.properties";
System.out.println(path);
System.setProperty("psw4j.configuration", path);
Thread.currentThread().setContextClassLoader(null);

// WHEN
PropertyReader.init();

// THEN
Assert.assertFalse(PropertyReader.properties.isEmpty());
Assert.assertEquals("hello!!", PropertyReader.readString("check.this.out", "kappa", null));
}

}

0 comments on commit 313c43c

Please sign in to comment.