-
Notifications
You must be signed in to change notification settings - Fork 9
/
TransactionTest.java
123 lines (93 loc) · 4.39 KB
/
TransactionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.ajlopez.blockchain.core;
import com.ajlopez.blockchain.core.types.Address;
import com.ajlopez.blockchain.core.types.Coin;
import com.ajlopez.blockchain.test.utils.FactoryHelper;
import org.junit.Assert;
import org.junit.Test;
/**
* Created by ajlopez on 30/09/2017.
*/
public class TransactionTest {
@Test
public void createTransaction() {
Address sender = FactoryHelper.createRandomAddress();
Address receiver = FactoryHelper.createRandomAddress();
Coin value = Coin.ONE;
Transaction tx = new Transaction(sender, receiver, value, 42, null, 6000000, Coin.ZERO);
Assert.assertEquals(sender, tx.getSender());
Assert.assertEquals(receiver, tx.getReceiver());
Assert.assertEquals(value, tx.getValue());
Assert.assertEquals(42, tx.getNonce());
Assert.assertNotNull(tx.getHash());
Assert.assertNull(tx.getData());
Assert.assertEquals(6000000, tx.getGas());
Assert.assertEquals(Coin.ZERO, tx.getGasPrice());
}
@Test
public void createTransactionWithNullValue() {
Address sender = FactoryHelper.createRandomAddress();
Address receiver = FactoryHelper.createRandomAddress();
Transaction tx = new Transaction(sender, receiver, null, 42, null, 6000000, Coin.ZERO);
Assert.assertEquals(sender, tx.getSender());
Assert.assertEquals(receiver, tx.getReceiver());
Assert.assertEquals(Coin.ZERO, tx.getValue());
Assert.assertEquals(42, tx.getNonce());
Assert.assertNotNull(tx.getHash());
Assert.assertNull(tx.getData());
Assert.assertEquals(6000000, tx.getGas());
Assert.assertEquals(Coin.ZERO, tx.getGasPrice());
}
@Test
public void createTransactionWithNullGasPrice() {
Address sender = FactoryHelper.createRandomAddress();
Address receiver = FactoryHelper.createRandomAddress();
Transaction tx = new Transaction(sender, receiver, null, 42, null, 6000000, null);
Assert.assertEquals(sender, tx.getSender());
Assert.assertEquals(receiver, tx.getReceiver());
Assert.assertEquals(Coin.ZERO, tx.getValue());
Assert.assertEquals(42, tx.getNonce());
Assert.assertNotNull(tx.getHash());
Assert.assertNull(tx.getData());
Assert.assertEquals(6000000, tx.getGas());
Assert.assertEquals(Coin.ZERO, tx.getGasPrice());
}
@Test(expected = IllegalStateException.class)
public void createTransactionWithNegativeNonce() {
Address sender = FactoryHelper.createRandomAddress();
Address receiver = FactoryHelper.createRandomAddress();
Coin value = Coin.ONE;
new Transaction(sender, receiver, value, -1, null, 6000000, Coin.ZERO);
}
@Test(expected = IllegalStateException.class)
public void createTransactionWithNoSender() {
Address receiver = FactoryHelper.createRandomAddress();
Coin value = Coin.ONE;
new Transaction(null, receiver, value, 42, null, 6000000, Coin.ZERO);
}
@Test(expected = IllegalStateException.class)
public void createTransactionWithNoReceiver() {
Address sender = FactoryHelper.createRandomAddress();
Coin value = Coin.ONE;
new Transaction(sender, null, value, 42, null, 6000000, Coin.ZERO);
}
@Test
public void equalTransaction() {
Address sender = FactoryHelper.createRandomAddress();
Address receiver = FactoryHelper.createRandomAddress();
Coin value = Coin.ONE;
byte[] data = FactoryHelper.createRandomBytes(128);
Transaction tx = new Transaction(sender, receiver, value, 42, data, 6000000, Coin.ZERO);
Transaction tx2 = new Transaction(sender, receiver, value, 42, data, 6000000, Coin.ZERO);
Transaction tx3 = new Transaction(sender, receiver, value, 42, data, 6000000, Coin.ONE);
Assert.assertTrue(tx.equals(tx));
Assert.assertTrue(tx.equals(tx2));
Assert.assertTrue(tx2.equals(tx));
Assert.assertEquals(tx.hashCode(), tx2.hashCode());
Assert.assertFalse(tx.equals(tx3));
Assert.assertFalse(tx2.equals(tx3));
Assert.assertFalse(tx3.equals(tx));
Assert.assertFalse(tx3.equals(tx2));
Assert.assertFalse(tx.equals(null));
Assert.assertFalse(tx.equals("foo"));
}
}