]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/test/java/net/ktnx/mobileledger/model/MobileLedgerProfileTest.java
fix merging of account lists so that changes are detected
[mobile-ledger-staging.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 =
41                 MobileLedgerProfile.mergeAccountListsFromWeb(listFromArray(oldList),
42                         listFromArray(newList));
43         assertArrayEquals(expectedResult, result.toArray());
44     }
45     private void negTest(LedgerAccount[] oldList, LedgerAccount[] newList,
46                          LedgerAccount[] expectedResult) {
47         List<LedgerAccount> result =
48                 MobileLedgerProfile.mergeAccountListsFromWeb(listFromArray(oldList),
49                         listFromArray(newList));
50         assertThrows(ArrayComparisonFailure.class,
51                 () -> assertArrayEquals(expectedResult, result.toArray()));
52     }
53     private LedgerAccount[] emptyArray() {
54         return new LedgerAccount[]{};
55     }
56     @Test
57     public void mergeEmptyLists() {
58         aTest(emptyArray(), emptyArray(), emptyArray());
59     }
60     @Test
61     public void mergeIntoEmptyLists() {
62         LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null);
63         aTest(emptyArray(), new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1});
64     }
65     @Test
66     public void mergeEmptyList() {
67         LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null);
68         aTest(new LedgerAccount[]{acc1}, emptyArray(), emptyArray());
69     }
70     @Test
71     public void mergeEqualLists() {
72         LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null);
73         aTest(new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1});
74     }
75     @Test
76     public void mergeFlags() {
77         LedgerAccount acc1a = new LedgerAccount(null, "Acc1", null);
78         LedgerAccount acc1b = new LedgerAccount(null, "Acc1", null);
79         acc1b.setExpanded(true);
80         acc1b.setAmountsExpanded(true);
81         List<LedgerAccount> merged = MobileLedgerProfile.mergeAccountListsFromWeb(
82                 listFromArray(new LedgerAccount[]{acc1a}),
83                 listFromArray(new LedgerAccount[]{acc1b}));
84         assertArrayEquals(new LedgerAccount[]{acc1b}, merged.toArray());
85         assertSame(merged.get(0), acc1a);
86         // restore original values, modified by the merge
87         acc1a.setExpanded(false);
88         acc1a.setAmountsExpanded(false);
89         negTest(new LedgerAccount[]{acc1a}, new LedgerAccount[]{acc1b},
90                 new LedgerAccount[]{new LedgerAccount(null, "Acc1", null)});
91     }
92 }