1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
6 | import static org.junit.jupiter.api.Assertions.fail;
|
---|
7 |
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.nio.file.Paths;
|
---|
11 | import java.security.GeneralSecurityException;
|
---|
12 | import java.text.MessageFormat;
|
---|
13 | import java.util.Locale;
|
---|
14 | import java.util.TimeZone;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.actions.DeleteAction;
|
---|
17 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
18 | import org.openstreetmap.josm.data.Preferences;
|
---|
19 | import org.openstreetmap.josm.data.preferences.JosmBaseDirectories;
|
---|
20 | import org.openstreetmap.josm.data.preferences.JosmUrls;
|
---|
21 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
22 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
23 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
24 | import org.openstreetmap.josm.gui.MainApplicationTest;
|
---|
25 | import org.openstreetmap.josm.gui.MainInitialization;
|
---|
26 | import org.openstreetmap.josm.gui.layer.LayerManagerTest.TestLayer;
|
---|
27 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
28 | import org.openstreetmap.josm.io.CertificateAmendment;
|
---|
29 | import org.openstreetmap.josm.io.OsmApi;
|
---|
30 | import org.openstreetmap.josm.spi.lifecycle.Lifecycle;
|
---|
31 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
32 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
33 | import org.openstreetmap.josm.tools.Http1Client;
|
---|
34 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
35 | import org.openstreetmap.josm.tools.I18n;
|
---|
36 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
37 | import org.openstreetmap.josm.tools.Logging;
|
---|
38 | import org.openstreetmap.josm.tools.PlatformManager;
|
---|
39 | import org.openstreetmap.josm.tools.Utils;
|
---|
40 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Fixture to define a proper and safe environment before running tests.
|
---|
44 | */
|
---|
45 | public 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 | }
|
---|