finalize KeyPathConverter

This commit is contained in:
marhali 2022-04-10 21:39:56 +02:00
parent 9d5be9caa0
commit fc1eb07443
2 changed files with 31 additions and 12 deletions

View File

@ -61,7 +61,8 @@ public class KeyPathConverter {
// Missing namespace // Missing namespace
if(i == 0 && settings.getFolderStrategy().isNamespaceMode() && hasDefaultNamespace()) { if(i == 0 && settings.getFolderStrategy().isNamespaceMode() && hasDefaultNamespace()) {
if(section.length() == literalPath.length() || !String.valueOf(literalPath.charAt(section.length())).equals(settings.getNamespaceDelimiter())) { String namespaceDelim = (settings.isNestedKeys() ? "" : "\\") + settings.getNamespaceDelimiter();
if(section.length() == literalPath.length() || !literalPath.substring(section.length()).startsWith(namespaceDelim)) {
path.add(settings.getDefaultNamespace()); path.add(settings.getDefaultNamespace());
} }
} }
@ -102,7 +103,7 @@ public class KeyPathConverter {
builder.append(Pattern.quote(settings.getSectionDelimiter())); builder.append(Pattern.quote(settings.getSectionDelimiter()));
// Add optional namespace delimiter if present // Add optional namespace delimiter if present
if(hasDefaultNamespace()) { if(settings.getNamespaceDelimiter() != null && !settings.getNamespaceDelimiter().isEmpty()) {
builder.append("|"); builder.append("|");
builder.append(Pattern.quote(Objects.requireNonNull(settings.getNamespaceDelimiter()))); builder.append(Pattern.quote(Objects.requireNonNull(settings.getNamespaceDelimiter())));
} }
@ -115,25 +116,27 @@ public class KeyPathConverter {
* Securely escape found delimiters inside provided section according to the configured policy. * Securely escape found delimiters inside provided section according to the configured policy.
*/ */
private String quoteSection(String section) { private String quoteSection(String section) {
String quoted = section;
if(!settings.isNestedKeys()) { if(!settings.isNestedKeys()) {
return section; return quoted;
} }
if(hasDefaultNamespace()) { if(hasDefaultNamespace()) {
section = section.replace(settings.getNamespaceDelimiter(), "\\" + settings.getNamespaceDelimiter()); quoted = quoted.replace(settings.getNamespaceDelimiter(), "\\" + settings.getNamespaceDelimiter());
} }
section = section.replace(settings.getSectionDelimiter(), "\\" + settings.getSectionDelimiter()); quoted = quoted.replace(settings.getSectionDelimiter(), "\\" + settings.getSectionDelimiter());
return section; return quoted;
} }
private String unquoteSection(String section) { private String unquoteSection(String section) {
String unquoted = section;
if(hasDefaultNamespace()) { if(hasDefaultNamespace()) {
section = section.replace("\\" + settings.getNamespaceDelimiter(), settings.getNamespaceDelimiter()); unquoted = unquoted.replace("\\" + settings.getNamespaceDelimiter(), settings.getNamespaceDelimiter());
} }
section = section.replace("\\" + settings.getSectionDelimiter(), settings.getSectionDelimiter()); unquoted = unquoted.replace("\\" + settings.getSectionDelimiter(), settings.getSectionDelimiter());
return section; return unquoted;
} }
/** /**

View File

@ -11,13 +11,30 @@ import org.jetbrains.annotations.Nullable;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/** /**
* Unit tests for {@link KeyPathConverter}. * Unit tests for {@link KeyPathConverter}.
* @author marhali * @author marhali
*/ */
public class KeyPathConverterTest { public class KeyPathConverterTest {
@Test
public void noNamespaceDelimiter() {
KeyPathConverter converter = getConverter(FolderStrategyType.MODULARIZED_NAMESPACE, null, ".", null, true);
Assert.assertEquals(new KeyPath("username"), converter.fromString("username"));
Assert.assertEquals(new KeyPath("username:nested"), converter.fromString("username:nested"));
Assert.assertEquals(new KeyPath("username:nested", "leaf"), converter.fromString("username:nested.leaf"));
}
@Test
public void emptyDefaultNamespace() {
KeyPathConverter converter = getConverter(FolderStrategyType.MODULARIZED_NAMESPACE, ":", ".", null, true);
Assert.assertEquals(new KeyPath("username"), converter.fromString("username"));
Assert.assertEquals(new KeyPath("username", "nested"), converter.fromString("username:nested"));
Assert.assertEquals(new KeyPath("username", "nested", "leaf"), converter.fromString("username:nested.leaf"));
}
@Test @Test
public void nonNestedSingle() { public void nonNestedSingle() {
KeyPathConverter converter = getConverter(FolderStrategyType.SINGLE, null, ".", null, false); KeyPathConverter converter = getConverter(FolderStrategyType.SINGLE, null, ".", null, false);
@ -39,8 +56,7 @@ public class KeyPathConverterTest {
Assert.assertEquals("username.title\\:concat.leaf\\.node", converter.toString(new KeyPath("username.title", "concat.leaf", "node"))); Assert.assertEquals("username.title\\:concat.leaf\\.node", converter.toString(new KeyPath("username.title", "concat.leaf", "node")));
Assert.assertEquals(new KeyPath("common", "username"), converter.fromString("username")); Assert.assertEquals(new KeyPath("common", "username"), converter.fromString("username"));
// TODO: problem here Assert.assertEquals(new KeyPath("username.title", "concat", "leaf.node"), converter.fromString("username.title\\:concat\\.leaf.node"));
Assert.assertEquals(new KeyPath("username.title", "concat.leaf", "node"), converter.fromString("username.title\\:concat\\.leaf.node"));
} }
@Test @Test