]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/test/java/net/ktnx/mobileledger/model/MobileLedgerProfileTest.java
rework account list management to be fully asynchronous
[mobile-ledger.git] / app / src / test / java / net / ktnx / mobileledger / model / MobileLedgerProfileTest.java
1 /*
2  * Copyright © 2020 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.model;
19
20 import org.junit.Test;
21 import org.junit.internal.ArrayComparisonFailure;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26
27 import static org.junit.Assert.assertArrayEquals;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertThrows;
30
31 public class MobileLedgerProfileTest {
32     private List<LedgerAccount> listFromArray(LedgerAccount[] array) {
33         ArrayList<LedgerAccount> result = new ArrayList<>();
34         Collections.addAll(result, array);
35
36         return result;
37     }
38     private void aTest(LedgerAccount[] oldList, LedgerAccount[] newList,
39                        LedgerAccount[] expectedResult) {
40         List<LedgerAccount> result = MobileLedgerProfile.mergeAccountLists(listFromArray(oldList),
41                 listFromArray(newList));
42         assertArrayEquals(expectedResult, result.toArray());
43     }
44     private void negTest(LedgerAccount[] oldList, LedgerAccount[] newList,
45                          LedgerAccount[] expectedResult) {
46         List<LedgerAccount> result = MobileLedgerProfile.mergeAccountLists(listFromArray(oldList),
47                 listFromArray(newList));
48         assertThrows(ArrayComparisonFailure.class,
49                 () -> assertArrayEquals(expectedResult, result.toArray()));
50     }
51     private LedgerAccount[] emptyArray() {
52         return new LedgerAccount[]{};
53     }
54     @Test
55     public void mergeEmptyLists() {
56         aTest(emptyArray(), emptyArray(), emptyArray());
57     }
58     @Test
59     public void mergeIntoEmptyLists() {
60         LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null);
61         aTest(emptyArray(), new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1});
62     }
63     @Test
64     public void mergeEmptyList() {
65         LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null);
66         aTest(new LedgerAccount[]{acc1}, emptyArray(), emptyArray());
67     }
68     @Test
69     public void mergeEqualLists() {
70         LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null);
71         aTest(new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1});
72     }
73     @Test
74     public void mergeFlags() {
75         LedgerAccount acc1a = new LedgerAccount(null, "Acc1", null);
76         LedgerAccount acc1b = new LedgerAccount(null, "Acc1", null);
77         acc1b.setExpanded(true);
78         acc1b.setAmountsExpanded(true);
79         List<LedgerAccount> merged =
80                 MobileLedgerProfile.mergeAccountLists(listFromArray(new LedgerAccount[]{acc1a}),
81                         listFromArray(new LedgerAccount[]{acc1b}));
82         assertArrayEquals(new LedgerAccount[]{acc1b}, merged.toArray());
83         assertSame(merged.get(0), acc1a);
84         // restore original values, modified by the merge
85         acc1a.setExpanded(false);
86         acc1a.setAmountsExpanded(false);
87         negTest(new LedgerAccount[]{acc1a}, new LedgerAccount[]{acc1b},
88                 new LedgerAccount[]{new LedgerAccount(null, "Acc1", null)});
89     }
90 }