protected int getPort(int defaultPort) {
    ServerSocket server = null;
    int port = -1;
    try {
      server = new ServerSocket(defaultPort);
      port = server.getLocalPort();
    } catch (IOException e) {
    } finally {
      if (server != null) {
        try {
          server.close();
        } catch (IOException e) {
        }
      }
    }
    if ((port != -1) || (defaultPort == 0)) {
      this.testPort = port;
      return port;
    }
    return getPort(0);
  }
Makes JerseyTest use random port in case default one is unavailable.
ambari-server/src/test/java/org/apache/ambari/server/RandomPortJerseyTest.java