Ticket #16567: 16567.initial_extensions.2.patch
| File 16567.initial_extensions.2.patch, 39.2 KB (added by , 5 years ago) |
|---|
-
test/unit/org/openstreetmap/josm/gui/bbox/SizeButtonTest.java
5 5 import static org.junit.jupiter.api.Assertions.assertFalse; 6 6 import static org.junit.jupiter.api.Assertions.assertTrue; 7 7 8 import org.junit.jupiter.api.extension.RegisterExtension;9 8 import org.junit.jupiter.api.Test; 10 9 import org.openstreetmap.josm.TestUtils; 11 10 import org.openstreetmap.josm.gui.bbox.SizeButton.AccessibleSizeButton; 12 import org.openstreetmap.josm.testutils. JOSMTestRules;11 import org.openstreetmap.josm.testutils.annotations.FullPreferences; 13 12 14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;15 16 13 /** 17 14 * Unit tests of {@link SizeButton} class. 18 15 */ 16 @FullPreferences 19 17 class SizeButtonTest { 20 18 21 /**22 * Setup tests23 */24 @RegisterExtension25 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")26 public JOSMTestRules test = new JOSMTestRules().preferences();27 19 28 20 /** 29 21 * Unit test of {@link SizeButton#SizeButton}. -
test/unit/org/openstreetmap/josm/gui/datatransfer/OsmTransferHandlerTest.java
6 6 7 7 import java.util.Collections; 8 8 9 import org.junit.jupiter.api.extension.RegisterExtension;10 9 import org.junit.jupiter.api.Test; 11 10 import org.openstreetmap.josm.actions.CopyAction; 12 11 import org.openstreetmap.josm.data.coor.LatLon; … … 15 14 import org.openstreetmap.josm.data.projection.ProjectionRegistry; 16 15 import org.openstreetmap.josm.gui.MainApplication; 17 16 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 18 import org.openstreetmap.josm.testutils.JOSMTestRules; 17 import org.openstreetmap.josm.testutils.annotations.FullPreferences; 18 import org.openstreetmap.josm.testutils.annotations.Main; 19 import org.openstreetmap.josm.testutils.annotations.Projection; 19 20 20 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;21 22 21 /** 23 22 * Unit tests of {@link OsmTransferHandler} class. 24 23 */ 24 @FullPreferences 25 @Projection 26 @Main 25 27 class OsmTransferHandlerTest { 26 /**27 * Prefs to use OSM primitives28 */29 @RegisterExtension30 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")31 public JOSMTestRules test = new JOSMTestRules().preferences().projection().main();32 33 28 private final OsmTransferHandler transferHandler = new OsmTransferHandler(); 34 29 35 30 /** -
test/unit/org/openstreetmap/josm/io/auth/CredentialsAgentExceptionTest.java
3 3 4 4 import static org.junit.jupiter.api.Assertions.assertEquals; 5 5 6 import org.junit.jupiter.api.extension.RegisterExtension;7 6 import org.junit.jupiter.api.Test; 8 import org.openstreetmap.josm.testutils.JOSMTestRules;9 7 10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;11 12 8 /** 13 9 * Unit tests of {@link CredentialsAgentException} class. 14 10 */ 15 11 class CredentialsAgentExceptionTest { 16 17 12 /** 18 * Setup test19 */20 @RegisterExtension21 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")22 public JOSMTestRules test = new JOSMTestRules();23 24 /**25 13 * Unit test of {@code CredentialsAgentException#CredentialsAgentException} 26 14 */ 27 15 @Test -
test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
60 60 import org.openstreetmap.josm.io.OsmTransferCanceledException; 61 61 import org.openstreetmap.josm.spi.preferences.Config; 62 62 import org.openstreetmap.josm.spi.preferences.Setting; 63 import org.openstreetmap.josm.testutils.annotations.FullPreferences; 64 import org.openstreetmap.josm.testutils.annotations.JosmHome; 65 import org.openstreetmap.josm.testutils.annotations.Main; 66 import org.openstreetmap.josm.testutils.annotations.OsmApiType; 67 import org.openstreetmap.josm.testutils.annotations.Presets; 68 import org.openstreetmap.josm.testutils.annotations.Projection; 69 import org.openstreetmap.josm.testutils.annotations.ProjectionNadGrids; 63 70 import org.openstreetmap.josm.testutils.mockers.EDTAssertionMocker; 64 71 import org.openstreetmap.josm.testutils.mockers.WindowlessMapViewStateMocker; 65 72 import org.openstreetmap.josm.testutils.mockers.WindowlessNavigatableComponentMocker; … … 134 141 /** 135 142 * Enable the use of default preferences. 136 143 * @return this instance, for easy chaining 144 * @deprecated Use {@link FullPreferences} instead. 137 145 */ 146 @Deprecated 138 147 public JOSMTestRules preferences() { 139 148 josmHome(); 140 149 usePreferences = true; … … 144 153 /** 145 154 * Set JOSM home to a valid, empty directory. 146 155 * @return this instance, for easy chaining 156 * @deprecated Use {@link JosmHome} instead. 147 157 */ 158 @Deprecated 148 159 private JOSMTestRules josmHome() { 149 160 josmHome = new TemporaryFolder(); 150 161 return this; … … 153 164 /** 154 165 * Enables the i18n module for this test in english. 155 166 * @return this instance, for easy chaining 167 * @deprecated Use {org.openstreetmap.josm.testutils.annotations.I18n} instead. 156 168 */ 169 @Deprecated 157 170 public JOSMTestRules i18n() { 158 171 return i18n("en"); 159 172 } … … 162 175 * Enables the i18n module for this test. 163 176 * @param language The language to use. 164 177 * @return this instance, for easy chaining 178 * @deprecated Use {org.openstreetmap.josm.testutils.annotations.I18n} instead. 165 179 */ 180 @Deprecated 166 181 public JOSMTestRules i18n(String language) { 167 182 i18n = language; 168 183 return this; … … 172 187 * Mock this test's assumed JOSM version (as reported by {@link Version}). 173 188 * @param revisionProperties mock contents of JOSM's {@code REVISION} properties file 174 189 * @return this instance, for easy chaining 190 * @deprecated Use {@link OverrideAssumeRevision} instead. 175 191 */ 192 @Deprecated 176 193 public JOSMTestRules assumeRevision(final String revisionProperties) { 177 194 this.assumeRevisionString = revisionProperties; 178 195 return this; … … 181 198 /** 182 199 * Enable the dev.openstreetmap.org API for this test. 183 200 * @return this instance, for easy chaining 201 * @deprecated Use {@link OsmApiType} instead. 184 202 */ 203 @Deprecated 185 204 public JOSMTestRules devAPI() { 186 205 preferences(); 187 206 useAPI = APIType.DEV; … … 191 210 /** 192 211 * Use the {@link FakeOsmApi} for testing. 193 212 * @return this instance, for easy chaining 213 * @deprecated Use {@link OsmApiType} instead. 194 214 */ 215 @Deprecated 195 216 public JOSMTestRules fakeAPI() { 196 217 useAPI = APIType.FAKE; 197 218 return this; … … 200 221 /** 201 222 * Set up default projection (Mercator) 202 223 * @return this instance, for easy chaining 224 * @deprecated Use {@link Projection} instead. 203 225 */ 226 @Deprecated 204 227 public JOSMTestRules projection() { 205 228 useProjection = true; 206 229 return this; … … 209 232 /** 210 233 * Set up loading of NTV2 grit shift files to support projections that need them. 211 234 * @return this instance, for easy chaining 235 * @deprecated Use {@link ProjectionNadGrids} instead. 212 236 */ 237 @Deprecated 213 238 public JOSMTestRules projectionNadGrids() { 214 239 useProjectionNadGrids = true; 215 240 return this; … … 257 282 * Use presets in this test. 258 283 * @return this instance, for easy chaining 259 284 * @since 12568 285 * @deprecated Use {@link Presets} instead. 260 286 */ 287 @Deprecated 261 288 public JOSMTestRules presets() { 262 289 preferences(); 263 290 usePresets = true; … … 268 295 * Use boundaries dataset in this test. 269 296 * @return this instance, for easy chaining 270 297 * @since 12545 298 * @deprecated Use {@link org.openstreetmap.josm.testutils.annotations.Territories} instead. 271 299 */ 300 @Deprecated 272 301 public JOSMTestRules territories() { 273 302 territories = true; 274 303 return this; … … 351 380 * global variables in this test. 352 381 * @return this instance, for easy chaining 353 382 * @since 12557 383 * @deprecated Use {@link Main} instead 354 384 */ 385 @Deprecated 355 386 public JOSMTestRules main() { 356 387 return this.main( 357 388 WindowlessMapViewStateMocker::new, … … 368 399 * of {@link org.openstreetmap.josm.gui.NavigatableComponent}, null to skip. 369 400 * 370 401 * @return this instance, for easy chaining 402 * @deprecated Use {@link Main} instead 371 403 */ 404 @Deprecated 372 405 public JOSMTestRules main( 373 406 final Runnable mapViewStateMockingRunnable, 374 407 final Runnable navigableComponentMockingRunnable … … 397 430 return this; 398 431 } 399 432 400 private static class MockVersion extends Version { 401 MockVersion(final String propertiesString) { 433 /* 434 * Mock the JOSM version. This should only be used in JOSM Core test extensions. 435 */ 436 public static class MockVersion extends Version { 437 public MockVersion(final String propertiesString) { 402 438 super.initFromRevisionInfo( 403 439 new ByteArrayInputStream(propertiesString.getBytes(StandardCharsets.UTF_8)) 404 440 ); … … 409 445 public Statement apply(Statement base, Description description) { 410 446 // First process any Override* annotations for per-test overrides. 411 447 // The following only work because "option" methods modify JOSMTestRules in-place 412 finalOverrideAssumeRevision overrideAssumeRevision = description.getAnnotation(OverrideAssumeRevision.class);448 OverrideAssumeRevision overrideAssumeRevision = description.getAnnotation(OverrideAssumeRevision.class); 413 449 if (overrideAssumeRevision != null) { 414 450 this.assumeRevision(overrideAssumeRevision.value()); 415 451 } … … 790 826 /** 791 827 * Override this test's assumed JOSM version (as reported by {@link Version}). 792 828 * @see JOSMTestRules#assumeRevision(String) 829 * @deprecated Use {@link org.openstreetmap.josm.testutils.annotations.AssumeRevision} 830 * when using JUnit 5 Extensions. 793 831 */ 794 832 @Documented 795 833 @Retention(RetentionPolicy.RUNTIME) 796 834 @Target(ElementType.METHOD) 835 @Deprecated 797 836 public @interface OverrideAssumeRevision { 798 837 /** 799 838 * Returns overridden assumed JOSM version. -
test/unit/org/openstreetmap/josm/testutils/annotations/AssumeRevision.java
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.AfterEachCallback; 12 import org.junit.jupiter.api.extension.BeforeEachCallback; 13 import org.junit.jupiter.api.extension.ExtendWith; 14 import org.junit.jupiter.api.extension.ExtensionContext; 15 import org.junit.jupiter.api.extension.ExtensionContext.Namespace; 16 import org.junit.jupiter.api.extension.ExtensionContext.Store; 17 import org.junit.platform.commons.support.AnnotationSupport; 18 import org.openstreetmap.josm.TestUtils; 19 import org.openstreetmap.josm.data.Version; 20 import org.openstreetmap.josm.testutils.JOSMTestRules; 21 22 23 /** 24 * Override this test's assumed JOSM version (as reported by {@link Version}). 25 * @author Taylor Smock 26 * @see JOSMTestRules#assumeRevision(String) 27 * @since xxx 28 */ 29 @Documented 30 @Retention(RetentionPolicy.RUNTIME) 31 @Target({ElementType.METHOD, ElementType.TYPE}) 32 @ExtendWith(AssumeRevision.AssumeRevisionExtension.class) 33 public @interface AssumeRevision { 34 /** 35 * Returns overridden assumed JOSM version. 36 * @return overridden assumed JOSM version 37 */ 38 String value(); 39 40 /** 41 * Override the JOSM revision information. Use {@link AssumeRevision} instead of directly using this extension. 42 * @author Taylor Smock 43 * @since xxx 44 */ 45 class AssumeRevisionExtension implements BeforeEachCallback, AfterEachCallback { 46 47 @Override 48 public void afterEach(ExtensionContext context) throws Exception { 49 Store store = context.getStore(Namespace.create(AssumeRevisionExtension.class)); 50 String originalVersion = store.getOrDefault(store, String.class, null); 51 if (originalVersion != null) { 52 TestUtils.setPrivateStaticField(Version.class, "instance", originalVersion); 53 } 54 } 55 56 @Override 57 public void beforeEach(ExtensionContext context) throws Exception { 58 Optional<AssumeRevision> annotation = AnnotationSupport.findAnnotation(context.getElement(), AssumeRevision.class); 59 if (annotation.isPresent()) { 60 Store store = context.getStore(Namespace.create(AssumeRevisionExtension.class)); 61 store.put("originalVersion", Version.getInstance()); 62 final Version replacementVersion = new JOSMTestRules.MockVersion(annotation.get().value()); 63 TestUtils.setPrivateStaticField(Version.class, "instance", replacementVersion); 64 } 65 } 66 } 67 } -
test/unit/org/openstreetmap/josm/testutils/annotations/BasicPreferences.java
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 10 import org.junit.jupiter.api.extension.BeforeAllCallback; 11 import org.junit.jupiter.api.extension.ExtendWith; 12 import org.junit.jupiter.api.extension.ExtensionContext; 13 import org.junit.jupiter.api.extension.ExtensionContext.Namespace; 14 import org.openstreetmap.josm.data.Preferences; 15 import org.openstreetmap.josm.data.preferences.JosmBaseDirectories; 16 import org.openstreetmap.josm.data.preferences.JosmUrls; 17 import org.openstreetmap.josm.spi.preferences.Config; 18 import org.openstreetmap.josm.testutils.JOSMTestRules; 19 20 /** 21 * Allow tests to use JOSM preferences (see {@link JOSMTestRules#preferences()}) 22 * @author Taylor Smock 23 * @see FullPreferences 24 * @since xxx 25 */ 26 @Documented 27 @Retention(RetentionPolicy.RUNTIME) 28 @Target(ElementType.TYPE) 29 @ExtendWith(BasicPreferences.BasicPreferencesExtension.class) 30 public @interface BasicPreferences { 31 32 /** 33 * Initialize basic preferences. This is often more than enough for basic tests. 34 * @author Taylor Smock 35 * 36 */ 37 class BasicPreferencesExtension implements BeforeAllCallback { 38 39 @Override 40 public void beforeAll(ExtensionContext context) throws Exception { 41 Preferences pref = Preferences.main(); 42 Config.setPreferencesInstance(pref); 43 Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance()); 44 Config.setUrlsProvider(JosmUrls.getInstance()); 45 context.getStore(Namespace.create(BasicPreferencesExtension.class)).put("preferences", pref); 46 } 47 48 } 49 } -
test/unit/org/openstreetmap/josm/testutils/annotations/FullPreferences.java
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.Map; 10 11 import org.junit.jupiter.api.extension.BeforeEachCallback; 12 import org.junit.jupiter.api.extension.ExtendWith; 13 import org.junit.jupiter.api.extension.ExtensionContext; 14 import org.junit.jupiter.api.extension.ExtensionContext.Namespace; 15 import org.openstreetmap.josm.TestUtils; 16 import org.openstreetmap.josm.data.Preferences; 17 import org.openstreetmap.josm.spi.preferences.Config; 18 import org.openstreetmap.josm.spi.preferences.Setting; 19 import org.openstreetmap.josm.testutils.JOSMTestRules; 20 import org.openstreetmap.josm.testutils.annotations.BasicPreferences.BasicPreferencesExtension; 21 22 /** 23 * Allow tests to use JOSM preferences (see {@link JOSMTestRules#preferences()}) 24 * @author Taylor Smock 25 * @see BasicPreferences (often enough for simple tests). 26 * @since xxx 27 */ 28 @Documented 29 @Retention(RetentionPolicy.RUNTIME) 30 @Target(ElementType.TYPE) 31 @JosmHome 32 @BasicPreferences 33 @ExtendWith(FullPreferences.UsePreferencesExtension.class) 34 public @interface FullPreferences { 35 /** 36 * Initialize preferences. 37 */ 38 class UsePreferencesExtension implements BeforeEachCallback { 39 @Override 40 public void beforeEach(ExtensionContext context) throws Exception { 41 Preferences pref = context.getStore(Namespace.create(BasicPreferencesExtension.class)).get("preferences", Preferences.class); 42 @SuppressWarnings("unchecked") 43 final Map<String, Setting<?>> defaultsMap = (Map<String, Setting<?>>) TestUtils.getPrivateField(pref, "defaultsMap"); 44 defaultsMap.clear(); 45 pref.resetToInitialState(); 46 pref.enableSaveOnPut(false); 47 // No pref init -> that would only create the preferences file. 48 // We force the use of a wrong API server, just in case anyone attempts an upload 49 Config.getPref().put("osm-server.url", "http://invalid"); 50 } 51 52 } 53 } -
test/unit/org/openstreetmap/josm/testutils/annotations/I18n.java
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.BeforeAllCallback; 12 import org.junit.jupiter.api.extension.ExtendWith; 13 import org.junit.jupiter.api.extension.ExtensionContext; 14 import org.junit.platform.commons.support.AnnotationSupport; 15 import org.openstreetmap.josm.testutils.JOSMTestRules; 16 17 /** 18 * Enables the i18n module for this test. 19 * @author Taylor Smock 20 * @see JOSMTestRules#i18n(String) 21 * 22 */ 23 @Documented 24 @Retention(RetentionPolicy.RUNTIME) 25 @Target(ElementType.TYPE) 26 @ExtendWith(I18n.I18nExtension.class) 27 public @interface I18n { 28 /** 29 * Get the language to use for i18n 30 * @return The language (default "en"). 31 */ 32 String language() default "en"; 33 34 /** 35 * Enables the i18n module for this test. 36 * @author Taylor Smock 37 * @see JOSMTestRules#i18n(String) 38 * 39 */ 40 class I18nExtension implements BeforeAllCallback { 41 @Override 42 public void beforeAll(ExtensionContext context) throws Exception { 43 Optional<I18n> annotation = AnnotationSupport.findAnnotation(context.getElement(), I18n.class); 44 String language = "en"; 45 if (annotation.isPresent()) { 46 language = annotation.get().language(); 47 } 48 org.openstreetmap.josm.tools.I18n.set(language); 49 } 50 } 51 } -
test/unit/org/openstreetmap/josm/testutils/annotations/JosmHome.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.testutils.annotations; 3 4 import java.io.File; 5 import java.io.IOException; 6 import java.lang.annotation.Documented; 7 import java.lang.annotation.ElementType; 8 import java.lang.annotation.Retention; 9 import java.lang.annotation.RetentionPolicy; 10 import java.lang.annotation.Target; 11 import java.nio.file.FileVisitResult; 12 import java.nio.file.Files; 13 import java.nio.file.Path; 14 import java.nio.file.SimpleFileVisitor; 15 import java.nio.file.attribute.BasicFileAttributes; 16 import java.util.UUID; 17 18 import org.junit.jupiter.api.extension.AfterAllCallback; 19 import org.junit.jupiter.api.extension.BeforeAllCallback; 20 import org.junit.jupiter.api.extension.ExtendWith; 21 import org.junit.jupiter.api.extension.ExtensionContext; 22 import org.junit.jupiter.api.extension.ExtensionContext.Namespace; 23 import org.openstreetmap.josm.data.preferences.JosmBaseDirectories; 24 import org.openstreetmap.josm.testutils.JOSMTestRules; 25 26 /** 27 * Use the JOSM home directory. See {@link JOSMTestRules}. 28 * Typically only used by {@link FullPreferences}. 29 * 30 * @author Taylor Smock 31 * 32 */ 33 @Documented 34 @Retention(RetentionPolicy.RUNTIME) 35 @Target(ElementType.TYPE) 36 @ExtendWith(JosmHome.JosmHomeExtension.class) 37 public @interface JosmHome { 38 /** 39 * Create a JOSM home directory. Prefer using {@link JosmHome}. 40 * @author Taylor Smock 41 */ 42 class JosmHomeExtension implements BeforeAllCallback, AfterAllCallback { 43 @Override 44 public void afterAll(ExtensionContext context) throws Exception { 45 Path tempDir = context.getStore(Namespace.create(JosmHome.class)).get("home", Path.class); 46 Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() { 47 @Override 48 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { 49 Files.delete(dir); 50 return FileVisitResult.CONTINUE; 51 } 52 53 @Override 54 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 55 Files.delete(file); 56 return FileVisitResult.CONTINUE; 57 } 58 }); 59 } 60 61 @Override 62 public void beforeAll(ExtensionContext context) throws Exception { 63 Path tempDir = Files.createTempDirectory(UUID.randomUUID().toString()); 64 context.getStore(Namespace.create(JosmHome.class)).put("home", tempDir); 65 File home = tempDir.toFile(); 66 System.setProperty("josm.home", home.getAbsolutePath()); 67 JosmBaseDirectories.getInstance().clearMemos(); 68 } 69 } 70 } -
test/unit/org/openstreetmap/josm/testutils/annotations/Main.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.testutils.annotations; 3 4 import static java.lang.annotation.ElementType.TYPE; 5 import static java.lang.annotation.RetentionPolicy.RUNTIME; 6 7 import java.lang.annotation.Documented; 8 import java.lang.annotation.Retention; 9 import java.lang.annotation.Target; 10 import java.util.Optional; 11 12 import org.junit.jupiter.api.extension.BeforeAllCallback; 13 import org.junit.jupiter.api.extension.ExtendWith; 14 import org.junit.jupiter.api.extension.ExtensionContext; 15 import org.junit.platform.commons.support.AnnotationSupport; 16 import org.openstreetmap.josm.JOSMFixture; 17 import org.openstreetmap.josm.gui.MainApplication; 18 import org.openstreetmap.josm.testutils.JOSMTestRules; 19 import org.openstreetmap.josm.testutils.mockers.WindowlessMapViewStateMocker; 20 import org.openstreetmap.josm.testutils.mockers.WindowlessNavigatableComponentMocker; 21 22 /** 23 * Use the {@link MainApplication#main}, {@code Main.contentPanePrivate}, {@code Main.mainPanel}, global variables in this test. 24 * @author Taylor Smock 25 * @see JOSMTestRules#main() 26 * @since xxx 27 */ 28 @Documented 29 @Retention(RUNTIME) 30 @Target(TYPE) 31 @ExtendWith(Main.MainExtension.class) 32 public @interface Main { 33 /** 34 * Get the class to use as the mocker for the map view 35 * @return The mocker class for the map view 36 */ 37 Class<?> mapViewStateMocker() default WindowlessMapViewStateMocker.class; 38 /** 39 * Get the class to use for the navigable component 40 * @return The class to use for the navigable component. 41 */ 42 Class<?> navigableComponentMocker() default WindowlessNavigatableComponentMocker.class; 43 44 /** 45 * Initialize the MainApplication 46 * @author Taylor Smock 47 */ 48 class MainExtension implements BeforeAllCallback { 49 @Override 50 public void beforeAll(ExtensionContext context) throws Exception { 51 Optional<Main> annotation = AnnotationSupport.findAnnotation(context.getElement(), Main.class); 52 Class<?> mapViewStateMocker = null; 53 Class<?> navigableComponentMocker = null; 54 if (annotation.isPresent()) { 55 mapViewStateMocker = annotation.get().mapViewStateMocker(); 56 navigableComponentMocker = annotation.get().navigableComponentMocker(); 57 } 58 59 // apply mockers to MapViewState and NavigableComponent whether we're headless or not 60 // as we generally don't create the josm main window even in non-headless mode. 61 if (mapViewStateMocker != null) { 62 mapViewStateMocker.getConstructor().newInstance(); 63 } 64 if (navigableComponentMocker != null) { 65 navigableComponentMocker.getConstructor().newInstance(); 66 } 67 68 new MainApplication(); 69 JOSMFixture.initContentPane(); 70 JOSMFixture.initMainPanel(true); 71 JOSMFixture.initToolbar(); 72 JOSMFixture.initMainMenu(); 73 } 74 } 75 } -
test/unit/org/openstreetmap/josm/testutils/annotations/OsmApiType.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.testutils.annotations; 3 4 import static java.lang.annotation.ElementType.METHOD; 5 import static java.lang.annotation.ElementType.TYPE; 6 import static java.lang.annotation.RetentionPolicy.RUNTIME; 7 8 import java.lang.annotation.Documented; 9 import java.lang.annotation.Retention; 10 import java.lang.annotation.Target; 11 import java.util.Optional; 12 13 import org.junit.jupiter.api.extension.BeforeAllCallback; 14 import org.junit.jupiter.api.extension.BeforeEachCallback; 15 import org.junit.jupiter.api.extension.ExtendWith; 16 import org.junit.jupiter.api.extension.ExtensionContext; 17 import org.junit.platform.commons.support.AnnotationSupport; 18 import org.openstreetmap.josm.io.OsmApi; 19 import org.openstreetmap.josm.spi.preferences.Config; 20 import org.openstreetmap.josm.testutils.FakeOsmApi; 21 import org.openstreetmap.josm.testutils.JOSMTestRules; 22 23 /** 24 * Specify the OSM API to use for the test. {@link APIType#NONE} has no effect. 25 * 26 * @author Taylor Smock 27 * @see JOSMTestRules#devAPI() 28 * @see JOSMTestRules#fakeAPI() 29 * @since xxx 30 */ 31 @Documented 32 @Retention(RUNTIME) 33 @Target({ TYPE, METHOD }) 34 @FullPreferences 35 @ExtendWith(OsmApiType.OsmApiTypeExtension.class) 36 public @interface OsmApiType { 37 /** 38 * API types to initialize 39 * @author Taylor Smock 40 * 41 */ 42 enum APIType { 43 NONE, FAKE, DEV; 44 } 45 46 /** 47 * The API type to use 48 * @return The API type to use (default NONE) 49 */ 50 APIType apiType() default APIType.NONE; 51 52 /** 53 * Initialize the OSM api 54 * @author Taylor Smock 55 * 56 */ 57 class OsmApiTypeExtension implements BeforeAllCallback, BeforeEachCallback { 58 59 @Override 60 public void beforeEach(ExtensionContext context) throws Exception { 61 Optional<OsmApiType> annotation = AnnotationSupport.findAnnotation(context.getElement(), OsmApiType.class); 62 APIType useAPI = APIType.NONE; 63 if (annotation.isPresent()) { 64 useAPI = annotation.get().apiType(); 65 } 66 // Set API 67 if (useAPI == APIType.DEV) { 68 Config.getPref().put("osm-server.url", "https://api06.dev.openstreetmap.org/api"); 69 } else if (useAPI == APIType.FAKE) { 70 FakeOsmApi api = FakeOsmApi.getInstance(); 71 Config.getPref().put("osm-server.url", api.getServerUrl()); 72 } 73 74 // Initialize API 75 if (useAPI != APIType.NONE) { 76 OsmApi.getOsmApi().initialize(null); 77 } 78 } 79 80 @Override 81 public void beforeAll(ExtensionContext context) throws Exception { 82 beforeEach(context); 83 } 84 85 } 86 } -
test/unit/org/openstreetmap/josm/testutils/annotations/Presets.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.testutils.annotations; 3 4 import static java.lang.annotation.ElementType.METHOD; 5 import static java.lang.annotation.ElementType.TYPE; 6 import static java.lang.annotation.RetentionPolicy.RUNTIME; 7 8 import java.lang.annotation.Documented; 9 import java.lang.annotation.Retention; 10 import java.lang.annotation.Target; 11 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.openstreetmap.josm.gui.tagging.presets.TaggingPresets; 17 import org.openstreetmap.josm.testutils.JOSMTestRules; 18 19 /** 20 * Use presets in this test. 21 * 22 * @author Taylor Smock 23 * @see JOSMTestRules#presets() 24 * @since xxx 25 */ 26 @Documented 27 @Retention(RUNTIME) 28 @Target({ TYPE, METHOD }) 29 @ExtendWith(Presets.PresetsExtension.class) 30 public @interface Presets { 31 /** 32 * Initialize the presets 33 * @author Taylor Smock 34 * 35 */ 36 class PresetsExtension implements BeforeAllCallback, BeforeEachCallback { 37 @Override 38 public void beforeAll(ExtensionContext context) throws Exception { 39 TaggingPresets.readFromPreferences(); 40 } 41 42 @Override 43 public void beforeEach(ExtensionContext context) throws Exception { 44 // TODO only run if method level 45 beforeAll(context); 46 } 47 } 48 } -
test/unit/org/openstreetmap/josm/testutils/annotations/Projection.java
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.BeforeEachCallback; 12 import org.junit.jupiter.api.extension.ExtendWith; 13 import org.junit.jupiter.api.extension.ExtensionContext; 14 import org.junit.platform.commons.support.AnnotationSupport; 15 import org.openstreetmap.josm.data.projection.ProjectionRegistry; 16 import org.openstreetmap.josm.data.projection.Projections; 17 import org.openstreetmap.josm.testutils.JOSMTestRules; 18 19 /** 20 * Use projections in tests (Mercator). 21 * @author Taylor Smock 22 * @see JOSMTestRules#projection() 23 * 24 */ 25 @Documented 26 @Retention(RetentionPolicy.RUNTIME) 27 @Target({ElementType.METHOD, ElementType.TYPE}) 28 @ExtendWith(Projection.ProjectionExtension.class) 29 public @interface Projection { 30 /** 31 * The value to use for the projection. Defaults to EPSG:3857 (Mercator). 32 * @return The value to use to get the projection from {@link Projections#getProjectionByCode}. 33 */ 34 String projectionCode() default "EPSG:3857"; 35 36 /** 37 * Use projections in tests. Use {@link Projection} preferentially. 38 * @author Taylor Smock 39 * 40 */ 41 class ProjectionExtension implements BeforeEachCallback { 42 @Override 43 public void beforeEach(ExtensionContext context) throws Exception { 44 Optional<Projection> annotation = AnnotationSupport.findAnnotation(context.getElement(), Projection.class); 45 if (annotation.isPresent()) { 46 ProjectionRegistry.setProjection(Projections.getProjectionByCode(annotation.get().projectionCode())); 47 } else { 48 ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator 49 } 50 } 51 52 } 53 } -
test/unit/org/openstreetmap/josm/testutils/annotations/ProjectionNadGrids.java
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 10 import org.junit.jupiter.api.extension.BeforeAllCallback; 11 import org.junit.jupiter.api.extension.ExtendWith; 12 import org.junit.jupiter.api.extension.ExtensionContext; 13 import org.openstreetmap.josm.gui.MainApplication; 14 import org.openstreetmap.josm.testutils.JOSMTestRules; 15 16 /** 17 * Set up loading of NTV2 grit shift files to support projections that need them. 18 * @author Taylor Smock 19 * @see JOSMTestRules#projectionNadGrids() 20 * @since xxx 21 */ 22 @Documented 23 @Retention(RetentionPolicy.RUNTIME) 24 @Target(ElementType.TYPE) 25 @ExtendWith(ProjectionNadGrids.ProjectionNadGridsExtension.class) 26 public @interface ProjectionNadGrids { 27 /** 28 * Set up loading of NTV2 grit shift files to support projections that need them. 29 * Use {@link ProjectionNadGrids} instead. 30 * @author Taylor Smock 31 * @see JOSMTestRules#projectionNadGrids() 32 */ 33 class ProjectionNadGridsExtension implements BeforeAllCallback { 34 @Override 35 public void beforeAll(ExtensionContext context) throws Exception { 36 MainApplication.setupNadGridSources(); 37 } 38 } 39 } -
test/unit/org/openstreetmap/josm/testutils/annotations/Territories.java
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.BeforeAllCallback; 12 import org.junit.jupiter.api.extension.BeforeEachCallback; 13 import org.junit.jupiter.api.extension.ExtendWith; 14 import org.junit.jupiter.api.extension.ExtensionContext; 15 import org.junit.platform.commons.support.AnnotationSupport; 16 import org.openstreetmap.josm.testutils.JOSMTestRules; 17 18 /** 19 * Use boundaries dataset in this test. 20 * @see JOSMTestRules#territories() 21 * @author Taylor Smock 22 * @since xxx 23 */ 24 @Documented 25 @Retention(RetentionPolicy.RUNTIME) 26 @Target({ElementType.TYPE, ElementType.METHOD}) 27 @ExtendWith(Territories.TerritoriesExtension.class) 28 public @interface Territories { 29 /** 30 * Initialization states. Please note that the highest initialization state holds. 31 * @author Taylor Smock 32 */ 33 enum Initialize { 34 /** Don't initialize */ 35 NONE, 36 /** Initialize only internal data */ 37 INTERNAL, 38 /** Initialize internal and external data */ 39 ALL 40 } 41 42 /** 43 * The way to initialize Territories 44 * @return The value to use 45 */ 46 Initialize value() default Initialize.INTERNAL; 47 /** 48 * Initialize boundaries prior to use 49 * @author Taylor Smock 50 * 51 */ 52 class TerritoriesExtension implements BeforeAllCallback, BeforeEachCallback { 53 // Take advantage of the fact that Territories is essentially a static class 54 private static Initialize initialized = Initialize.NONE; 55 @Override 56 public void beforeAll(ExtensionContext context) throws Exception { 57 beforeEach(context); 58 } 59 60 @Override 61 public void beforeEach(ExtensionContext context) throws Exception { 62 Optional<Territories> annotation = AnnotationSupport.findAnnotation(context.getElement(), Territories.class); 63 if (annotation.isPresent()) { 64 Initialize current = annotation.get().value(); 65 // Avoid potential race conditions if tests are parallelized 66 synchronized(TerritoriesExtension.class) { 67 if (current.compareTo(initialized) > 0) { 68 if (current == Initialize.INTERNAL) { 69 org.openstreetmap.josm.tools.Territories.initializeInternalData(); 70 } else if (current == Initialize.ALL) { 71 org.openstreetmap.josm.tools.Territories.initialize(); 72 } 73 initialized = current; 74 } 75 } 76 } 77 } 78 } 79 }
