1
1
package org .dataloader ;
2
2
3
+ import java .util .concurrent .CompletableFuture ;
3
4
import org .dataloader .stats .Statistics ;
4
5
import org .junit .Test ;
5
6
6
- import java .util .concurrent .CompletableFuture ;
7
-
8
7
import static java .util .Arrays .asList ;
9
8
import static org .hamcrest .Matchers .equalTo ;
10
9
import static org .hamcrest .Matchers .hasItems ;
@@ -101,4 +100,35 @@ public void stats_can_be_collected() throws Exception {
101
100
assertThat (statistics .getLoadErrorCount (), equalTo (0L ));
102
101
assertThat (statistics .getBatchLoadExceptionCount (), equalTo (0L ));
103
102
}
104
- }
103
+
104
+ @ Test
105
+ public void computeIfAbsent_creates_a_data_loader_if_there_was_no_value_at_key () {
106
+
107
+ DataLoaderRegistry registry = new DataLoaderRegistry ();
108
+
109
+ DataLoader <Object , Object > dlA = new DataLoader <>(identityBatchLoader );
110
+ DataLoader <Object , Object > registered = registry .computeIfAbsent ("a" , (key ) -> dlA );
111
+
112
+ assertThat (registered , equalTo (dlA ));
113
+ assertThat (registry .getKeys (), hasItems ("a" ));
114
+ assertThat (registry .getDataLoaders (), hasItems (dlA ));
115
+ }
116
+
117
+ @ Test
118
+ public void computeIfAbsent_returns_an_existing_data_loader_if_there_was_a_value_at_key () {
119
+
120
+ DataLoaderRegistry registry = new DataLoaderRegistry ();
121
+
122
+ DataLoader <Object , Object > dlA = new DataLoader <>(identityBatchLoader );
123
+ registry .computeIfAbsent ("a" , (key ) -> dlA );
124
+
125
+ // register again at same key
126
+ DataLoader <Object , Object > dlA2 = new DataLoader <>(identityBatchLoader );
127
+ DataLoader <Object , Object > registered = registry .computeIfAbsent ("a" , (key ) -> dlA2 );
128
+
129
+ assertThat (registered , equalTo (dlA ));
130
+ assertThat (registry .getKeys (), hasItems ("a" ));
131
+ assertThat (registry .getDataLoaders (), hasItems (dlA ));
132
+ }
133
+
134
+ }
0 commit comments