source: josm/trunk/test/unit/org/openstreetmap/josm/JOSMFixture.java

Last change on this file was 18690, checked in by taylor.smock, 13 months ago

See #16567: Convert all assertion calls to JUnit 5 (patch by gaben, modified)

The modifications are as follows:

  • Merge DomainValidatorTest.testIDN and DomainValidatorTest.testIDNJava6OrLater
  • Update some tests to use @ParameterizedTest (DomainValidatorTest)
  • Replace various exception blocks with assertThrows. These typically looked like
        try {
            // Something that should throw an exception here
            fail("An exception should have been thrown");
        } catch (Exception e) {
            // Verify the exception matches expectations here
        }
    
  • Replace assertTrue(val instanceof Clazz) with assertInstanceOf
  • Replace JUnit 4 @Suite with JUnit 5 @Suite

Both the original patch and the modified patch fix various lint issues.

  • Property svn:eol-style set to native
File size: 7.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm;
3
4import static org.junit.jupiter.api.Assertions.assertNull;
5import static org.junit.jupiter.api.Assertions.assertTrue;
6import static org.junit.jupiter.api.Assertions.fail;
7
8import java.io.File;
9import java.io.IOException;
10import java.nio.file.Paths;
11import java.security.GeneralSecurityException;
12import java.text.MessageFormat;
13import java.util.Locale;
14import java.util.TimeZone;
15
16import org.openstreetmap.josm.actions.DeleteAction;
17import org.openstreetmap.josm.command.DeleteCommand;
18import org.openstreetmap.josm.data.Preferences;
19import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
20import org.openstreetmap.josm.data.preferences.JosmUrls;
21import org.openstreetmap.josm.data.projection.ProjectionRegistry;
22import org.openstreetmap.josm.data.projection.Projections;
23import org.openstreetmap.josm.gui.MainApplication;
24import org.openstreetmap.josm.gui.MainApplicationTest;
25import org.openstreetmap.josm.gui.MainInitialization;
26import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
27import org.openstreetmap.josm.gui.util.GuiHelper;
28import org.openstreetmap.josm.io.CertificateAmendment;
29import org.openstreetmap.josm.io.OsmApi;
30import org.openstreetmap.josm.spi.lifecycle.Lifecycle;
31import org.openstreetmap.josm.spi.preferences.Config;
32import org.openstreetmap.josm.testutils.JOSMTestRules;
33import org.openstreetmap.josm.tools.Http1Client;
34import org.openstreetmap.josm.tools.HttpClient;
35import org.openstreetmap.josm.tools.I18n;
36import org.openstreetmap.josm.tools.JosmRuntimeException;
37import org.openstreetmap.josm.tools.Logging;
38import org.openstreetmap.josm.tools.PlatformManager;
39import org.openstreetmap.josm.tools.Utils;
40import org.openstreetmap.josm.tools.date.DateUtils;
41
42/**
43 * Fixture to define a proper and safe environment before running tests.
44 */
45public class JOSMFixture {
46
47 /**
48 * Returns a new test fixture initialized to "unit" home.
49 * @return A new test fixture for unit tests
50 */
51 public static JOSMFixture createUnitTestFixture() {
52 return new JOSMFixture("test/config/unit-josm.home");
53 }
54
55 /**
56 * Returns a new test fixture initialized to "functional" home.
57 * @return A new test fixture for functional tests
58 */
59 public static JOSMFixture createFunctionalTestFixture() {
60 return new JOSMFixture("test/config/functional-josm.home");
61 }
62
63 /**
64 * Returns a new test fixture initialized to "performance" home.
65 * @return A new test fixture for performance tests
66 */
67 public static JOSMFixture createPerformanceTestFixture() {
68 return new JOSMFixture("test/config/performance-josm.home");
69 }
70
71 private final String josmHome;
72
73 /**
74 * Constructs a new text fixture initialized to a given josm home.
75 * @param josmHome The user home where preferences are to be read/written
76 */
77 public JOSMFixture(String josmHome) {
78 this.josmHome = josmHome;
79 }
80
81 /**
82 * Initializes the test fixture, without GUI.
83 */
84 public void init() {
85 init(false);
86 }
87
88 /**
89 * Initializes the test fixture, with or without GUI.
90 * @param createGui if {@code true} creates main GUI components
91 */
92 public void init(boolean createGui) {
93
94 // check josm.home
95 //
96 if (josmHome == null) {
97 fail(MessageFormat.format("property ''{0}'' not set in test environment", "josm.home"));
98 } else {
99 File f = new File(josmHome);
100 if (!f.exists() || !f.canRead()) {
101 fail(MessageFormat.format(
102 // CHECKSTYLE.OFF: LineLength
103 "property ''{0}'' points to ''{1}'' which is either not existing ({2}) or not readable ({3}). Current directory is ''{4}''.",
104 // CHECKSTYLE.ON: LineLength
105 "josm.home", josmHome, f.exists(), f.canRead(), Paths.get("").toAbsolutePath()));
106 }
107 }
108 System.setProperty("josm.home", josmHome);
109 TimeZone.setDefault(DateUtils.UTC);
110 Preferences pref = Preferences.main();
111 Config.setPreferencesInstance(pref);
112 Config.setBaseDirectoriesProvider(JosmBaseDirectories.getInstance());
113 Config.setUrlsProvider(JosmUrls.getInstance());
114 HttpClient.setFactory(Http1Client::new);
115 pref.resetToInitialState();
116 pref.enableSaveOnPut(false);
117 I18n.init();
118 // initialize the platform hook, and
119 // call the really early hook before anything else
120 PlatformManager.getPlatform().preStartupHook();
121
122 Logging.setLogLevel(Logging.LEVEL_INFO);
123 pref.init(false);
124 String url = OsmApi.getOsmApi().getServerUrl();
125 if (Utils.isEmpty(url) || isProductionApiUrl(url)) {
126 Config.getPref().put("osm-server.url", "https://api06.dev.openstreetmap.org/api");
127 }
128 I18n.set(Config.getPref().get("language", "en"));
129
130 try {
131 CertificateAmendment.addMissingCertificates();
132 } catch (IOException | GeneralSecurityException ex) {
133 throw new JosmRuntimeException(ex);
134 }
135
136 // init projection
137 ProjectionRegistry.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
138
139 // setup projection grid files
140 MainApplication.setupNadGridSources();
141
142 // make sure we don't upload to or test against production
143 url = OsmApi.getOsmApi().getBaseUrl().toLowerCase(Locale.ENGLISH).trim();
144 if (isProductionApiUrl(url)) {
145 fail(MessageFormat.format("configured server url ''{0}'' seems to be a productive url, aborting.", url));
146 }
147
148 // Setup callbacks
149 DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback);
150
151 if (createGui) {
152 GuiHelper.runInEDTAndWaitWithException(this::setupGUI);
153 }
154 }
155
156 private static boolean isProductionApiUrl(String url) {
157 return url.startsWith("http://www.openstreetmap.org") || url.startsWith("http://api.openstreetmap.org")
158 || url.startsWith("https://www.openstreetmap.org") || url.startsWith("https://api.openstreetmap.org");
159 }
160
161 private void setupGUI() {
162 JOSMTestRules.cleanLayerEnvironment();
163 assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
164 assertNull(MainApplication.getLayerManager().getEditLayer());
165 assertNull(MainApplication.getLayerManager().getActiveLayer());
166
167 initContentPane();
168 initMainPanel(false);
169 initToolbar();
170 if (MainApplication.getMenu() == null) {
171 Lifecycle.initialize(new MainInitialization(new MainApplication()));
172 }
173 // Add a test layer to the layer manager to get the MapFrame
174 MainApplication.getLayerManager().addLayer(new TestLayer());
175 }
176
177 /**
178 * Make sure {@code MainApplication.contentPanePrivate} is initialized.
179 */
180 public static void initContentPane() {
181 MainApplicationTest.initContentPane();
182 }
183
184 /**
185 * Make sure {@code MainApplication.mainPanel} is initialized.
186 */
187 public static void initMainPanel() {
188 initMainPanel(false);
189 }
190
191 /**
192 * Make sure {@code MainApplication.mainPanel} is initialized.
193 * @param reAddListeners {@code true} to re-add listeners
194 */
195 public static void initMainPanel(boolean reAddListeners) {
196 MainApplicationTest.initMainPanel(reAddListeners);
197 }
198
199 /**
200 * Make sure {@code MainApplication.toolbar} is initialized.
201 */
202 public static void initToolbar() {
203 MainApplicationTest.initToolbar();
204 }
205
206 /**
207 * Make sure {@code MainApplication.menu} is initialized.
208 */
209 public static void initMainMenu() {
210 MainApplicationTest.initMainMenu();
211 }
212}
Note: See TracBrowser for help on using the repository browser.