View Javadoc
1   /*
2    * Copyright 2008-2016 Brian Thomas Matthews
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.btmatthews.maven.plugins.ldap;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.net.InetSocketAddress;
22  import java.net.Socket;
23  import java.net.URISyntaxException;
24  import java.net.URL;
25  import java.util.Arrays;
26  
27  /**
28   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
29   * @since 1.2.0
30   */
31  public class TestUtils {
32  
33      private static int DEFAULT_PORT_RANGE_END = 49152;
34  
35      /**
36       * Get a {@link URL} for referencing a file on the classpath.
37       *
38       * @param filename The filename.
39       * @return The {@link URL}.
40       */
41      public static URL getURL(final String filename) {
42          final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
43          return classLoader.getResource(filename);
44      }
45  
46      /**
47       * Get a {@link File} for referencing a file on the classpath.
48       *
49       * @param filename The filename.
50       * @return The {@link File}.
51       * @throws URISyntaxException If the filename is invalid.
52       */
53      public static File getFile(final String filename) throws URISyntaxException {
54          final URL url = getURL(filename);
55          return new File(url.toURI());
56      }
57  
58      public static int[] getUnusedPorts(final int count, final int start) {
59          return getUnusedPorts(count, start, DEFAULT_PORT_RANGE_END);
60      }
61  
62      public static int[] getUnusedPorts(final int count, final int start, final int end) {
63          final int[] ports = new int[count];
64          int port = start;
65          int index = 0;
66          while (index < count && port < end) {
67              port = getUnusedPort(port, end);
68              if (port != -1) {
69                  ports[index++] = port++;
70              } else {
71                  return Arrays.copyOf(ports, index);
72              }
73          }
74          return ports;
75      }
76  
77      public static int getUnusedPort(final int start) {
78          return getUnusedPort(start, DEFAULT_PORT_RANGE_END);
79      }
80  
81      public static int getUnusedPort(final int start, final int end) {
82          int port = start;
83          while (port < end) {
84              final Socket socket = new Socket();
85              try {
86                  socket.connect(new InetSocketAddress("localhost", port), 0);
87                  socket.setSoLinger(false, 0);
88              } catch (final IOException e) {
89                  return port;
90              } finally {
91                  try {
92                      socket.close();
93                  } catch (final IOException e) {
94                  }
95              }
96              port++;
97          }
98          return -1;
99      }
100 }