| 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.platform.engine; |
| 12 |
|
| 13 |
import static java.util.Collections.singletonList; |
| 14 |
import static java.util.Collections.unmodifiableList; |
| 15 |
import static org.apiguardian.api.API.Status.STABLE; |
| 16 |
|
| 17 |
import java.io.Serializable; |
| 18 |
import java.lang.ref.SoftReference; |
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.List; |
| 21 |
import java.util.Objects; |
| 22 |
import java.util.Optional; |
| 23 |
|
| 24 |
import org.apiguardian.api.API; |
| 25 |
import org.junit.platform.commons.JUnitException; |
| 26 |
import org.junit.platform.commons.util.Preconditions; |
| 27 |
import org.junit.platform.commons.util.ToStringBuilder; |
| 28 |
|
| 29 |
/** |
| 30 |
* {@code UniqueId} encapsulates the creation, parsing, and display of unique IDs |
| 31 |
* for {@link TestDescriptor TestDescriptors}. |
| 32 |
* |
| 33 |
* <p>Instances of this class have value semantics and are immutable. |
| 34 |
* |
| 35 |
* @since 1.0 |
| 36 |
*/ |
| 37 |
@API(status = STABLE, since = "1.0") |
| 38 |
public class UniqueId implements Cloneable, Serializable { |
| 39 |
|
| 40 |
private static final long serialVersionUID = 1L; |
| 41 |
|
| 42 |
private static final String ENGINE_SEGMENT_TYPE = "engine"; |
| 43 |
|
| 44 |
/** |
| 45 |
* Parse a {@code UniqueId} from the supplied string representation using the |
| 46 |
* default format. |
| 47 |
* |
| 48 |
* @param uniqueId the string representation to parse; never {@code null} or blank |
| 49 |
* @return a properly constructed {@code UniqueId} |
| 50 |
* @throws JUnitException if the string cannot be parsed |
| 51 |
*/ |
| 52 |
public static UniqueId parse(String uniqueId) throws JUnitException { |
| 53 |
Preconditions.notBlank(uniqueId, "Unique ID string must not be null or blank"); |
| 54 |
return UniqueIdFormat.getDefault().parse(uniqueId); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Create an engine's unique ID from its {@code engineId} using the default |
| 59 |
* format. |
| 60 |
* |
| 61 |
* <p>The engine ID will be stored in a {@link Segment} with |
| 62 |
* {@link Segment#getType type} {@code "engine"}. |
| 63 |
* |
| 64 |
* @param engineId the engine ID; never {@code null} or blank |
| 65 |
* @see #root(String, String) |
| 66 |
*/ |
| 67 |
public static UniqueId forEngine(String engineId) { |
| 68 |
Preconditions.notBlank(engineId, "engineId must not be null or blank"); |
| 69 |
return root(ENGINE_SEGMENT_TYPE, engineId); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Create a root unique ID from the supplied {@code segmentType} and |
| 74 |
* {@code value} using the default format. |
| 75 |
* |
| 76 |
* @param segmentType the segment type; never {@code null} or blank |
| 77 |
* @param value the value; never {@code null} or blank |
| 78 |
* @see #forEngine(String) |
| 79 |
*/ |
| 80 |
public static UniqueId root(String segmentType, String value) { |
| 81 |
return new UniqueId(UniqueIdFormat.getDefault(), new Segment(segmentType, value)); |
| 82 |
} |
| 83 |
|
| 84 |
private final UniqueIdFormat uniqueIdFormat; |
| 85 |
|
| 86 |
@SuppressWarnings({ "serial", "RedundantSuppression" }) // always used with serializable implementation (singletonList() or ArrayList) |
| 87 |
private final List<Segment> segments; |
| 88 |
|
| 89 |
// lazily computed |
| 90 |
private transient int hashCode; |
| 91 |
|
| 92 |
// lazily computed |
| 93 |
private transient SoftReference<String> toString; |
| 94 |
|
| 95 |
private UniqueId(UniqueIdFormat uniqueIdFormat, Segment segment) { |
| 96 |
this(uniqueIdFormat, singletonList(segment)); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Initialize a {@code UniqueId} instance. |
| 101 |
* |
| 102 |
* @implNote A defensive copy of the segment list is <b>not</b> created by |
| 103 |
* this implementation. All callers should immediately drop the reference |
| 104 |
* to the list instance that they pass into this constructor. |
| 105 |
*/ |
| 106 |
UniqueId(UniqueIdFormat uniqueIdFormat, List<Segment> segments) { |
| 107 |
this.uniqueIdFormat = uniqueIdFormat; |
| 108 |
this.segments = segments; |
| 109 |
} |
| 110 |
|
| 111 |
final Optional<Segment> getRoot() { |
| 112 |
return this.segments.isEmpty() ? Optional.empty() : Optional.of(this.segments.get(0)); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get the engine ID stored in this {@code UniqueId}, if available. |
| 117 |
* |
| 118 |
* @see #forEngine(String) |
| 119 |
*/ |
| 120 |
public final Optional<String> getEngineId() { |
| 121 |
return getRoot().filter(segment -> segment.getType().equals(ENGINE_SEGMENT_TYPE)).map(Segment::getValue); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get the immutable list of {@linkplain Segment segments} that make up this |
| 126 |
* {@code UniqueId}. |
| 127 |
*/ |
| 128 |
public final List<Segment> getSegments() { |
| 129 |
return unmodifiableList(this.segments); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Construct a new {@code UniqueId} by appending a new {@link Segment}, based |
| 134 |
* on the supplied {@code segmentType} and {@code value}, to the end of this |
| 135 |
* {@code UniqueId}. |
| 136 |
* |
| 137 |
* <p>This {@code UniqueId} will not be modified. |
| 138 |
* |
| 139 |
* <p>Neither the {@code segmentType} nor the {@code value} may contain any |
| 140 |
* of the special characters used for constructing the string representation |
| 141 |
* of this {@code UniqueId}. |
| 142 |
* |
| 143 |
* @param segmentType the type of the segment; never {@code null} or blank |
| 144 |
* @param value the value of the segment; never {@code null} or blank |
| 145 |
*/ |
| 146 |
public final UniqueId append(String segmentType, String value) { |
| 147 |
return append(new Segment(segmentType, value)); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Construct a new {@code UniqueId} by appending a new {@link Segment} to |
| 152 |
* the end of this {@code UniqueId}. |
| 153 |
* |
| 154 |
* <p>This {@code UniqueId} will not be modified. |
| 155 |
* |
| 156 |
* @param segment the segment to be appended; never {@code null} |
| 157 |
* |
| 158 |
* @since 1.1 |
| 159 |
*/ |
| 160 |
@API(status = STABLE, since = "1.1") |
| 161 |
public final UniqueId append(Segment segment) { |
| 162 |
Preconditions.notNull(segment, "segment must not be null"); |
| 163 |
List<Segment> baseSegments = new ArrayList<>(this.segments.size() + 1); |
| 164 |
baseSegments.addAll(this.segments); |
| 165 |
baseSegments.add(segment); |
| 166 |
return new UniqueId(this.uniqueIdFormat, baseSegments); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Construct a new {@code UniqueId} by appending a new {@link Segment}, based |
| 171 |
* on the supplied {@code engineId}, to the end of this {@code UniqueId}. |
| 172 |
* |
| 173 |
* <p>This {@code UniqueId} will not be modified. |
| 174 |
* |
| 175 |
* <p>The engine ID will be stored in a {@link Segment} with |
| 176 |
* {@link Segment#getType type} {@value #ENGINE_SEGMENT_TYPE}. |
| 177 |
* |
| 178 |
* @param engineId the engine ID; never {@code null} or blank |
| 179 |
* |
| 180 |
* @since 1.8 |
| 181 |
*/ |
| 182 |
@API(status = STABLE, since = "1.10") |
| 183 |
public UniqueId appendEngine(String engineId) { |
| 184 |
return append(new Segment(ENGINE_SEGMENT_TYPE, engineId)); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Determine if the supplied {@code UniqueId} is a prefix for this |
| 189 |
* {@code UniqueId}. |
| 190 |
* |
| 191 |
* @param potentialPrefix the {@code UniqueId} to be checked; never {@code null} |
| 192 |
* |
| 193 |
* @since 1.1 |
| 194 |
*/ |
| 195 |
@API(status = STABLE, since = "1.1") |
| 196 |
public boolean hasPrefix(UniqueId potentialPrefix) { |
| 197 |
Preconditions.notNull(potentialPrefix, "potentialPrefix must not be null"); |
| 198 |
int size = this.segments.size(); |
| 199 |
int prefixSize = potentialPrefix.segments.size(); |
| 200 |
return size >= prefixSize && this.segments.subList(0, prefixSize).equals(potentialPrefix.segments); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Construct a new {@code UniqueId} and removing the last {@link Segment} of |
| 205 |
* this {@code UniqueId}. |
| 206 |
* |
| 207 |
* <p>This {@code UniqueId} will not be modified. |
| 208 |
* |
| 209 |
* @return a new {@code UniqueId}; never {@code null} |
| 210 |
* @throws org.junit.platform.commons.PreconditionViolationException |
| 211 |
* if this {@code UniqueId} contains a single segment |
| 212 |
* @since 1.5 |
| 213 |
*/ |
| 214 |
@API(status = STABLE, since = "1.5") |
| 215 |
public UniqueId removeLastSegment() { |
| 216 |
Preconditions.condition(this.segments.size() > 1, "Cannot remove last remaining segment"); |
| 217 |
return new UniqueId(uniqueIdFormat, new ArrayList<>(this.segments.subList(0, this.segments.size() - 1))); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Get the last {@link Segment} of this {@code UniqueId}. |
| 222 |
* |
| 223 |
* @return the last {@code Segment}; never {@code null} |
| 224 |
* @since 1.5 |
| 225 |
*/ |
| 226 |
@API(status = STABLE, since = "1.5") |
| 227 |
public Segment getLastSegment() { |
| 228 |
return this.segments.get(this.segments.size() - 1); |
| 229 |
} |
| 230 |
|
| 231 |
@Override |
| 232 |
protected Object clone() throws CloneNotSupportedException { |
| 233 |
return super.clone(); |
| 234 |
} |
| 235 |
|
| 236 |
@Override |
| 237 |
public boolean equals(Object o) { |
| 238 |
if (this == o) { |
| 239 |
return true; |
| 240 |
} |
| 241 |
if (o == null || getClass() != o.getClass()) { |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
UniqueId that = (UniqueId) o; |
| 246 |
return this.segments.equals(that.segments); |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public int hashCode() { |
| 251 |
int value = this.hashCode; |
| 252 |
if (value == 0) { |
| 253 |
value = this.segments.hashCode(); |
| 254 |
if (value == 0) { |
| 255 |
// handle the edge case of the computed hashCode being 0 |
| 256 |
value = 1; |
| 257 |
} |
| 258 |
// this is a benign race like String#hash |
| 259 |
// we potentially read and write values from multiple threads |
| 260 |
// without a happens-before relationship |
| 261 |
// however the JMM guarantees us that we only ever see values |
| 262 |
// that were valid at one point, either 0 or the hash code |
| 263 |
// so we might end up not seeing a value that a different thread |
| 264 |
// has computed or multiple threads writing the same value |
| 265 |
this.hashCode = value; |
| 266 |
} |
| 267 |
return value; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Generate the unique, formatted string representation of this {@code UniqueId} |
| 272 |
* using the configured {@link UniqueIdFormat}. |
| 273 |
*/ |
| 274 |
@Override |
| 275 |
public String toString() { |
| 276 |
SoftReference<String> s = this.toString; |
| 277 |
String value = s == null ? null : s.get(); |
| 278 |
if (value == null) { |
| 279 |
value = this.uniqueIdFormat.format(this); |
| 280 |
// this is a benign race like String#hash |
| 281 |
// we potentially read and write values from multiple threads |
| 282 |
// without a happens-before relationship |
| 283 |
// however the JMM guarantees us that we only ever see values |
| 284 |
// that were valid at one point, either null or the toString value |
| 285 |
// so we might end up not seeing a value that a different thread |
| 286 |
// has computed or multiple threads writing the same value |
| 287 |
this.toString = new SoftReference<>(value); |
| 288 |
} |
| 289 |
return value; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* A segment of a {@link UniqueId} comprises a <em>type</em> and a |
| 294 |
* <em>value</em>. |
| 295 |
*/ |
| 296 |
@API(status = STABLE, since = "1.0") |
| 297 |
public static class Segment implements Serializable { |
| 298 |
|
| 299 |
private static final long serialVersionUID = 1L; |
| 300 |
|
| 301 |
private final String type; |
| 302 |
private final String value; |
| 303 |
|
| 304 |
/** |
| 305 |
* Create a new {@code Segment} using the supplied {@code type} and |
| 306 |
* {@code value}. |
| 307 |
* |
| 308 |
* @param type the type of this segment |
| 309 |
* @param value the value of this segment |
| 310 |
*/ |
| 311 |
Segment(String type, String value) { |
| 312 |
Preconditions.notBlank(type, "type must not be null or blank"); |
| 313 |
Preconditions.notBlank(value, "value must not be null or blank"); |
| 314 |
this.type = type; |
| 315 |
this.value = value; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Get the type of this segment. |
| 320 |
*/ |
| 321 |
public String getType() { |
| 322 |
return this.type; |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Get the value of this segment. |
| 327 |
*/ |
| 328 |
public String getValue() { |
| 329 |
return this.value; |
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
public int hashCode() { |
| 334 |
return Objects.hash(this.type, this.value); |
| 335 |
} |
| 336 |
|
| 337 |
@Override |
| 338 |
public boolean equals(Object o) { |
| 339 |
if (this == o) { |
| 340 |
return true; |
| 341 |
} |
| 342 |
if (o == null || getClass() != o.getClass()) { |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
Segment that = (Segment) o; |
| 347 |
return Objects.equals(this.type, that.type) && Objects.equals(this.value, that.value); |
| 348 |
} |
| 349 |
|
| 350 |
@Override |
| 351 |
public String toString() { |
| 352 |
// @formatter:off |
| 353 |
return new ToStringBuilder(this) |
| 354 |
.append("type", this.type) |
| 355 |
.append("value", this.value) |
| 356 |
.toString(); |
| 357 |
// @formatter:on |
| 358 |
} |
| 359 |
|
| 360 |
} |
| 361 |
|
| 362 |
} |