33import org .dataloader .fixtures .TestKit ;
44import org .dataloader .fixtures .parameterized .DelegatingDataLoaderFactory ;
55import org .jspecify .annotations .NonNull ;
6+ import org .jspecify .annotations .NullMarked ;
67import org .jspecify .annotations .Nullable ;
7- import org .junit .jupiter .api .Assertions ;
88import org .junit .jupiter .api .Test ;
99
1010import java .util .List ;
11+ import java .util .Map ;
1112import java .util .concurrent .CompletableFuture ;
13+ import java .util .concurrent .atomic .AtomicInteger ;
1214
1315import static org .awaitility .Awaitility .await ;
1416import static org .hamcrest .CoreMatchers .equalTo ;
1517import static org .hamcrest .CoreMatchers .is ;
1618import static org .hamcrest .MatcherAssert .assertThat ;
17- import static org .junit .jupiter .api .Assertions .* ;
19+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
1820
1921/**
2022 * There are WAY more tests via the {@link DelegatingDataLoaderFactory}
@@ -73,8 +75,69 @@ void can_delegate_simple_properties() {
7375 DelegatingDataLoader <String , String > delegate = new DelegatingDataLoader <>(rawLoader );
7476
7577 assertNotNull (delegate .getName ());
76- assertThat (delegate .getName (),equalTo ("name" ));
77- assertThat (delegate .getOptions (),equalTo (options ));
78- assertThat (delegate .getBatchLoadFunction (),equalTo (loadFunction ));
78+ assertThat (delegate .getName (), equalTo ("name" ));
79+ assertThat (delegate .getOptions (), equalTo (options ));
80+ assertThat (delegate .getBatchLoadFunction (), equalTo (loadFunction ));
81+ }
82+
83+ @ NullMarked
84+ @ Test
85+ void can_create_a_delegate_class_that_has_post_side_effects () {
86+ DataLoaderOptions options = DataLoaderOptions .newOptions ().build ();
87+ BatchLoader <String , String > loadFunction = CompletableFuture ::completedFuture ;
88+ DataLoader <String , String > rawLoader = DataLoaderFactory .newDataLoader ("name" , loadFunction , options );
89+
90+ AtomicInteger loadCalled = new AtomicInteger (0 );
91+ AtomicInteger loadManyCalled = new AtomicInteger (0 );
92+ AtomicInteger loadManyMapCalled = new AtomicInteger (0 );
93+ DelegatingDataLoader <String , String > delegate = new DelegatingDataLoader <>(rawLoader ) {
94+
95+ @ Override
96+ public CompletableFuture <String > load (String key ) {
97+ CompletableFuture <String > cf = super .load (key );
98+ loadCalled .incrementAndGet ();
99+ return cf ;
100+ }
101+
102+ @ Override
103+ public CompletableFuture <String > load (String key , @ Nullable Object keyContext ) {
104+ CompletableFuture <String > cf = super .load (key , keyContext );
105+ loadCalled .incrementAndGet ();
106+ return cf ;
107+ }
108+
109+ @ Override
110+ public CompletableFuture <List <String >> loadMany (List <String > keys , List <Object > keyContexts ) {
111+ CompletableFuture <List <String >> cf = super .loadMany (keys , keyContexts );
112+ loadManyCalled .incrementAndGet ();
113+ return cf ;
114+ }
115+
116+ @ Override
117+ public CompletableFuture <List <String >> loadMany (List <String > keys ) {
118+ CompletableFuture <List <String >> cf = super .loadMany (keys );
119+ loadManyCalled .incrementAndGet ();
120+ return cf ;
121+ }
122+
123+ @ Override
124+ public CompletableFuture <Map <String , String >> loadMany (Map <String , ?> keysAndContexts ) {
125+ CompletableFuture <Map <String , String >> cf = super .loadMany (keysAndContexts );
126+ loadManyMapCalled .incrementAndGet ();
127+ return cf ;
128+ }
129+ };
130+
131+
132+ delegate .load ("L1" );
133+ delegate .load ("L2" , null );
134+ delegate .loadMany (List .of ("LM1" , "LM2" ), List .of ());
135+ delegate .loadMany (List .of ("LM3" ));
136+ delegate .loadMany (Map .of ("LMM1" , "kc1" , "LMM2" , "kc2" ));
137+
138+ assertNotNull (delegate .getDelegate ());
139+ assertThat (loadCalled .get (), equalTo (2 ));
140+ assertThat (loadManyCalled .get (), equalTo (2 ));
141+ assertThat (loadManyMapCalled .get (), equalTo (1 ));
79142 }
80143}
0 commit comments