Skip to content

Commit

Permalink
SAK-39751 - Add getLong method to ServerConfigurationService (sakaipr…
Browse files Browse the repository at this point in the history
…oject#8598)

Co-authored-by: Paul Lukasewych <[email protected]>
  • Loading branch information
jonespm and plukasew authored Sep 23, 2020
1 parent 98b3916 commit e2686bb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ public interface ServerConfigurationService
*/
String[] getStrings(String name);

/**
* Access some named configuration value as a long
*
* @param name
* The configuration value name.
* @param dflt
* The value to return if not found.
* @return The configuration value with this name, or the default value if not found.
*/
long getLong(String name, long dflt);

/**
* Access some named configuration value as an int.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand Down Expand Up @@ -628,13 +628,17 @@ public String[] getStrings(String name) {
/**
* {@inheritDoc}
*/
public int getInt(String name, int dflt)
public long getLong(String name, long dflt)
{
String value = getString(name);

if (StringUtils.isEmpty(value)) return dflt;
return NumberUtils.toLong(getString(name), dflt);
}

return Integer.parseInt(value);
/**
* {@inheritDoc}
*/
public int getInt(String name, int dflt)
{
return NumberUtils.toInt(getString(name), dflt);
}

/**
Expand Down Expand Up @@ -1093,9 +1097,15 @@ public <T> T getConfig(String name, T defaultValue) {
}
} else {
if (defaultValue instanceof Number) {
int num = ((Number) defaultValue).intValue();
int intValue = this.getInt(name, num);
returnValue = (T) Integer.valueOf(intValue);
if (defaultValue instanceof Long) {
long longValue = this.getLong(name, (Long) defaultValue);
returnValue = (T) Long.valueOf(longValue);
}
else {
int num = ((Number) defaultValue).intValue();
int intValue = this.getInt(name, num);
returnValue = (T) Integer.valueOf(intValue);
}
} else if (defaultValue instanceof Boolean) {
boolean bool = (Boolean) defaultValue;
boolean boolValue = this.getBoolean(name, bool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public String getHelpUrl(String helpContext) {
return null;
}

public long getLong(String name, long dflt) {
return 0L;
}

public int getInt(String name, int dflt) {
// TODO Auto-generated method stub
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void setUp() throws Exception {
basicConfigurationService.addConfigItem( new ConfigItemImpl("test6", "test6"), SOURCE);
basicConfigurationService.addConfigItem( new ConfigItemImpl("test7", "${AZ}"), SOURCE);
basicConfigurationService.addConfigItem( new ConfigItemImpl("intVal", 11), SOURCE);
basicConfigurationService.addConfigItem( new ConfigItemImpl("longVal", 15L), SOURCE);
basicConfigurationService.addConfigItem( new ConfigItemImpl("booleanVal", true), SOURCE);
log.info(basicConfigurationService.getConfigData().toString());
}
Expand Down Expand Up @@ -104,7 +105,7 @@ public void testDereferenceAll() throws Exception {
// https://jira.sakaiproject.org/browse/SAK-22148
int changed = basicConfigurationService.dereferenceConfig();
ConfigData cd = basicConfigurationService.getConfigData();
Assert.assertEquals(16, cd.getTotalConfigItems());
Assert.assertEquals(17, cd.getTotalConfigItems());
Assert.assertEquals(3, changed); // 4 of them have keys but 1 key is invalid so it will not be replaced
Assert.assertEquals("Aaron", basicConfigurationService.getConfig("name", "default") );
Assert.assertEquals("testing name=Aaron testing", basicConfigurationService.getConfig("testKeyNested", "default") );
Expand Down Expand Up @@ -268,6 +269,19 @@ public void testKNL_1132() {
Assert.assertEquals(false, booleanValue4);
}

@Test
public void testSAK_39751() {
long longDefault = Long.valueOf(Integer.MAX_VALUE) * 2L;
long longVal = basicConfigurationService.getLong("longVal", longDefault);
Assert.assertEquals(15L, longVal);
longVal = basicConfigurationService.getLong("longVal2", longDefault); // doesn't exist
Assert.assertEquals(longDefault, longVal);

//Testing getConfig default value
longVal = basicConfigurationService.getConfig("longVal2", longDefault);
Assert.assertEquals(longDefault, longVal);
}

@Test
public void testKNL_1137() {
// verify that types are handled correctly for
Expand Down

0 comments on commit e2686bb

Please sign in to comment.