Skip to content

Commit

Permalink
feat(test): improve unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lxcmyf committed Sep 5, 2024
1 parent 773b374 commit d40da4a
Show file tree
Hide file tree
Showing 42 changed files with 3,369 additions and 56 deletions.
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.tron.common.logsfilter.capsule.ContractEventTriggerCapsule;
import org.tron.common.logsfilter.capsule.FilterTriggerCapsule;
import org.tron.common.logsfilter.capsule.TriggerCapsule;
import org.tron.common.runtime.LogEventWrapper;
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI.Entry;

@Slf4j
public class FilterQueryTest {

@Test
Expand All @@ -37,52 +41,72 @@ public synchronized void testParseFilterQueryBlockNumber() {

@Test
public synchronized void testMatchFilter() {
String[] addrList = {"address1", "address2"};
String[] adrList = {"address1", "address2"};
String[] topList = {"top1", "top2"};
Map topMap = new HashMap<String, String>();
Map<String, String> topMap = new HashMap<>();
List<byte[]> addressList = new ArrayList<>();
addressList.add(addrList[0].getBytes());
addressList.add(addrList[1].getBytes());
addressList.add(adrList[0].getBytes());
addressList.add(adrList[1].getBytes());
topMap.put("1", topList[0]);
topMap.put("2", topList[1]);
LogEventWrapper event = new LogEventWrapper();
((LogEventWrapper) event).setTopicList(addressList);
((LogEventWrapper) event).setData(new byte[]{});
((LogEventWrapper) event).setEventSignature("");
((LogEventWrapper) event).setAbiEntry(Entry.newBuilder().setName("testABI").build());
event.setBlockNumber(new Long(123));
event.setTopicList(addressList);
event.setData(new byte[]{});
event.setEventSignature("");
event.setAbiEntry(Entry.newBuilder().setName("testABI").build());
event.setBlockNumber(123L);
ContractEventTriggerCapsule capsule = new ContractEventTriggerCapsule(event);
capsule.setContractEventTrigger(capsule.getContractEventTrigger());
capsule.getContractEventTrigger().setContractAddress("address1");
capsule.setLatestSolidifiedBlockNumber(0L);
capsule.setData(capsule.getData());
capsule.setTopicList(capsule.getTopicList());
capsule.setAbiEntry(capsule.getAbiEntry());
capsule.getContractEventTrigger().setTopicMap(topMap);

{
Assert.assertEquals(true, matchFilter(capsule.getContractEventTrigger()));
Assert.assertTrue(matchFilter(capsule.getContractEventTrigger()));
}

{
FilterQuery filterQuery = new FilterQuery();
filterQuery.setFromBlock(1);
filterQuery.setToBlock(100);
EventPluginLoader.getInstance().setFilterQuery(filterQuery);
Assert.assertEquals(false, matchFilter(capsule.getContractEventTrigger()));
Assert.assertFalse(matchFilter(capsule.getContractEventTrigger()));
}

{
FilterQuery filterQuery = new FilterQuery();
filterQuery.setFromBlock(133);
filterQuery.setToBlock(190);
EventPluginLoader.getInstance().setFilterQuery(filterQuery);
Assert.assertEquals(false, matchFilter(capsule.getContractEventTrigger()));
Assert.assertFalse(matchFilter(capsule.getContractEventTrigger()));
}

{
FilterQuery filterQuery = new FilterQuery();
filterQuery.setFromBlock(100);
filterQuery.setToBlock(190);
filterQuery.setContractAddressList(Arrays.asList(addrList));
filterQuery.setContractAddressList(Arrays.asList(adrList));
filterQuery.setContractTopicList(Arrays.asList(topList));
EventPluginLoader.getInstance().setFilterQuery(filterQuery);
Assert.assertEquals(true, matchFilter(capsule.getContractEventTrigger()));
Assert.assertTrue(matchFilter(capsule.getContractEventTrigger()));
capsule.processTrigger();
}

FilterTriggerCapsule filterTriggerCapsule = new FilterTriggerCapsule();
try {
filterTriggerCapsule.processFilterTrigger();
} catch (Exception e) {
logger.info(e.getMessage());
}

TriggerCapsule triggerCapsule = new TriggerCapsule();
try {
triggerCapsule.processTrigger();
} catch (Exception e) {
logger.info(e.getMessage());
}
}
}
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());
}
}
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());
}
}
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());
}
}

}
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());
}
}

}
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();
}

}
Loading

0 comments on commit d40da4a

Please sign in to comment.