Skip to content

Commit ccec7da

Browse files
committed
refactor(test): use Typesafe Config system property for DB engine override
Replace withDbEngineOverride() CLI injection with Gradle systemProperty 'storage.db.engine' which ConfigFactory.load() merges automatically. Rename TestEnv back to TestConstants to minimize review diff.
1 parent 7d961c9 commit ccec7da

13 files changed

Lines changed: 76 additions & 90 deletions

framework/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ test {
137137
classDumpDir = file("$buildDir/jacoco/classpathdumps")
138138
}
139139
if (rootProject.archInfo.isArm64) {
140-
systemProperty 'tron.test.db.engine', 'ROCKSDB'
140+
systemProperty 'storage.db.engine', 'ROCKSDB'
141141
exclude '**/LevelDbDataSourceImplTest.class'
142142
}
143143
}
@@ -146,7 +146,7 @@ task testWithRocksDb(type: Test) {
146146
description = 'Run tests with RocksDB engine'
147147
group = 'verification'
148148
configureTestTask(it)
149-
systemProperty 'tron.test.db.engine', 'ROCKSDB'
149+
systemProperty 'storage.db.engine', 'ROCKSDB'
150150
exclude '**/LevelDbDataSourceImplTest.class'
151151
}
152152

framework/src/test/java/org/tron/common/BaseMethodTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ protected String[] extraArgs() {
5252
}
5353

5454
protected String configFile() {
55-
return TestEnv.TEST_CONF;
55+
return TestConstants.TEST_CONF;
5656
}
5757

5858
@Before
5959
public final void initContext() throws IOException {
60-
String[] baseArgs = TestEnv.withDbEngineOverride(
61-
"--output-directory", temporaryFolder.newFolder().toString());
60+
String[] baseArgs = new String[]{
61+
"--output-directory", temporaryFolder.newFolder().toString()};
6262
String[] allArgs = mergeArgs(baseArgs, extraArgs());
6363
Args.setParam(allArgs, configFile());
6464
beforeContext();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.tron.common;
2+
3+
import static org.junit.Assume.assumeFalse;
4+
5+
import org.tron.common.arch.Arch;
6+
7+
/**
8+
* Centralized test environment constants and utilities.
9+
*
10+
* <h3>DB engine override for dual-engine testing</h3>
11+
* Gradle tasks ({@code test} on ARM64, {@code testWithRocksDb}) set the JVM system property
12+
* {@code -Dstorage.db.engine=ROCKSDB}. Because test config files are loaded from the classpath
13+
* via {@code ConfigFactory.load(fileName)}, Typesafe Config automatically merges system
14+
* properties with higher priority than the config file values. This means the config's
15+
* {@code storage.db.engine = "LEVELDB"} is overridden transparently, without any code changes
16+
* in individual tests.
17+
*
18+
* <p><b>IMPORTANT:</b> Config files MUST be classpath resources (in {@code src/test/resources/}),
19+
* NOT standalone files in the working directory. If a config file exists on disk,
20+
* {@code Configuration.getByFileName} falls back to {@code ConfigFactory.parseFile()},
21+
* which does NOT merge system properties, and the engine override will silently fail.
22+
*/
23+
public class TestConstants {
24+
25+
public static final String TEST_CONF = "config-test.conf";
26+
public static final String NET_CONF = "config.conf";
27+
public static final String MAINNET_CONF = "config-test-mainnet.conf";
28+
public static final String DBBACKUP_CONF = "config-test-dbbackup.conf";
29+
public static final String LOCAL_CONF = "config-localtest.conf";
30+
public static final String STORAGE_CONF = "config-test-storagetest.conf";
31+
public static final String INDEX_CONF = "config-test-index.conf";
32+
33+
/**
34+
* Skips the current test on ARM64 where LevelDB JNI is unavailable.
35+
*/
36+
public static void assumeLevelDbAvailable() {
37+
assumeFalse("LevelDB JNI unavailable on ARM64", Arch.isArm64());
38+
}
39+
}

framework/src/test/java/org/tron/common/TestEnv.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

framework/src/test/java/org/tron/common/storage/Arm64EngineOverrideTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.junit.Test;
1414
import org.junit.rules.TemporaryFolder;
1515
import org.mockito.MockedStatic;
16-
import org.tron.common.TestEnv;
16+
import org.tron.common.TestConstants;
1717
import org.tron.common.arch.Arch;
1818
import org.tron.core.config.args.Args;
1919
import org.tron.core.config.args.Storage;
@@ -51,7 +51,7 @@ public void testArm64RejectsLevelDbCli() throws IOException {
5151
Args.setParam(new String[]{
5252
"--output-directory", temporaryFolder.newFolder().toString(),
5353
"--storage-db-engine", "LEVELDB"
54-
}, TestEnv.TEST_CONF);
54+
}, TestConstants.TEST_CONF);
5555

