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.Retention;
|
---|
7 | import java.lang.annotation.RetentionPolicy;
|
---|
8 | import java.lang.annotation.Target;
|
---|
9 | import java.util.Optional;
|
---|
10 |
|
---|
11 | import org.junit.jupiter.api.extension.AfterAllCallback;
|
---|
12 | import org.junit.jupiter.api.extension.BeforeAllCallback;
|
---|
13 | import org.junit.jupiter.api.extension.BeforeEachCallback;
|
---|
14 | import org.junit.jupiter.api.extension.ExtendWith;
|
---|
15 | import org.junit.jupiter.api.extension.ExtensionContext;
|
---|
16 | import org.junit.platform.commons.support.AnnotationSupport;
|
---|
17 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
18 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
19 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Use projections in tests (Mercator).
|
---|
23 | *
|
---|
24 | * @author Taylor Smock
|
---|
25 | * @see JOSMTestRules#projection()
|
---|
26 | *
|
---|
27 | */
|
---|
28 | @Documented
|
---|
29 | @Retention(RetentionPolicy.RUNTIME)
|
---|
30 | @Target({ ElementType.METHOD, ElementType.TYPE })
|
---|
31 | @ExtendWith(Projection.ProjectionExtension.class)
|
---|
32 | public @interface Projection {
|
---|
33 | /**
|
---|
34 | * The value to use for the projection. Defaults to EPSG:3857 (Mercator).
|
---|
35 | *
|
---|
36 | * @return The value to use to get the projection from
|
---|
37 | * {@link Projections#getProjectionByCode}.
|
---|
38 | */
|
---|
39 | String projectionCode() default "EPSG:3857";
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Use projections in tests.
|
---|
43 | *
|
---|
44 | * @author Taylor Smock
|
---|
45 | *
|
---|
46 | */
|
---|
47 | class ProjectionExtension implements BeforeEachCallback, BeforeAllCallback, AfterAllCallback {
|
---|
48 | @Override
|
---|
49 | public void afterAll(ExtensionContext context) throws Exception {
|
---|
50 | ProjectionRegistry.clearProjectionChangeListeners();
|
---|
51 | AnnotationUtils.resetStaticClass(ProjectionRegistry.class);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void beforeAll(ExtensionContext context) throws Exception {
|
---|
56 | // Needed in order to run prior to @Territories
|
---|
57 | beforeEach(context);
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public void beforeEach(ExtensionContext context) throws Exception {
|
---|
62 | Optional<Projection> annotation = AnnotationSupport.findAnnotation(context.getElement(), Projection.class);
|
---|
63 | if (annotation.isPresent()) {
|
---|
64 | ProjectionRegistry.setProjection(Projections.getProjectionByCode(annotation.get().projectionCode()));
|
---|
65 | } else {
|
---|
66 | ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | }
|
---|
71 | }
|
---|