source: josm/trunk/test/unit/org/openstreetmap/josm/testutils/annotations/ResetUniquePrimitiveIdCounters.java

Last change on this file was 18974, checked in by taylor.smock, 2 years ago

Fix #23465: Remove custom checkstyle plugin

TopLevelJavadocCheck.java is duplicating functionality from MissingJavadocType.
Our custom class is from #14794 (closed 2017-10-16). The check that makes it
redundant was added in checkstyle 8.20 (released 2019-04-28).

This adds the missing javadocs for the more comprehensive checkstyle version.

File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.testutils.annotations;
3
4import java.lang.annotation.Documented;
5import java.lang.annotation.ElementType;
6import java.lang.annotation.Inherited;
7import java.lang.annotation.Retention;
8import java.lang.annotation.RetentionPolicy;
9import java.lang.annotation.Target;
10import java.lang.reflect.Field;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.List;
14import java.util.concurrent.atomic.AtomicLong;
15
16import org.junit.jupiter.api.extension.BeforeEachCallback;
17import org.junit.jupiter.api.extension.ExtendWith;
18import org.junit.jupiter.api.extension.ExtensionContext;
19import org.junit.platform.commons.function.Try;
20import org.junit.platform.commons.support.ReflectionSupport;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.Relation;
23import org.openstreetmap.josm.data.osm.UniqueIdGenerator;
24import org.openstreetmap.josm.data.osm.Way;
25import org.openstreetmap.josm.spi.preferences.Config;
26import org.openstreetmap.josm.spi.preferences.MemoryPreferences;
27
28/**
29 * Reset {@link org.openstreetmap.josm.data.osm.OsmPrimitive} id counters for tests where it makes a difference.
30 * This is most likely an ordering issue with a {@link java.util.Set} collection.
31 */
32@Documented
33@Retention(RetentionPolicy.RUNTIME)
34@Target(ElementType.METHOD)
35@Inherited
36@BasicPreferences
37@ExtendWith(ResetUniquePrimitiveIdCounters.Reset.class)
38public @interface ResetUniquePrimitiveIdCounters {
39 /**
40 * Reset the id counters for {@link Node}, {@link Way}, and {@link Relation}
41 * {@link org.openstreetmap.josm.data.osm.AbstractPrimitive#getIdGenerator} calls.
42 */
43 class Reset implements BeforeEachCallback {
44 private static AtomicLong[] ID_COUNTERS;
45
46 @Override
47 public void beforeEach(ExtensionContext extensionContext) throws Exception {
48 if (ID_COUNTERS == null) {
49 ID_COUNTERS = getIdCounters();
50 }
51
52 for (AtomicLong counter : ID_COUNTERS) {
53 counter.set(0);
54 }
55 }
56
57 private static AtomicLong[] getIdCounters() throws ReflectiveOperationException {
58 Config.setPreferencesInstance(new MemoryPreferences());
59 List<AtomicLong> idCounters = new ArrayList<>(3);
60 final Field idCounter = UniqueIdGenerator.class.getDeclaredField("idCounter");
61 for (Try<Object> primitive : Arrays.asList(
62 ReflectionSupport.tryToReadFieldValue(Node.class.getDeclaredField("idGenerator"), null),
63 ReflectionSupport.tryToReadFieldValue(Way.class.getDeclaredField("idGenerator"), null),
64 ReflectionSupport.tryToReadFieldValue(Relation.class.getDeclaredField("idGenerator"), null)
65 )) {
66 primitive.andThen(generator -> ReflectionSupport.tryToReadFieldValue(idCounter, generator))
67 .ifSuccess(counter -> idCounters.add((AtomicLong) counter));
68 }
69 return idCounters.toArray(new AtomicLong[0]);
70 }
71 }
72}
Note: See TracBrowser for help on using the repository browser.