1
exercism fetch java bank-account

src/test/java/BankAccountTest.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Random;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class BankAccountTest {
    @Rule
    public ExpectedException expectedException = ExpectedException.none();
    private BankAccount bankAccount;

    @Before
    public void setup() {
        bankAccount = new BankAccount();
    }

    @Test
    public void newlyOpenedAccountHasEmptyBalance() throws BankAccountActionInvalidException {
        bankAccount.open();

        assertEquals(0, bankAccount.getBalance());
    }

    @Ignore("Remove to run test")
    @Test
    public void canDepositMoney() throws BankAccountActionInvalidException {
        bankAccount.open();

        bankAccount.deposit(10);

        assertEquals(10, bankAccount.getBalance());
    }

    @Ignore("Remove to run test")
    @Test
    public void canDepositMoneySequentially() throws BankAccountActionInvalidException {
        bankAccount.open();

        bankAccount.deposit(5);
        bankAccount.deposit(23);

        assertEquals(28, bankAccount.getBalance());
    }

    @Ignore("Remove to run test")
    @Test
    public void canWithdrawMoney() throws BankAccountActionInvalidException {
        bankAccount.open();
        bankAccount.deposit(10);

        bankAccount.withdraw(5);

        assertEquals(5, bankAccount.getBalance());
    }

    @Ignore("Remove to run test")
    @Test
    public void canWithdrawMoneySequentially() throws BankAccountActionInvalidException {
        bankAccount.open();
        bankAccount.deposit(23);

        bankAccount.withdraw(10);
        bankAccount.withdraw(13);

        assertEquals(0, bankAccount.getBalance());
    }

    @Ignore("Remove to run test")
    @Test
    public void cannotWithdrawMoneyFromEmptyAccount() throws BankAccountActionInvalidException {
        bankAccount.open();

        expectedException.expect(BankAccountActionInvalidException.class);
        expectedException.expectMessage("Cannot withdraw money from an empty account");

        bankAccount.withdraw(5);
    }

    @Ignore("Remove to run test")
    @Test
    public void cannotWithdrawMoreMoneyThanYouHave() throws BankAccountActionInvalidException {
        bankAccount.open();
        bankAccount.deposit(6);

        expectedException.expect(BankAccountActionInvalidException.class);
        expectedException.expectMessage("Cannot withdraw more money than is currently in the account");

        bankAccount.withdraw(7);
    }

    @Ignore("Remove to run test")
    @Test
    public void cannotDepositNegativeAmount() throws BankAccountActionInvalidException {
        bankAccount.open();

        expectedException.expect(BankAccountActionInvalidException.class);
        expectedException.expectMessage("Cannot deposit or withdraw negative amount");

        bankAccount.deposit(-1);
    }

    @Ignore("Remove to run test")
    @Test
    public void cannotWithdrawNegativeAmount() throws BankAccountActionInvalidException {
        bankAccount.open();
        bankAccount.deposit(105);

        expectedException.expect(BankAccountActionInvalidException.class);
        expectedException.expectMessage("Cannot deposit or withdraw negative amount");

        bankAccount.withdraw(-5);
    }

    @Ignore("Remove to run test")
    @Test
    public void cannotGetBalanceOfClosedAccount() throws BankAccountActionInvalidException {
        bankAccount.open();
        bankAccount.deposit(10);
        bankAccount.close();

        expectedException.expect(BankAccountActionInvalidException.class);
        expectedException.expectMessage("Account closed");

        bankAccount.getBalance();
    }

    @Ignore("Remove to run test")
    @Test
    public void cannotDepositMoneyIntoClosedAccount() throws BankAccountActionInvalidException {
        bankAccount.open();
        bankAccount.close();

        expectedException.expect(BankAccountActionInvalidException.class);
        expectedException.expectMessage("Account closed");

        bankAccount.deposit(5);
    }

    @Ignore("Remove to run test")
    @Test
    public void cannotWithdrawMoneyFromClosedAccount() throws BankAccountActionInvalidException {
        bankAccount.open();
        bankAccount.deposit(20);
        bankAccount.close();

        expectedException.expect(BankAccountActionInvalidException.class);
        expectedException.expectMessage("Account closed");

        bankAccount.withdraw(5);
    }

    @Ignore("Remove to run test")
    @Test
    public void bankAccountIsClosedBeforeItIsOpened() throws BankAccountActionInvalidException {
        expectedException.expect(BankAccountActionInvalidException.class);
        expectedException.expectMessage("Account closed");

        bankAccount.getBalance();
    }

    @Ignore("Remove to run test")
    @Test
    public void canAdjustBalanceConcurrently() throws BankAccountActionInvalidException, InterruptedException {
        bankAccount.open();
        bankAccount.deposit(1000);

        for (int i = 0; i < 10; i++) {
            adjustBalanceConcurrently();
        }
    }

    private void adjustBalanceConcurrently() throws BankAccountActionInvalidException, InterruptedException {
        Random random = new Random();

        Thread[] threads = new Thread[1000];
        for (int i = 0; i < 1000; i++) {
            threads[i] = new Thread(() -> {
                try {
                    bankAccount.deposit(5);
                    Thread.sleep(random.nextInt(10));
                    bankAccount.withdraw(5);
                } catch (BankAccountActionInvalidException e) {
                    fail("Exception should not be thrown: " + e.getMessage());
                } catch (InterruptedException ignored) {
                }
            });
            threads[i].start();
        }

        for (Thread thread : threads) {
            thread.join();
        }

        assertEquals(1000, bankAccount.getBalance());
    }
}