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

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

see #15229 - move Main initialization methods to lifecycle SPI + new class MainInitialization

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