1616
1717package com .techsenger .toolkit .fx .collections ;
1818
19+ import java .util .ArrayList ;
20+ import java .util .Comparator ;
21+ import java .util .List ;
22+ import javafx .beans .property .IntegerProperty ;
23+ import javafx .beans .property .Property ;
24+ import javafx .beans .property .SimpleIntegerProperty ;
1925import javafx .collections .FXCollections ;
2026import javafx .collections .ObservableList ;
2127import static org .assertj .core .api .Assertions .assertThat ;
2935 */
3036public class ListSynchronizerTest {
3137
38+ private static class TestItem {
39+
40+ private final IntegerProperty value = new SimpleIntegerProperty ();
41+
42+ TestItem (int value ) {
43+ this .value .set (value );
44+ }
45+
46+ public int getValue () {
47+ return value .get ();
48+ }
49+
50+ public void setValue (int value ) {
51+ this .value .set (value );
52+ }
53+
54+ public IntegerProperty valueProperty () {
55+ return value ;
56+ }
57+ }
58+
3259 private ObservableList <Integer > source ;
3360 private ObservableList <String > target ;
3461 private ListSynchronizer <Integer , String > synchronizer ;
62+ private List <Integer > addedItems ;
63+ private List <Integer > removedItems ;
3564
3665 @ BeforeEach
3766 void setUp () {
3867 source = FXCollections .observableArrayList ();
3968 target = FXCollections .observableArrayList ();
40- synchronizer = new ListSynchronizer <>(source , target , Object ::toString );
69+ addedItems = new ArrayList <>();
70+ removedItems = new ArrayList <>();
71+ synchronizer = new ListSynchronizer <>(source , target , Object ::toString , addedItems ::add , removedItems ::add );
4172 }
4273
4374 @ AfterEach
@@ -49,57 +80,110 @@ void tearDown() {
4980 void initialSync_withAddedElements_shouldMirrorToSecondary () {
5081 source .addAll (1 , 2 , 3 );
5182
52- assertThat (target )
53- .hasSize (3 )
54- .containsExactly ("1" , "2" , "3" );
83+ assertThat (target ).containsExactly ("1" , "2" , "3" );
84+ assertThat (addedItems ).containsExactly (1 , 2 , 3 );
5585 }
5686
5787 @ Test
5888 void addAtIndex_whenInsertingMiddleElement_shouldMaintainOrder () {
5989 source .addAll (1 , 3 );
6090 source .add (1 , 2 );
6191
62- assertThat (target )
63- .hasSize (3 )
64- .containsExactly ("1" , "2" , "3" );
92+ assertThat (target ).containsExactly ("1" , "2" , "3" );
93+ assertThat (addedItems ).containsExactly (1 , 3 , 2 );
6594 }
6695
6796 @ Test
6897 void remove_whenMiddleElementRemoved_shouldUpdateSecondary () {
6998 source .addAll (1 , 2 , 3 );
7099 source .remove (1 );
71100
72- assertThat (target )
73- .hasSize (2 )
74- .containsExactly ("1" , "3" );
101+ assertThat (target ).containsExactly ("1" , "3" );
102+ assertThat (removedItems ).containsExactly (2 );
75103 }
76104
77105 @ Test
78- void replace_whenFirstElementReplaced_shouldUpdateSecondary () {
79- source .addAll (1 , 2 );
80- source .set (0 , 3 );
106+ void replace_whenElementReplaced_shouldUpdateSecondary () {
107+ source .addAll (1 , 2 , 3 );
108+ source .set (1 , 4 );
81109
82- assertThat (target )
83- . hasSize ( 2 )
84- .containsExactly ("3" , "2" );
110+ assertThat (target ). containsExactly ( "1" , "4" , "3" );
111+ assertThat ( addedItems ). containsExactly ( 1 , 2 , 3 , 4 );
112+ assertThat ( removedItems ) .containsExactly (2 );
85113 }
86114
87115 @ Test
88- void dispose_whenCalled_shouldStopSynchronization () {
89- synchronizer . dispose ( );
90- source . add ( 1 );
116+ void permutation_whenListSorted_shouldMirrorOrder () {
117+ source . addAll ( 3 , 1 , 2 );
118+ FXCollections . sort ( source );
91119
92- assertThat (target ).isEmpty ();
120+ assertThat (target ).containsExactly ("1" , "2" , "3" );
121+ assertThat (addedItems ).containsExactly (3 , 1 , 2 );
122+ assertThat (removedItems ).isEmpty ();
93123 }
94124
95125 @ Test
96- void permutation_whenListSorted_shouldMirrorOrder () {
97- source .addAll (3 , 1 , 2 );
98- source .sort (Integer ::compareTo );
126+ void permutation_whenListReversed_shouldMirrorOrder () {
127+ source .addAll (1 , 2 , 3 );
128+ source .sort (Comparator .reverseOrder ());
129+
130+ assertThat (target ).containsExactly ("3" , "2" , "1" );
131+ assertThat (addedItems ).containsExactly (1 , 2 , 3 );
132+ assertThat (removedItems ).isEmpty ();
133+ }
99134
100- assertThat (target )
101- .hasSize (3 )
102- .containsExactly ("1" , "2" , "3" );
135+ @ Test
136+ void update_whenElementUpdated_shouldUpdateSecondary () {
137+ ObservableList <TestItem > sourceItems = FXCollections .observableArrayList (
138+ item -> new Property [] {item .valueProperty ()}
139+ );
140+ ObservableList <String > targetItems = FXCollections .observableArrayList ();
141+ List <TestItem > added = new ArrayList <>();
142+ List <TestItem > removed = new ArrayList <>();
143+
144+ ListSynchronizer <TestItem , String > itemSynchronizer = new ListSynchronizer <>(
145+ sourceItems , targetItems ,
146+ item -> Integer .toString (item .getValue ()),
147+ added ::add ,
148+ removed ::add
149+ );
150+
151+ TestItem item1 = new TestItem (1 );
152+ TestItem item2 = new TestItem (2 );
153+ sourceItems .addAll (item1 , item2 );
154+
155+ item1 .setValue (10 );
156+
157+ assertThat (targetItems ).containsExactly ("10" , "2" );
158+ assertThat (added ).containsExactly (item1 , item2 );
159+ assertThat (removed ).isEmpty ();
160+
161+ itemSynchronizer .dispose ();
162+ }
163+
164+ @ Test
165+ void multipleReplacements_shouldWorkCorrectly () {
166+ source .addAll (1 , 2 , 3 , 4 );
167+ source .set (0 , 5 );
168+ source .set (2 , 6 );
169+ source .set (3 , 7 );
170+
171+ assertThat (target ).containsExactly ("5" , "2" , "6" , "7" );
172+ assertThat (addedItems ).containsExactly (1 , 2 , 3 , 4 , 5 , 6 , 7 );
173+ assertThat (removedItems ).containsExactly (1 , 3 , 4 );
103174 }
104175
176+ @ Test
177+ void complexOperations_combinationOfAllTypes () {
178+ source .addAll (1 , 2 , 3 );
179+ source .remove (1 );
180+ source .add (4 );
181+ source .set (0 , 5 );
182+ FXCollections .sort (source );
183+
184+ assertThat (target ).containsExactly ("3" , "4" , "5" );
185+ assertThat (addedItems ).containsExactly (1 , 2 , 3 , 4 , 5 );
186+ assertThat (removedItems ).containsExactly (2 , 1 );
187+ }
105188}
189+
0 commit comments