| 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 | /**
|
|---|
| 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 | }
|
|---|