3232package org .scijava .service ;
3333
3434import static org .junit .Assert .assertEquals ;
35+ import static org .junit .Assert .assertNull ;
3536import static org .junit .Assert .assertSame ;
3637
3738import java .util .List ;
4041import org .scijava .Context ;
4142import org .scijava .event .DefaultEventService ;
4243import org .scijava .log .StderrLogService ;
44+ import org .scijava .options .DefaultOptionsService ;
45+ import org .scijava .options .OptionsService ;
4346import org .scijava .plugin .DefaultPluginService ;
4447import org .scijava .plugin .PluginService ;
4548import org .scijava .thread .DefaultThreadService ;
@@ -63,4 +66,80 @@ public void testGetAll() {
6366 assertSame (StderrLogService .class , all .get (3 ).getClass ());
6467 }
6568
69+ /**
70+ * Test the {@link ServiceIndex#getPrevService(Class, Class)} operation.
71+ */
72+ @ Test
73+ public void testGetPrevService () {
74+ // Create a service index where the OptionsService hierarchy should be:
75+ // HigherOptionsService > DefaultOptionsService > LowerOptionsService
76+ final ServiceIndex serviceIndex = setUpPrivateServices ();
77+
78+ // DefaultOptionsService should be the previous service to LowerOptionsService
79+ assertEquals (DefaultOptionsService .class , serviceIndex .getPrevService (
80+ OptionsService .class , LowerOptionsService .class ).getClass ());
81+
82+ // HigherOptionsService should be the previous service to
83+ // DefaultOptionsService
84+ assertEquals (HigherOptionsService .class , serviceIndex .getPrevService (
85+ OptionsService .class , DefaultOptionsService .class ).getClass ());
86+
87+ // There should not be a previous service before HigherOptionsService
88+ assertNull (serviceIndex .getPrevService (OptionsService .class ,
89+ HigherOptionsService .class ));
90+ }
91+
92+ /**
93+ * Test the {@link ServiceIndex#getNextService(Class, Class)} operation.
94+ */
95+ @ Test
96+ public void testGetNextService () {
97+ // Create a service index where the OptionsService hierarchy should be:
98+ // HigherOptionService > DefaultOptionService > LowerOptionService
99+ final ServiceIndex serviceIndex = setUpPrivateServices ();
100+
101+ // DefaultOptionsService should be the next service to HigherOptionsService
102+ assertEquals (DefaultOptionsService .class , serviceIndex .getNextService (
103+ OptionsService .class , HigherOptionsService .class ).getClass ());
104+
105+ // HigherOptionsService should be the previous service to
106+ // DefaultOptionsService
107+ assertEquals (LowerOptionsService .class , serviceIndex .getNextService (
108+ OptionsService .class , DefaultOptionsService .class ).getClass ());
109+
110+ // There should not be a next service after LowerOptionsService
111+ assertNull (serviceIndex .getNextService (OptionsService .class ,
112+ LowerOptionsService .class ));
113+ }
114+
115+ // -- Helper methods --
116+
117+ /**
118+ * @return A {@link ServiceIndex} with all private services manually added.
119+ */
120+ private ServiceIndex setUpPrivateServices () {
121+ final Context context = new Context (SciJavaService .class );
122+ final ServiceIndex serviceIndex = context .getServiceIndex ();
123+ serviceIndex .add (new HigherOptionsService ());
124+ serviceIndex .add (new LowerOptionsService ());
125+ return serviceIndex ;
126+ }
127+
128+ // -- Private services --
129+
130+ private static class HigherOptionsService extends DefaultOptionsService {
131+
132+ @ Override
133+ public double getPriority () {
134+ return super .getPriority () + 25 ;
135+ }
136+ }
137+
138+ private static class LowerOptionsService extends DefaultOptionsService {
139+
140+ @ Override
141+ public double getPriority () {
142+ return super .getPriority () - 30 ;
143+ }
144+ }
66145}
0 commit comments