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

Last change on this file since 14138 was 14138, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.platform and related methods - new class PlatformManager

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