• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

coditory / quark-uri / #3

pending completion
#3

push

github-actions

ogesaku
init

1212 of 1212 new or added lines in 21 files covered. (100.0%)

926 of 1212 relevant lines covered (76.4%)

0.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

44.12
/src/main/java/com/coditory/quark/uri/Ports.java
1
package com.coditory.quark.uri;
2

3
import java.io.IOException;
4
import java.net.DatagramSocket;
5
import java.net.ServerSocket;
6

7
import static com.coditory.quark.uri.Preconditions.expect;
8

9
public final class Ports {
10
    private Ports() {
×
11
        throw new UnsupportedOperationException("Do not instantiate utility class");
×
12
    }
13

14
    public final static int SCHEME_DEFAULT_PORT_NUMBER = -1;
15
    public final static int MIN_PORT_VALUE = 0;
16
    public final static int MAX_PORT_VALUE = 65535;
17

18
    public static boolean isValidPortNumberOrSchemeDefault(int port) {
19
        return port >= SCHEME_DEFAULT_PORT_NUMBER && port <= MAX_PORT_VALUE;
1✔
20
    }
21

22
    public static boolean isValidPortNumber(int port) {
23
        return port >= MIN_PORT_VALUE && port <= MAX_PORT_VALUE;
1✔
24
    }
25

26
    public static boolean isSchemeDefault(int port) {
27
        return port == SCHEME_DEFAULT_PORT_NUMBER;
1✔
28
    }
29

30
    public static void validatePortNumberOrSchemeDefault(int port) {
31
        if (!isValidPortNumberOrSchemeDefault(port)) {
1✔
32
            String message = String.format("Expected port number in range [%d, %d]. Got: %d",
1✔
33
                    SCHEME_DEFAULT_PORT_NUMBER, MAX_PORT_VALUE, port);
1✔
34
            throw new IllegalArgumentException(message);
1✔
35
        }
36
    }
1✔
37

38
    public static void validatePortNumber(int port) {
39
        if (!isValidPortNumber(port)) {
1✔
40
            String message = String.format("Expected port number in range [%d, %d]. Got: %d",
1✔
41
                    MIN_PORT_VALUE, MAX_PORT_VALUE, port);
1✔
42
            throw new IllegalArgumentException(message);
1✔
43
        }
44
    }
1✔
45

46
    public static int nextAvailablePort() {
47
        try {
48
            try (ServerSocket socket = new ServerSocket(0)) {
1✔
49
                return socket.getLocalPort();
1✔
50
            }
51
        } catch (IOException exception) {
×
52
            throw new IllegalStateException("Could not get available port", exception);
×
53
        }
54
    }
55

56
    public static int nextAvailablePort(int min, int max) {
57
        expect(min <= max, "Expected min <= max. Got: %d > %d", min, max);
×
58
        validatePortNumber(min);
×
59
        validatePortNumber(max);
×
60
        for (int port = min; port <= max; ++port) {
×
61
            if (isPortAvailable(port)) {
×
62
                return port;
×
63
            }
64
        }
65
        throw new IllegalStateException("Could not get available port between: " + min + " and " + max);
×
66
    }
67

68
    public static boolean isPortAvailable(int port) {
69
        validatePortNumber(port);
×
70
        try {
71
            try (ServerSocket socket = new ServerSocket(port)) {
×
72
                socket.setReuseAddress(true);
×
73
                try (DatagramSocket datagramSocket = new DatagramSocket(port)) {
×
74
                    datagramSocket.setReuseAddress(true);
×
75
                    return true;
×
76
                }
77
            }
78
        } catch (IOException e) {
×
79
            return false;
×
80
        }
81
    }
82
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc