Skip to content

Commit

Permalink
Merge pull request #5988 from lxcmyf/feature/improve_coverage
Browse files Browse the repository at this point in the history
feat(test): improve unit test coverage
  • Loading branch information
lvs007 authored Oct 9, 2024
2 parents db74739 + f9a86f7 commit f7ecfc3
Show file tree
Hide file tree
Showing 48 changed files with 3,978 additions and 100 deletions.
67 changes: 67 additions & 0 deletions framework/src/test/java/org/tron/common/EntityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.tron.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.Lists;
import java.util.HashMap;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Before;
import org.junit.Test;
import org.tron.common.entity.NodeInfo;
import org.tron.common.entity.NodeInfo.MachineInfo;
import org.tron.common.entity.NodeInfo.MachineInfo.DeadLockThreadInfo;

public class EntityTest {

private final MachineInfo machineInfo = new MachineInfo();
private final DeadLockThreadInfo deadLockThreadInfo = new DeadLockThreadInfo();

@Before
public void setup() {
deadLockThreadInfo.setName("name");
deadLockThreadInfo.setLockName("lockName");
deadLockThreadInfo.setLockOwner("lockOwner");
deadLockThreadInfo.setState("state");
deadLockThreadInfo.setStackTrace("stackTrace");
deadLockThreadInfo.setWaitTime(0L);
deadLockThreadInfo.setBlockTime(0L);
machineInfo.setDeadLockThreadInfoList(Lists.newArrayList(deadLockThreadInfo));
machineInfo.setJavaVersion("1.8");
machineInfo.setOsName("linux");
}

@Test
public void testMachineInfo() {
machineInfo.setDeadLockThreadCount(3);
assertTrue(CollectionUtils.isNotEmpty(machineInfo.getDeadLockThreadInfoList()));
assertEquals(3, machineInfo.getDeadLockThreadCount());

}

@Test
public void testDeadLockThreadInfo() {
assertEquals("name", deadLockThreadInfo.getName());
assertEquals("lockName", deadLockThreadInfo.getLockName());
assertEquals("lockOwner", deadLockThreadInfo.getLockOwner());
assertEquals("state", deadLockThreadInfo.getState());
assertEquals("stackTrace", deadLockThreadInfo.getStackTrace());
assertEquals(0, deadLockThreadInfo.getBlockTime());
assertEquals(0, deadLockThreadInfo.getWaitTime());

}

@Test
public void testNodeInfo() {
NodeInfo nodeInfo = new NodeInfo();
nodeInfo.setTotalFlow(1L);
nodeInfo.setCheatWitnessInfoMap(new HashMap<>());
assertEquals(1, nodeInfo.getTotalFlow());
assertNotNull(nodeInfo.getCheatWitnessInfoMap());
nodeInfo.setMachineInfo(machineInfo);
nodeInfo.setBlock("block");
nodeInfo.setSolidityBlock("solidityBlock");
nodeInfo.transferToProtoEntity();
}
}
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);
}

}
Loading

0 comments on commit f7ecfc3

Please sign in to comment.