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

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

see #15229 - deprecate Main.pref

  • 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 @SuppressWarnings("deprecation")
158 private void setupGUI() {
159 JOSMTestRules.cleanLayerEnvironment();
160 assertTrue(MainApplication.getLayerManager().getLayers().isEmpty());
161 assertNull(MainApplication.getLayerManager().getEditLayer());
162 assertNull(MainApplication.getLayerManager().getActiveLayer());
163
164 initContentPane();
165 initMainPanel(false);
166 initToolbar();
167 if (Main.main == null) {
168 Lifecycle.initialize(new MainInitialization(new MainApplication()));
169 }
170 // Add a test layer to the layer manager to get the MapFrame
171 MainApplication.getLayerManager().addLayer(new TestLayer());
172 }
173
174 /**
175 * Make sure {@code MainApplication.contentPanePrivate} is initialized.
176 */
177 public static void initContentPane() {
178 MainApplicationTest.initContentPane();
179 }
180
181 /**
182 * Make sure {@code MainApplication.mainPanel} is initialized.
183 */
184 public static void initMainPanel() {
185 initMainPanel(false);
186 }
187
188 /**
189 * Make sure {@code MainApplication.mainPanel} is initialized.
190 * @param reAddListeners {@code true} to re-add listeners
191 */
192 public static void initMainPanel(boolean reAddListeners) {
193 MainApplicationTest.initMainPanel(reAddListeners);
194 }
195
196 /**
197 * Make sure {@code MainApplication.toolbar} is initialized.
198 */
199 public static void initToolbar() {
200 MainApplicationTest.initToolbar();
201 }
202
203 /**
204 * Make sure {@code MainApplication.menu} is initialized.
205 */
206 public static void initMainMenu() {
207 MainApplicationTest.initMainMenu();
208 }
209}
Note: See TracBrowser for help on using the repository browser.