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