1+ package org .tron .common .logsfilter ;
2+
3+ import static org .mockito .Mockito .mock ;
4+ import static org .mockito .Mockito .spy ;
5+ import static org .mockito .Mockito .when ;
6+
7+ import com .google .protobuf .ByteString ;
8+
9+ import java .lang .reflect .Method ;
10+ import java .util .ArrayList ;
11+ import java .util .HashMap ;
12+ import java .util .List ;
13+
14+ import org .junit .After ;
15+ import org .junit .Assert ;
16+ import org .junit .Before ;
17+ import org .junit .Test ;
18+ import org .tron .common .logsfilter .capsule .TransactionLogTriggerCapsule ;
19+ import org .tron .common .logsfilter .trigger .InternalTransactionPojo ;
20+ import org .tron .common .runtime .InternalTransaction ;
21+ import org .tron .common .runtime .ProgramResult ;
22+ import org .tron .common .runtime .RuntimeImpl ;
23+ import org .tron .common .utils .Sha256Hash ;
24+ import org .tron .core .capsule .BlockCapsule ;
25+ import org .tron .core .capsule .ReceiptCapsule ;
26+ import org .tron .core .capsule .TransactionCapsule ;
27+ import org .tron .core .db .TransactionTrace ;
28+ import org .tron .p2p .utils .ByteArray ;
29+ import org .tron .protos .Protocol ;
30+ import org .tron .protos .contract .BalanceContract ;
31+
32+
33+
34+
35+ public class TransactionLogTriggerCapsuleMockTest {
36+
37+ private static final String OWNER_ADDRESS = "41548794500882809695a8a687866e76d4271a1abc" ;
38+ private static final String RECEIVER_ADDRESS = "41abd4b9367799eaa3197fecb144eb71de1e049150" ;
39+ private static final String CONTRACT_ADDRESS = "A0B4750E2CD76E19DCA331BF5D089B71C3C2798548" ;
40+
41+ private TransactionCapsule transactionCapsule ;
42+ private BlockCapsule blockCapsule ;
43+
44+ @ Before
45+ public void setup () {
46+ blockCapsule = new BlockCapsule (1 ,
47+ Sha256Hash .ZERO_HASH ,
48+ System .currentTimeMillis (),
49+ Sha256Hash .ZERO_HASH .getByteString ()
50+ );
51+ }
52+
53+ @ After
54+ public void clearMocks () {
55+
56+ }
57+
58+
59+ @ Test
60+ public void testConstructorWithTransactionTrace () {
61+ BalanceContract .TransferContract .Builder builder2 =
62+ BalanceContract .TransferContract .newBuilder ()
63+ .setOwnerAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
64+ .setToAddress (ByteString .copyFrom (ByteArray .fromHexString (RECEIVER_ADDRESS )));
65+ transactionCapsule = spy (new TransactionCapsule (builder2 .build (),
66+ Protocol .Transaction .Contract .ContractType .TransferContract ));
67+
68+ TransactionTrace trace = mock (TransactionTrace .class );
69+ ReceiptCapsule receiptCapsule = new ReceiptCapsule (Sha256Hash .ZERO_HASH );
70+ RuntimeImpl runtime = mock (RuntimeImpl .class );
71+ List <Protocol .TransactionInfo .Log > logs = new ArrayList <>();
72+ logs .add (Protocol .TransactionInfo .Log .newBuilder ()
73+ .setAddress (ByteString .copyFrom ("address" .getBytes ()))
74+ .setData (ByteString .copyFrom ("data" .getBytes ()))
75+ .addTopics (ByteString .copyFrom ("topic" .getBytes ()))
76+ .build ());
77+
78+ Protocol .TransactionInfo .Builder builder = Protocol .TransactionInfo .newBuilder ()
79+ .addAllLog (logs );
80+
81+ ProgramResult programResult = ProgramResult .createEmpty ();
82+ programResult .setHReturn ("hreturn" .getBytes ());
83+ programResult .setContractAddress (CONTRACT_ADDRESS .getBytes ());
84+
85+ when (transactionCapsule .getTrxTrace ()).thenReturn (trace );
86+ when (trace .getReceipt ()).thenReturn (receiptCapsule );
87+ when (trace .getRuntime ()).thenReturn (runtime );
88+ when (runtime .getResult ()).thenReturn (programResult );
89+
90+ transactionCapsule .setTrxTrace (trace );
91+
92+ TransactionLogTriggerCapsule triggerCapsule = new TransactionLogTriggerCapsule (
93+ transactionCapsule , blockCapsule ,0 ,0 ,0 ,
94+ builder .build (),0 );
95+
96+ Assert .assertNotNull (triggerCapsule .getTransactionLogTrigger ());
97+ }
98+
99+ @ Test
100+ public void testGetInternalTransactionList () throws Exception {
101+ BalanceContract .TransferContract .Builder builder2 =
102+ BalanceContract .TransferContract .newBuilder ()
103+ .setOwnerAddress (ByteString .copyFrom (ByteArray .fromHexString (OWNER_ADDRESS )))
104+ .setToAddress (ByteString .copyFrom (ByteArray .fromHexString (RECEIVER_ADDRESS )));
105+ transactionCapsule = new TransactionCapsule (builder2 .build (),
106+ Protocol .Transaction .Contract .ContractType .TransferContract );
107+ InternalTransaction internalTransaction = new InternalTransaction (
108+ "parentHash" .getBytes (), 10 , 0 ,
109+ "sendAddress" .getBytes (),
110+ "transferToAddress" .getBytes (),
111+ 100L , "data" .getBytes (), "note" ,
112+ 0L , new HashMap <>()
113+ );
114+ List <InternalTransaction > internalTransactionList = new ArrayList <>();
115+ internalTransactionList .add (internalTransaction );
116+ TransactionLogTriggerCapsule triggerCapsule =
117+ new TransactionLogTriggerCapsule (transactionCapsule , blockCapsule );
118+
119+ Method privateMethod = TransactionLogTriggerCapsule .class .getDeclaredMethod (
120+ "getInternalTransactionList" , List .class );
121+ privateMethod .setAccessible (true );
122+ List <InternalTransactionPojo > pojoList = (List <InternalTransactionPojo >)
123+ privateMethod .invoke (triggerCapsule , internalTransactionList );
124+
125+ Assert .assertNotNull (pojoList );
126+ }
127+
128+ }
0 commit comments