diff --git a/src/main/java/com/sshtools/jini/INI.java b/src/main/java/com/sshtools/jini/INI.java index 30266fe..f88636e 100644 --- a/src/main/java/com/sshtools/jini/INI.java +++ b/src/main/java/com/sshtools/jini/INI.java @@ -1241,50 +1241,4 @@ public Entry next() { } } - @SuppressWarnings("unchecked") - public static Function compound(Function... sources) { - return (k) -> { - for(var src : sources) { - var v = src.apply(k); - if(v != null) - return v; - } - return null; - }; - } - - public static Function systemProperties() { - return (k) -> { - if(k.startsWith("sys.")) { - return System.getProperty(k.substring(4)); - } - else if(k.startsWith("env.")) { - return System.getenv(k.substring(4)); - } - else - return null; - }; - } - - public static String str(String text, Function source) { - var pattern = Pattern.compile("\\$\\{(.*?)\\}"); - var matcher = pattern.matcher(text); - var builder = new StringBuilder(); - int i = 0; - while (matcher.find()) { - var variable = matcher.group(1); - var replacement = source.apply(variable); - builder.append(text.substring(i, matcher.start())); - if (replacement == null) { - throw new IllegalArgumentException(MessageFormat.format("Unknown string variable $\\{{0}\\}", variable)); - } else { - builder.append(replacement); - } - i = matcher.end(); - - } - builder.append(text.substring(i, text.length())); - text = builder.toString(); - return text; - } } diff --git a/src/main/java/com/sshtools/jini/INIPreferences.java b/src/main/java/com/sshtools/jini/INIPreferences.java index df87fe6..5410199 100644 --- a/src/main/java/com/sshtools/jini/INIPreferences.java +++ b/src/main/java/com/sshtools/jini/INIPreferences.java @@ -87,9 +87,6 @@ public INIStoreBuilder withFailOnParsingError(boolean failOnParsingError) { } public INIStoreBuilder withMode(Mode mode) { - if (customRoot.isPresent() && scope != Scope.CUSTOM) { - throw new IllegalArgumentException("A custom root has been set. Scope may not be used."); - } this.mode = mode; return this; } diff --git a/src/main/java/com/sshtools/jini/Interpolation.java b/src/main/java/com/sshtools/jini/Interpolation.java new file mode 100644 index 0000000..de4c183 --- /dev/null +++ b/src/main/java/com/sshtools/jini/Interpolation.java @@ -0,0 +1,55 @@ +package com.sshtools.jini; + +import java.text.MessageFormat; +import java.util.function.Function; +import java.util.regex.Pattern; + +public class Interpolation { + + @SuppressWarnings("unchecked") + public static Function compound(Function... sources) { + return (k) -> { + for(var src : sources) { + var v = src.apply(k); + if(v != null) + return v; + } + return null; + }; + } + + public static Function systemProperties() { + return (k) -> { + if(k.startsWith("sys.")) { + return System.getProperty(k.substring(4)); + } + else if(k.startsWith("env.")) { + return System.getenv(k.substring(4)); + } + else + return null; + }; + } + + public static String str(String text, Function source) { + var pattern = Pattern.compile("\\$\\{(.*?)\\}"); + var matcher = pattern.matcher(text); + var builder = new StringBuilder(); + int i = 0; + while (matcher.find()) { + var variable = matcher.group(1); + var replacement = source.apply(variable); + builder.append(text.substring(i, matcher.start())); + if (replacement == null) { + throw new IllegalArgumentException(MessageFormat.format("Unknown string variable $\\{{0}\\}", variable)); + } else { + builder.append(replacement); + } + i = matcher.end(); + + } + builder.append(text.substring(i, text.length())); + text = builder.toString(); + return text; + } +} diff --git a/src/test/java/com/sshtools/jini/INITest.java b/src/test/java/com/sshtools/jini/INITest.java index 6d1ebe0..b7b1de3 100644 --- a/src/test/java/com/sshtools/jini/INITest.java +++ b/src/test/java/com/sshtools/jini/INITest.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.Reader; import java.io.UncheckedIOException; @@ -113,6 +114,32 @@ public void testReadFromFile() throws Exception { assertBasicInsensitive(ini); } + @Test + public void testReadFromStream() throws Exception { + var ini = INI.fromInput(new ByteArrayInputStream(getBasicIni().getBytes())); + assertBasic(ini); + assertBasicOrder(ini); + assertBasicInsensitive(ini); + } + + @Test + public void testReadOnly() throws Exception { + assertThrows(UnsupportedOperationException.class, () -> { + var ini = INI.fromString(getBasicIni()).readOnly(); + ini.put("abc", true); + }); + } + + @Test + public void testEmpty() throws Exception { + assertThrows(UnsupportedOperationException.class, () -> { + var ini = INI.empty(); + assertEquals(0, ini.keys().size()); + assertEquals(0, ini.sections().size()); + ini.put("abc", true); + }); + } + @Test public void testFailReadFromFile() throws Exception { assertThrows(UncheckedIOException.class, () -> INI.fromFile(Paths.get(UUID.randomUUID().toString())));