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