-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): improve unit test coverage
- Loading branch information
Showing
42 changed files
with
3,369 additions
and
56 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
framework/src/test/java/org/tron/common/MultiLayoutPatternTest.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,72 @@ | ||
package org.tron.common; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.classic.spi.LoggingEvent; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.slf4j.LoggerFactory; | ||
import org.tron.common.log.layout.MultiLayoutPattern; | ||
|
||
public class MultiLayoutPatternTest { | ||
|
||
private MultiLayoutPattern multiLayoutPattern; | ||
private LoggerContext context; | ||
|
||
@Before | ||
public void setUp() { | ||
context = new LoggerContext(); | ||
multiLayoutPattern = new MultiLayoutPattern(); | ||
multiLayoutPattern.setContext(context); | ||
|
||
MultiLayoutPattern.Rule rule1 = new MultiLayoutPattern.Rule(); | ||
rule1.setLogger("com.example.app1"); | ||
assertNotNull(rule1.getLogger()); | ||
rule1.setPattern("%date [%thread] %-5level %logger{36} - %msg%n"); | ||
assertNotNull(rule1.getPattern()); | ||
rule1.setOutputPatternAsHeader(true); | ||
assertTrue(rule1.isOutputPatternAsHeader()); | ||
multiLayoutPattern.addRule(rule1); | ||
|
||
MultiLayoutPattern.Rule rule2 = new MultiLayoutPattern.Rule(); | ||
rule2.setLogger("com.example.app2"); | ||
rule2.setPattern("%msg%n"); | ||
multiLayoutPattern.addRule(rule2); | ||
|
||
multiLayoutPattern.start(); | ||
} | ||
|
||
@Test | ||
public void testEncodeForSpecificLogger() { | ||
ILoggingEvent event1 = createLoggingEvent("com.example.app1", "Test message 1"); | ||
byte[] encoded1 = multiLayoutPattern.encode(event1); | ||
String result1 = new String(encoded1); | ||
assertTrue(result1.contains("Test message 1")); | ||
|
||
ILoggingEvent event2 = createLoggingEvent("com.example.app2", "Test message 2"); | ||
byte[] encoded2 = multiLayoutPattern.encode(event2); | ||
String result2 = new String(encoded2); | ||
assertEquals("Test message 2\n", result2); | ||
} | ||
|
||
@Test | ||
public void testEncodeForRootLogger() { | ||
ILoggingEvent event = createLoggingEvent(Logger.ROOT_LOGGER_NAME, "Root logger message"); | ||
byte[] encoded = multiLayoutPattern.encode(event); | ||
String result = new String(encoded); | ||
assertFalse(result.contains("Root logger message")); | ||
} | ||
|
||
private ILoggingEvent createLoggingEvent(String loggerName, String message) { | ||
Logger logger = (Logger) LoggerFactory.getLogger(loggerName); | ||
return new LoggingEvent(loggerName, logger, Level.INFO, message, null, null); | ||
} | ||
|
||
} |
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
38 changes: 38 additions & 0 deletions
38
framework/src/test/java/org/tron/common/logsfilter/capsule/BlockFilterCapsuleTest.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,38 @@ | ||
package org.tron.common.logsfilter.capsule; | ||
|
||
import com.google.protobuf.ByteString; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.tron.common.utils.Sha256Hash; | ||
import org.tron.core.capsule.BlockCapsule; | ||
|
||
public class BlockFilterCapsuleTest { | ||
|
||
private BlockFilterCapsule blockFilterCapsule; | ||
|
||
@Before | ||
public void setUp() { | ||
BlockCapsule blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH, | ||
System.currentTimeMillis(), ByteString.EMPTY); | ||
blockFilterCapsule = new BlockFilterCapsule(blockCapsule, false); | ||
} | ||
|
||
@Test | ||
public void testSetAndGetBlockHash() { | ||
blockFilterCapsule | ||
.setBlockHash("e58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f"); | ||
System.out.println(blockFilterCapsule); | ||
Assert.assertEquals("e58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f", | ||
blockFilterCapsule.getBlockHash()); | ||
} | ||
|
||
@Test | ||
public void testSetAndIsSolidified() { | ||
blockFilterCapsule = new BlockFilterCapsule( | ||
"e58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f", false); | ||
blockFilterCapsule.setSolidified(true); | ||
blockFilterCapsule.processFilterTrigger(); | ||
Assert.assertTrue(blockFilterCapsule.isSolidified()); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
framework/src/test/java/org/tron/common/logsfilter/capsule/BlockLogTriggerCapsuleTest.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,35 @@ | ||
package org.tron.common.logsfilter.capsule; | ||
|
||
import com.google.protobuf.ByteString; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.tron.common.utils.Sha256Hash; | ||
import org.tron.core.capsule.BlockCapsule; | ||
|
||
public class BlockLogTriggerCapsuleTest { | ||
|
||
private BlockLogTriggerCapsule blockLogTriggerCapsule; | ||
|
||
@Before | ||
public void setUp() { | ||
BlockCapsule blockCapsule = new BlockCapsule(1, Sha256Hash.ZERO_HASH, | ||
System.currentTimeMillis(), ByteString.EMPTY); | ||
blockLogTriggerCapsule = new BlockLogTriggerCapsule(blockCapsule); | ||
} | ||
|
||
@Test | ||
public void testSetAndGetBlockLogTrigger() { | ||
blockLogTriggerCapsule | ||
.setBlockLogTrigger(blockLogTriggerCapsule.getBlockLogTrigger()); | ||
Assert.assertEquals(1, | ||
blockLogTriggerCapsule.getBlockLogTrigger().getBlockNumber()); | ||
} | ||
|
||
@Test | ||
public void testSetLatestSolidifiedBlockNumber() { | ||
blockLogTriggerCapsule.setLatestSolidifiedBlockNumber(0); | ||
Assert.assertEquals(0, | ||
blockLogTriggerCapsule.getBlockLogTrigger().getLatestSolidifiedBlockNumber()); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ework/src/test/java/org/tron/common/logsfilter/capsule/ContractLogTriggerCapsuleTest.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,35 @@ | ||
package org.tron.common.logsfilter.capsule; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.tron.common.logsfilter.trigger.Trigger.CONTRACTLOG_TRIGGER_NAME; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.tron.common.logsfilter.trigger.ContractLogTrigger; | ||
|
||
@Slf4j | ||
public class ContractLogTriggerCapsuleTest { | ||
|
||
private ContractLogTriggerCapsule capsule; | ||
|
||
@Before | ||
public void setUp() { | ||
ContractLogTrigger contractLogTrigger = new ContractLogTrigger(); | ||
contractLogTrigger.setBlockNumber(0L); | ||
capsule = new ContractLogTriggerCapsule(contractLogTrigger); | ||
capsule.setLatestSolidifiedBlockNumber(0); | ||
} | ||
|
||
@Test | ||
public void testSetAndGetContractLogTrigger() { | ||
capsule.setContractLogTrigger(capsule.getContractLogTrigger()); | ||
assertEquals(CONTRACTLOG_TRIGGER_NAME, capsule.getContractLogTrigger().getTriggerName()); | ||
try { | ||
capsule.processTrigger(); | ||
} catch (Exception e) { | ||
logger.info(e.getMessage()); | ||
} | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
framework/src/test/java/org/tron/common/logsfilter/capsule/ContractTriggerCapsuleTest.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,52 @@ | ||
package org.tron.common.logsfilter.capsule; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.ArrayList; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.tron.common.logsfilter.trigger.ContractTrigger; | ||
import org.tron.common.runtime.vm.LogInfo; | ||
|
||
@Slf4j | ||
public class ContractTriggerCapsuleTest { | ||
|
||
private ContractTriggerCapsule capsule; | ||
|
||
@Before | ||
public void setUp() { | ||
ContractTrigger contractTrigger = new ContractTrigger(); | ||
contractTrigger.setBlockNumber(0L); | ||
contractTrigger.setRemoved(false); | ||
LogInfo logInfo = | ||
new LogInfo(bytesToAddress(new byte[] {0x11}), new ArrayList<>(), new byte[0]); | ||
contractTrigger.setLogInfo(logInfo); | ||
contractTrigger.setRawData(new RawData(null, null, null)); | ||
contractTrigger.setAbi(contractTrigger.getAbi()); | ||
capsule = new ContractTriggerCapsule(contractTrigger); | ||
|
||
} | ||
|
||
private byte[] bytesToAddress(byte[] address) { | ||
byte[] data = new byte[20]; | ||
System.arraycopy(address, 0, data, 20 - address.length, address.length); | ||
return data; | ||
} | ||
|
||
@Test | ||
public void testSetAndGetContractTrigger() { | ||
capsule.setContractTrigger(capsule.getContractTrigger()); | ||
capsule.setBlockHash("e58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f"); | ||
capsule.setLatestSolidifiedBlockNumber(0); | ||
assertEquals(0, capsule.getContractTrigger().getLatestSolidifiedBlockNumber()); | ||
assertEquals("e58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f", | ||
capsule.getContractTrigger().getBlockHash()); | ||
try { | ||
capsule.processTrigger(); | ||
} catch (Exception e) { | ||
logger.info(e.getMessage()); | ||
} | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
framework/src/test/java/org/tron/common/logsfilter/capsule/LogsFilterCapsuleTest.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,33 @@ | ||
package org.tron.common.logsfilter.capsule; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
|
||
import java.util.ArrayList; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.tron.common.bloom.Bloom; | ||
|
||
public class LogsFilterCapsuleTest { | ||
|
||
private LogsFilterCapsule capsule; | ||
|
||
@Before | ||
public void setUp() { | ||
capsule = new LogsFilterCapsule(0, | ||
"e58f33f9baf9305dc6f82b9f1934ea8f0ade2defb951258d50167028c780351f", | ||
new Bloom(), new ArrayList<>(), true, false); | ||
} | ||
|
||
@Test | ||
public void testSetAndGetLogsFilterCapsule() { | ||
capsule.setBlockNumber(capsule.getBlockNumber()); | ||
capsule.setBlockHash(capsule.getBlockHash()); | ||
capsule.setSolidified(capsule.isSolidified()); | ||
capsule.setBloom(capsule.getBloom()); | ||
capsule.setRemoved(capsule.isRemoved()); | ||
capsule.setTxInfoList(capsule.getTxInfoList()); | ||
assertNotNull(capsule.toString()); | ||
capsule.processFilterTrigger(); | ||
} | ||
|
||
} |
Oops, something went wrong.