1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.testutils.annotations;
|
---|
3 |
|
---|
4 | import java.lang.annotation.Documented;
|
---|
5 | import java.lang.annotation.ElementType;
|
---|
6 | import java.lang.annotation.Inherited;
|
---|
7 | import java.lang.annotation.Retention;
|
---|
8 | import java.lang.annotation.RetentionPolicy;
|
---|
9 | import java.lang.annotation.Target;
|
---|
10 | import java.lang.reflect.Field;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.concurrent.atomic.AtomicLong;
|
---|
15 |
|
---|
16 | import org.junit.jupiter.api.extension.BeforeEachCallback;
|
---|
17 | import org.junit.jupiter.api.extension.ExtendWith;
|
---|
18 | import org.junit.jupiter.api.extension.ExtensionContext;
|
---|
19 | import org.junit.platform.commons.function.Try;
|
---|
20 | import org.junit.platform.commons.support.ReflectionSupport;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
23 | import org.openstreetmap.josm.data.osm.UniqueIdGenerator;
|
---|
24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
25 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
26 | import 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)
|
---|
38 | public @interface ResetUniquePrimitiveIdCounters {
|
---|
39 | class Reset implements BeforeEachCallback {
|
---|
40 | private static AtomicLong[] ID_COUNTERS;
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void beforeEach(ExtensionContext extensionContext) throws Exception {
|
---|
44 | if (ID_COUNTERS == null) {
|
---|
45 | ID_COUNTERS = getIdCounters();
|
---|
46 | }
|
---|
47 |
|
---|
48 | for (AtomicLong counter : ID_COUNTERS) {
|
---|
49 | counter.set(0);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | private static AtomicLong[] getIdCounters() throws ReflectiveOperationException {
|
---|
54 | Config.setPreferencesInstance(new MemoryPreferences());
|
---|
55 | List<AtomicLong> idCounters = new ArrayList<>(3);
|
---|
56 | final Field idCounter = UniqueIdGenerator.class.getDeclaredField("idCounter");
|
---|
57 | for (Try<Object> primitive : Arrays.asList(
|
---|
58 | ReflectionSupport.tryToReadFieldValue(Node.class.getDeclaredField("idGenerator"), null),
|
---|
59 | ReflectionSupport.tryToReadFieldValue(Way.class.getDeclaredField("idGenerator"), null),
|
---|
60 | ReflectionSupport.tryToReadFieldValue(Relation.class.getDeclaredField("idGenerator"), null)
|
---|
61 | )) {
|
---|
62 | primitive.andThen(generator -> ReflectionSupport.tryToReadFieldValue(idCounter, generator))
|
---|
63 | .ifSuccess(counter -> idCounters.add((AtomicLong) counter));
|
---|
64 | }
|
---|
65 | return idCounters.toArray(new AtomicLong[0]);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|