source: josm/trunk/test/unit/org/openstreetmap/josm/testutils/annotations/Projection.java@ 18649

Last change on this file since 18649 was 18649, checked in by taylor.smock, 15 months ago

Fix #22712: ignore list doesn't work

This occurred due to OsmValidator#save modifying the list, and expecting
all error codes to be positive.

TestErrorTest now checks one of the tests known to have a negative hashcode.

This also adds some additional JUnit 5 annotations. The additional
annotations are:

  • @LayerManager (cleans up layers after each test run)
    • This is automatically registered for all tests
  • @Projection (sets up the ProjectionRegistry for each test)
File size: 2.5 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.Retention;
7import java.lang.annotation.RetentionPolicy;
8import java.lang.annotation.Target;
9import java.util.Optional;
10
11import org.junit.jupiter.api.extension.AfterAllCallback;
12import org.junit.jupiter.api.extension.BeforeAllCallback;
13import org.junit.jupiter.api.extension.BeforeEachCallback;
14import org.junit.jupiter.api.extension.ExtendWith;
15import org.junit.jupiter.api.extension.ExtensionContext;
16import org.junit.platform.commons.support.AnnotationSupport;
17import org.openstreetmap.josm.data.projection.ProjectionRegistry;
18import org.openstreetmap.josm.data.projection.Projections;
19import 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)
32public @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}
Note: See TracBrowser for help on using the repository browser.