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

Last change on this file since 16159 was 15233, checked in by Don-vip, 5 years ago

see #17861 - last bugfixes

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