-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankTest.java
100 lines (75 loc) · 2.69 KB
/
BankTest.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
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import static org.junit.jupiter.api.Assertions.*;
class BankTest {
Bank bank;
@Test
public void testSmallSingleThread() throws IOException, InterruptedException {
bank = new Bank(1);
bank.processFile("small.txt", 1);
testAccountsSmall();
}
@Test
public void testSmallMultipleThread() throws IOException, InterruptedException {
bank = new Bank(10);
bank.processFile("small.txt", 10);
testAccountsSmall();
}
@Test
public void testMain5k() throws IOException, InterruptedException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream print = new PrintStream(out);
PrintStream ignored = System.out;
System.setOut(print);
String [] args = {"5k.txt", "7"};
Bank.main(args);
String balances = out.toString();
testAccounts5k(balances);
}
@Test
public void testBadMain() throws IOException, InterruptedException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream print = new PrintStream(out);
PrintStream ignored = System.out;
System.setOut(print);
String [] args = { };
Bank.main(args);
String error = out.toString();
assertTrue(error.contains("Args: transaction-file [num-workers [limit]]"));
}
@Test
public void testMain100kSingle() throws IOException, InterruptedException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream print = new PrintStream(out);
PrintStream ignored = System.out;
System.setOut(print);
String [] args = {"100k.txt"};
Bank.main(args);
String balances = out.toString();
testAccounts5k(balances);
}
private void testAccounts5k(String balances) {
StringTokenizer tk = new StringTokenizer(balances, "\n");
while (tk.hasMoreTokens()) {
String currAccount = tk.nextToken();
assertTrue(currAccount.contains("balance: 1000"));
}
}
private void testAccountsSmall() {
Map<Integer, Account> accounts = bank.getBalances();
for (int i = 0; i <accounts.size(); i++) {
if (i % 2 == 0) {
assertEquals(999, accounts.get(i).getBalance());
} else {
assertEquals(1001, accounts.get(i).getBalance());
}
assertEquals(1, accounts.get(i).getTransactions());
}
}
}