forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseMethodTest.java
More file actions
95 lines (83 loc) · 3.08 KB
/
BaseMethodTest.java
File metadata and controls
95 lines (83 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.tron.common;
import java.io.IOException;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.tron.common.application.Application;
import org.tron.common.application.ApplicationFactory;
import org.tron.common.application.TronApplicationContext;
import org.tron.core.ChainBaseManager;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.args.Args;
import org.tron.core.db.Manager;
/**
* Base class for tests that need a fresh Spring context per test method.
*
* Each @Test method gets its own TronApplicationContext, created in @Before
* and destroyed in @After, ensuring full isolation between tests.
*
* Subclasses can customize behavior by overriding hook methods:
* extraArgs() — additional CLI args (e.g. "--debug")
* configFile() — config file (default: config-test.conf)
* beforeContext() — runs after Args.setParam, before Spring context creation
* afterInit() — runs after Spring context is ready (e.g. get extra beans)
* beforeDestroy() — runs before context shutdown (e.g. close resources)
*
* Use this when:
* - methods modify database state that would affect other methods
* - methods need different CommonParameter settings (e.g. setP2pDisable)
* - methods need beforeContext() to configure state before Spring starts
*
* If methods are read-only and don't interfere with each other, use BaseTest instead.
* Tests that don't need Spring (e.g. pure unit tests) should NOT extend either base class.
*/
@Slf4j
public abstract class BaseMethodTest {
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
protected TronApplicationContext context;
protected Application appT;
protected Manager dbManager;
protected ChainBaseManager chainBaseManager;
protected String[] extraArgs() {
return new String[0];
}
protected String configFile() {
return TestConstants.TEST_CONF;
}
@Before
public final void initContext() throws IOException {
String[] baseArgs = new String[]{
"--output-directory", temporaryFolder.newFolder().toString()};
String[] allArgs = mergeArgs(baseArgs, extraArgs());
Args.setParam(allArgs, configFile());
beforeContext();
context = new TronApplicationContext(DefaultConfig.class);
appT = ApplicationFactory.create(context);
dbManager = context.getBean(Manager.class);
chainBaseManager = context.getBean(ChainBaseManager.class);
afterInit();
}
protected void beforeContext() {
}
protected void afterInit() {
}
@After
public final void destroyContext() {
beforeDestroy();
if (context != null) {
context.close(); // triggers appT.shutdown() via TronApplicationContext
}
Args.clearParam();
}
protected void beforeDestroy() {
}
private static String[] mergeArgs(String[] base, String[] extra) {
String[] result = Arrays.copyOf(base, base.length + extra.length);
System.arraycopy(extra, 0, result, base.length, extra.length);
return result;
}
}