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

Last change on this file since 12848 was 12848, checked in by bastiK, 7 years ago

see #15229 - set up Config for tests

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