-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HH-76991 remove jdebug dependency, rename properties file #82
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package ru.hh.nab.core.util; | ||
|
||
import com.google.common.base.Strings; | ||
|
||
import java.util.ArrayList; | ||
import static java.util.Arrays.asList; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import static org.springframework.util.Assert.hasLength; | ||
|
||
public class FileSettings { | ||
private final Properties properties; | ||
|
@@ -31,25 +33,29 @@ public Boolean getBoolean(String key) { | |
} | ||
|
||
public Properties getSubProperties(String prefix) { | ||
return getSubProperties(prefix, null); | ||
} | ||
|
||
public Properties getSubProperties(String prefix, String newPrefix) { | ||
final String namespacePrefix = Strings.isNullOrEmpty(prefix) ? "" : prefix + "."; | ||
hasLength(prefix, "prefix should not be null or empty"); | ||
final Properties subProperties = new Properties(); | ||
|
||
properties.stringPropertyNames().stream() | ||
.filter(key -> key.startsWith(namespacePrefix)) | ||
.filter(key -> key.startsWith(prefix + ".")) | ||
.forEach(key -> { | ||
String newKey = Strings.isNullOrEmpty(newPrefix) ? "" : newPrefix + "."; | ||
newKey += prefix.isEmpty() ? key : key.substring(prefix.length() + 1); | ||
String newKey = prefix.isEmpty() ? key : key.substring(prefix.length() + 1); | ||
subProperties.put(newKey, properties.getProperty(key)); | ||
}); | ||
|
||
return subProperties; | ||
} | ||
|
||
public FileSettings getSubSettings(String prefix) { | ||
return new FileSettings(getSubProperties(prefix)); | ||
} | ||
|
||
public List<String> getStringList(String key) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Для совместимости с hh.ru |
||
String value = getString(key); | ||
return value != null ? asList(value.split("[,\\s]+")) : new ArrayList<>(); | ||
} | ||
|
||
public Properties getProperties() { | ||
Properties propertiesCopy = new Properties(); | ||
propertiesCopy.putAll(this.properties); | ||
return propertiesCopy; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
nab-core/src/test/java/ru/hh/nab/core/util/FileSettingsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package ru.hh.nab.core.util; | ||
|
||
import java.util.List; | ||
import java.util.Properties; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class FileSettingsTest { | ||
private static FileSettings fileSettings; | ||
|
||
@BeforeClass | ||
public static void setUpClass() { | ||
Properties properties = new Properties(); | ||
properties.put("strProperty", "value"); | ||
properties.put("intProperty1", "0"); | ||
properties.put("intProperty2", "123"); | ||
properties.put("namespace.boolProperty1", "true"); | ||
properties.put("namespace.boolProperty2", "false"); | ||
properties.put("listProperty", "value1, value2,value3"); | ||
|
||
fileSettings = new FileSettings(properties); | ||
} | ||
|
||
@Test | ||
public void testGetString() { | ||
assertEquals("value", fileSettings.getString("strProperty")); | ||
assertNull(fileSettings.getString("missingKey")); | ||
} | ||
|
||
@Test | ||
public void testGetInteger() { | ||
assertEquals(0, fileSettings.getInteger("intProperty1").intValue()); | ||
assertEquals(123, fileSettings.getInteger("intProperty2").intValue()); | ||
} | ||
|
||
@Test | ||
public void testGetBoolean() { | ||
assertTrue(fileSettings.getBoolean("namespace.boolProperty1")); | ||
assertFalse(fileSettings.getBoolean("namespace.boolProperty2")); | ||
} | ||
|
||
@Test | ||
public void testGetSubProperties() { | ||
Properties subProperties = fileSettings.getSubProperties("namespace"); | ||
assertEquals(2, subProperties.size()); | ||
assertTrue(subProperties.containsKey("boolProperty1")); | ||
assertTrue(subProperties.containsKey("boolProperty2")); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testGetSubPropertiesThrowsExceptionIfPrefixIsEmpty() { | ||
fileSettings.getSubProperties(""); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testGetSubPropertiesThrowsExceptionIfPrefixIsNull() { | ||
fileSettings.getSubProperties(null); | ||
} | ||
|
||
@Test | ||
public void testGetSubSettings() { | ||
FileSettings subSettings = fileSettings.getSubSettings("namespace"); | ||
assertNotNull(subSettings.getString("boolProperty1")); | ||
assertNotNull(subSettings.getString("boolProperty2")); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testGetSubSettingsThrowsExceptionIfPrefixIsEmpty() { | ||
fileSettings.getSubSettings(""); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testGetSubSettingsThrowsExceptionIfPrefixIsNull() { | ||
fileSettings.getSubSettings(null); | ||
} | ||
|
||
@Test | ||
public void testGetStringList() { | ||
List<String> settings = fileSettings.getStringList("listProperty"); | ||
assertEquals(3, settings.size()); | ||
assertEquals("value1", settings.get(0)); | ||
assertEquals("value2", settings.get(1)); | ||
assertEquals("value3", settings.get(2)); | ||
} | ||
|
||
@Test | ||
public void testGetProperties() { | ||
Properties properties = fileSettings.getProperties(); | ||
assertEquals("value", properties.getProperty("strProperty")); | ||
|
||
properties.setProperty("strProperty", "newValue"); | ||
assertEquals("newValue", properties.getProperty("strProperty")); | ||
|
||
assertEquals("value", fileSettings.getString("strProperty")); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Пока не используются.