11package org .tron .core .exception ;
22
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertThrows ;
5+ import static org .mockito .ArgumentMatchers .any ;
6+ import static org .mockito .Mockito .mockStatic ;
7+
8+ import java .io .IOException ;
9+ import java .nio .file .Path ;
10+ import org .junit .After ;
311import org .junit .Assert ;
12+ import org .junit .Rule ;
413import org .junit .Test ;
14+ import org .junit .rules .TemporaryFolder ;
15+ import org .junit .runner .RunWith ;
16+ import org .mockito .MockedStatic ;
17+ import org .mockito .Mockito ;
18+ import org .mockito .junit .MockitoJUnitRunner ;
19+ import org .tron .common .log .LogService ;
20+ import org .tron .common .zksnark .JLibrustzcash ;
21+ import org .tron .core .zen .ZksnarkInitService ;
522
23+ @ RunWith (MockitoJUnitRunner .class )
624public class TronErrorTest {
725
26+ @ Rule
27+ public TemporaryFolder temporaryFolder = new TemporaryFolder ();
28+
29+ @ After
30+ public void clearMocks () {
31+ Mockito .clearAllCaches ();
32+ }
33+
834 @ Test
935 public void testTronError () {
1036 TronError tronError = new TronError ("message" , TronError .ErrCode .WITNESS_KEYSTORE_LOAD );
@@ -16,4 +42,26 @@ public void testTronError() {
1642 tronError = new TronError (new Throwable (), TronError .ErrCode .LEVELDB_INIT );
1743 Assert .assertEquals (tronError .getErrCode (), TronError .ErrCode .LEVELDB_INIT );
1844 }
45+
46+ @ Test
47+ public void ZksnarkInitTest () {
48+ try (MockedStatic <JLibrustzcash > mock = mockStatic (JLibrustzcash .class )) {
49+ mock .when (JLibrustzcash ::isOpenZen ).thenReturn (true );
50+ mock .when (() -> JLibrustzcash .librustzcashInitZksnarkParams (any ()))
51+ .thenAnswer (invocation -> {
52+ throw new ZksnarkException ("Zksnark init failed" );
53+ });
54+ TronError thrown = assertThrows (TronError .class ,
55+ ZksnarkInitService ::librustzcashInitZksnarkParams );
56+ assertEquals (TronError .ErrCode .ZCASH_INIT , thrown .getErrCode ());
57+ }
58+ }
59+
60+ @ Test
61+ public void LogLoadTest () throws IOException {
62+ LogService .load ("non-existent.xml" );
63+ Path path = temporaryFolder .newFile ("logback.xml" ).toPath ();
64+ TronError thrown = assertThrows (TronError .class , () -> LogService .load (path .toString ()));
65+ assertEquals (TronError .ErrCode .LOG_LOAD , thrown .getErrCode ());
66+ }
1967}
0 commit comments