4747import java .util .ArrayList ;
4848import java .util .Collection ;
4949import java .util .Comparator ;
50+ import java .util .HashMap ;
5051import java .util .List ;
5152import java .util .Map ;
5253import java .util .Optional ;
@@ -243,11 +244,16 @@ private void addDriverConfigs(
243244 Function <Capabilities , Collection <SessionFactory >> factoryFactory ,
244245 ImmutableMultimap .Builder <Capabilities , SessionFactory > sessionFactories ) {
245246 Multimap <WebDriverInfo , SessionFactory > driverConfigs = HashMultimap .create ();
246- int configElements = 3 ;
247247 config .getAll (NODE_SECTION , "driver-configuration" ).ifPresent (drivers -> {
248- if (drivers .size () % configElements != 0 ) {
249- throw new ConfigException ("Expected each driver config to have three elements " +
250- "(name, stereotype and max-sessions)" );
248+ /*
249+ The four accepted keys are: name, max-sessions, stereotype, webdriver-executable. The
250+ mandatory keys are name and stereotype. When configs are read, they keys always come
251+ alphabetically ordered. This means that we know a new config is present when we find
252+ the "name" key again.
253+ */
254+
255+ if (drivers .size () == 0 ) {
256+ throw new ConfigException ("No driver configs were found!" );
251257 }
252258
253259 drivers .stream ()
@@ -260,19 +266,24 @@ private void addDriverConfigs(
260266 "required 'key=value' structure" );
261267 });
262268
269+ // Find all indexes where "name" is present, as it marks the start of a config
270+ int [] configIndexes = IntStream .range (0 , drivers .size ())
271+ .filter (index -> drivers .get (index ).startsWith ("name" )).toArray ();
272+
273+ if (configIndexes .length == 0 ) {
274+ throw new ConfigException ("No 'name' keyword was found in the provided configs!" );
275+ }
276+
263277 List <Map <String , String >> driversMap = new ArrayList <>();
264- IntStream .range (0 , drivers .size ()/configElements ).boxed ()
265- .forEach (i -> {
266- ImmutableMap <String , String > configMap = ImmutableMap .of (
267- drivers .get (i *configElements ).split ("=" )[0 ],
268- drivers .get (i *configElements ).split ("=" )[1 ],
269- drivers .get (i *configElements +1 ).split ("=" )[0 ],
270- drivers .get (i *configElements +1 ).split ("=" )[1 ],
271- drivers .get (i *configElements +2 ).split ("=" )[0 ],
272- drivers .get (i *configElements +2 ).split ("=" )[1 ]
273- );
274- driversMap .add (configMap );
278+ for (int i = 0 ; i < configIndexes .length ; i ++) {
279+ int fromIndex = configIndexes [i ];
280+ int toIndex = (i + 1 ) >= configIndexes .length ? drivers .size () : configIndexes [i + 1 ];
281+ Map <String , String > configMap = new HashMap <>();
282+ drivers .subList (fromIndex , toIndex ).forEach (keyValue -> {
283+ configMap .put (keyValue .split ("=" )[0 ], keyValue .split ("=" )[1 ]);
275284 });
285+ driversMap .add (configMap );
286+ }
276287
277288 List <DriverService .Builder <?, ?>> builders = new ArrayList <>();
278289 ServiceLoader .load (DriverService .Builder .class ).forEach (builders ::add );
0 commit comments