-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Manage all hardcode properties in one class + Add tests to ensure all…
… hardcoded properties exist #1438
- Loading branch information
1 parent
a4bbbf1
commit 9158d5d
Showing
3 changed files
with
159 additions
and
9 deletions.
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
46 changes: 46 additions & 0 deletions
46
...ommon/src/main/java/im/turms/server/common/infra/property/HardcodedPropertyNameConst.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,46 @@ | ||
/* | ||
* Copyright (C) 2019 The Turms Project | ||
* https://github.com/turms-im/turms | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package im.turms.server.common.infra.property; | ||
|
||
/** | ||
* Define all hardcoded property names here to manage them in one place. | ||
* <p> | ||
* We need these hardcoded property names because we need to use them before Spring Boot prepares | ||
* properties. | ||
* | ||
* @author James Chen | ||
*/ | ||
public final class HardcodedPropertyNameConst { | ||
|
||
public static final String TURMS_CLUSTER_NODE_ID = "turms.cluster.node.id"; | ||
|
||
public static final String TURMS_LOGGING_CONSOLE_ENABLED = "turms.logging.console.enabled"; | ||
public static final String TURMS_LOGGING_CONSOLE_LEVEL = "turms.logging.console.level"; | ||
public static final String TURMS_LOGGING_FILE_ENABLED = "turms.logging.file.enabled"; | ||
public static final String TURMS_LOGGING_FILE_LEVEL = "turms.logging.file.level"; | ||
public static final String TURMS_LOGGING_FILE_FILE_PATH = "turms.logging.file.file-path"; | ||
public static final String TURMS_LOGGING_FILE_MAX_FILES = "turms.logging.file.max-files"; | ||
public static final String TURMS_LOGGING_FILE_MAX_FILE_SIZE_MB = | ||
"turms.logging.file.max-file-size-mb"; | ||
public static final String TURMS_LOGGING_FILE_COMPRESSION_ENABLED = | ||
"turms.logging.file.compression.enabled"; | ||
|
||
private HardcodedPropertyNameConst() { | ||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
...test/java/unit/im/turms/server/common/infra/property/HardcodedPropertyNameConstTests.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,94 @@ | ||
/* | ||
* Copyright (C) 2019 The Turms Project | ||
* https://github.com/turms-im/turms | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package unit.im.turms.server.common.infra.property; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import im.turms.server.common.infra.lang.ClassUtil; | ||
import im.turms.server.common.infra.lang.StringUtil; | ||
import im.turms.server.common.infra.property.HardcodedPropertyNameConst; | ||
import im.turms.server.common.infra.property.TurmsProperties; | ||
|
||
/** | ||
* @author James Chen | ||
*/ | ||
class HardcodedPropertyNameConstTests { | ||
|
||
@Test | ||
void allPropertiesShouldBePresent() { | ||
Field[] fields = HardcodedPropertyNameConst.class.getDeclaredFields(); | ||
for (Field field : fields) { | ||
testField(field); | ||
} | ||
} | ||
|
||
private static void testField(Field field) { | ||
int modifiers = field.getModifiers(); | ||
if (!Modifier.isStatic(modifiers)) { | ||
return; | ||
} | ||
Object value; | ||
try { | ||
value = field.get(null); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException( | ||
"Failed to get the value of: " | ||
+ field.getName(), | ||
e); | ||
} | ||
if (!(value instanceof String valueStr)) { | ||
throw new RuntimeException( | ||
"The field value is not a string: " | ||
+ field.getName()); | ||
} | ||
String[] parts = valueStr.split("\\."); | ||
String firstPart = parts[0]; | ||
if (!TurmsProperties.PROPERTIES_PREFIX.equals(firstPart)) { | ||
throw new RuntimeException( | ||
"The first part of the value of the field (" | ||
+ field.getName() | ||
+ ") is not \"" | ||
+ TurmsProperties.PROPERTIES_PREFIX | ||
+ "\". Actual: \"" | ||
+ firstPart | ||
+ "\""); | ||
} | ||
int length = parts.length; | ||
Class<?> currentPropertyClass = TurmsProperties.class; | ||
for (int i = 1; i < length; i++) { | ||
String part = parts[i]; | ||
Optional<Field> matchedField = ClassUtil.getNonStaticFields(currentPropertyClass) | ||
.stream() | ||
.filter(f -> StringUtil.lowerCamelToLowerHyphenLatin1(f.getName()) | ||
.equals(part)) | ||
.findFirst(); | ||
if (matchedField.isEmpty()) { | ||
throw new RuntimeException( | ||
"The value of the field (" | ||
+ field.getName() | ||
+ ") is not found"); | ||
} | ||
currentPropertyClass = matchedField.get() | ||
.getType(); | ||
} | ||
} | ||
} |