5656
TronError error = assertThrows(TronError.class, Args::validateConfig);
5757
assertEquals(TronError.ErrCode.PARAMETER_INIT, error.getErrCode());
@@ -66,7 +66,7 @@ public void testArm64RejectsLevelDbConfigDefault() throws IOException {
6666
// config-test.conf has db.engine=LEVELDB
6767
Args.setParam(new String[]{
6868
"--output-directory", temporaryFolder.newFolder().toString()
69-
}, TestEnv.TEST_CONF);
69+
}, TestConstants.TEST_CONF);
7070

7171
TronError error = assertThrows(TronError.class, Args::validateConfig);
7272
assertEquals(TronError.ErrCode.PARAMETER_INIT, error.getErrCode());
@@ -81,7 +81,7 @@ public void testArm64AcceptsRocksDb() throws IOException {
8181
Args.setParam(new String[]{
8282
"--output-directory", temporaryFolder.newFolder().toString(),
8383
"--storage-db-engine", "ROCKSDB"
84-
}, TestEnv.DBBACKUP_CONF);
84+
}, TestConstants.DBBACKUP_CONF);
8585

8686
Args.validateConfig(); // should not throw
8787
assertEquals("ROCKSDB",
@@ -96,7 +96,7 @@ public void testX86AllowsLevelDb() throws IOException {
9696

9797
Args.setParam(new String[]{
9898
"--output-directory", temporaryFolder.newFolder().toString()
99-
}, TestEnv.TEST_CONF);
99+
}, TestConstants.TEST_CONF);
100100

101101
Args.validateConfig(); // should not throw
102102
assertEquals("LEVELDB",
@@ -112,7 +112,7 @@ public void testX86AllowsRocksDb() throws IOException {
112112
Args.setParam(new String[]{
113113
"--output-directory", temporaryFolder.newFolder().toString(),
114114
"--storage-db-engine", "ROCKSDB"
115-
}, TestEnv.TEST_CONF);
115+
}, TestConstants.TEST_CONF);
116116

117117
Args.validateConfig(); // should not throw
118118
assertEquals("ROCKSDB",

framework/src/test/java/org/tron/common/storage/DbDataSourceImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import org.junit.runners.Parameterized;
3434
import org.junit.runners.Parameterized.Parameters;
3535
import org.rocksdb.RocksDB;
36-
import org.tron.common.TestEnv;
36+
import org.tron.common.TestConstants;
3737
import org.tron.common.arch.Arch;
3838
import org.tron.common.storage.WriteOptionsWrapper;
3939
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
@@ -96,7 +96,7 @@ public static void destroy() {
9696
@Before
9797
public void initDb() throws IOException {
9898
Args.setParam(new String[]{"--output-directory",
99-
temporaryFolder.newFolder().toString()}, TestEnv.TEST_CONF);
99+
temporaryFolder.newFolder().toString()}, TestConstants.TEST_CONF);
100100
dataSourceTest = createDataSource(
101101
Args.getInstance().getOutputDirectory() + File.separator, "test_db");
102102
}

framework/src/test/java/org/tron/common/storage/leveldb/LevelDbDataSourceImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.junit.rules.ExpectedException;
3333
import org.junit.rules.TemporaryFolder;
3434
import org.rocksdb.RocksDB;
35-
import org.tron.common.TestEnv;
35+
import org.tron.common.TestConstants;
3636
import org.tron.common.parameter.CommonParameter;
3737
import org.tron.common.storage.rocksdb.RocksDbDataSourceImpl;
3838
import org.tron.common.utils.FileUtil;
@@ -68,7 +68,7 @@ public static void destroy() {
6868
@Before
6969
public void initDb() throws IOException {
7070
Args.setParam(new String[]{"--output-directory",
71-
temporaryFolder.newFolder().toString()}, TestEnv.TEST_CONF);
71+
temporaryFolder.newFolder().toString()}, TestConstants.TEST_CONF);
7272
}
7373

7474
@Test

framework/src/test/java/org/tron/common/storage/rocksdb/RocksDbDataSourceImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertThrows;
5-
import static org.tron.common.TestEnv.DBBACKUP_CONF;
6-
import static org.tron.common.TestEnv.assumeLevelDbAvailable;
5+
import static org.tron.common.TestConstants.DBBACKUP_CONF;
6+
import static org.tron.common.TestConstants.assumeLevelDbAvailable;
77

88
import java.io.File;
99
import java.io.IOException;

