Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/OS.java

raw symbols SCIP
1 /*
2 * Copyright 2015-2024 the original author or authors.
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v2.0 which
6 * accompanies this distribution and is available at
7 *
8 * https://www.eclipse.org/legal/epl-v20.html
9 */
10
11 package org.junit.jupiter.api.condition;
12
13 import static org.apiguardian.api.API.Status.STABLE;
14
15 import java.util.Locale;
16
17 import org.apiguardian.api.API;
18 import org.junit.platform.commons.logging.Logger;
19 import org.junit.platform.commons.logging.LoggerFactory;
20 import org.junit.platform.commons.util.StringUtils;
21
22 /**
23 * Enumeration of common operating systems used for testing Java applications.
24 *
25 * <p>If the current operating system cannot be detected &mdash; for example,
26 * if the {@code os.name} JVM system property is undefined &mdash; then none
27 * of the constants defined in this enum will be considered to be the
28 * {@linkplain #isCurrentOs current operating system}.
29 *
30 * @since 5.1
31 * @see #AIX
32 * @see #FREEBSD
33 * @see #LINUX
34 * @see #MAC
35 * @see #OPENBSD
36 * @see #SOLARIS
37 * @see #WINDOWS
38 * @see #OTHER
39 * @see EnabledOnOs
40 * @see DisabledOnOs
41 */
42 @API(status = STABLE, since = "5.1")
43 public enum OS {
44
45 /**
46 * IBM AIX operating system.
47 *
48 * @since 5.3
49 */
50 @API(status = STABLE, since = "5.3")
51 AIX,
52
53 /**
54 * FreeBSD operating system.
55 *
56 * @since 5.9
57 */
58 @API(status = STABLE, since = "5.9")
59 FREEBSD,
60
61 /**
62 * Linux-based operating system.
63 */
64 LINUX,
65
66 /**
67 * Apple Macintosh operating system (e.g., macOS).
68 */
69 MAC,
70
71 /**
72 * OpenBSD operating system.
73 *
74 * @since 5.9
75 */
76 @API(status = STABLE, since = "5.9")
77 OPENBSD,
78
79 /**
80 * Oracle Solaris operating system.
81 */
82 SOLARIS,
83
84 /**
85 * Microsoft Windows operating system.
86 */
87 WINDOWS,
88
89 /**
90 * An operating system other than {@link #AIX}, {@link #FREEBSD}, {@link #LINUX},
91 * {@link #MAC}, {@link #OPENBSD}, {@link #SOLARIS}, or {@link #WINDOWS}.
92 */
93 OTHER;
94
95 private static final Logger logger = LoggerFactory.getLogger(OS.class);
96
97 private static final OS CURRENT_OS = determineCurrentOs();
98
99 /**
100 * Get the current operating system.
101 *
102 * @since 5.9
103 */
104 @API(status = STABLE, since = "5.10")
105 public static OS current() {
106 return CURRENT_OS;
107 }
108
109 private static OS determineCurrentOs() {
110 return parse(System.getProperty("os.name"));
111 }
112
113 static OS parse(String osName) {
114 if (StringUtils.isBlank(osName)) {
115 logger.debug(
116 () -> "JVM system property 'os.name' is undefined. It is therefore not possible to detect the current OS.");
117
118 // null signals that the current OS is "unknown"
119 return null;
120 }
121
122 osName = osName.toLowerCase(Locale.ENGLISH);
123
124 if (osName.contains("aix")) {
125 return AIX;
126 }
127 if (osName.contains("freebsd")) {
128 return FREEBSD;
129 }
130 if (osName.contains("linux")) {
131 return LINUX;
132 }
133 if (osName.contains("mac")) {
134 return MAC;
135 }
136 if (osName.contains("openbsd")) {
137 return OPENBSD;
138 }
139 if (osName.contains("sunos") || osName.contains("solaris")) {
140 return SOLARIS;
141 }
142 if (osName.contains("win")) {
143 return WINDOWS;
144 }
145 return OTHER;
146 }
147
148 /**
149 * @return {@code true} if <em>this</em> {@code OS} is known to be the
150 * operating system on which the current JVM is executing
151 */
152 public boolean isCurrentOs() {
153 return this == CURRENT_OS;
154 }
155
156 }