remove property settings loader by default

This commit is contained in:
Jörg Prante 2022-01-05 16:58:43 +01:00
parent 15a7273b17
commit abe8e4adf8
10 changed files with 144 additions and 25 deletions

View file

@ -1,9 +1,5 @@
package org.xbib.config; package org.xbib.config;
import org.xbib.settings.Settings;
import org.xbib.settings.SettingsBuilder;
import org.xbib.settings.SettingsLoader;
import org.xbib.settings.SettingsLoaderService;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -23,6 +19,10 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.xbib.settings.Settings;
import org.xbib.settings.SettingsBuilder;
import org.xbib.settings.SettingsLoader;
import org.xbib.settings.SettingsLoaderService;
/** /**
* A configuration loader for configuration files. * A configuration loader for configuration files.

View file

@ -1,7 +1,5 @@
package org.xbib.config; package org.xbib.config;
import org.xbib.settings.Settings;
import org.xbib.settings.datastructures.DatastructureSettings;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.sql.Connection; import java.sql.Connection;
@ -10,6 +8,8 @@ import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import org.xbib.settings.Settings;
import org.xbib.settings.datastructures.DatastructureSettings;
public class ConfigParams implements Comparable<ConfigParams> { public class ConfigParams implements Comparable<ConfigParams> {

View file

@ -1,15 +1,14 @@
package org.xbib.config.test; package org.xbib.config.test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.xbib.settings.Settings;
import org.xbib.config.ConfigLoader;
import org.xbib.config.ConfigParams;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import org.junit.jupiter.api.Test;
import org.xbib.config.ConfigLoader;
import org.xbib.config.ConfigParams;
import org.xbib.settings.Settings;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ConfigLoaderTest { public class ConfigLoaderTest {

View file

@ -56,10 +56,19 @@ page at http://checkstyle.sourceforge.net/config.html -->
<!-- Checks for out of order import statements. --> <!-- Checks for out of order import statements. -->
<property name="severity" value="warning"/> <property name="severity" value="warning"/>
<property name="groups" value="com,junit,net,org,java,javax"/> <!-- <property name="tokens" value="IMPORT, STATIC_IMPORT"/> -->
<!-- This ensures that static imports go first. --> <property name="separated" value="false"/>
<property name="option" value="top"/> <property name="groups" value="*"/>
<property name="tokens" value="STATIC_IMPORT, IMPORT"/> <!-- <property name="option" value="above"/> -->
<property name="sortStaticImportsAlphabetically" value="true"/>
</module>
<module name="CustomImportOrder">
<!-- <property name="customImportOrderRules" value="THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/> -->
<!-- <property name="specialImportsRegExp" value="^javax\."/> -->
<!-- <property name="standardPackageRegExp" value="^java\."/> -->
<property name="sortImportsInGroupAlphabetically" value="true"/>
<property name="separateLineBetweenGroups" value="false"/>
</module> </module>
<!-- <!--

View file

@ -48,6 +48,7 @@ public abstract class Foo {
</rule> </rule>
<rule name="AccessorClassGeneration" <rule name="AccessorClassGeneration"
language="java"
since="1.04" since="1.04"
maximumLanguageVersion="10" maximumLanguageVersion="10"
message="Avoid instantiation through private constructors from outside of the constructor's class." message="Avoid instantiation through private constructors from outside of the constructor's class."
@ -109,6 +110,7 @@ public class OuterClass {
</rule> </rule>
<rule name="ArrayIsStoredDirectly" <rule name="ArrayIsStoredDirectly"
language="java"
since="2.2" since="2.2"
message="The user-supplied array ''{0}'' is stored directly." message="The user-supplied array ''{0}'' is stored directly."
class="net.sourceforge.pmd.lang.java.rule.bestpractices.ArrayIsStoredDirectlyRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.ArrayIsStoredDirectlyRule"
@ -168,6 +170,7 @@ class Foo {
</rule> </rule>
<rule name="AvoidReassigningLoopVariables" <rule name="AvoidReassigningLoopVariables"
language="java"
since="6.11.0" since="6.11.0"
message="Avoid reassigning the loop control variable ''{0}''" message="Avoid reassigning the loop control variable ''{0}''"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningLoopVariablesRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningLoopVariablesRule"
@ -216,6 +219,7 @@ public class Foo {
</rule> </rule>
<rule name="AvoidReassigningParameters" <rule name="AvoidReassigningParameters"
language="java"
since="1.0" since="1.0"
message="Avoid reassigning parameters such as ''{0}''" message="Avoid reassigning parameters such as ''{0}''"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningParametersRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningParametersRule"
@ -265,6 +269,7 @@ public class Foo {
</rule> </rule>
<rule name="AvoidUsingHardCodedIP" <rule name="AvoidUsingHardCodedIP"
language="java"
since="4.1" since="4.1"
message="Do not hard code the IP address ${variableName}" message="Do not hard code the IP address ${variableName}"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidUsingHardCodedIPRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidUsingHardCodedIPRule"
@ -493,7 +498,7 @@ for (int i = 0, j = 0; i < 10; i++, j += 2) {
<property name="xpath"> <property name="xpath">
<value> <value>
<![CDATA[ <![CDATA[
//ClassOrInterfaceBodyDeclaration[MethodDeclaration/MethodDeclarator[@Image='suite']] //ClassOrInterfaceBodyDeclaration[MethodDeclaration/MethodDeclarator[@Name='suite']]
[MethodDeclaration/ResultType/Type/ReferenceType/ClassOrInterfaceType[pmd-java:typeIs('junit.framework.Test')]] [MethodDeclaration/ResultType/Type/ReferenceType/ClassOrInterfaceType[pmd-java:typeIs('junit.framework.Test')]]
[not(MethodDeclaration/Block//ClassOrInterfaceType[pmd-java:typeIs('junit.framework.JUnit4TestAdapter')])] [not(MethodDeclaration/Block//ClassOrInterfaceType[pmd-java:typeIs('junit.framework.JUnit4TestAdapter')])]
]]> ]]>
@ -535,7 +540,7 @@ public class GoodTest {
<value> <value>
<![CDATA[ <![CDATA[
//ClassOrInterfaceBodyDeclaration //ClassOrInterfaceBodyDeclaration
[MethodDeclaration/MethodDeclarator[@Image='tearDown']] [MethodDeclaration/MethodDeclarator[@Name='tearDown']]
[count(Annotation//Name[ [count(Annotation//Name[
pmd-java:typeIs('org.junit.After') pmd-java:typeIs('org.junit.After')
or pmd-java:typeIs('org.junit.jupiter.api.AfterEach') or pmd-java:typeIs('org.junit.jupiter.api.AfterEach')
@ -579,7 +584,7 @@ public class MyTest2 {
<value> <value>
<![CDATA[ <![CDATA[
//ClassOrInterfaceBodyDeclaration //ClassOrInterfaceBodyDeclaration
[MethodDeclaration/MethodDeclarator[@Image='setUp']] [MethodDeclaration/MethodDeclarator[@Name='setUp']]
[count(Annotation//Name[ [count(Annotation//Name[
pmd-java:typeIs('org.junit.Before') pmd-java:typeIs('org.junit.Before')
or pmd-java:typeIs('org.junit.jupiter.api.BeforeEach') or pmd-java:typeIs('org.junit.jupiter.api.BeforeEach')
@ -623,10 +628,10 @@ public class MyTest2 {
<value> <value>
<![CDATA[ <![CDATA[
//ClassOrInterfaceDeclaration[ //ClassOrInterfaceDeclaration[
matches(@Image, $testClassPattern) matches(@SimpleName, $testClassPattern)
or ExtendsList/ClassOrInterfaceType[pmd-java:typeIs('junit.framework.TestCase')]] or ExtendsList/ClassOrInterfaceType[pmd-java:typeIs('junit.framework.TestCase')]]
/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration[MethodDeclaration[@Public=true()]/MethodDeclarator[starts-with(@Image, 'test')]] /ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration[MethodDeclaration[@Public=true()]/MethodDeclarator[starts-with(@Name, 'test')]]
[not(Annotation//Name[ [not(Annotation//Name[
pmd-java:typeIs('org.junit.Test') pmd-java:typeIs('org.junit.Test')
or pmd-java:typeIs('org.junit.jupiter.api.Test') or pmd-java:typeIs('org.junit.jupiter.api.RepeatedTest') or pmd-java:typeIs('org.junit.jupiter.api.Test') or pmd-java:typeIs('org.junit.jupiter.api.RepeatedTest')
@ -656,6 +661,7 @@ public class MyTest {
</rule> </rule>
<rule name="JUnitAssertionsShouldIncludeMessage" <rule name="JUnitAssertionsShouldIncludeMessage"
language="java"
since="1.04" since="1.04"
message="JUnit assertions should include a message" message="JUnit assertions should include a message"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitAssertionsShouldIncludeMessageRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitAssertionsShouldIncludeMessageRule"
@ -734,6 +740,7 @@ public class MyTestCase extends TestCase {
</rule> </rule>
<rule name="JUnitTestsShouldIncludeAssert" <rule name="JUnitTestsShouldIncludeAssert"
language="java"
since="2.0" since="2.0"
message="JUnit tests should include assert() or fail()" message="JUnit tests should include assert() or fail()"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitTestsShouldIncludeAssertRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitTestsShouldIncludeAssertRule"
@ -758,6 +765,7 @@ public class Foo extends TestCase {
</rule> </rule>
<rule name="JUnitUseExpected" <rule name="JUnitUseExpected"
language="java"
since="4.0" since="4.0"
message="In JUnit4, use the @Test(expected) annotation to denote tests that should throw exceptions" message="In JUnit4, use the @Test(expected) annotation to denote tests that should throw exceptions"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitUseExpectedRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.JUnitUseExpectedRule"
@ -788,6 +796,7 @@ public class MyTest {
</rule> </rule>
<rule name="LooseCoupling" <rule name="LooseCoupling"
language="java"
since="0.7" since="0.7"
message="Avoid using implementation types like ''{0}''; use the interface instead" message="Avoid using implementation types like ''{0}''; use the interface instead"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.LooseCouplingRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.LooseCouplingRule"
@ -824,6 +833,7 @@ public class Bar {
</rule> </rule>
<rule name="MethodReturnsInternalArray" <rule name="MethodReturnsInternalArray"
language="java"
since="2.2" since="2.2"
message="Returning ''{0}'' may expose an internal array." message="Returning ''{0}'' may expose an internal array."
class="net.sourceforge.pmd.lang.java.rule.bestpractices.MethodReturnsInternalArrayRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.MethodReturnsInternalArrayRule"
@ -848,6 +858,7 @@ public class SecureSystem {
<rule name="MissingOverride" <rule name="MissingOverride"
language="java"
since="6.2.0" since="6.2.0"
minimumLanguageVersion="1.5" minimumLanguageVersion="1.5"
message="The method ''{0}'' is missing an @Override annotation." message="The method ''{0}'' is missing an @Override annotation."
@ -1000,6 +1011,7 @@ class Foo {
</rule> </rule>
<rule name="PreserveStackTrace" <rule name="PreserveStackTrace"
language="java"
since="3.7" since="3.7"
message="New exception is thrown in catch block, original stack trace may be lost" message="New exception is thrown in catch block, original stack trace may be lost"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.PreserveStackTraceRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.PreserveStackTraceRule"
@ -1217,6 +1229,7 @@ public class Foo {
</rule> </rule>
<rule name="UnusedImports" <rule name="UnusedImports"
language="java"
since="1.0" since="1.0"
message="Avoid unused imports such as ''{0}''" message="Avoid unused imports such as ''{0}''"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedImportsRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.UnusedImportsRule"
@ -1489,6 +1502,7 @@ public class MyTestCase extends TestCase {
</rule> </rule>
<rule name="UseCollectionIsEmpty" <rule name="UseCollectionIsEmpty"
language="java"
since="3.9" since="3.9"
message="Substitute calls to size() == 0 (or size() != 0, size() &gt; 0, size() &lt; 1) with calls to isEmpty()" message="Substitute calls to size() == 0 (or size() != 0, size() &gt; 0, size() &lt; 1) with calls to isEmpty()"
class="net.sourceforge.pmd.lang.java.rule.bestpractices.UseCollectionIsEmptyRule" class="net.sourceforge.pmd.lang.java.rule.bestpractices.UseCollectionIsEmptyRule"
@ -1546,7 +1560,7 @@ public class Foo {
][ ][
pmd-java:typeIs('java.lang.AutoCloseable') pmd-java:typeIs('java.lang.AutoCloseable')
or or
../../PrimarySuffix/Arguments[@ArgumentCount = 1]//PrimaryPrefix[pmd-java:typeIs('java.lang.AutoCloseable')] ../../PrimarySuffix/Arguments[@Size = 1]//PrimaryPrefix[pmd-java:typeIs('java.lang.AutoCloseable')]
]] ]]
]]> ]]>
</value> </value>

View file

@ -0,0 +1,82 @@
package org.xbib.settings.content.json;
import org.junit.jupiter.api.Test;
import org.xbib.settings.Settings;
import org.xbib.settings.SettingsLoader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class JsonSettingsTest {
@Test
public void testMapForSettings() throws IOException {
Map<String, String> map = new HashMap<>();
map.put("hello", "world");
Map<String, Object> settingsMap = new HashMap<>();
settingsMap.put("map", map);
SettingsLoader settingsLoader = new JsonSettingsLoader();
Settings settings = Settings.settingsBuilder()
.put(settingsLoader.load(settingsMap))
.build();
assertEquals("{map.hello=world}", settings.getAsMap().toString());
}
@Test
public void testMapSettingsFromReader() throws IOException {
Map<String, Object> map = Map.of("map", Map.of("hello", "world"));
SettingsLoader settingsLoader = new JsonSettingsLoader();
Settings settings = Settings.settingsBuilder()
.put(settingsLoader.load(map))
.build();
assertEquals("{map.hello=world}", settings.getAsMap().toString());
}
@Test
public void testLoadFromString() throws IOException {
String json = "{\"Hello\":\"World\"}";
SettingsLoader loader = new JsonSettingsLoader();
Map<String, String> result = loader.load(json);
assertEquals("{Hello=World}", result.toString());
}
@Test
public void testLoadSettingsFromString() {
String json = "{\"Hello\":\"World\"}";
Settings settings = Settings.settingsBuilder().loadFromString(json).build();
assertEquals("{Hello=World}", settings.getAsMap().toString());
}
@Test
public void testFlatLoader() throws IOException {
String s = "{\"a\":{\"b\":\"c\"}}";
SettingsLoader loader = new JsonSettingsLoader();
Map<String, String> flatMap = loader.load(s);
assertEquals("{a.b=c}", flatMap.toString());
}
@Test
public void testLoadFromMap() throws IOException {
Map<String, Object> map = new LinkedHashMap<>();
Map<String, Object> code = new LinkedHashMap<>();
code.put("a", "b");
code.put("b", "c");
Map<String, Object> name = new LinkedHashMap<>();
name.put("a", "b");
name.put("b", "c");
List<String> list = Arrays.asList("a", "b");
map.put("code", code);
map.put("name", name);
map.put("list", list);
map.put("null", null);
SettingsLoader loader = new JsonSettingsLoader();
Map<String, String> result = loader.load(map);
assertEquals("{code.a=b, code.b=c, name.a=b, name.b=c, list.0=a, list.1=b, null=null}", result.toString());
}
}

View file

@ -38,6 +38,21 @@ public class JsonSettingsTest {
assertEquals("{map.hello=world}", settings.getAsMap().toString()); assertEquals("{map.hello=world}", settings.getAsMap().toString());
} }
@Test
public void testLoadFromString() throws IOException {
String json = "{\"Hello\":\"World\"}";
SettingsLoader loader = new JsonSettingsLoader();
Map<String, String> result = loader.load(json);
assertEquals("{Hello=World}", result.toString());
}
@Test
public void testLoadSettingsFromString() {
String json = "{\"Hello\":\"World\"}";
Settings settings = Settings.settingsBuilder().loadFromString(json).build();
assertEquals("{Hello=World}", settings.getAsMap().toString());
}
@Test @Test
public void testFlatLoader() throws IOException { public void testFlatLoader() throws IOException {
String s = "{\"a\":{\"b\":\"c\"}}"; String s = "{\"a\":{\"b\":\"c\"}}";

View file

@ -0,0 +1 @@
org.xbib.settings.datastructures.json.JsonSettingsLoader

View file

@ -229,8 +229,7 @@ public class DatastructureSettingsBuilder implements SettingsBuilder {
public DatastructureSettingsBuilder loadFromString(String source) { public DatastructureSettingsBuilder loadFromString(String source) {
SettingsLoader settingsLoader = settingsLoaderService.loaderFromString(source); SettingsLoader settingsLoader = settingsLoaderService.loaderFromString(source);
try { try {
Map<String, String> loadedSettings = settingsLoader.load(source); put(settingsLoader.load(source));
put(loadedSettings);
} catch (Exception e) { } catch (Exception e) {
throw new SettingsException("failed to load settings from [" + source + "]", e); throw new SettingsException("failed to load settings from [" + source + "]", e);
} }