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

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

see #15229 - kill Main. RIP

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