framework/src/test/java/org/tron/core/config/args/ArgsTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.junit.Rule;
3232
import org.junit.Test;
3333
import org.junit.rules.ExpectedException;
34-
import org.tron.common.TestEnv;
34+
import org.tron.common.TestConstants;
3535
import org.tron.common.args.GenesisBlock;
3636
import org.tron.common.parameter.CommonParameter;
3737
import org.tron.common.utils.ByteArray;
@@ -57,8 +57,8 @@ public void destroy() {
5757

5858
@Test
5959
public void get() {
60-
Args.setParam(new String[] {"-c", TestEnv.TEST_CONF, "--keystore-factory"},
61-
TestEnv.NET_CONF);
60+
Args.setParam(new String[] {"-c", TestConstants.TEST_CONF, "--keystore-factory"},
61+
TestConstants.NET_CONF);
6262

6363
CommonParameter parameter = Args.getInstance();
6464

@@ -71,7 +71,7 @@ public void get() {
7171
address = ByteArray.toHexString(Args.getLocalWitnesses()
7272
.getWitnessAccountAddress());
7373
Assert.assertEquals("41", DecodeUtil.addressPreFixString);
74-
Assert.assertEquals(TestEnv.TEST_CONF, Args.getConfigFilePath());
74+
Assert.assertEquals(TestConstants.TEST_CONF, Args.getConfigFilePath());
7575
Assert.assertEquals(0, parameter.getBackupPriority());
7676

7777
Assert.assertEquals(3000, parameter.getKeepAliveInterval());
@@ -136,14 +136,14 @@ public void get() {
136136
@Test
137137
public void testIpFromLibP2p()
138138
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
139-
Args.setParam(new String[] {}, TestEnv.TEST_CONF);
139+
Args.setParam(new String[] {}, TestConstants.TEST_CONF);
140140
CommonParameter parameter = Args.getInstance();
141-
Assert.assertEquals(TestEnv.TEST_CONF, Args.getConfigFilePath());
141+
Assert.assertEquals(TestConstants.TEST_CONF, Args.getConfigFilePath());
142142

143143
String configuredExternalIp = parameter.getNodeExternalIp();
144144
Assert.assertEquals("46.168.1.1", configuredExternalIp);
145145

146-
Config config = Configuration.getByFileName(TestEnv.TEST_CONF);
146+
Config config = Configuration.getByFileName(TestConstants.TEST_CONF);
147147
Config config3 = config.withoutPath(ConfigKey.NODE_DISCOVERY_EXTERNAL_IP);
148148

149149
CommonParameter.getInstance().setNodeExternalIp(null);
@@ -158,7 +158,7 @@ public void testIpFromLibP2p()
158158
@Test
159159
public void testOldRewardOpt() {
160160
thrown.expect(IllegalArgumentException.class);
161-
Args.setParam(new String[] {"-c", "args-test.conf"}, TestEnv.NET_CONF);
161+
Args.setParam(new String[] {"-c", "args-test.conf"}, TestConstants.NET_CONF);
162162
}
163163

164164
@Test
@@ -303,7 +303,7 @@ public void testCliOverridesStorageConfig() {
303303
"--storage-index-switch", "cli-index-switch",
304304
"--storage-transactionHistory-switch", "off",
305305
"--contract-parse-enable", "false"
306-
}, TestEnv.TEST_CONF);
306+
}, TestConstants.TEST_CONF);
307307

308308
CommonParameter parameter = Args.getInstance();
309309

@@ -327,7 +327,7 @@ public void testCliOverridesStorageConfig() {
327327
*/
328328
@Test
329329
public void testConfigStorageDefaults() {
330-
Args.setParam(new String[] {}, TestEnv.TEST_CONF);
330+
Args.setParam(new String[] {}, TestConstants.TEST_CONF);
331331

332332
CommonParameter parameter = Args.getInstance();
333333

framework/src/test/java/org/tron/core/config/args/DynamicArgsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.junit.Assert;
55
import org.junit.Test;
66
import org.tron.common.BaseMethodTest;
7-
import org.tron.common.TestEnv;
7+
import org.tron.common.TestConstants;
88
import org.tron.common.parameter.CommonParameter;
99
import org.tron.common.utils.ReflectUtils;
1010
import org.tron.core.net.TronNetService;
@@ -21,14 +21,14 @@ protected void afterInit() {
2121
@Test
2222
public void start() {
2323
CommonParameter parameter = Args.getInstance();
24-
Assert.assertEquals(TestEnv.TEST_CONF, Args.getConfigFilePath());
24+
Assert.assertEquals(TestConstants.TEST_CONF, Args.getConfigFilePath());
2525
Assert.assertTrue(parameter.isDynamicConfigEnable());
2626
Assert.assertEquals(600, parameter.getDynamicConfigCheckInterval());
2727

2828
dynamicArgs.init();
2929
File configFile = (File) ReflectUtils.getFieldObject(dynamicArgs, "configFile");
3030
Assert.assertNotNull(configFile);
31-
Assert.assertEquals(TestEnv.TEST_CONF, configFile.getName());
31+
Assert.assertEquals(TestConstants.TEST_CONF, configFile.getName());
3232
Assert.assertEquals(0, (long) ReflectUtils.getFieldObject(dynamicArgs, "lastModified"));
3333

3434
TronNetService tronNetService = context.getBean(TronNetService.class);

0 commit comments

Comments
 (0)