View Javadoc
1   /*
2    * Copyright © 2016 Greg Chabala
3    *
4    * This file is part of brick-control-lab.
5    *
6    * brick-control-lab is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   *
11   * brick-control-lab is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with brick-control-lab.  If not, see http://www.gnu.org/licenses/.
18   */
19  package org.chabala.brick.controllab;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import java.lang.invoke.MethodHandles;
25  import java.util.List;
26  
27  import static org.hamcrest.Matchers.*;
28  import static org.junit.Assume.assumeThat;
29  
30  /**
31   * Intergration tests use this class to pick a serial port to connect to. If the
32   * default selection doesn't suit your system, you can override it with a system
33   * property: {@value OVERRIDE_TEST_PORT_PROPERTY}.
34   */
35  @SuppressWarnings("WeakerAccess")
36  public final class PortChooser {
37      private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
38      private static final String TOO_FEW_PORTS_MESSAGE = "as many serial ports as requested should be available";
39  
40      /** System property to override the IT serial port with a specific port identifier. */
41      public static final String OVERRIDE_TEST_PORT_PROPERTY = "brick-control-lab.overrideTestPort";
42  
43      /**
44       * Picks the first available serial port based on what the serial library finds.
45       * @param controlLab control lab instance for querying the serial library
46       * @return a system specific serial port identifier
47       */
48      public static String choosePort(ControlLab controlLab) {
49          return choosePort(controlLab, 1);
50      }
51  
52      /**
53       * Picks the <i>N</i>th available serial port based on what the serial library finds.
54       * @param controlLab control lab instance for querying the serial library
55       * @param position one-based position field for choosing the port
56       * @return a system specific serial port identifier
57       */
58      public static String choosePort(ControlLab controlLab, int position) {
59          String overrideTestPort = System.getProperty(OVERRIDE_TEST_PORT_PROPERTY, "");
60          int zeroBasedPosition = position - 1;
61  
62          if (overrideTestPort.isEmpty()) {
63              List<String> availablePorts = controlLab.getAvailablePorts();
64              assumeThat("a serial port should be available", availablePorts, is(not(empty())));
65              log.debug("Available ports: {}", String.join(", ", availablePorts));
66              assumeThat(TOO_FEW_PORTS_MESSAGE, availablePorts, hasSize(greaterThanOrEqualTo(position)));
67              return availablePorts.get(zeroBasedPosition);
68          } else {
69              log.debug("{} property is defined: {}", OVERRIDE_TEST_PORT_PROPERTY, overrideTestPort);
70              String[] overrideTestPorts = overrideTestPort.split(",");
71              assumeThat(TOO_FEW_PORTS_MESSAGE, overrideTestPorts, arrayWithSize(greaterThanOrEqualTo(position)));
72              return overrideTestPorts[zeroBasedPosition];
73          }
74      }
75  
76      private PortChooser() {
77          throw new UnsupportedOperationException();
78      }
79  }