File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
lazy-loading/src/test/java/com/iluwatar/lazy/loading Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .iluwatar .lazy .loading ;
2+
3+ import org .junit .Test ;
4+
5+ import java .lang .reflect .Field ;
6+
7+ import static org .junit .Assert .assertEquals ;
8+ import static org .junit .Assert .assertNotNull ;
9+ import static org .junit .Assert .assertNull ;
10+
11+ /**
12+ * Using reflection this test shows that the heavy field is not instantiated until the method getHeavy is called
13+ *
14+ * Created by jones on 11/10/2015.
15+ */
16+ public class HolderThreadSafeTest {
17+
18+ @ Test
19+ public void test () throws IllegalAccessException {
20+ HolderThreadSafe hts = new HolderThreadSafe ();
21+
22+ {//first call is null
23+ Field [] f = HolderThreadSafe .class .getDeclaredFields ();
24+ assertEquals ("One field only in HolderThreadSafe" , 1 , f .length );
25+ f [0 ].setAccessible (true );
26+
27+ assertNull (f [0 ].get (hts ));
28+ }
29+
30+ // now it is lazily loaded
31+ hts .getHeavy ();
32+
33+ {//now it is not null - call via reflection so that the test is the same before and after
34+ Field [] f = HolderThreadSafe .class .getDeclaredFields ();
35+ assertEquals ("One field only in HolderThreadSafe" , 1 , f .length );
36+ f [0 ].setAccessible (true );
37+
38+ assertNotNull (f [0 ].get (hts ));
39+ }
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments