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.
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.
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/>.
18 package net.ktnx.mobileledger.model;
20 import org.junit.Test;
21 import org.junit.internal.ArrayComparisonFailure;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
27 import static org.junit.Assert.assertArrayEquals;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertThrows;
31 public class MobileLedgerProfileTest {
32 private List<LedgerAccount> listFromArray(LedgerAccount[] array) {
33 ArrayList<LedgerAccount> result = new ArrayList<>();
34 Collections.addAll(result, array);
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());
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()));
51 private LedgerAccount[] emptyArray() {
52 return new LedgerAccount[]{};
55 public void mergeEmptyLists() {
56 aTest(emptyArray(), emptyArray(), emptyArray());
59 public void mergeIntoEmptyLists() {
60 LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null);
61 aTest(emptyArray(), new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1});
64 public void mergeEmptyList() {
65 LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null);
66 aTest(new LedgerAccount[]{acc1}, emptyArray(), emptyArray());
69 public void mergeEqualLists() {
70 LedgerAccount acc1 = new LedgerAccount(null, "Acc1", null);
71 aTest(new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1}, new LedgerAccount[]{acc1});
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)});